forked from rwightman/efficientdet-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficientdet.py
513 lines (443 loc) · 21.7 KB
/
efficientdet.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
510
511
512
513
""" PyTorch EfficientDet model
Based on official Tensorflow version at: https://github.com/google/automl/tree/master/efficientdet
Paper: https://arxiv.org/abs/1911.09070
Hacked together by Ross Wightman
"""
import torch
import torch.nn as nn
import logging
import math
from collections import OrderedDict
from typing import List, Callable
from timm import create_model
from timm.models.layers import create_conv2d, drop_path, create_pool2d, Swish, get_act_layer
from .config import get_fpn_config
_DEBUG = False
_ACT_LAYER = Swish
class SequentialAppend(nn.Sequential):
def __init__(self, *args):
super(SequentialAppend, self).__init__(*args)
def forward(self, x: List[torch.Tensor]):
for module in self:
x.append(module(x))
return x
class SequentialAppendLast(nn.Sequential):
def __init__(self, *args):
super(SequentialAppendLast, self).__init__(*args)
def forward(self, x: List[torch.Tensor]):
for module in self:
x.append(module(x[-1]))
return x
class ConvBnAct2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, padding='', bias=False,
norm_layer=nn.BatchNorm2d, norm_kwargs=None, act_layer=_ACT_LAYER):
super(ConvBnAct2d, self).__init__()
norm_kwargs = norm_kwargs or {}
self.conv = create_conv2d(
in_channels, out_channels, kernel_size, stride=stride, dilation=dilation, padding=padding, bias=bias)
self.bn = None if norm_layer is None else norm_layer(out_channels, **norm_kwargs)
self.act = None if act_layer is None else act_layer(inplace=True)
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.act is not None:
x = self.act(x)
return x
class SeparableConv2d(nn.Module):
""" Separable Conv
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, dilation=1, padding='', bias=False,
channel_multiplier=1.0, pw_kernel_size=1, act_layer=_ACT_LAYER,
norm_layer=nn.BatchNorm2d, norm_kwargs=None):
super(SeparableConv2d, self).__init__()
norm_kwargs = norm_kwargs or {}
self.conv_dw = create_conv2d(
in_channels, int(in_channels * channel_multiplier), kernel_size,
stride=stride, dilation=dilation, padding=padding, depthwise=True)
self.conv_pw = create_conv2d(
int(in_channels * channel_multiplier), out_channels, pw_kernel_size, padding=padding, bias=bias)
self.bn = None if norm_layer is None else norm_layer(out_channels, **norm_kwargs)
self.act = None if act_layer is None else act_layer(inplace=True)
def forward(self, x):
x = self.conv_dw(x)
x = self.conv_pw(x)
if self.bn is not None:
x = self.bn(x)
if self.act is not None:
x = self.act(x)
return x
# phager: FIX https://discuss.pytorch.org/t/using-nn-function-interpolate-inside-nn-sequential/23588
class Interpolate(nn.Module):
def __init__(self, scale_factor, mode):
super(Interpolate, self).__init__()
self.interp = nn.functional.interpolate
self.scale_factor = scale_factor
self.mode = mode
def forward(self, x):
# FIX: https://github.com/pytorch/pytorch/issues/27376 + simpler but not yet really nice...
# issues are actualy batch size and channel interposlation
x = self.interp(x, size=[int(self.scale_factor * x.shape[2]), int(self.scale_factor * x.shape[3])], mode=self.mode)
#x = self.interp(x, scale_factor=[int(self.scale_factor), int(self.scale_factor)], mode=self.mode)
return x
class ResampleFeatureMap(nn.Sequential):
def __init__(self, in_channels, out_channels, reduction_ratio=1., pad_type='', pooling_type='max',
norm_layer=nn.BatchNorm2d, norm_kwargs=None, apply_bn=False, conv_after_downsample=False,
redundant_bias=False):
super(ResampleFeatureMap, self).__init__()
pooling_type = pooling_type or 'max'
self.in_channels = in_channels
self.out_channels = out_channels
self.reduction_ratio = reduction_ratio
self.conv_after_downsample = conv_after_downsample
conv = None
if in_channels != out_channels:
conv = ConvBnAct2d(
in_channels, out_channels, kernel_size=1, padding=pad_type,
norm_layer=norm_layer if apply_bn else None, norm_kwargs=norm_kwargs,
bias=not apply_bn or redundant_bias, act_layer=None)
if reduction_ratio > 1:
stride_size = int(reduction_ratio)
#print("XXX "+ str(int(reduction_ratio)))
#assert int(reduction_ratio) == 2 # HACK: making stuff static...
#stride_size = 2
if conv is not None and not self.conv_after_downsample:
self.add_module('conv', conv)
self.add_module(
'downsample',
create_pool2d(
pooling_type, kernel_size=stride_size + 1, stride=stride_size, padding=pad_type))
if conv is not None and self.conv_after_downsample:
self.add_module('conv', conv)
else:
if conv is not None:
self.add_module('conv', conv)
if reduction_ratio < 1:
scale = int(1 // reduction_ratio)
#print("YYY "+ str(int(1 // reduction_ratio)))
#assert int(1 // reduction_ratio) == 2 # HACK: making stuff static...
#scale = 2
# phager: FIX FOR https://github.com/pytorch/pytorch/issues/27376 -> adding interpolation module
# OLD: self.add_module('upsample', nn.UpsamplingNearest2d(scale_factor=scale))
self.add_module('upsample', Interpolate(scale_factor=scale, mode='nearest'))
# def forward(self, x):
# # here for debugging only
# assert x.shape[1] == self.in_channels
# if self.reduction_ratio > 1:
# if hasattr(self, 'conv') and not self.conv_after_downsample:
# x = self.conv(x)
# x = self.downsample(x)
# if hasattr(self, 'conv') and self.conv_after_downsample:
# x = self.conv(x)
# else:
# if hasattr(self, 'conv'):
# x = self.conv(x)
# if self.reduction_ratio < 1:
# x = self.upsample(x)
# return x
class FpnCombine(nn.Module):
def __init__(self, feature_info, fpn_config, fpn_channels, inputs_offsets, target_reduction, pad_type='',
pooling_type='max', norm_layer=nn.BatchNorm2d, norm_kwargs=None, apply_bn_for_resampling=False,
conv_after_downsample=False, redundant_bias=False, weight_method='attn'):
super(FpnCombine, self).__init__()
self.inputs_offsets = inputs_offsets
self.weight_method = weight_method
self.resample = nn.ModuleDict()
for idx, offset in enumerate(inputs_offsets):
in_channels = fpn_channels
if offset < len(feature_info):
in_channels = feature_info[offset]['num_chs']
input_reduction = feature_info[offset]['reduction']
else:
node_idx = offset - len(feature_info)
input_reduction = fpn_config.nodes[node_idx]['reduction']
print(input_reduction)
reduction_ratio = (target_reduction / input_reduction) # DETCH
print("ZZZ "+str(reduction_ratio))
self.resample[str(offset)] = ResampleFeatureMap(
in_channels, fpn_channels, reduction_ratio=reduction_ratio, pad_type=pad_type,
pooling_type=pooling_type, norm_layer=norm_layer, norm_kwargs=norm_kwargs,
apply_bn=apply_bn_for_resampling, conv_after_downsample=conv_after_downsample,
redundant_bias=redundant_bias)
if weight_method == 'attn' or weight_method == 'fastattn':
# WSM
self.edge_weights = nn.Parameter(torch.ones(len(inputs_offsets)), requires_grad=True)
else:
self.edge_weights = None
def forward(self, x):
dtype = x[0].dtype
nodes = []
sum_nodes = []
for offset in self.inputs_offsets:
input_node = x[offset]
input_node = self.resample[str(offset)](input_node)
nodes.append(input_node)
if self.weight_method == 'attn':
normalized_weights = torch.softmax(self.edge_weights.type(dtype), dim=0)
sum_nodes = [node * normalized_weights[i] for node, i in enumerate(nodes)]
#x = torch.stack(nodes, dim=-1) * normalized_weights
elif self.weight_method == 'fastattn':
print("FPN detach")
edge_weights = nn.functional.relu(self.edge_weights.type(dtype))
weights_sum = torch.sum(edge_weights)
weights_sum = weights_sum.item()
#x = torch.stack([nodes[i] * (edge_weights[i].item() / (weights_sum + 0.0001)) for i in range(len(nodes))], dim=-1)
sum_nodes = [nodes[i] * (edge_weights[i].item() / (weights_sum + 0.0001)) for i in range(len(nodes))]
elif self.weight_method == 'sum':
sum_nodes = nodes
#x = torch.stack(nodes, dim=-1)
else:
raise ValueError('unknown weight_method {}'.format(self.weight_method))
#x = torch.sum(x, dim=-1)
x = sum_nodes[0]
for i in range(1,len(sum_nodes)):
x += sum_nodes[i]
return x
class BiFpnLayer(nn.Module):
def __init__(self, feature_info, fpn_config, fpn_channels, num_levels=5, pad_type='',
pooling_type='max', norm_layer=nn.BatchNorm2d, norm_kwargs=None, act_layer=_ACT_LAYER,
apply_bn_for_resampling=False, conv_after_downsample=True, conv_bn_relu_pattern=False,
separable_conv=True, redundant_bias=False):
super(BiFpnLayer, self).__init__()
self.fpn_config = fpn_config
self.num_levels = num_levels
self.conv_bn_relu_pattern = False
self.feature_info = []
self.fnode = SequentialAppend()
for i, fnode_cfg in enumerate(fpn_config.nodes):
logging.debug('fnode {} : {}'.format(i, fnode_cfg))
fnode_layers = OrderedDict()
# combine features
reduction = fnode_cfg['reduction']
fnode_layers['combine'] = FpnCombine(
feature_info, fpn_config, fpn_channels, fnode_cfg['inputs_offsets'], target_reduction=reduction,
pad_type=pad_type, pooling_type=pooling_type, norm_layer=norm_layer, norm_kwargs=norm_kwargs,
apply_bn_for_resampling=apply_bn_for_resampling, conv_after_downsample=conv_after_downsample,
redundant_bias=redundant_bias, weight_method=fpn_config.weight_method)
self.feature_info.append(dict(num_chs=fpn_channels, reduction=reduction))
# after combine ops
after_combine = OrderedDict()
if not conv_bn_relu_pattern:
after_combine['act'] = act_layer(inplace=True)
conv_bias = redundant_bias
conv_act = None
else:
conv_bias = False
conv_act = act_layer
conv_kwargs = dict(
in_channels=fpn_channels, out_channels=fpn_channels, kernel_size=3, padding=pad_type,
bias=conv_bias, norm_layer=norm_layer, norm_kwargs=norm_kwargs, act_layer=conv_act)
after_combine['conv'] = SeparableConv2d(**conv_kwargs) if separable_conv else ConvBnAct2d(**conv_kwargs)
fnode_layers['after_combine'] = nn.Sequential(after_combine)
self.fnode.add_module(str(i), nn.Sequential(fnode_layers))
self.feature_info = self.feature_info[-num_levels::]
def forward(self, x):
x = self.fnode(x)
return x[-self.num_levels::]
class BiFpn(nn.Module):
def __init__(self, config, feature_info, norm_layer=nn.BatchNorm2d, norm_kwargs=None, act_layer=_ACT_LAYER):
super(BiFpn, self).__init__()
self.config = config
fpn_config = config.fpn_config or get_fpn_config(
config.fpn_name, min_level=config.min_level, max_level=config.max_level)
self.resample = SequentialAppendLast()
for level in range(config.num_levels):
if level < len(feature_info):
in_chs = feature_info[level]['num_chs']
reduction = feature_info[level]['reduction']
else:
# Adds a coarser level by downsampling the last feature map
reduction_ratio = 2
self.resample.add_module(str(level), ResampleFeatureMap(
in_channels=in_chs,
out_channels=config.fpn_channels,
pad_type=config.pad_type,
pooling_type=config.pooling_type,
norm_layer=norm_layer,
norm_kwargs=norm_kwargs,
reduction_ratio=reduction_ratio,
apply_bn=config.apply_bn_for_resampling,
conv_after_downsample=config.conv_after_downsample,
redundant_bias=config.redundant_bias,
))
in_chs = config.fpn_channels
reduction = int(reduction * reduction_ratio)
feature_info.append(dict(num_chs=in_chs, reduction=reduction))
self.cell = nn.Sequential()
for rep in range(config.fpn_cell_repeats):
logging.debug('building cell {}'.format(rep))
fpn_layer = BiFpnLayer(
feature_info=feature_info,
fpn_config=fpn_config,
fpn_channels=config.fpn_channels,
num_levels=config.num_levels,
pad_type=config.pad_type,
pooling_type=config.pooling_type,
norm_layer=norm_layer,
norm_kwargs=norm_kwargs,
act_layer=act_layer,
separable_conv=config.separable_conv,
apply_bn_for_resampling=config.apply_bn_for_resampling,
conv_after_downsample=config.conv_after_downsample,
conv_bn_relu_pattern=config.conv_bn_relu_pattern,
redundant_bias=config.redundant_bias,
)
self.cell.add_module(str(rep), fpn_layer)
feature_info = fpn_layer.feature_info
def forward(self, x):
assert len(self.resample) == self.config.num_levels - len(x)
x = self.resample(x)
x = self.cell(x)
return x
class HeadNet(nn.Module):
def __init__(self, config, num_outputs, norm_layer=nn.BatchNorm2d, norm_kwargs=None, act_layer=_ACT_LAYER):
super(HeadNet, self).__init__()
norm_kwargs = norm_kwargs or {}
self.config = config
num_anchors = len(config.aspect_ratios) * config.num_scales
self.conv_rep = nn.ModuleList()
self.bn_rep = nn.ModuleList()
conv_kwargs = dict(
in_channels=config.fpn_channels, out_channels=config.fpn_channels, kernel_size=3,
padding=self.config.pad_type, bias=config.redundant_bias, act_layer=None, norm_layer=None)
for i in range(config.box_class_repeats):
conv = SeparableConv2d(**conv_kwargs) if config.separable_conv else ConvBnAct2d(**conv_kwargs)
self.conv_rep.append(conv)
bn_levels = []
for _ in range(config.num_levels):
bn_seq = nn.Sequential()
bn_seq.add_module('bn', norm_layer(config.fpn_channels, **norm_kwargs))
bn_levels.append(bn_seq)
self.bn_rep.append(nn.ModuleList(bn_levels))
self.act = act_layer(inplace=True)
predict_kwargs = dict(
in_channels=config.fpn_channels, out_channels=num_outputs * num_anchors, kernel_size=3,
padding=self.config.pad_type, bias=True, norm_layer=None, act_layer=None)
if config.separable_conv:
self.predict = SeparableConv2d(**predict_kwargs)
else:
self.predict = ConvBnAct2d(**predict_kwargs)
def forward(self, x):
outputs = []
for level in range(self.config.num_levels):
x_level = x[level]
for i in range(self.config.box_class_repeats):
x_level_ident = x_level
x_level = self.conv_rep[i](x_level)
x_level = self.bn_rep[i][level](x_level)
x_level = self.act(x_level)
if i > 0 and self.config.fpn_drop_path_rate:
x_level = drop_path(x_level, self.config.fpn_drop_path_rate, self.training)
x_level += x_level_ident
outputs.append(self.predict(x_level))
return outputs
def _init_weight(m, n='', ):
""" Weight initialization as per Tensorflow official implementations.
"""
def _fan_in_out(w, groups=1):
dimensions = w.dim()
if dimensions < 2:
raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions")
num_input_fmaps = w.size(1)
num_output_fmaps = w.size(0)
receptive_field_size = 1
if w.dim() > 2:
receptive_field_size = w[0][0].numel()
fan_in = num_input_fmaps * receptive_field_size
fan_out = num_output_fmaps * receptive_field_size
fan_out //= groups
return fan_in, fan_out
def _glorot_uniform(w, gain=1, groups=1):
fan_in, fan_out = _fan_in_out(w, groups)
gain /= max(1., (fan_in + fan_out) / 2.) # fan avg
limit = math.sqrt(3.0 * gain)
w.data.uniform_(-limit, limit)
def _variance_scaling(w, gain=1, groups=1):
fan_in, fan_out = _fan_in_out(w, groups)
gain /= max(1., fan_in) # fan in
# gain /= max(1., (fan_in + fan_out) / 2.) # fan
# should it be normal or trunc normal? using normal for now since no good trunc in PT
# constant taken from scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
# std = math.sqrt(gain) / .87962566103423978
# w.data.trunc_normal(std=std)
std = math.sqrt(gain)
w.data.normal_(std=std)
if isinstance(m, SeparableConv2d):
if 'box_net' in n or 'class_net' in n:
_variance_scaling(m.conv_dw.weight, groups=m.conv_dw.groups)
_variance_scaling(m.conv_pw.weight)
if m.conv_pw.bias is not None:
if 'class_net.predict' in n:
m.conv_pw.bias.data.fill_(-math.log((1 - 0.01) / 0.01))
else:
m.conv_pw.bias.data.zero_()
else:
_glorot_uniform(m.conv_dw.weight, groups=m.conv_dw.groups)
_glorot_uniform(m.conv_pw.weight)
if m.conv_pw.bias is not None:
m.conv_pw.bias.data.zero_()
elif isinstance(m, ConvBnAct2d):
if 'box_net' in n or 'class_net' in n:
m.conv.weight.data.normal_(std=.01)
if m.conv.bias is not None:
if 'class_net.predict' in n:
m.conv.bias.data.fill_(-math.log((1 - 0.01) / 0.01))
else:
m.conv.bias.data.zero_()
else:
_glorot_uniform(m.conv.weight)
if m.conv.bias is not None:
m.conv.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
# looks like all bn init the same?
m.weight.data.fill_(1.0)
m.bias.data.zero_()
def _init_weight_alt(m, n='', ):
""" Weight initialization alternative, based on EfficientNet bacbkone init w/ class bias addition
NOTE: this will likely be removed after some experimentation
"""
if isinstance(m, nn.Conv2d):
fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
fan_out //= m.groups
m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
if m.bias is not None:
if 'class_net.predict' in n:
m.bias.data.fill_(-math.log((1 - 0.01) / 0.01))
else:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1.0)
m.bias.data.zero_()
def get_feature_info(backbone):
if isinstance(backbone.feature_info, Callable):
# old accessor for timm versions <= 0.1.30, efficientnet and mobilenetv3 and related nets only
feature_info = [dict(num_chs=f['num_chs'], reduction=f['reduction'])
for i, f in enumerate(backbone.feature_info())]
else:
# new feature info accessor, timm >= 0.2, all models supported
feature_info = backbone.feature_info.get_dicts(keys=['num_chs', 'reduction'])
return feature_info
class EfficientDet(nn.Module):
def __init__(self, config, norm_kwargs=None, pretrained_backbone=True, alternate_init=False):
super(EfficientDet, self).__init__()
norm_kwargs = norm_kwargs or dict(eps=.001, momentum=.01)
self.backbone = create_model(
config.backbone_name, features_only=True, out_indices=(2, 3, 4),
pretrained=pretrained_backbone, **config.backbone_args)
feature_info = get_feature_info(self.backbone)
act_layer = get_act_layer(config.act_type)
self.fpn = BiFpn(config, feature_info, norm_kwargs=norm_kwargs, act_layer=act_layer)
self.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=norm_kwargs, act_layer=act_layer)
self.box_net = HeadNet(config, num_outputs=4, norm_kwargs=norm_kwargs, act_layer=act_layer)
for n, m in self.named_modules():
if 'backbone' not in n:
if alternate_init:
_init_weight_alt(m, n)
else:
_init_weight(m, n)
def forward(self, x):
x = self.backbone(x)
x = self.fpn(x)
x_class = self.class_net(x)
x_box = self.box_net(x)
return x_class, x_box