forked from d-li14/mobilenetv3.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmobilenetv3.py
277 lines (243 loc) · 8.94 KB
/
mobilenetv3.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
"""
Creates a MobileNetV3 Model as defined in:
Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu, Ruoming Pang, Vijay Vasudevan, Quoc V. Le, Hartwig Adam. (2019).
Searching for MobileNetV3
arXiv preprint arXiv:1905.02244.
"""
import torch
import torch.nn as nn
import math
import struct
import time
import torchvision
__all__ = ['mobilenetv3_large', 'mobilenetv3_small']
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 h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swish(nn.Module):
def __init__(self, inplace=True):
super(h_swish, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, x):
y = self.sigmoid(x)
return x * y
class SELayer(nn.Module):
def __init__(self, channel, reduction=4):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
h_sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x)
y = y.view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
def conv_3x3_bn(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
h_swish()
)
def conv_1x1_bn(inp, oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
h_swish()
)
class InvertedResidual(nn.Module):
def __init__(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs):
super(InvertedResidual, self).__init__()
assert stride in [1, 2]
self.identity = stride == 1 and inp == oup
if inp == hidden_dim:
self.conv = nn.Sequential(
# dw
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Sequential(),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# dw
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Sequential(),
h_swish() if use_hs else nn.ReLU(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
y = self.conv(x)
if self.identity:
return x + y
else:
return y
class MobileNetV3(nn.Module):
def __init__(self, cfgs, mode, num_classes=1000, width_mult=1.):
super(MobileNetV3, self).__init__()
# setting of inverted residual blocks
self.cfgs = cfgs
assert mode in ['large', 'small']
# building first layer
input_channel = _make_divisible(16 * width_mult, 8)
layers = [conv_3x3_bn(3, input_channel, 2)]
# building inverted residual blocks
block = InvertedResidual
for k, exp_size, c, use_se, use_hs, s in self.cfgs:
output_channel = _make_divisible(c * width_mult, 8)
layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs))
input_channel = output_channel
self.features = nn.Sequential(*layers)
# building last several layers
self.conv = nn.Sequential(
conv_1x1_bn(input_channel, _make_divisible(exp_size * width_mult, 8)),
SELayer(_make_divisible(exp_size * width_mult, 8)) if mode == 'small' else nn.Sequential()
)
self.avgpool = nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)),
h_swish()
)
output_channel = _make_divisible(1280 * width_mult, 8) if width_mult > 1.0 else 1280
self.classifier = nn.Sequential(
nn.Linear(_make_divisible(exp_size * width_mult, 8), output_channel),
nn.BatchNorm1d(output_channel) if mode == 'small' else nn.Sequential(),
h_swish(),
nn.Linear(output_channel, num_classes),
nn.BatchNorm1d(num_classes) if mode == 'small' else nn.Sequential(),
h_swish() if mode == 'small' else nn.Sequential()
)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = self.conv(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def mobilenetv3_large(**kwargs):
"""
Constructs a MobileNetV3-Large model
"""
cfgs = [
# k, t, c, SE, NL, s
[3, 16, 16, 0, 0, 1],
[3, 64, 24, 0, 0, 2],
[3, 72, 24, 0, 0, 1],
[5, 72, 40, 1, 0, 2],
[5, 120, 40, 1, 0, 1],
[5, 120, 40, 1, 0, 1],
[3, 240, 80, 0, 1, 2],
[3, 200, 80, 0, 1, 1],
[3, 184, 80, 0, 1, 1],
[3, 184, 80, 0, 1, 1],
[3, 480, 112, 1, 1, 1],
[3, 672, 112, 1, 1, 1],
[5, 672, 160, 1, 1, 1],
[5, 672, 160, 1, 1, 2],
[5, 960, 160, 1, 1, 1]
]
return MobileNetV3(cfgs, mode='large', **kwargs)
def mobilenetv3_small(**kwargs):
"""
Constructs a MobileNetV3-Small model
"""
cfgs = [
# k, t, c, SE, NL, s
[3, 16, 16, 1, 0, 2],
[3, 72, 24, 0, 0, 2],
[3, 88, 24, 0, 0, 1],
[5, 96, 40, 1, 1, 2],
[5, 240, 40, 1, 1, 1],
[5, 240, 40, 1, 1, 1],
[5, 120, 48, 1, 1, 1],
[5, 144, 48, 1, 1, 1],
[5, 288, 96, 1, 1, 2],
[5, 576, 96, 1, 1, 1],
[5, 576, 96, 1, 1, 1],
]
return MobileNetV3(cfgs, mode='small', **kwargs)
net_large = mobilenetv3_large()
net_large.eval()
net_large.to('cuda:0')
#net_small = mobilenetv3_small()
#net_small.eval()
#net_small.to('cuda:0')
state_dict = torch.load(('pretrained/mobilenetv3-large-657e7b3d.pth'))
state_dict["classifier.0.weight"] = state_dict["classifier.1.weight"]
del state_dict["classifier.1.weight"]
state_dict["classifier.0.bias"] = state_dict["classifier.1.bias"]
del state_dict["classifier.1.bias"]
state_dict["classifier.3.weight"] = state_dict["classifier.5.weight"]
state_dict["classifier.3.bias"] = state_dict["classifier.5.bias"]
del state_dict["classifier.5.weight"]
del state_dict["classifier.5.bias"]
net_large.load_state_dict(state_dict)
#net_small.load_state_dict(torch.load('pretrained/mobilenetv3-small-c7eb32fe.pth'))
#f = open("mbv3_small.wts", "w")
f = open("mbv3_large.wts", "w")
f.write("{}\n".format(len(net_large.state_dict().keys())))
for k, v in net_large.state_dict().items():
vr = v.reshape(-1).cpu().numpy()
f.write("{} {}".format(k,len(vr)))
for vv in vr:
f.write(" ")
f.write(struct.pack(">f", float(vv)).hex())
f.write("\n")
x = torch.ones(1,3,224,224).to('cuda:0')
#print(net_small)
for i in range(10):
s = time.time()
y = net_large(x)
print("time:",time.time() -s)
print(y)