-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
244 lines (229 loc) · 7.04 KB
/
parser.py
File metadata and controls
244 lines (229 loc) · 7.04 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
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
import os
import argparse
from utils import timestamp
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, description):
super(ArgumentParser, self).__init__(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=description,
add_help=True,
allow_abbrev=False
)
def add_model_args(self):
group = self.add_argument_group('model')
group.add_argument(
'--backbone',
help='Visual backbone. "+tr" adds an transformer after the CNN',
type=str,
default='resnet50',
choices=(
'resnet18', 'resnet34', 'resnet50', 'resnet101', # imagenet
'resnet18+tr', 'resnet34+tr', 'resnet50+tr', 'resnet101+tr',
'resnet18+fpn', 'resnet34+fpn', 'resnet50+fpn', 'resnet101+fpn',
'resnet50d', # COCO detection
'resnet50d+tr',
'resnet50d+fpn',
'resnet50s', 'resnet101s', # COCO segmentation
'resnet50s+tr', 'resnet101s+tr',
'resnet50s+fpn', 'resnet101s+fpn',
'cspdarknet53', # timm
'efficientnet-b0', 'efficientnet-b3',
),
)
group.add_argument(
'--mask-pooling',
help='if set, pool visual features using a mask',
action='store_true'
)
group.add_argument(
'--dropout-p',
help='Dropout p',
type=float,
default=0.1,
)
group.add_argument(
'--num-heads',
help='number of heads for the cross-modal encoder',
type=int,
default=8,
)
group.add_argument(
'--num-layers',
help='number of layers for the cross-modal encoder',
type=int,
default=6,
)
group.add_argument(
'--num-conv',
help='number of convolutional blocks (post transformer)',
type=int,
default=8,
)
def add_data_args(self):
group = self.add_argument_group('data')
group.add_argument(
'--dataset',
help='dataset',
type=str,
default='refcoco',
choices=('refclef', 'refcoco', 'refcoco+', 'refcocog', 'vg')
)
group.add_argument(
'--max-length',
help='max token sequence length',
type=int,
default=32
)
group.add_argument(
'--input-size',
help='images will be resized to INPUT_SIZExINPUT_SIZE pixels',
type=int,
default=512
)
def add_loss_args(self):
group = self.add_argument_group('loss function')
group.add_argument(
'--beta',
help='smooth L1 loss beta parameter',
default=0.1,
type=float
)
group.add_argument(
'--gamma',
help='GIoU loss term weight',
default=0.1,
type=float
)
group.add_argument(
'--mu',
help='box segmentation term weight',
default=0.1,
type=float
)
def add_trainer_args(self):
group = self.add_argument_group('trainer')
group.add_argument(
'--learning-rate',
help='learning rate',
default=1e-4,
type=float
)
group.add_argument(
'--weight-decay',
help='weight decay',
default=0.0,
type=float
)
group.add_argument(
'--batch-size',
help='batch size',
default=16,
type=int
)
group.add_argument(
'--grad-steps',
help='accumulates gradient every GRAD_STEPS batches',
default=1,
type=int
)
group.add_argument(
'--max-epochs',
help='max number of epochs',
default=50,
type=int
)
group.add_argument(
'--scheduler',
help='use a multistep scheduler (no warmup)',
action='store_true'
)
def add_runtime_args(self):
group = self.add_argument_group('runtime arguments')
group.add_argument(
'--gpus',
help='GPUs identifiers',
type=str
)
# group.add_argument(
# '--num-threads',
# help='torch num threads',
# type=int
# )
group.add_argument(
'--num-workers',
help='dataloader num workers',
type=int
)
group.add_argument(
'--seed',
help='random seed',
type=int,
default=3407 # https://arxiv.org/pdf/2109.08203v1.pdf :-)
)
group.add_argument(
'--suffix',
help='path suffix',
type=str
)
group.add_argument(
'--cache',
help='cache path',
type=str,
default='./cache'
)
group.add_argument(
'--debug',
help='if set, run on a small percentage of the (training) data',
action='store_true',
)
group.add_argument(
'--early-stopping',
help='if set, enables the early stopping callback',
action='store_true',
)
group.add_argument(
'--amp',
help='if set, enables automatic mixed precision (AMP) training',
action='store_true',
)
group.add_argument(
'--force-ddp',
help='if set, force strategy=DDP',
action='store_true',
)
group.add_argument(
'--profile',
help='if set, enables profiling',
action='store_true',
)
group.add_argument(
'--checkpoint',
help='resume training from CHECKPOINT',
type=str
)
group.add_argument(
'--save-last',
help='if set, allways save last epoch checkpoint',
action='store_true',
)
@staticmethod
def args_to_path(args, keys, values_only=False):
path = os.path.join(os.path.abspath(args.cache), timestamp())
keys = [k.lstrip('-').replace('-', '_') for k in keys if k not in ('', None)]
vargs = vars(args)
for k in keys:
if k == 'suffix':
continue
if values_only:
if type(vargs[k]) is bool:
path += f'_{int(vargs[k])}' # _0 or _1 for a bool var
else:
path += f'_{vargs[k]}'
else:
if type(vargs[k]) is bool and vargs[k]: # if bool and set
path += f'_{k.replace("_", "-")}'
else:
path += f'_{k.replace("_", "-")}_{vargs[k]}'
if args.suffix is not None:
path += f'_{args.suffix}'
return path