-
Notifications
You must be signed in to change notification settings - Fork 82
/
lfd_resnet.py
509 lines (427 loc) · 22.5 KB
/
lfd_resnet.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# -*- coding: utf-8 -*-
import os
import torch
import torch.nn as nn
__all__ = ['FastBlock', 'FasterBlock', 'FastestBlock', 'LFDResNet']
def get_operator_from_cfg(operator_cfg):
operator_cfg_copy = operator_cfg.copy()
construct_str = 'nn.'
construct_str += operator_cfg_copy.pop('type') + '('
for k, v in operator_cfg_copy.items():
construct_str += k + '=' + str(v) + ','
construct_str += ')'
return eval(construct_str)
class FastBlock(nn.Module):
def __init__(self,
num_input_channels,
num_block_channels,
stride=1,
downsample=None,
activation_cfg=dict(type='ReLU', inplace=True),
norm_cfg=None):
super(FastBlock, self).__init__()
if downsample is not None:
assert stride == 2
if norm_cfg is not None:
assert norm_cfg['type'] in ['BatchNorm2d', 'GroupNorm']
self._num_input_channel = num_input_channels
self._num_block_channel = num_block_channels
self._stride = stride
self._activation_cfg = activation_cfg
self._norm_cfg = norm_cfg
self._downsample = downsample
self._conv1 = nn.Conv2d(in_channels=self._num_input_channel, out_channels=self._num_block_channel, kernel_size=3, stride=self._stride, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm1 = get_operator_from_cfg(temp_norm_cfg)
self._activation = get_operator_from_cfg(self._activation_cfg)
self._conv2 = nn.Conv2d(in_channels=self._num_block_channel, out_channels=self._num_block_channel, kernel_size=1, stride=1, padding=0, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm2 = get_operator_from_cfg(temp_norm_cfg)
self._conv3 = nn.Conv2d(in_channels=self._num_block_channel, out_channels=self._num_block_channel, kernel_size=3, stride=1, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm3 = get_operator_from_cfg(temp_norm_cfg)
def forward(self, x):
identity = x
out = self._conv1(x)
if self._norm_cfg is not None:
out = self._norm1(out)
out = self._activation(out)
out = self._conv2(out)
if self._norm_cfg is not None:
out = self._norm2(out)
out = self._activation(out)
out = self._conv3(out)
if self._norm_cfg is not None:
out = self._norm3(out)
if self._downsample is not None:
identity = self._downsample(x)
out += identity
out = self._activation(out)
return out
class FasterBlock(nn.Module):
def __init__(self,
num_input_channels,
num_block_channels,
stride=1,
downsample=None,
activation_cfg=dict(type='ReLU', inplace=True),
norm_cfg=None):
super(FasterBlock, self).__init__()
if downsample is not None:
assert stride == 2
if norm_cfg is not None:
assert norm_cfg['type'] in ['BatchNorm2d', 'GroupNorm']
self._num_input_channel = num_input_channels
self._num_block_channel = num_block_channels
self._stride = stride
self._activation_cfg = activation_cfg
self._norm_cfg = norm_cfg
self._downsample = downsample
self._conv1 = nn.Conv2d(in_channels=self._num_input_channel, out_channels=self._num_block_channel, kernel_size=3, stride=self._stride, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm1 = get_operator_from_cfg(temp_norm_cfg)
self._activation = get_operator_from_cfg(self._activation_cfg)
self._conv2 = nn.Conv2d(in_channels=self._num_block_channel, out_channels=self._num_block_channel, kernel_size=3, stride=1, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm2 = get_operator_from_cfg(temp_norm_cfg)
def forward(self, x):
identity = x
out = self._conv1(x)
if self._norm_cfg is not None:
out = self._norm1(out)
out = self._activation(out)
out = self._conv2(out)
if self._norm_cfg is not None:
out = self._norm2(out)
if self._downsample is not None:
identity = self._downsample(x)
out += identity
out = self._activation(out)
return out
class FastestBlock(nn.Module):
def __init__(self,
num_input_channels,
num_block_channels,
stride=1,
downsample=None,
activation_cfg=dict(type='ReLU', inplace=True),
norm_cfg=None):
super(FastestBlock, self).__init__()
if downsample is not None:
assert stride == 2
if norm_cfg is not None:
assert norm_cfg['type'] in ['BatchNorm2d', 'GroupNorm']
self._num_input_channel = num_input_channels
self._num_block_channel = num_block_channels
self._stride = stride
self._activation_cfg = activation_cfg
self._norm_cfg = norm_cfg
self._downsample = downsample
self._conv1 = nn.Conv2d(in_channels=self._num_input_channel, out_channels=self._num_block_channel // 2, kernel_size=3, stride=self._stride, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel // 2
else:
temp_norm_cfg['num_channels'] = self._num_block_channel // 2
self._norm1 = get_operator_from_cfg(temp_norm_cfg)
self._activation = get_operator_from_cfg(self._activation_cfg)
self._conv2 = nn.Conv2d(in_channels=self._num_block_channel // 2, out_channels=self._num_block_channel, kernel_size=3, stride=1, padding=1, bias=True if self._norm_cfg is None else False)
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._num_block_channel
else:
temp_norm_cfg['num_channels'] = self._num_block_channel
self._norm2 = get_operator_from_cfg(temp_norm_cfg)
def forward(self, x):
identity = x
out = self._conv1(x)
if self._norm_cfg is not None:
out = self._norm1(out)
out = self._activation(out)
out = self._conv2(out)
if self._norm_cfg is not None:
out = self._norm2(out)
if self._downsample is not None:
identity = self._downsample(x)
out += identity
out = self._activation(out)
return out
class LFDResNet(nn.Module):
# default body architectures are set, or you can specify your own architectures
# by default, all default bodies contain 5 stages
mode_to_body_architectures = {
'fast': [4, 2, 2, 1, 1],
'faster': [2, 1, 1, 1, 1],
'fastest': [2, 1, 1, 1, 1]
}
mode_to_body_channels = {
'fast': [64, 64, 128, 256, 512],
'faster': [64, 64, 128, 128, 256],
'fastest': [32, 32, 64, 64, 128]
}
def __init__(self,
block_mode='fast', # affect block type
stem_mode='fast', # affect stem type
body_mode='fast', # affect body architecture
input_channels=3,
stem_channels=64,
body_architecture=None,
body_channels=None,
out_indices=((0, 3), (1, 1), (2, 1), (3, 0), (4, 0)),
frozen_stages=-1,
activation_cfg=dict(type='ReLU', inplace=True),
norm_cfg=dict(type='BatchNorm2d'),
init_with_weight_file=None,
norm_eval=False
):
super(LFDResNet, self).__init__()
assert block_mode in ['fast', 'faster', 'fastest']
assert stem_mode in ['fast', 'faster', 'fastest']
assert body_mode in ['fast', 'faster', 'fastest', None]
# when body mode is None, body_architecture and body_channels must be provided
if body_mode is None:
assert body_architecture is not None
assert body_channels is not None
# get body architecture
if body_mode is not None:
self._body_architecture = self.mode_to_body_architectures[body_mode]
self._body_channels = self.mode_to_body_channels[body_mode] if body_channels is None else body_channels
else:
self._body_architecture = body_architecture
self._body_channels = body_channels
assert len(self._body_architecture) == len(self._body_channels)
self._block_mode = block_mode
self._stem_mode = stem_mode
self._input_channels = input_channels
self._stem_channels = stem_channels
out_indices = sorted(out_indices, key=lambda x: (x[0], x[1]))
self._out_indices = out_indices
for index in self._out_indices:
assert 0 <= index[0] < len(self._body_architecture)
assert 0 <= index[1] < self._body_architecture[index[0]]
max_stage_index = max([index[0] for index in self._out_indices])
# adjust body according to the max stage index
self._body_architecture = self._body_architecture[:max_stage_index + 1]
self._body_channels = self._body_channels[:max_stage_index + 1]
assert frozen_stages <= max_stage_index + 1
self._frozen_stages = frozen_stages
self._activation_cfg = activation_cfg
self._norm_cfg = norm_cfg
self._init_with_weight_file = init_with_weight_file
self._norm_eval = norm_eval
self._make_stem()
self._make_stages()
self._init_weights()
if self._init_with_weight_file is not None:
assert isinstance(self._init_with_weight_file, str), 'weight file must be the string path of the file!'
self._init_with_pretrained_weights()
# obtain out channels based on out indices; obtain strides for each output map
# both of them will be used by subsequent modules
self._num_output_channels_list = []
self._num_output_strides_list = []
stem_stride = 2 if self._stem_mode == 'fast' else 4
for i, (stage_index, _) in enumerate(self._out_indices):
self._num_output_channels_list.append(self._body_channels[stage_index])
self._num_output_strides_list.append(stem_stride * (2 ** (stage_index + 1)))
@property
def num_output_channels_list(self):
return self._num_output_channels_list
@property
def num_output_strides_list(self):
return self._num_output_strides_list
def _init_with_pretrained_weights(self):
assert os.path.isfile(self._init_with_weight_file), 'pretrained weight file [{}] does not exist!'.format(self._init_with_weight_file)
weights = torch.load(self._init_with_weight_file)
# rename keys of 'state_dict' (pth from pre-train may contain 'backbone')
new_state_dict = dict()
for k in weights['state_dict']:
v = weights['state_dict'][k]
k_splits = k.split('.')
if 'backbone' in k_splits[0]:
del k_splits[0]
new_k = '.'.join(k_splits)
new_state_dict[new_k] = v
missing_keys, unexpected_keys = self.load_state_dict(new_state_dict, strict=False)
if missing_keys:
print('[WARNING: ResNet pretrained weights load] missing keys:')
for i, key in enumerate(missing_keys):
print(key + '\t', end='') if i < len(missing_keys) - 1 else print(key)
if unexpected_keys:
print('[WARNING: ResNet pretrained weights load] unexpected keys:')
for i, key in enumerate(unexpected_keys):
print(key + '\t', end='') if i < len(unexpected_keys) - 1 else print(key)
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if hasattr(m, 'bias') and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
if hasattr(m, 'weight') and m.weight is not None:
nn.init.constant_(m.weight, 1)
if hasattr(m, 'bias') and m.bias is not None:
nn.init.constant_(m.bias, 0)
def _make_stem(self):
stem_layer_list = list()
if self._stem_mode == 'fast':
stem_layer_list.append(nn.Conv2d(in_channels=self._input_channels, out_channels=self._stem_channels, kernel_size=3, stride=2, padding=1, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
stem_layer_list.append(nn.Conv2d(in_channels=self._stem_channels, out_channels=self._stem_channels, kernel_size=1, stride=1, padding=0, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
elif self._stem_mode == 'faster':
stem_layer_list.append(nn.Conv2d(in_channels=self._input_channels, out_channels=self._stem_channels, kernel_size=3, stride=2, padding=1, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
stem_layer_list.append(nn.Conv2d(in_channels=self._stem_channels, out_channels=self._stem_channels, kernel_size=1, stride=1, padding=0, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
stem_layer_list.append(nn.Conv2d(in_channels=self._stem_channels, out_channels=self._stem_channels, kernel_size=3, stride=2, padding=1, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
stem_layer_list.append(nn.Conv2d(in_channels=self._stem_channels, out_channels=self._stem_channels, kernel_size=1, stride=1, padding=0, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
elif self._stem_mode == 'fastest':
stem_layer_list.append(nn.Conv2d(in_channels=self._input_channels, out_channels=self._stem_channels // 2, kernel_size=3, stride=2, padding=1, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels // 2
else:
temp_norm_cfg['num_channels'] = self._stem_channels // 2
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
stem_layer_list.append(nn.Conv2d(in_channels=self._stem_channels // 2, out_channels=self._stem_channels, kernel_size=3, stride=2, padding=1, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = self._stem_channels
else:
temp_norm_cfg['num_channels'] = self._stem_channels
stem_layer_list.append(get_operator_from_cfg(temp_norm_cfg))
stem_layer_list.append(get_operator_from_cfg(self._activation_cfg))
else:
raise ValueError('Unsupported stem_mode!')
self._stem = nn.Sequential(*stem_layer_list)
def _make_stages(self):
if self._block_mode == 'fast':
self._block = FastBlock
elif self._block_mode == 'faster':
self._block = FasterBlock
elif self._block_mode == 'fastest':
self._block = FastestBlock
else:
raise ValueError('Unsupported block mode!')
for i, num_blocks in enumerate(self._body_architecture):
num_stage_channels = self._body_channels[i]
stage_list = nn.ModuleList()
in_channels = self._stem_channels if i == 0 else self._body_channels[i-1]
for j in range(num_blocks):
if j == 0:
downsample_list = list()
downsample_list.append(nn.Conv2d(in_channels=in_channels, out_channels=num_stage_channels, kernel_size=1, stride=2, padding=0, bias=True if self._norm_cfg is None else False))
if self._norm_cfg is not None:
temp_norm_cfg = self._norm_cfg.copy()
if temp_norm_cfg['type'] == 'BatchNorm2d':
temp_norm_cfg['num_features'] = num_stage_channels
else:
temp_norm_cfg['num_channels'] = num_stage_channels
downsample_list.append(get_operator_from_cfg(temp_norm_cfg))
downsample = nn.Sequential(*downsample_list)
stage_list.append(self._block(num_input_channels=in_channels, num_block_channels=num_stage_channels, stride=2, downsample=downsample, activation_cfg=self._activation_cfg, norm_cfg=self._norm_cfg))
else:
stage_list.append(self._block(num_input_channels=num_stage_channels, num_block_channels=num_stage_channels, stride=1, downsample=None, activation_cfg=self._activation_cfg, norm_cfg=self._norm_cfg))
setattr(self, 'stage%d' % i, stage_list)
def _freeze_stages(self):
if self._frozen_stages > 0:
self._stem.eval()
for param in self._stem.parameters():
param.requires_grad = False
for i in range(0, self._frozen_stages):
for j in range(self._body_architecture[i]):
m = getattr(self, 'stage%d' % i)[j]
m.eval()
for param in m.parameters():
param.requires_grad = False
def forward(self, x):
x = self._stem(x)
outs = []
for i, num_blocks in enumerate(self._body_architecture):
for j in range(num_blocks):
block = getattr(self, 'stage%d' % i)[j]
x = block(x)
if (i, j) in self._out_indices:
outs.append(x)
return tuple(outs)
def train(self, mode=True):
super(LFDResNet, self).train(mode)
self._freeze_stages()
if mode and self._norm_eval:
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()