-
Notifications
You must be signed in to change notification settings - Fork 52
/
coco_multilabel_unet.py
292 lines (283 loc) · 9.41 KB
/
coco_multilabel_unet.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
import cv2
# 1. configuration for inference
nclasses = 80
ignore_label = 255
multi_label = True
crop_size_h, crop_size_w = 513, 513
test_size_h, test_size_w = 641, 641
image_pad_value = (123.675, 116.280, 103.530)
img_norm_cfg = dict(
max_pixel_value=255.0,
std=(0.229, 0.224, 0.225),
mean=(0.485, 0.456, 0.406),
)
norm_cfg = dict(type='SyncBN')
gfpn_post = dict(
type='ConvModules',
kernel_size=3,
padding=1,
norm_cfg=norm_cfg,
act_cfg=dict(type='Relu', inplace=True),
num_convs=2,
)
gfpn_upsample_2x = dict(
type='Upsample',
scale_factor=2,
scale_bias=-1,
mode='bilinear',
align_corners=True,
)
inference = dict(
multi_label=multi_label,
transforms=[
dict(type='PadIfNeeded', min_height=test_size_h, min_width=test_size_w,
value=image_pad_value, mask_value=ignore_label),
dict(type='Normalize', **img_norm_cfg),
dict(type='ToTensor'),
],
model=dict(
# model/encoder
encoder=dict(
backbone=dict(
type='ResNet',
arch='resnet101',
pretrain=True,
norm_cfg=norm_cfg,
),
),
# model/decoder
decoder=dict(
type='GFPN',
# model/decoder/blocks
neck=[
# model/decoder/blocks/block1
dict(
type='JunctionBlock',
fusion_method='concat',
verticals=[{'from_layer': 'c5', **gfpn_upsample_2x}, ],
laterals=[dict(from_layer='c4', type='Identity'), ],
to_layer='m4',
), # 16
dict(
type='JunctionBlock',
laterals=[
{
'from_layer': 'm4',
'in_channels': 3072, # 2048 + 1024 resnet101
# in_channels: 768, # 512 + 256 resnet18
'out_channels': 256,
**gfpn_post,
},
],
to_layer='p4',
), # 16
# model/decoder/blocks/block2
dict(
type='JunctionBlock',
fusion_method='concat',
verticals=[{'from_layer': 'p4', **gfpn_upsample_2x}, ],
laterals=[dict(from_layer='c3', type='Identity'), ],
to_layer='m3',
), # 8
dict(
type='JunctionBlock',
laterals=[
{
'from_layer': 'm3',
'in_channels': 768, # 256 + 512
# 'in_channels': 384, # 256 + 128
'out_channels': 128,
**gfpn_post,
},
],
to_layer='p3',
), # 8
# model/decoder/blocks/block3
dict(
type='JunctionBlock',
fusion_method='concat',
verticals=[{'from_layer': 'p3', **gfpn_upsample_2x}, ],
laterals=[dict(from_layer='c2', type='Identity'), ],
to_layer='m2',
), # 4
dict(
type='JunctionBlock',
laterals=[
{
'from_layer': 'm2',
'in_channels': 384, # 128 + 256
# 'in_channels': 192, # 128 + 64
'out_channels': 64,
**gfpn_post,
},
],
to_layer='p2',
), # 4
# model/decoder/blocks/block4
dict(
type='JunctionBlock',
fusion_method='concat',
verticals=[{'from_layer': 'p2', **gfpn_upsample_2x}, ],
laterals=[dict(from_layer='c1', type='Identity'), ],
to_layer='m1',
), # 2
dict(
type='JunctionBlock',
laterals=[
{
'from_layer': 'm1',
'in_channels': 128,
'out_channels': 32,
**gfpn_post,
},
],
to_layer='p1',
), # 2
# model/decoder/blocks/block5
dict(
type='JunctionBlock',
verticals=[{'from_layer': 'p1', **gfpn_upsample_2x}, ],
to_layer='m0',
), # 1
dict(
type='JunctionBlock',
laterals=[
{
'from_layer': 'm0',
'in_channels': 32,
'out_channels': 16,
**gfpn_post,
},
],
to_layer='p0',
), # 1
]),
# model/decoer/head
head=dict(
type='Head',
in_channels=16,
out_channels=nclasses,
num_convs=0,
),
),
)
# 2. configuration for train/test
root_workdir = 'workdir'
dataset_type = 'CocoDataset'
dataset_root = 'data/COCO2017'
common = dict(
seed=0,
logger=dict(
handlers=(
dict(type='StreamHandler', level='INFO'),
dict(type='FileHandler', level='INFO'),
),
),
cudnn_deterministic=False,
cudnn_benchmark=True,
metrics=[
dict(type='MultiLabelIoU', num_classes=nclasses),
dict(type='MultiLabelMIoU', num_classes=nclasses),
],
dist_params=dict(backend='nccl'),
)
## 2.1 configuration for test
test = dict(
data=dict(
dataset=dict(
type=dataset_type,
root=dataset_root,
ann_file='instances_val2017.json',
img_prefix='val2017',
multi_label=multi_label,
),
transforms=inference['transforms'],
sampler=dict(
type='DefaultSampler',
),
dataloader=dict(
type='DataLoader',
samples_per_gpu=4,
workers_per_gpu=4,
shuffle=False,
drop_last=False,
pin_memory=True,
),
),
# tta=dict(
# scales=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
# biases=[0.5, 0.25, 0.0, -0.25, -0.5, -0.75],
# flip=True,
# ),
)
## 2.2 configuration for train
max_epochs = 50
train = dict(
data=dict(
train=dict(
dataset=dict(
type=dataset_type,
root=dataset_root,
ann_file='instances_train2017.json',
img_prefix='train2017',
multi_label=multi_label,
),
transforms=[
dict(type='RandomScale', scale_limit=(0.5, 2),
interpolation=cv2.INTER_LINEAR),
dict(type='PadIfNeeded', min_height=crop_size_h,
min_width=crop_size_w, value=image_pad_value,
mask_value=ignore_label),
dict(type='RandomCrop', height=crop_size_h, width=crop_size_w),
dict(type='Rotate', limit=10, interpolation=cv2.INTER_LINEAR,
border_mode=cv2.BORDER_CONSTANT, value=image_pad_value,
mask_value=ignore_label, p=0.5),
dict(type='GaussianBlur', blur_limit=7, p=0.5),
dict(type='HorizontalFlip', p=0.5),
dict(type='Normalize', **img_norm_cfg),
dict(type='ToTensor'),
],
sampler=dict(
type='DefaultSampler',
),
dataloader=dict(
type='DataLoader',
samples_per_gpu=8,
workers_per_gpu=4,
shuffle=True,
drop_last=True,
pin_memory=True,
),
),
val=dict(
dataset=dict(
type=dataset_type,
root=dataset_root,
ann_file='instances_val2017.json',
img_prefix='val2017',
multi_label=multi_label,
),
transforms=inference['transforms'],
sampler=dict(
type='DefaultSampler',
),
dataloader=dict(
type='DataLoader',
samples_per_gpu=8,
workers_per_gpu=4,
shuffle=False,
drop_last=False,
pin_memory=True,
),
),
),
resume=None,
criterion=dict(type='BCEWithLogitsLoss', ignore_index=ignore_label),
optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001),
lr_scheduler=dict(type='PolyLR', max_epochs=max_epochs),
max_epochs=max_epochs,
trainval_ratio=1,
log_interval=10,
snapshot_interval=5,
save_best=True,
)