-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.py
457 lines (436 loc) · 17.5 KB
/
experiments.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import itertools
import logging
import math
def get_model_base(architecture, backbone):
architecture = architecture.replace('sfa_', '')
architecture = architecture.replace('_nodbn', '')
if 'segformer' in architecture:
return {
'mitb5': f'_base_/models/{architecture}_b5.py',
# It's intended that <=b4 refers to b5 config
'mitb4': f'_base_/models/{architecture}_b5.py',
'mitb3': f'_base_/models/{architecture}_b5.py',
'r101v1c': f'_base_/models/{architecture}_r101.py',
}[backbone]
if 'daformer_' in architecture and 'mitb5' in backbone:
return f'_base_/models/{architecture}_mitb5.py'
if 'upernet' in architecture and 'mit' in backbone:
return f'_base_/models/{architecture}_mit.py'
assert 'mit' not in backbone or '-del' in backbone
return {
'dlv2': '_base_/models/deeplabv2_r50-d8.py',
'dlv2red': '_base_/models/deeplabv2red_r50-d8.py',
'dlv3p': '_base_/models/deeplabv3plus_r50-d8.py',
'da': '_base_/models/danet_r50-d8.py',
'isa': '_base_/models/isanet_r50-d8.py',
'uper': '_base_/models/upernet_r50.py',
}[architecture]
def get_pretraining_file(backbone):
if 'mitb5' in backbone:
return 'pretrained/mit_b5.pth'
if 'mitb4' in backbone:
return 'pretrained/mit_b4.pth'
if 'mitb3' in backbone:
return 'pretrained/mit_b3.pth'
if 'r101v1c' in backbone:
return 'open-mmlab://resnet101_v1c'
return {
'r50v1c': 'open-mmlab://resnet50_v1c',
'x50-32': 'open-mmlab://resnext50_32x4d',
'x101-32': 'open-mmlab://resnext101_32x4d',
's50': 'open-mmlab://resnest50',
's101': 'open-mmlab://resnest101',
's200': 'open-mmlab://resnest200',
}[backbone]
def get_backbone_cfg(backbone):
for i in [1, 2, 3, 4, 5]:
if backbone == f'mitb{i}':
return dict(type=f'mit_b{i}')
if backbone == f'mitb{i}-del':
return dict(_delete_=True, type=f'mit_b{i}')
return {
'r50v1c': {
'depth': 50
},
'r101v1c': {
'depth': 101
},
'x50-32': {
'type': 'ResNeXt',
'depth': 50,
'groups': 32,
'base_width': 4,
},
'x101-32': {
'type': 'ResNeXt',
'depth': 101,
'groups': 32,
'base_width': 4,
},
's50': {
'type': 'ResNeSt',
'depth': 50,
'stem_channels': 64,
'radix': 2,
'reduction_factor': 4,
'avg_down_stride': True
},
's101': {
'type': 'ResNeSt',
'depth': 101,
'stem_channels': 128,
'radix': 2,
'reduction_factor': 4,
'avg_down_stride': True
},
's200': {
'type': 'ResNeSt',
'depth': 200,
'stem_channels': 128,
'radix': 2,
'reduction_factor': 4,
'avg_down_stride': True,
},
}[backbone]
def update_decoder_in_channels(cfg, architecture, backbone):
cfg.setdefault('model', {}).setdefault('decode_head', {})
if 'dlv3p' in architecture and 'mit' in backbone:
cfg['model']['decode_head']['c1_in_channels'] = 64
if 'sfa' in architecture:
cfg['model']['decode_head']['in_channels'] = 512
return cfg
def setup_rcs(cfg, temperature):
cfg.setdefault('data', {}).setdefault('train', {})
cfg['data']['train']['rare_class_sampling'] = dict(
min_pixels=3000, class_temp=temperature, min_crop_ratio=0.5)
return cfg
def generate_experiment_cfgs(id):
def config_from_vars():
cfg = {'_base_': ['_base_/default_runtime.py'], 'n_gpus': n_gpus}
if seed is not None:
cfg['seed'] = seed
# Setup model config
architecture_mod = architecture
model_base = get_model_base(architecture_mod, backbone)
cfg['_base_'].append(model_base)
cfg['model'] = {
'pretrained': get_pretraining_file(backbone),
'backbone': get_backbone_cfg(backbone),
}
if 'sfa_' in architecture_mod:
cfg['model']['neck'] = dict(type='SegFormerAdapter')
if '_nodbn' in architecture_mod:
cfg['model'].setdefault('decode_head', {})
cfg['model']['decode_head']['norm_cfg'] = None
cfg = update_decoder_in_channels(cfg, architecture_mod, backbone)
# Setup UDA config
if uda == 'target-only':
cfg['_base_'].append(f'_base_/datasets/{target}_half_{crop}.py')
elif uda == 'source-only':
cfg['_base_'].append(
f'_base_/datasets/{source}_to_{target}_{crop}.py')
else:
cfg['_base_'].append(
f'_base_/datasets/uda_{source}_to_{target}_{crop}.py')
cfg['_base_'].append(f'_base_/uda/{uda}.py')
if 'dacs' in uda and plcrop:
cfg.setdefault('uda', {})
cfg['uda']['pseudo_weight_ignore_top'] = 15
cfg['uda']['pseudo_weight_ignore_bottom'] = 120
cfg['data'] = dict(
samples_per_gpu=batch_size,
workers_per_gpu=workers_per_gpu,
train={})
if 'dacs' in uda and rcs_T is not None:
cfg = setup_rcs(cfg, rcs_T)
# Setup optimizer and schedule
if 'dacs' in uda:
cfg['optimizer_config'] = None # Don't use outer optimizer
cfg['_base_'].extend(
[f'_base_/schedules/{opt}.py', f'_base_/schedules/{schedule}.py'])
cfg['optimizer'] = {'lr': lr}
cfg['optimizer'].setdefault('paramwise_cfg', {})
cfg['optimizer']['paramwise_cfg'].setdefault('custom_keys', {})
opt_param_cfg = cfg['optimizer']['paramwise_cfg']['custom_keys']
if pmult:
opt_param_cfg['head'] = dict(lr_mult=10.)
if 'mit' in backbone:
opt_param_cfg['pos_block'] = dict(decay_mult=0.)
opt_param_cfg['norm'] = dict(decay_mult=0.)
# Setup runner
cfg['runner'] = dict(type='IterBasedRunner', max_iters=iters)
cfg['checkpoint_config'] = dict(
by_epoch=False, interval=iters, max_keep_ckpts=1)
cfg['evaluation'] = dict(interval=iters // 10, metric='mIoU')
# Construct config name
uda_mod = uda
if 'dacs' in uda and rcs_T is not None:
uda_mod += f'_rcs{rcs_T}'
if 'dacs' in uda and plcrop:
uda_mod += '_cpl'
cfg['name'] = f'{source}2{target}_{uda_mod}_{architecture_mod}_' \
f'{backbone}_{schedule}'
cfg['exp'] = id
cfg['name_dataset'] = f'{source}2{target}'
cfg['name_architecture'] = f'{architecture_mod}_{backbone}'
cfg['name_encoder'] = backbone
cfg['name_decoder'] = architecture_mod
cfg['name_uda'] = uda_mod
cfg['name_opt'] = f'{opt}_{lr}_pm{pmult}_{schedule}' \
f'_{n_gpus}x{batch_size}_{iters // 1000}k'
if seed is not None:
cfg['name'] += f'_s{seed}'
cfg['name'] = cfg['name'].replace('.', '').replace('True', 'T') \
.replace('False', 'F').replace('cityscapes', 'cs') \
.replace('synthia', 'syn') \
.replace('darkzurich', 'dzur')
return cfg
# -------------------------------------------------------------------------
# Set some defaults
# -------------------------------------------------------------------------
cfgs = []
n_gpus = 1
batch_size = 2
iters = 40000
opt, lr, schedule, pmult = 'adamw', 0.00006, 'poly10warm', True
crop = '512x512'
datasets = [
('gta', 'cityscapes'),
]
architecture = None
workers_per_gpu = 4
rcs_T = None
plcrop = False
# -------------------------------------------------------------------------
# UDA Architecture Comparison (Table 1)
# -------------------------------------------------------------------------
if id == 1:
seeds = [0, 1, 2]
models = [
# Note: For the DeepLabV2 decoder, we follow AdaptSegNet as well as
# many follow-up works using the same source code for the network
# architecture (e.g. DACS or ProDA) and use only the dilation rates
# 6 and 12. We point this out as it is hidden in the source code by
# a return statement within a loop:
# https://github.com/wasidennis/AdaptSegNet/blob/fca9ff0f09dab45d44bf6d26091377ac66607028/model/deeplab.py#L116
('dlv2red', 'r101v1c'),
# Note: For the decoders used in combination with CNN encoders, we
# do not apply BatchNorm in the *decoder* as it decreases
# the UDA performance. In the encoder, BatchNorm is still applied.
# The decoder of DeepLabV2 has no BatchNorm layer by default.
('da_nodbn', 'r101v1c'),
('isa_nodbn', 'r101v1c'),
('dlv3p_nodbn', 'r101v1c'),
('segformer', 'mitb5'),
]
udas = [
'source-only',
'dacs',
'target-only',
]
for (source, target), (architecture, backbone), uda, seed in \
itertools.product(datasets, models, udas, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# SegFormer Encoder / Decoder Ablation (Table 2)
# -------------------------------------------------------------------------
elif id == 2:
seeds = [0, 1, 2]
models = [
# ('segformer', 'mitb5'), # already run in exp 1
('sfa_dlv3p_nodbn', 'mitb5-del'),
('segformer', 'r101v1c'),
# ('dlv3p_nodbn', 'r101v1c'), # already run in exp 1
]
udas = [
'dacs',
'target-only',
]
for (source, target), (architecture, backbone), uda, seed in \
itertools.product(datasets, models, udas, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Encoder Study (Table 3)
# -------------------------------------------------------------------------
elif id == 3:
seeds = [0]
models = [
('dlv2red', 'r50v1c'),
# ('dlv2red', 'r101v1c'), # already run in exp 1
('dlv2red', 's50'),
('dlv2red', 's101'),
('dlv2red', 's200'),
('segformer', 'mitb3'),
('segformer', 'mitb4'),
# ('segformer', 'mitb5'), # already run in exp 1
]
udas = [
'source-only',
'dacs',
'target-only',
]
for (source, target), (architecture, backbone), uda, seed in \
itertools.product(datasets, models, udas, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Learning Rate Warmup Ablation (Table 4)
# -------------------------------------------------------------------------
elif id == 4:
seeds = [0]
models = [
('dlv2red', 'r101v1c'),
('segformer', 'mitb5'),
]
udas = ['dacs', 'target-only']
opts = [
('adamw', 0.00006, 'poly10', True),
# ('adamw', 0.00006, 'poly10warm', True), # already run in exp 1
]
for (source, target), (architecture, backbone), \
(opt, lr, schedule, pmult), uda, seed in \
itertools.product(datasets, models, opts, udas, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# RCS and FD (Table 5)
# -------------------------------------------------------------------------
elif id == 5:
seeds = [0, 1, 2]
for architecture, backbone, uda, rcs_T, plcrop in [
('segformer', 'mitb5', 'dacs', math.inf, False),
('segformer', 'mitb5', 'dacs', 0.01, False),
('segformer', 'mitb5', 'dacs_fd', None, False),
('segformer', 'mitb5', 'dacs_fdthings', None, False),
('segformer', 'mitb5', 'dacs_fdthings', 0.01, False),
('segformer', 'mitb5', 'dacs_a999_fdthings', 0.01, True),
('dlv2red', 'r101v1c', 'dacs_a999_fdthings', 0.01, True),
]:
for (source, target), seed in \
itertools.product(datasets, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Decoder Study (Table 7)
# -------------------------------------------------------------------------
elif id == 6:
seeds = [0, 1, 2]
udas = [
'dacs_a999_fdthings',
'target-only',
]
rcs_T = 0.01
plcrop = True
models = [
# ('segformer', 'mitb5'), # already run in exp 5
('daformer_conv1', 'mitb5'), # this is segformer with 256 channels
('upernet', 'mitb5'),
('upernet_ch256', 'mitb5'),
('daformer_isa', 'mitb5'),
('daformer_sepaspp_bottleneck', 'mitb5'), # Context only at F4
('daformer_aspp', 'mitb5'), # DAFormer w/o DSC
('daformer_sepaspp', 'mitb5'), # DAFormer
]
for (source, target), (architecture, backbone), uda, seed in \
itertools.product(datasets, models, udas, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Final DAFormer (Table 6)
# -------------------------------------------------------------------------
elif id == 7:
seeds = [0, 1, 2]
datasets = [
# ('gta', 'cityscapes'), # already run in exp 6
('synthia', 'cityscapes'),
]
architecture, backbone = ('daformer_sepaspp', 'mitb5')
uda = 'dacs_a999_fdthings'
rcs_T = 0.01
plcrop = True
for (source, target), seed in \
itertools.product(datasets, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Further Datasets
# -------------------------------------------------------------------------
elif id == 8:
seeds = [0, 1, 2]
datasets = [
('cityscapes', 'acdc'),
('cityscapes', 'darkzurich'),
]
architecture, backbone = ('daformer_sepaspp', 'mitb5')
uda = 'dacs_a999_fdthings'
rcs_T = 0.01
plcrop = True
for (source, target), seed in \
itertools.product(datasets, seeds):
cfg = config_from_vars()
cfgs.append(cfg)
# -------------------------------------------------------------------------
# Architecture Startup Test
# -------------------------------------------------------------------------
elif id == 100:
iters = 2
seeds = [0]
models = [
('dlv2red', 'r101v1c'),
('dlv3p_nodbn', 'r101v1c'),
('da_nodbn', 'r101v1c'),
('segformer', 'mitb5'),
('isa_nodbn', 'r101v1c'),
('dlv2red', 'r50v1c'),
('dlv2red', 's50'),
('dlv2red', 's101'),
('dlv2red', 's200'),
('dlv2red', 'x50-32'),
('dlv2red', 'x101-32'),
('segformer', 'mitb4'),
('segformer', 'mitb3'),
('sfa_dlv3p_nodbn', 'mitb5-del'),
('segformer', 'r101v1c'),
('daformer_conv1', 'mitb5'),
('daformer_isa', 'mitb5'),
('daformer_sepaspp_bottleneck', 'mitb5'),
('daformer_aspp', 'mitb5'),
('daformer_sepaspp', 'mitb5'),
('upernet', 'mitb5'),
('upernet_ch256', 'mitb5'),
]
udas = ['target-only']
for (source, target), (architecture, backbone), uda, seed in \
itertools.product(datasets, models, udas, seeds):
cfg = config_from_vars()
cfg['log_level'] = logging.ERROR
cfg['evaluation']['interval'] = 100
cfgs.append(cfg)
# -------------------------------------------------------------------------
# UDA Training Startup Test
# -------------------------------------------------------------------------
elif id == 101:
iters = 2
seeds = [0]
for architecture, backbone, uda, rcs_T, plcrop in [
('segformer', 'mitb5', 'source-only', None, False),
('segformer', 'mitb5', 'target-only', None, False),
('segformer', 'mitb5', 'dacs', None, False),
('segformer', 'mitb5', 'dacs', math.inf, False),
('segformer', 'mitb5', 'dacs', 0.01, False),
('segformer', 'mitb5', 'dacs_fd', None, False),
('segformer', 'mitb5', 'dacs_fdthings', None, False),
('segformer', 'mitb5', 'dacs_fdthings', 0.01, False),
('segformer', 'mitb5', 'dacs_a999_fdthings', 0.01, True),
]:
for (source, target), seed in \
itertools.product(datasets, seeds):
cfg = config_from_vars()
cfg['log_level'] = logging.ERROR
cfg['evaluation']['interval'] = 100
cfgs.append(cfg)
else:
raise NotImplementedError('Unknown id {}'.format(id))
return cfgs