forked from hysts/pytorch_image_classification
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathargparser.py
372 lines (323 loc) · 10.2 KB
/
argparser.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import json
from collections import OrderedDict
import torch
def _args2config(args, keys, json_keys):
if json_keys is None:
json_keys = []
args = vars(args)
config = OrderedDict()
for key in keys:
value = args[key]
if value is None:
continue
if key in json_keys and isinstance(value, str):
value = json.loads(value)
config[key] = value
return config
def _get_model_config(args):
keys = [
'arch',
'input_shape',
'n_classes',
# vgg
'n_channels',
'n_layers',
'use_bn',
#
'base_channels',
'block_type',
'depth',
# resnet_preact, se_resnet_preact
'remove_first_relu',
'add_last_bn',
'preact_stage',
# wrn
'widening_factor',
# densenet
'growth_rate',
'compression_rate',
# wrn, densenet
'drop_rate',
# pyramidnet
'pyramid_alpha',
# resnext
'cardinality',
# shake_shake
'shake_forward',
'shake_backward',
'shake_image',
# se_resnet_preact
'se_reduction',
]
json_keys = ['preact_stage']
config = _args2config(args, keys, json_keys)
return config
def _check_optim_config(config):
optimizer = config['optimizer']
for key in ['base_lr', 'weight_decay']:
message = 'Key `{}` must be specified.'.format(key)
assert key in config.keys(), message
if optimizer == 'sgd':
for key in ['momentum', 'nesterov']:
message = 'When using SGD, key `{}` must be specified.'.format(key)
assert key in config.keys(), message
elif optimizer == 'adam':
for key in ['betas']:
message = 'When using Adam, key `{}` must be specified.'.format(
key)
assert key in config.keys(), message
elif optimizer == 'lars':
for key in ['momentum']:
message = 'When using LARS, key `{}` must be specified.'.format(
key)
assert key in config.keys(), message
scheduler = config['scheduler']
if scheduler == 'multistep':
for key in ['milestones', 'lr_decay']:
message = 'Key `{}` must be specified.'.format(key)
assert key in config.keys(), message
elif scheduler == 'cosine':
for key in ['lr_min']:
message = 'Key `{}` must be specified.'.format(key)
assert key in config.keys(), message
elif scheduler == 'sgdr':
for key in ['lr_min', 'T0', 'Tmult']:
message = 'Key `{}` must be specified.'.format(key)
assert key in config.keys(), message
def _get_optim_config(args):
keys = [
'epochs',
'batch_size',
'optimizer',
'base_lr',
'weight_decay',
'momentum',
'nesterov',
'gradient_clip',
'scheduler',
'milestones',
'lr_decay',
'lr_min',
'T0',
'Tmult',
'betas',
'lars_eps',
'lars_thresh',
]
json_keys = ['milestones', 'betas']
config = _args2config(args, keys, json_keys)
_check_optim_config(config)
return config
def _get_data_config(args):
keys = [
'dataset',
'n_classes',
'num_workers',
'batch_size',
'use_random_crop',
'random_crop_padding',
'use_horizontal_flip',
'use_cutout',
'use_dual_cutout',
'cutout_size',
'cutout_prob',
'cutout_inside',
'use_random_erasing',
'dual_cutout_alpha',
'random_erasing_prob',
'random_erasing_area_ratio_range',
'random_erasing_min_aspect_ratio',
'random_erasing_max_attempt',
'use_mixup',
'mixup_alpha',
'use_ricap',
'ricap_beta',
'use_label_smoothing',
'label_smoothing_epsilon',
]
json_keys = ['random_erasing_area_ratio_range']
config = _args2config(args, keys, json_keys)
config['use_gpu'] = args.device != 'cpu'
_check_data_config(config)
return config
def _check_data_config(config):
if config['use_cutout'] and config['use_dual_cutout']:
raise ValueError(
'Only one of `use_cutout` and `use_dual_cutout` can be `True`.')
if sum([
config['use_mixup'], config['use_ricap'], config['use_dual_cutout']
]) > 1:
raise ValueError(
'Only one of `use_mixup`, `use_ricap` and `use_dual_cutout` can be `True`.'
)
def _get_run_config(args):
keys = [
'outdir',
'seed',
'test_first',
'device',
'fp16',
'tensorboard',
'tensorboard_train_images',
'tensorboard_test_images',
'tensorboard_model_params',
]
config = _args2config(args, keys, None)
return config
def _get_env_info(args):
info = OrderedDict({
'pytorch_version': torch.__version__,
'cuda_version': torch.version.cuda,
'cudnn_version': torch.backends.cudnn.version(),
})
def _get_device_info(device_id):
name = torch.cuda.get_device_name(device_id)
capability = torch.cuda.get_device_capability(device_id)
capability = '{}.{}'.format(*capability)
return name, capability
if args.device != 'cpu':
for gpu_id in range(torch.cuda.device_count()):
name, capability = _get_device_info(gpu_id)
info['gpu{}'.format(gpu_id)] = OrderedDict({
'name':
name,
'capability':
capability,
})
return info
def _cleanup_args(args):
# architecture
if args.arch == 'vgg':
args.base_channels = None
args.depth = None
if args.arch != 'vgg':
args.n_channels = None
args.n_layers = None
args.use_bn = None
if args.arch not in [
'resnet', 'resnet_preact', 'densenet', 'pyramidnet',
'se_resnet_preact'
]:
args.block_type = None
if args.arch not in ['resnet_preact', 'se_resnet_preact']:
args.remove_first_relu = None
args.add_last_bn = None
args.preact_stage = None
if args.arch != 'wrn':
args.widening_factor = None
if args.arch != 'densenet':
args.growth_rate = None
args.compression_rate = None
if args.arch not in ['wrn', 'densenet']:
args.drop_rate = None
if args.arch != 'pyramidnet':
args.pyramid_alpha = None
if args.arch != 'resnext':
args.cardinality = None
if args.arch != 'shake_shake':
args.shake_forward = None
args.shake_backward = None
args.shake_image = None
if args.arch != 'se_resnet_preact':
args.se_reduction = None
# optimizer
if args.optimizer not in ['sgd', 'lars']:
args.momentum = None
if args.optimizer != 'sgd':
args.nesterov = None
if args.optimizer != 'adam':
args.betas = None
if args.optimizer != 'lars':
args.lars_eps = None
args.lars_thresh = None
# scheduler
if args.scheduler != 'multistep':
args.milestones = None
args.lr_decay = None
if args.scheduler not in ['cosine', 'sgdr']:
args.lr_min = None
if args.scheduler != 'sgdr':
args.T0 = None
args.Tmult = None
# standard data augmentation
if args.use_random_crop is None:
if args.dataset in ['CIFAR10', 'CIFAR100', 'FashionMNIST', 'KMNIST', 'K49']:
args.use_random_crop = True
else:
args.use_random_crop = False
if not args.use_random_crop:
args.random_crop_padding = None
if args.use_horizontal_flip is None:
if args.dataset in ['CIFAR10', 'CIFAR100', 'FashionMNIST']:
args.use_horizontal_flip = True
else:
args.use_horizontal_flip = False
# (dual-)cutout
if not args.use_cutout and not args.use_dual_cutout:
args.cutout_size = None
args.cutout_prob = None
args.cutout_inside = None
if not args.use_dual_cutout:
args.dual_cutout_alpha = None
# random erasing
if not args.use_random_erasing:
args.random_erasing_prob = None
args.random_erasing_area_ratio_range = None
args.random_erasing_min_aspect_ratio = None
args.random_erasing_max_attempt = None
# mixup
if not args.use_mixup:
args.mixup_alpha = None
# RICAP
if not args.use_ricap:
args.ricap_beta = None
# label smoothing
if not args.use_label_smoothing:
args.label_smoothing_epsilon = None
# TensorBoard
if not args.tensorboard:
args.tensorboard_train_images = False
args.tensorboard_test_images = False
args.tensorboard_model_params = False
# data
if args.dataset == 'CIFAR10':
args.input_shape = (1, 3, 32, 32)
args.n_classes = 10
elif args.dataset == 'CIFAR100':
args.input_shape = (1, 3, 32, 32)
args.n_classes = 100
elif args.dataset == 'K49':
args.input_shape = (1, 1, 28, 28)
args.n_classes = 49
elif 'MNIST' in args.dataset:
args.input_shape = (1, 1, 28, 28)
args.n_classes = 10
return args
def _set_default_values(args):
if args.config is not None:
with open(args.config, 'r') as fin:
config = json.load(fin)
d_args = vars(args)
for config_key, default_config in config.items():
if config_key == 'env_info':
continue
for key, default_value in default_config.items():
if key not in d_args.keys() or d_args[key] is None:
setattr(args, key, default_value)
return args
def get_config(args):
if args.arch is None and args.config is None:
raise RuntimeError(
'One of args.arch and args.config must be specified')
if args.config is None:
args.config = 'configs/{}.json'.format(args.arch)
args = _set_default_values(args)
args = _cleanup_args(args)
config = OrderedDict({
'model_config': _get_model_config(args),
'optim_config': _get_optim_config(args),
'data_config': _get_data_config(args),
'run_config': _get_run_config(args),
'env_info': _get_env_info(args),
})
return config