-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexargs.py
More file actions
executable file
·55 lines (46 loc) · 1.66 KB
/
exargs.py
File metadata and controls
executable file
·55 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import argparse
from config import conf
parser = argparse.ArgumentParser(description='PyTorch Training')
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def str2str(v):
if isinstance(v, str):
if v == 'None':
return ''
else:
return v
else:
raise ValueError()
def str2tuple(v):
if isinstance(v, (tuple,list)):
return v
elif isinstance(v, str):
v=[int(k) for k in v.split('_')]
return v
else:
raise argparse.ArgumentTypeError()
for k, v in conf.__dict__.items():
if isinstance(v, bool):
parser.add_argument(f"--{k}", type=str2bool, nargs='?',
const=True, default=v)
elif isinstance(v, (float, int, )): # note bool is int
parser.add_argument(f'--{k}', default=v, type=type(v))
elif isinstance(v, (str,)):
parser.add_argument(f'--{k}', default=v, type=str2str, nargs='?', const=True, )
elif isinstance(v, (tuple, list)):
parser.add_argument(f'--{k}', default=v, type=str2tuple, nargs='?', const=True, )
parser.add_argument('--local_rank', default=None, type=int, )
parser.add_argument('--work_path', default=None, type=str, )
parser.add_argument('--data_url', default=None, type=str)
parser.add_argument('--init_method', default=None, type=str)
parser.add_argument('--train_url', default=None, type=str)
if __name__ == '__main__':
args = parser.parse_args()
print(args)