-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfmobilenetv3.py
286 lines (234 loc) · 9.85 KB
/
fmobilenetv3.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
import mxnet as mx
import mxnet.ndarray as nd
import mxnet.gluon as gluon
import mxnet.gluon.nn as nn
import mxnet.autograd as ag
# import symbol_utils
def make_divisible(x, divisible_by=8):
import numpy as np
return int(np.ceil(x * 1. / divisible_by) * divisible_by)
# def adaptiveAvgPool(inputsz, outputsz):
# import numpy as np
# s = np.floor(inputsz/outputsz).astype(np.int32)
# k = inputsz-(outputsz-1)*s
# return nn.AvgPool2D((k, k), s)
class AdaptiveAvgPool2D(nn.HybridBlock):
def __init__(self, output_size):
super(AdaptiveAvgPool2D, self).__init__()
self.output_size = output_size
def hybrid_forward(self, F, x):
return F.contrib.AdaptiveAvgPooling2D(x, self.output_size)
class ReLU6(nn.HybridBlock):
def __init__(self, **kwargs):
super(ReLU6, self).__init__(**kwargs)
def hybrid_forward(self, F, x):
return F.clip(x, 0, 6)
class HSwish(nn.HybridBlock):
def __init__(self):
super(HSwish, self).__init__()
def hybrid_forward(self, F, x):
return x * F.clip(x+3.0, 0, 6)/ 6.0
class HSigmoid(nn.HybridBlock):
def __init__(self):
super(HSigmoid, self).__init__()
def hybrid_forward(self, F, x):
return F.clip(x+3.0, 0, 6)/6.0
class SEBlock(nn.HybridBlock):
r"""SEBlock from `"Aggregated Residual Transformations for Deep Neural Network"
<http://arxiv.org/abs/1611.05431>`_ paper.
Parameters
----------
cardinality: int
Number of groups
bottleneck_width: int
Width of bottleneck block
stride : int
Stride size.
downsample : bool, default False
Whether to downsample the input.
"""
def __init__(self, channels, cardinality, bottleneck_width, stride,
downsample=False, **kwargs):
super(SEBlock, self).__init__(**kwargs)
D = int(math.floor(channels * (bottleneck_width / 64)))
group_width = cardinality * D
self.body = nn.HybridSequential(prefix='')
self.body.add(nn.Conv2D(group_width//2, kernel_size=1, use_bias=False))
self.body.add(nn.BatchNorm())
self.body.add(nn.Activation('relu'))
self.body.add(nn.Conv2D(group_width, kernel_size=3, strides=stride, padding=1,
use_bias=False))
self.body.add(nn.BatchNorm())
self.body.add(nn.Activation('relu'))
self.body.add(nn.Conv2D(channels * 4, kernel_size=1, use_bias=False))
self.body.add(nn.BatchNorm())
self.se = nn.HybridSequential(prefix='')
self.se.add(nn.Dense(channels // 4, use_bias=False))
self.se.add(nn.Activation('relu'))
self.se.add(nn.Dense(channels * 4, use_bias=False))
self.se.add(nn.Activation('sigmoid'))
if downsample:
self.downsample = nn.HybridSequential(prefix='')
self.downsample.add(nn.Conv2D(channels * 4, kernel_size=1, strides=stride,
use_bias=False))
self.downsample.add(nn.BatchNorm())
else:
self.downsample = None
def hybrid_forward(self, F, x):
residual = x
x = self.body(x)
w = F.contrib.AdaptiveAvgPooling2D(x, output_size=1)
w = self.se(w)
x = F.broadcast_mul(x, w.expand_dims(axis=2).expand_dims(axis=2))
if self.downsample:
residual = self.downsample(residual)
x = F.Activation(x + residual, act_type='relu')
return x
class SEModule(nn.HybridBlock):
def __init__(self, channel, reduction=4):
super(SEModule, self).__init__()
# self.avg_pool = nn.contrib.AdaptiveAvgPooling2D()
self.fc = nn.HybridSequential()
self.fc.add(nn.Conv2D(channel//reduction,kernel_size=1, padding=0, use_bias=False),
nn.Activation("relu"),
nn.Conv2D(channel,kernel_size=1, padding=0, use_bias=False),
HSigmoid())
def hybrid_forward(self, F, x):
w = F.contrib.AdaptiveAvgPooling2D(x, output_size=1)
w = self.fc(w)
x = F.broadcast_mul(x, w)
return x
def conv_bn(channels, filter_size, stride, activation=nn.Activation('relu')):
out = nn.HybridSequential()
out.add(
nn.Conv2D(channels, 3, stride, 1, use_bias=False),
nn.BatchNorm(scale=True),
activation
)
return out
def conv_1x1_bn(channels, activation=nn.Activation('relu')):
out = nn.HybridSequential()
out.add(
nn.Conv2D(channels, 1, 1, 0, use_bias=False),
nn.BatchNorm(scale=True),
activation
)
return out
class MobileBottleNeck(nn.HybridBlock):
def __init__(self, channels, kernel, stride, exp, se=False, short_cut = True, act="RE"):
super(MobileBottleNeck, self).__init__()
self.out = nn.HybridSequential()
assert stride in [1, 2]
assert kernel in [3, 5]
assert act in ["RE", "HS"]
padding = (kernel - 1) // 2
self.short_cut = short_cut
conv_layer = nn.Conv2D
norm_layer = nn.BatchNorm
activation = nn.Activation('relu') if act == "RE" else HSwish()
if se:
SELayer = SEModule(exp)
self.out.add(
conv_layer(exp, 1, 1, 0, use_bias=False),
norm_layer(scale=True),
activation,
conv_layer(exp, kernel, stride, padding, groups=exp, use_bias=False),
norm_layer(scale=True),
############################
SELayer,
############################
activation,
conv_layer(channels, 1, 1, 0, use_bias=False),
norm_layer(scale=True),
# SELayer(exp, )
)
else:
self.out.add(
conv_layer(exp, 1, 1, 0, use_bias=False),
norm_layer(scale=True),
activation,
conv_layer(exp, kernel, stride, padding, groups=exp, use_bias=False),
norm_layer(scale=True),
activation,
conv_layer(channels, 1, 1, 0, use_bias=False),
norm_layer(scale=True),
)
def hybrid_forward(self, F, x):
return x + self.out(x) if self.short_cut else self.out(x)
class MobileNetV3(nn.HybridBlock):
def __init__(self, classes=1000, width_mult=1.0, mode="large", **kwargs):
super(MobileNetV3, self).__init__()
assert mode in ["large", "small"]
# assert input_size%32 == 0
# self.w = width_mult
setting = []
last_channel = 1280
input_channel = 16
if mode=="large":
setting = [
# k, exp, c, se, nl, s, short_cut
[3, 16, 16, False, 'RE', 1, False],
[3, 64, 24, False, 'RE', 2, False],
[3, 72, 24, False, 'RE', 1, True],
[5, 72, 40, True, 'RE', 2, False],
[5, 120, 40, True, 'RE', 1, True],
[5, 120, 40, True, 'RE', 1, True],
[3, 240, 80, False, 'HS', 2, False],
[3, 200, 80, False, 'HS', 1, True],
[3, 184, 80, False, 'HS', 1, True],
[3, 184, 80, False, 'HS', 1, True],
[3, 480, 112, True, 'HS', 1, False],
[3, 672, 112, True, 'HS', 1, True],
[5, 672, 112, True, 'HS', 1, True],
[5, 672, 160, True, 'HS', 2, False],
[5, 960, 160, True, 'HS', 1, True],
]
else:
setting = [
# k, exp, c, se, nl, s,
[3, 16, 16, True, 'RE', 2, False],
[3, 72, 24, False, 'RE', 2, False],
[3, 88, 24, False, 'RE', 1, True],
[5, 96, 40, True, 'HS', 2, False], # stride = 2, paper set it to 1 by error
[5, 240, 40, True, 'HS', 1, True],
[5, 240, 40, True, 'HS', 1, True],
[5, 120, 48, True, 'HS', 1, False],
[5, 144, 48, True, 'HS', 1, True],
[5, 288, 96, True, 'HS', 2, False],
[5, 576, 96, True, 'HS', 1, True],
[5, 576, 96, True, 'HS', 1, True],
]
self.last_channel = make_divisible(last_channel * width_mult) if width_mult > 1.0 else last_channel
self.layers = [conv_bn(input_channel, 3, 2, activation=HSwish())]
for kernel_size, exp, channel, se, act, s, short_cut in setting:
# short_cut = (s == 1)
output_channel = make_divisible(channel * width_mult)
exp_channel = make_divisible(exp * width_mult)
self.layers.append(MobileBottleNeck(output_channel, kernel_size, s, exp_channel, se, short_cut, act))
if mode == "large":
last_conv = make_divisible(960 * width_mult)
self.layers.append(conv_1x1_bn(last_channel, HSwish()))
self.layers.append(AdaptiveAvgPool2D(output_size=1))
self.layers.append(HSwish())
self.layers.append(nn.Conv2D(last_channel, 1, 1, 0))
self.layers.append(HSwish())
else:
last_conv = make_divisible(576 * width_mult)
self.layers.append(conv_1x1_bn(last_channel, HSwish()))
self.layers.append(SEModule(last_channel))
self.layers.append(AdaptiveAvgPool2D(output_size=1))
self.layers.append(HSwish())
self.layers.append(conv_1x1_bn(last_channel, HSwish()))
self._layers = nn.HybridSequential()
self._layers.add(*self.layers)
def hybrid_forward(self, F, x):
return self._layers(x)
def get_symbol(num_classes=256, mode="small", **kwargs):
net = MobileNetV3(mode=mode)
data = mx.sym.Variable(name='data')
data = (data-127.5)
data = data*0.0078125
body = net(data)
import symbol_utils
body = symbol_utils.get_fc1(body, num_classes, "E")
return body