-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
184 lines (158 loc) · 5.96 KB
/
config.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import yaml
import argparse
from pathlib import Path
import torch
import collections.abc
import re
class NullScheduler:
""" Empty scheduler for use as a placeholder to keep code compatible"""
def __init__(self):
pass
def step(self, *args, **kwargs):
pass
def get_kwargs(args, key):
args_dict = vars(args).copy()
if key + '_class' not in args_dict:
return None, None
clazz = args_dict[key + '_class']
del args_dict[key + '_class']
kwargs = {}
for k, v in args_dict.items():
if k.startswith(key):
left, right = k.split('_', 1)
if left == key:
kwargs[right] = v
return clazz, kwargs
def get_optim(args, parameters):
"""
Reads the configuration and constructs a scheduler and optimizer
:param args: the configuration Namespace
:param parameters: model.parameters()
:return: optimizer, scheduler
if scheduler not specified a placeholder scheduler will be returned
"""
optim_class, optim_kwargs = get_kwargs(args, 'optim')
optim_class = getattr(torch.optim, optim_class)
optim = optim_class(parameters, **optim_kwargs)
scheduler_class, scheduler_kwargs = get_kwargs(args, 'scheduler')
if scheduler_class is None:
return optim, NullScheduler()
scheduler_class = getattr(torch.optim.lr_scheduler, scheduler_class)
scheduler = scheduler_class(optim, **scheduler_kwargs)
return optim, scheduler
def config(args=None):
"""
Reads the command switches and creates a config
Command line switches override config files
:return: a Namespace of args
"""
""" config """
parser = argparse.ArgumentParser(description='configuration switches')
parser.add_argument('-d', '--device', type=str)
parser.add_argument('-r', '--run_id', type=int, default=-1)
parser.add_argument('--comment', type=str)
parser.add_argument('--demo', action='store_true', default=False)
parser.add_argument('-l', '--load', type=str, default=None)
parser.add_argument('--transfer_load', type=str, default=None)
parser.add_argument('--checkpoint_freq', type=int)
parser.add_argument('--dataroot', type=str, default='data')
parser.add_argument('-c', '--config', type=str, default=None)
parser.add_argument('--epochs', type=int)
parser.add_argument('--processes', type=int)
parser.add_argument('--seed', type=int, default=None)
""" visualization params """
parser.add_argument('--display', type=int)
""" model parameters """
parser.add_argument('--optlevel', type=str)
parser.add_argument('--model_type', type=str)
""" hyper-parameters """
parser.add_argument('--optim_class', type=str)
parser.add_argument('--optim_lr', type=float)
parser.add_argument('--scheduler_class', type=str)
parser.add_argument('--batchsize', type=int)
""" data and data augmentation parameters """
parser.add_argument('--dataset_name', type=str)
parser.add_argument('--dataset_train_len', type=int)
parser.add_argument('--dataset_test_len', type=int)
parser.add_argument('--dataset_randomize', type=int)
args = parser.parse_args(args)
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.abc.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def set_if_not_set(args, dict):
"""
Sets an argument if it's not already set in the args
:param args: args namespace
:param dict: a dict containing arguments to check
:return:
"""
for key, value in dict.items():
if key in vars(args) and vars(args)[key] is None:
vars(args)[key] = dict[key]
elif key not in vars(args):
vars(args)[key] = dict[key]
return args
"""
required due to https://github.com/yaml/pyyaml/issues/173
pyyaml does not correctly parse scientific notation
"""
loader = yaml.SafeLoader
loader.add_implicit_resolver(
u'tag:yaml.org,2002:float',
re.compile(u'''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.'))
""" read the config file """
if args.config is not None:
with Path(args.config).open() as f:
conf = yaml.load(f, Loader=loader)
conf = flatten(conf)
args = set_if_not_set(args, conf)
""" args not set will be set to a default value """
defaults = {
'optim_class': 'Adam',
'optim_lr': 1e-4,
'checkpoint_freq': 1,
'opt_level': 'O0',
'display_kp_rows': 4,
'display_freq': 5000,
}
args = set_if_not_set(args, defaults)
""" default to cuda:0 if device is not set"""
if args.device is None:
args.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
else:
args.device = torch.device(args.device)
def counter():
"""
counter to keep track of run id
creates a file .run_id in the current directory which stores the most recent id
"""
run_id_pid = Path('./.run_id')
count = 1
if run_id_pid.exists():
with run_id_pid.open('r+') as f:
last_id = int(f.readline())
last_id += 1
count = last_id
f.seek(0)
f.write(str(last_id))
else:
with run_id_pid.open('w+') as f:
f.write(str(count))
return count
''' if run_id not explicitly set, then guess it'''
if args.run_id == -1:
args.run_id = counter()
return args