forked from Surrey-EEEM071-CVDL/CourseWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
312 lines (292 loc) · 9.71 KB
/
args.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
# Copyright (c) EEEM071, University of Surrey
import argparse
def argument_parser():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
# ************************************************************
# Datasets (general)
# ************************************************************
parser.add_argument(
"--root", type=str, default="./datasets", help="root path to data directory"
)
parser.add_argument(
"-s",
"--source-names",
type=str,
required=True,
nargs="+",
help="source dataset for training(delimited by space)",
)
parser.add_argument(
"-t",
"--target-names",
type=str,
required=True,
nargs="+",
help="target dataset for testing(delimited by space)",
)
parser.add_argument(
"-j",
"--workers",
default=4,
type=int,
help="number of data loading workers (tips: 4 or 8 times number of gpus)",
)
# split-id not used
parser.add_argument(
"--split-id", type=int, default=0, help="split index (note: 0-based)"
)
parser.add_argument("--height", type=int, default=128, help="height of an image")
parser.add_argument("--width", type=int, default=256, help="width of an image")
parser.add_argument(
"--train-sampler",
type=str,
default="RandomSampler",
help="sampler for trainloader",
)
# ************************************************************
# Data augmentation
# ************************************************************
parser.add_argument(
"--random-erase",
action="store_true",
help="use random erasing for data augmentation",
)
parser.add_argument(
"--color-jitter",
action="store_true",
help="randomly change the brightness, contrast and saturation",
)
parser.add_argument(
"--color-aug",
action="store_true",
help="randomly alter the intensities of RGB channels",
)
# ************************************************************
# Optimization options
# ************************************************************
parser.add_argument(
"--optim",
type=str,
default="adam",
help="optimization algorithm (see optimizers.py)",
)
parser.add_argument(
"--lr", default=0.0003, type=float, help="initial learning rate"
)
parser.add_argument(
"--weight-decay", default=5e-04, type=float, help="weight decay"
)
# sgd
parser.add_argument(
"--momentum",
default=0.9,
type=float,
help="momentum factor for sgd and rmsprop",
)
parser.add_argument(
"--sgd-dampening", default=0, type=float, help="sgd's dampening for momentum"
)
parser.add_argument(
"--sgd-nesterov",
action="store_true",
help="whether to enable sgd's Nesterov momentum",
)
# rmsprop
parser.add_argument(
"--rmsprop-alpha", default=0.99, type=float, help="rmsprop's smoothing constant"
)
# adam/amsgrad
parser.add_argument(
"--adam-beta1",
default=0.9,
type=float,
help="exponential decay rate for adam's first moment",
)
parser.add_argument(
"--adam-beta2",
default=0.999,
type=float,
help="exponential decay rate for adam's second moment",
)
# ************************************************************
# Training hyperparameters
# ************************************************************
parser.add_argument(
"--max-epoch", default=60, type=int, help="maximum epochs to run"
)
parser.add_argument(
"--start-epoch",
default=0,
type=int,
help="manual epoch number (useful when restart)",
)
parser.add_argument(
"--train-batch-size", default=32, type=int, help="training batch size"
)
parser.add_argument(
"--test-batch-size", default=100, type=int, help="test batch size"
)
# ************************************************************
# Learning rate scheduler options
# ************************************************************
parser.add_argument(
"--lr-scheduler",
type=str,
default="multi_step",
help="learning rate scheduler (see lr_schedulers.py)",
)
parser.add_argument(
"--stepsize",
default=[20, 40],
nargs="+",
type=int,
help="stepsize to decay learning rate",
)
parser.add_argument("--gamma", default=0.1, type=float, help="learning rate decay")
# ************************************************************
# Cross entropy loss-specific setting
# ************************************************************
parser.add_argument(
"--label-smooth",
action="store_true",
help="use label smoothing regularizer in cross entropy loss",
)
# ************************************************************
# Hard triplet loss-specific setting
# ************************************************************
parser.add_argument(
"--margin", type=float, default=0.3, help="margin for triplet loss"
)
parser.add_argument(
"--num-instances", type=int, default=4, help="number of instances per identity"
)
parser.add_argument(
"--lambda-xent",
type=float,
default=1,
help="weight to balance cross entropy loss",
)
parser.add_argument(
"--lambda-htri",
type=float,
default=1,
help="weight to balance hard triplet loss",
)
# ************************************************************
# Architecture
# ************************************************************
parser.add_argument("-a", "--arch", type=str, default="resnet50")
parser.add_argument(
"--no-pretrained", action="store_true", help="do not load pretrained weights"
)
# ************************************************************
# Test settings
# ************************************************************
parser.add_argument(
"--load-weights",
type=str,
default="",
help="load pretrained weights but ignore layers that don't match in size",
)
parser.add_argument("--evaluate", action="store_true", help="evaluate only")
parser.add_argument(
"--eval-freq",
type=int,
default=-1,
help="evaluation frequency (set to -1 to test only in the end)",
)
parser.add_argument(
"--start-eval",
type=int,
default=0,
help="start to evaluate after a specific epoch",
)
parser.add_argument(
"--test_size",
type=int,
default=800,
help="test-size for vehicleID dataset, choices=[800,1600,2400]",
)
parser.add_argument("--query-remove", type=bool, default=True)
# ************************************************************
# Miscs
# ************************************************************
parser.add_argument("--print-freq", type=int, default=10, help="print frequency")
parser.add_argument("--seed", type=int, default=1, help="manual seed")
parser.add_argument(
"--resume",
type=str,
default="",
metavar="PATH",
help="resume from a checkpoint",
)
parser.add_argument(
"--save-dir", type=str, default="log", help="path to save log and model weights"
)
parser.add_argument("--use-cpu", action="store_true", help="use cpu")
parser.add_argument(
"--gpu-devices",
default="0",
type=str,
help="gpu device ids for CUDA_VISIBLE_DEVICES",
)
parser.add_argument(
"--visualize-ranks",
action="store_true",
help="visualize ranked results, only available in evaluation mode",
)
parser.add_argument(
"--use-avai-gpus",
action="store_true",
help="use available gpus instead of specified devices (useful when using managed clusters)",
)
return parser
def dataset_kwargs(parsed_args):
"""
Build kwargs for ImageDataManager in data_manager.py from
the parsed command-line arguments.
"""
return {
"source_names": parsed_args.source_names,
"target_names": parsed_args.target_names,
"root": parsed_args.root,
"split_id": parsed_args.split_id,
"height": parsed_args.height,
"width": parsed_args.width,
"train_batch_size": parsed_args.train_batch_size,
"test_batch_size": parsed_args.test_batch_size,
"workers": parsed_args.workers,
"train_sampler": parsed_args.train_sampler,
"random_erase": parsed_args.random_erase,
"color_jitter": parsed_args.color_jitter,
"color_aug": parsed_args.color_aug,
}
def optimizer_kwargs(parsed_args):
"""
Build kwargs for optimizer in optimizers.py from
the parsed command-line arguments.
"""
return {
"optim": parsed_args.optim,
"lr": parsed_args.lr,
"weight_decay": parsed_args.weight_decay,
"momentum": parsed_args.momentum,
"sgd_dampening": parsed_args.sgd_dampening,
"sgd_nesterov": parsed_args.sgd_nesterov,
"rmsprop_alpha": parsed_args.rmsprop_alpha,
"adam_beta1": parsed_args.adam_beta1,
"adam_beta2": parsed_args.adam_beta2,
}
def lr_scheduler_kwargs(parsed_args):
"""
Build kwargs for lr_scheduler in lr_schedulers.py from
the parsed command-line arguments.
"""
return {
"lr_scheduler": parsed_args.lr_scheduler,
"epochs": parsed_args.max_epoch,
"stepsize": parsed_args.stepsize,
"gamma": parsed_args.gamma,
}