-
Notifications
You must be signed in to change notification settings - Fork 10
/
MobileNetV3.py
executable file
·382 lines (322 loc) · 14.6 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
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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# Copyright 2019 aiboy.wei Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Implementation of paper Searching for MobileNetV3, https://arxiv.org/abs/1905.02244
author: [email protected]
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
MobileNetV3_Small_Spec = [
# Op k exp out SE NL s
[ "ConvBnAct", 3, False, 16, False, "hswish", 2 ],
[ "bneck", 3, 16, 16, True, "relu", 2 ],
[ "bneck", 3, 72, 24, False, "relu", 2 ],
[ "bneck", 3, 88, 24, False, "relu", 1 ],
[ "bneck", 5, 96, 40, True, "hswish", 2 ],
[ "bneck", 5, 240, 40, True, "hswish", 1 ],
[ "bneck", 5, 240, 40, True, "hswish", 1 ],
[ "bneck", 5, 120, 48, True, "hswish", 1 ],
[ "bneck", 5, 144, 48, True, "hswish", 1 ],
[ "bneck", 5, 288, 96, True, "hswish", 2 ],
[ "bneck", 5, 576, 96, True, "hswish", 1 ],
[ "bneck", 5, 576, 96, True, "hswish", 1 ],
[ "ConvBnAct", 1, False, 576, True, "hswish", 1 ],
[ "pool", 7, False, False, False, "None", 1 ],
[ "ConvNBnAct", 1, False, 1280, False, "hswish", 1 ],
[ "ConvNBnAct", 1, False, 1000, False, "None", 1 ],
]
MobileNetV3_Large_Spec = [
# Op k exp out SE NL s
[ "ConvBnAct", 3, False, 16, False, "hswish", 2 ],
[ "bneck", 3, 16, 16, False, "relu", 1 ],
[ "bneck", 3, 64, 24, False, "relu", 2 ],
[ "bneck", 3, 72, 24, False, "relu", 1 ],
[ "bneck", 5, 72, 40, True, "relu", 2 ],
[ "bneck", 5, 120, 40, True, "relu", 1 ],
[ "bneck", 5, 120, 40, True, "relu", 1 ],
[ "bneck", 3, 240, 80, False, "hswish", 2 ],
[ "bneck", 3, 200, 80, False, "hswish", 1 ],
[ "bneck", 3, 184, 80, False, "hswish", 1 ],
[ "bneck", 3, 184, 80, False, "hswish", 1 ],
[ "bneck", 3, 480, 112, True, "hswish", 1 ],
[ "bneck", 3, 672, 112, True, "hswish", 1 ],
[ "bneck", 5, 672, 160, True, "hswish", 2 ],
[ "bneck", 5, 960, 160, True, "hswish", 1 ],
[ "bneck", 5, 960, 160, True, "hswish", 1 ],
[ "ConvBnAct", 1, False, 960, False, "hswish", 1 ],
[ "pool", 7, False, False, False, "None", 1 ],
[ "ConvNBnAct", 1, False, 1280, False, "hswish", 1 ],
[ "ConvNBnAct", 1, False, 1000, False, "None", 1 ],
]
def _make_divisible(v, divisor, min_value=None):
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 Identity(tf.keras.layers.Layer):
def __init__(self, name="Identity", **kwargs):
super(Identity, self).__init__(name=name, **kwargs)
def call(self, input):
return input
def get_config(self):
base_config = super(Identity, self).get_config()
return dict(list(base_config.items()))
class HardSigmoid(tf.keras.layers.Layer):
def __init__(self, name="HardSigmoid", **kwargs):
super(HardSigmoid, self).__init__(name=name, **kwargs)
self.relu6 = tf.keras.layers.ReLU(max_value=6, name="ReLU6", **kwargs)
def call(self, input):
return self.relu6(input + 3.0) / 6.0
def get_config(self):
base_config = super(HardSigmoid, self).get_config()
return dict(list(base_config.items()))
class HardSwish(tf.keras.layers.Layer):
def __init__(self, name="HardSwish", **kwargs):
super(HardSwish, self).__init__(name=name, **kwargs)
self.relu6 = tf.keras.layers.ReLU(max_value=6, name="ReLU6", **kwargs)
def call(self, input):
return input * self.relu6(input + 3.0) / 6.0
def get_config(self):
base_config = super(HardSwish, self).get_config()
return dict(list(base_config.items()))
_available_activation = {
"relu": tf.keras.layers.ReLU(name="ReLU"),
"relu6": tf.keras.layers.ReLU(max_value=6, name="ReLU6"),
"hswish": HardSwish(),
"hsigmoid": HardSigmoid(),
"softmax": tf.keras.layers.Softmax(name="Softmax"),
"None": Identity(),
}
class SENet(tf.keras.layers.Layer):
def __init__(self, reduction=4, l2=2e-4, name="SENet", **kwargs):
super(SENet, self).__init__(name=name, **kwargs)
self.reduction = reduction
self.l2 = l2
def build(self, input_shape):
_, h, w, c = input_shape
self.gap = tf.keras.layers.GlobalAveragePooling2D(name=f'AvgPool{h}x{w}')
self.fc1 = tf.keras.layers.Dense(units=c//self.reduction, activation="relu", use_bias=False,
kernel_regularizer=tf.keras.regularizers.l2(self.l2), name="Squeeze")
self.fc2 = tf.keras.layers.Dense(units=c, activation=HardSigmoid(), use_bias=False,
kernel_regularizer=tf.keras.regularizers.l2(self.l2), name="Excite")
self.reshape = tf.keras.layers.Reshape((1, 1, c), name=f'Reshape_None_1_1_{c}')
super().build(input_shape)
def call(self, input):
output = self.gap(input)
output = self.fc1(output)
output = self.fc2(output)
output = self.reshape(output)
return input * output
def get_config(self):
config = {"reduction":self.reduction, "l2":self.l2}
base_config = super(SENet, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class ConvBnAct(tf.keras.layers.Layer):
def __init__(self, k, exp, out, SE, NL, s, l2, name="ConvBnAct", **kwargs):
super(ConvBnAct, self).__init__(name=name, **kwargs)
self.k = k
self.exp = exp
self.out = out
self.se = SE
self.nl = NL
self.s = s
self.l2 = l2
self.conv2d = tf.keras.layers.Conv2D(filters=out, kernel_size=k, strides=s, activation=None, padding="same",
kernel_regularizer=tf.keras.regularizers.l2(l2), name="conv2d", **kwargs)
self.bn = tf.keras.layers.BatchNormalization(momentum=0.99, name="BatchNormalization", **kwargs)
self.act = _available_activation[NL]
def call(self, input):
output = self.conv2d(input)
output = self.bn(output)
output = self.act(output)
return output
def get_config(self):
config = {"k":self.k, "exp":self.exp, "out":self.out, "SE":self.se, "NL":self.nl, "s":self.s, "l2":self.l2}
base_config = super(ConvBnAct, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class ConvNBnAct(tf.keras.layers.Layer):
def __init__(self, k, exp, out, SE, NL, s, l2, name="ConvNBnAct", **kwargs):
super(ConvNBnAct, self).__init__(name=name, **kwargs)
self.k = k
self.exp = exp
self.out = out
self.se = SE
self.nl = NL
self.s = s
self.l2 = l2
self.act = _available_activation[NL]
self.fn = tf.keras.layers.Conv2D(filters=out, kernel_size=k, strides=s, activation=self.act, padding="same",
kernel_regularizer=tf.keras.regularizers.l2(l2),name="conv2d", **kwargs)
def call(self, input):
output = self.fn(input)
return output
def get_config(self):
config = {"k":self.k, "exp":self.exp, "out":self.out, "SE":self.se, "NL":self.nl, "s":self.s, "l2":self.l2}
base_config = super(ConvNBnAct, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class Pool(tf.keras.layers.Layer):
def __init__(self, k, exp, out, SE, NL, s, l2, name="Pool", **kwargs):
super(Pool, self).__init__(name=name, **kwargs)
self.k = k
self.exp = exp
self.out = out
self.se = SE
self.nl = NL
self.s = s
self.l2 = l2
self.gap = tf.keras.layers.AveragePooling2D(pool_size=(k, k), strides=1, name=f'AvgPool{k}x{k}', **kwargs)
def call(self, input):
output = self.gap(input)
return output
def get_config(self):
config = {"k":self.k, "exp":self.exp, "out":self.out, "SE":self.se, "NL":self.nl, "s":self.s, "l2":self.l2}
base_config = super(Pool, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class BottleNeck(tf.keras.layers.Layer):
def __init__(self, k, exp, out, SE, NL, s, l2, name="BottleNeck", **kwargs):
super(BottleNeck, self).__init__(name=name, **kwargs)
self.k = k
self.exp = exp
self.out = out
self.se = SE
self.nl = NL
self.s = s
self.l2 = l2
self.expand = ConvBnAct(k=1, exp=exp, out=exp, SE=SE, NL=NL, s=1, l2=l2, name="BottleNeckExpand", **kwargs)
self.depthwise = tf.keras.layers.DepthwiseConv2D(
kernel_size=k,
strides=s,
padding="same",
use_bias=False,
depthwise_regularizer=tf.keras.regularizers.l2(l2),
name=f'Depthwise{k}x{k}',
** kwargs,
)
self.pointwise = tf.keras.layers.Conv2D(
filters=out,
kernel_size=1,
strides=1,
use_bias=False,
kernel_regularizer=tf.keras.regularizers.l2(l2),
name=f'Pointwise1x1',
** kwargs,
)
self.bn_1 = tf.keras.layers.BatchNormalization(momentum=0.99, name="BatchNormalization_1", **kwargs)
self.bn_2 = tf.keras.layers.BatchNormalization(momentum=0.99, name="BatchNormalization_2", **kwargs)
if self.se:
self.SeNet = SENet(name="SEBottleneck", l2=l2, **kwargs)
self.act = _available_activation[NL]
def call(self, input):
output = self.expand(input)
output = self.depthwise(output)
output = self.bn_1(output)
if self.se:
output = self.SeNet(output)
output = self.act(output)
output = self.pointwise(output)
output = self.bn_2(output)
if self.s == 1 and self.exp == self.out:
return input + output
else:
return output
def get_config(self):
config = {"k":self.k, "exp":self.exp, "out":self.out, "SE":self.se, "NL":self.nl, "s":self.s, "l2":self.l2}
base_config = super(BottleNeck, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
_available_mobilenetv3_spec = {
"small": MobileNetV3_Small_Spec,
"large": MobileNetV3_Large_Spec,
}
_available_operation = {
"ConvBnAct": ConvBnAct,
"bneck": BottleNeck,
"pool": Pool,
"ConvNBnAct": ConvNBnAct,
}
class CusReshape(tf.keras.layers.Layer):
def __init__(self, out, name="Reshape", **kwargs):
super(CusReshape, self).__init__(name=name, **kwargs)
self.out = out
self.reshape = tf.keras.layers.Reshape((out,), name=f'Reshape_None_{out}', **kwargs)
def call(self, input):
output = self.reshape(input)
return output
def get_config(self):
config = {"out":self.out}
base_config = super(CusReshape, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class CusDropout(tf.keras.layers.Layer):
def __init__(self, dropout_rate, name="Dropout", **kwargs):
super(CusDropout, self).__init__(name=name, **kwargs)
self.dropout_rate = dropout_rate
self.dropout = tf.keras.layers.Dropout(rate=dropout_rate, name=f'Dropout', **kwargs)
def call(self, input):
output = self.dropout(input)
return output
def get_config(self):
config = {"dropout_rate":self.dropout_rate}
base_config = super(CusDropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def MobileNetV3(type="large", input_shape=(224, 224, 3), classes_number=1000, width_multiplier=1.0,
divisible_by=8, l2_reg=2e-5, dropout_rate=0.2, name="MobileNetV3"):
spec = _available_mobilenetv3_spec[type]
spec[-1][3] = classes_number # bottlenet layer size or class numbers
name = name + "_" + type
inputs = tf.keras.layers.Input(shape=input_shape, name="inputs")
for i, params in enumerate(spec):
Op, k, exp, out, SE, NL, s = params
inference_op = _available_operation[Op]
if isinstance(exp, int):
exp_ch = _make_divisible(exp * width_multiplier, divisible_by)
else:
exp_ch = None
if isinstance(out, int):
out_ch = _make_divisible(out * width_multiplier, divisible_by)
else:
out_ch = None
if i == len(spec) - 1: # fix output classes error.
out_ch = classes_number
op_name = f'{Op}_{i}'
if i == 0:
output = inference_op(k, exp_ch, out_ch, SE, NL, s, l2_reg, op_name)(inputs)
else:
output = inference_op(k, exp_ch, out_ch, SE, NL, s, l2_reg, op_name)(output)
if (type == "small" and i == 14) or (type == "large" and i == 18):
output = CusDropout(dropout_rate=dropout_rate)(output)
outputs = CusReshape(out=classes_number)(output)
model = tf.keras.Model(inputs=inputs, outputs=outputs, name=name)
model.summary()
return model
custom_objects = {
"ConvBnAct" : ConvBnAct,
"BottleNeck": BottleNeck,
"Pool" : Pool,
"ConvNBnAct": ConvNBnAct,
"CusReshape": CusReshape,
"CusDropout": CusDropout,
}
if __name__ == "__main__":
# if you use gpu device
for gpu in tf.config.experimental.list_physical_devices('GPU'):
tf.compat.v2.config.experimental.set_memory_growth(gpu, True)
model = MobileNetV3(type="small")
model = MobileNetV3(type="large")