-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfrostnet_features.py
362 lines (308 loc) · 13.3 KB
/
frostnet_features.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
import torch.nn as nn
import torch
import math
import os
import torch.nn.functional as F
from collections import OrderedDict
from ..builder import BACKBONES
def load_state_dict(checkpoint_path, use_ema=False):
if checkpoint_path and os.path.isfile(checkpoint_path):
checkpoint = torch.load(checkpoint_path, map_location='cpu')
state_dict_key = 'state_dict'
if isinstance(checkpoint, dict):
if use_ema and 'state_dict_ema' in checkpoint:
state_dict_key = 'state_dict_ema'
if state_dict_key and state_dict_key in checkpoint:
new_state_dict = OrderedDict()
for k, v in checkpoint[state_dict_key].items():
# strip `module.` prefix
name = k[7:] if k.startswith('module') else k
new_state_dict[name] = v
state_dict = new_state_dict
else:
state_dict = checkpoint
print("Loaded {} from checkpoint '{}'".format(state_dict_key, checkpoint_path))
return state_dict
else:
print("No checkpoint found at '{}'".format(checkpoint_path))
raise FileNotFoundError()
def load_checkpoint(model, checkpoint_path, use_ema=False, strict=True):
state_dict = load_state_dict(checkpoint_path, use_ema)
model.load_state_dict(state_dict, strict=strict)
class ConvBNReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0,
dilation=1, groups=1):
super(ConvBNReLU, self).__init__()
self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(False)
)
def forward(self, x):
x = self.conv(x)
return x
def fuse_model(self):
torch.quantization.fuse_modules(self.conv, ['0', '1','2'], inplace=True)
class ConvReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0,
dilation=1, groups=1):
super(ConvBN, self).__init__()
self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias=False),
nn.ReLU(False)
)
def forward(self, x):
x = self.conv(x)
return x
def fuse_model(self):
torch.quantization.fuse_modules(self.conv, ['0', '1'], inplace=True)
class ConvBN(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0,
dilation=1, groups=1):
super(ConvBN, self).__init__()
self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias=False),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
x = self.conv(x)
return x
def fuse_model(self):
torch.quantization.fuse_modules(self.conv, ['0', '1'], inplace=True)
def _make_divisible(v, divisor=8, min_value=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
:param v:
:param divisor:
:param min_value:
:return:
"""
if min_value is None:
min_value = divisor
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
class CascadePreExBottleneck(nn.Module):
def __init__(self, in_channels, out_channels, quantized = False,
kernel_size=3, stride=1, dilation=1,expand_ratio=6,
reduce_factor = 4, block_type = 'CAS'):
super(CascadePreExBottleneck, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.expand_ratio = expand_ratio
self.quantized = quantized
if in_channels//reduce_factor < 8:
block_type = 'MB'
self.block_type = block_type
r_channels = _make_divisible(in_channels//reduce_factor)
if stride == 1 and in_channels==out_channels:
self.reduction = False
else:
self.reduction = True
if self.expand_ratio == 1:
self.squeeze_conv = None
self.conv1 = None
n_channels = in_channels
else:
if block_type == 'CAS':
self.squeeze_conv = ConvBNReLU(in_channels,r_channels, 1)
n_channels = r_channels + in_channels
else:
n_channels = in_channels
self.conv1 = ConvBNReLU(n_channels,n_channels*expand_ratio, 1)
self.conv2 = ConvBNReLU(n_channels*expand_ratio, n_channels*expand_ratio, kernel_size, stride,
(kernel_size - 1) // 2 , 1,
groups=n_channels*expand_ratio)
self.reduce_conv = ConvBN(n_channels*expand_ratio, out_channels, 1)
if self.quantized:
self.skip_add = nn.quantized.FloatFunctional()
self.quant_cat = nn.quantized.FloatFunctional()
def forward(self, x):
if not self.expand_ratio == 1:
if self.block_type == 'CAS':
squeezed = self.squeeze_conv(x)
if self.quantized:
out = self.quant_cat.cat([squeezed,x],1)
else:
out = torch.cat([squeezed,x],1)
else:
out = x
out = self.conv1(out)
else:
out = x
out = self.conv2(out)
out = self.reduce_conv(out)
if not self.reduction:
if self.quantized:
out = self.skip_add.add(x,out)
else:
out = torch.add(x,out)
return out
@BACKBONES.register_module()
class FrostNet(nn.Module):
def __init__(self, mode='large', width_mult=1.0, bottleneck=CascadePreExBottleneck,quantized =False,pretrained='', **kwargs):
super(FrostNet, self).__init__()
self.quantized = quantized
if mode == 'large':
layer1_setting = [
# kernel_size, c, e, r, s
[3, 16, 1, 1, 1], #0
[3, 24, 6, 4, 2], #1
#[, , , , ], #2
#[, , , , ], #3
[3, 24, 3, 4, 1], #4
]
layer2_setting = [
[5, 40, 6, 4, 2], #5
#[, , , , ], #6
#[, , , , ], #7
[3, 40, 3, 4, 1], #8
]
layer3_setting = [
[5, 80, 6, 4, 2], #9
#[, , , , ], #10
[5, 80, 3, 4, 1], #11
[5, 80, 3, 4, 1], #12
[5, 96, 6, 4, 1], #13
#[, , , , ], #14
[5, 96, 3, 4, 1], #15
[3, 96, 3, 4, 1], #16
[3, 96, 3, 4, 1], #17
]
layer4_setting = [
[5, 192, 6, 2, 2], #18
[5, 192, 6, 4, 1], #19
[5, 192, 6, 4, 1], #20
[5, 192, 3, 4, 1], #21
[5, 192, 3, 4, 1], #22
]
layer5_setting = [
[5, 320, 6, 2, 1], #23
]
elif mode == 'base':
layer1_setting = [
# kernel_size, c, e, r, s
[3, 16, 1, 1, 1], #0
[5, 24, 6, 4, 2], #1
#[, , , , ], #2
#[, , , , ], #3
[3, 24, 3, 4, 1], #4
]
layer2_setting = [
[5, 40, 3, 4, 2], #5
#[, , , , ], #6
[5, 40, 3, 4, 1], #7
#[, , , , ], #8
]
layer3_setting = [
[5, 80, 3, 4, 2], #9
#[, , , , ], #10
#[, , , , ], #11
[3, 80, 3, 4, 1], #12
[5, 96, 3, 2, 1], #13
[3, 96, 3, 4, 1], #14
[5, 96, 3, 4, 1], #15
[5, 96, 3, 4, 1], #16
]
layer4_setting = [
[5, 192, 6, 2, 2], #17
[5, 192, 3, 2, 1], #18
[5, 192, 3, 2, 1], #19
[5, 192, 3, 2, 1], #20
]
layer5_setting = [
[5, 320, 6, 2, 1], #21
]
elif mode == 'small':
layer1_setting = [
# kernel_size, c, e, r, s
[3, 16, 1, 1, 1], #0
[5, 24, 3, 4, 2], #1
[3, 24, 3, 4, 1], #2
#[, , , , ], #3
]
layer2_setting = [
[5, 40, 3, 4, 2], #4
#[, , , , ], #5
#[, , , , ], #6
]
layer3_setting = [
[5, 80, 3, 4, 2], #7
[5, 80, 3, 4, 1], #8
[3, 80, 3, 4, 1], #9
[5, 96, 3, 2, 1], #10
[5, 96, 3, 4, 1], #11
[5, 96, 3, 4, 1], #12
]
layer4_setting = [
[5, 192, 6, 4, 2], #13
[5, 192, 6, 4, 1], #14
[5, 192, 6, 4, 1], #15
]
layer5_setting = [
[5, 320, 6, 2, 1], #16
]
else:
raise ValueError('Unknown mode.')
# building first layer
self.in_channels = _make_divisible(int(32*min(1.0,width_mult)))
self.conv1 = ConvBNReLU(3, self.in_channels, 3, 2, 1)
# building bottleneck blocks
self.layer1 = self._make_layer(bottleneck, layer1_setting, width_mult, 1)
self.layer2 = self._make_layer(bottleneck, layer2_setting, width_mult, 1)
self.layer3 = self._make_layer(bottleneck, layer3_setting, width_mult, 1)
self.layer4 = self._make_layer(bottleneck, layer4_setting, width_mult, 1)
self.layer5 = self._make_layer(bottleneck, layer5_setting, width_mult, 1)
# building last several layers
last_in_channels = self.in_channels
self.mode = mode
#self.init_weights(pretrained)
#self._freeze_stages()
def init_weights(self, pretrained):
if pretrained !='':
load_checkpoint(self, pretrained, use_ema=True, strict=False)
else:
print('No pretrained backbone provided')
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
if m.bias is not None:
nn.init.zeros_(m.bias)
def _make_layer(self, block, block_setting, width_mult, dilation=1):
layers = list()
for k, c, e, r, s in block_setting:
out_channels = _make_divisible(int(c * width_mult))
stride = s if (dilation == 1) else 1
layers.append(block(self.in_channels, out_channels, quantized = self.quantized, kernel_size = k,
stride=s, dilation=dilation, expand_ratio=e, reduce_factor = r))
self.in_channels = out_channels
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x4 = self.layer4(x3)
x5 = self.layer5(x4)
features = [x1, x2, x3, x5]
return features
def _freeze_stages(self):
'''Freeze BatchNorm layers.'''
print('Freeze BatchNorm layers.')
for layer in self.modules():
if isinstance(layer, nn.BatchNorm2d):
layer.eval()