-
Notifications
You must be signed in to change notification settings - Fork 16
/
mobilenetv2.py
278 lines (237 loc) · 9.42 KB
/
mobilenetv2.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
from torch import nn
import torch.utils.model_zoo as model_zoo
from collections import OrderedDict
import math
__all__ = ['MobileNetV2']
model_urls = {
'mobilenet_v2': 'https://download.pytorch.org/models/mobilenet_v2-b0353104.pth',
}
def _make_divisible(v, divisor, 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 ConvBNReLU(nn.Sequential):
def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
padding = (kernel_size - 1) // 2
super(ConvBNReLU, self).__init__(
nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU6(inplace=True)
)
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride, expand_ratio):
super(InvertedResidual, self).__init__()
self.stride = stride
assert stride in [1, 2]
hidden_dim = int(round(inp * expand_ratio))
self.use_res_connect = self.stride == 1 and inp == oup
layers = []
if expand_ratio != 1:
# pw
layers.append(ConvBNReLU(inp, hidden_dim, kernel_size=1))
layers.extend([
# dw
ConvBNReLU(hidden_dim, hidden_dim, stride=stride, groups=hidden_dim),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
])
self.conv = nn.Sequential(*layers)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
class MobileNetV2(nn.Module):
def __init__(self,width_mult=1.0,round_nearest=8,):
super(MobileNetV2, self).__init__()
block = InvertedResidual
input_channel = 32
inverted_residual_setting = [
# t, c, n, s
[1, 16, 1, 1], # 0
[6, 24, 2, 2], # 1
[6, 32, 3, 2], # 2
[6, 64, 4, 2], # 3
[6, 96, 3, 1], # 4
[6, 160, 3, 2],# 5
[6, 320, 1, 1],# 6
]
self.feat_id = [1,2,4,6]
self.feat_channel = []
# only check the first element, assuming user knows t,c,n,s are required
if len(inverted_residual_setting) == 0 or len(inverted_residual_setting[0]) != 4:
raise ValueError("inverted_residual_setting should be non-empty "
"or a 4-element list, got {}".format(inverted_residual_setting))
# building first layer
input_channel = _make_divisible(input_channel * width_mult, round_nearest)
features = [ConvBNReLU(3, input_channel, stride=2)]
# building inverted residual blocks
for id,(t, c, n, s) in enumerate(inverted_residual_setting):
output_channel = _make_divisible(c * width_mult, round_nearest)
for i in range(n):
stride = s if i == 0 else 1
features.append(block(input_channel, output_channel, stride, expand_ratio=t))
input_channel = output_channel
if id in self.feat_id :
self.__setattr__("feature_%d"%id,nn.Sequential(*features))
self.feat_channel.append(output_channel)
features = []
# weight initialization
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)
def forward(self, x):
y = []
for id in self.feat_id:
x = self.__getattr__("feature_%d"%id)(x)
y.append(x)
return y
def load_model(model,state_dict):
new_model=model.state_dict()
new_keys = list(new_model.keys())
old_keys = list(state_dict.keys())
restore_dict = OrderedDict()
for id in range(len(new_keys)):
restore_dict[new_keys[id]] = state_dict[old_keys[id]]
model.load_state_dict(restore_dict)
def fill_up_weights(up):
w = up.weight.data
f = math.ceil(w.size(2) / 2)
c = (2 * f - 1 - f % 2) / (2. * f)
for i in range(w.size(2)):
for j in range(w.size(3)):
w[0, 0, i, j] = \
(1 - math.fabs(i / f - c)) * (1 - math.fabs(j / f - c))
for c in range(1, w.size(0)):
w[c, 0, :, :] = w[0, 0, :, :]
def fill_fc_weights(layers):
for m in layers.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, std=0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
class IDAUp(nn.Module):
def __init__(self, out_dim, channel):
super(IDAUp, self).__init__()
self.out_dim = out_dim
self.up = nn.Sequential(
nn.ConvTranspose2d(
out_dim, out_dim, kernel_size=2, stride=2, padding=0,
output_padding=0, groups=out_dim, bias=False),
nn.BatchNorm2d(out_dim,eps=0.001,momentum=0.1),
nn.ReLU())
self.conv = nn.Sequential(
nn.Conv2d(channel, out_dim,
kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(out_dim,eps=0.001,momentum=0.1),
nn.ReLU(inplace=True))
def forward(self, layers):
layers = list(layers)
x = self.up(layers[0])
y = self.conv(layers[1])
out = x + y
return out
class MobileNetUp(nn.Module):
def __init__(self, channels, out_dim = 24):
super(MobileNetUp, self).__init__()
channels = channels[::-1]
self.conv = nn.Sequential(
nn.Conv2d(channels[0], out_dim,
kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(out_dim,eps=0.001,momentum=0.1),
nn.ReLU(inplace=True))
self.conv_last = nn.Sequential(
nn.Conv2d(out_dim,out_dim,
kernel_size=3, stride=1, padding=1 ,bias=False),
nn.BatchNorm2d(out_dim,eps=1e-5,momentum=0.01),
nn.ReLU(inplace=True))
for i,channel in enumerate(channels[1:]):
setattr(self,'up_%d'%(i),IDAUp(out_dim,channel))
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.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m,nn.ConvTranspose2d):
fill_up_weights(m)
def forward(self, layers):
layers = list(layers)
assert len(layers) > 1
x = self.conv(layers[-1])
for i in range(0,len(layers)-1):
up = getattr(self, 'up_{}'.format(i))
x = up([x,layers[len(layers)-2-i]])
x = self.conv_last(x)
return x
class MobileNetSeg(nn.Module):
def __init__(self, base_name,heads,head_conv=24, pretrained = True):
super(MobileNetSeg, self).__init__()
self.heads = heads
self.base = globals()[base_name](
pretrained=pretrained)
channels = self.base.feat_channel
self.dla_up = MobileNetUp(channels, out_dim=head_conv)
for head in self.heads:
classes = self.heads[head]
fc =nn.Conv2d(head_conv, classes,
kernel_size=1, stride=1,
padding=0, bias=True)
if 'hm' in head:
fc.bias.data.fill_(-2.19)
else:
nn.init.normal_(fc.weight, std=0.001)
nn.init.constant_(fc.bias, 0)
self.__setattr__(head, fc)
def forward(self, x):
x = self.base(x)
x = self.dla_up(x)
ret = {}
for head in self.heads:
ret[head] = self.__getattr__(head)(x)
return [ret]
def mobilenetv2_10(pretrained=True, **kwargs):
model = MobileNetV2(width_mult=1.0)
if pretrained:
state_dict = model_zoo.load_url(model_urls['mobilenet_v2'],
progress=True)
load_model(model,state_dict)
return model
def mobilenetv2_5(pretrained=False, **kwargs):
model = MobileNetV2(width_mult=0.5)
if pretrained:
print('This version does not have pretrain weights.')
return model
# num_layers : [10 , 5]
def get_mobile_net(num_layers, heads, head_conv=24):
model = MobileNetSeg('mobilenetv2_{}'.format(num_layers), heads,
pretrained=True,
head_conv=head_conv)
return model
if __name__ == '__main__':
import torch
input = torch.zeros([1,3,416,416])
model = get_mobile_net(5,{'hm':20,'reg':2,'wh':2},head_conv=24)
model(input)