-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
280 lines (230 loc) · 10 KB
/
model.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
from typing import Union
from functools import partial
from tensorflow.keras import layers, Model
from CA import coordinate
def _make_divisible(ch, divisor=8, min_ch=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
"""
if min_ch is None:
min_ch = divisor
new_ch = max(min_ch, int(ch + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_ch < 0.9 * ch:
new_ch += divisor
return new_ch
def correct_pad(input_size: Union[int, tuple], kernel_size: int):
"""Returns a tuple for zero-padding for 2D convolution with downsampling.
Arguments:
input_size: Input tensor size.
kernel_size: An integer or tuple/list of 2 integers.
Returns:
A tuple.
"""
if isinstance(input_size, int):
input_size = (input_size, input_size)
kernel_size = (kernel_size, kernel_size)
adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2)
correct = (kernel_size[0] // 2, kernel_size[1] // 2)
return ((correct[0] - adjust[0], correct[0]),
(correct[1] - adjust[1], correct[1]))
class HardSigmoid(layers.Layer):
def __init__(self, **kwargs):
super(HardSigmoid, self).__init__(**kwargs)
self.relu6 = layers.ReLU(6.)
def call(self, inputs, **kwargs):
x = self.relu6(inputs + 3) * (1. / 6)
return x
class HardSwish(layers.Layer):
def __init__(self, **kwargs):
super(HardSwish, self).__init__(**kwargs)
self.hard_sigmoid = HardSigmoid()
def call(self, inputs, **kwargs):
x = self.hard_sigmoid(inputs) * inputs
return x
def _se_block(inputs, filters, prefix, se_ratio=1 / 4.):
# [batch, height, width, channel] -> [batch, channel]
x = layers.GlobalAveragePooling2D(name=prefix + 'squeeze_excite/AvgPool')(inputs)
# Target shape. Tuple of integers, does not include the samples dimension (batch size).
# [batch, channel] -> [batch, 1, 1, channel]
x = layers.Reshape((1, 1, filters))(x)
# fc1
x = layers.Conv2D(filters=_make_divisible(filters * se_ratio),
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv')(x)
x = layers.ReLU(name=prefix + 'squeeze_excite/Relu')(x)
# fc2
x = layers.Conv2D(filters=filters,
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv_1')(x)
x = HardSigmoid(name=prefix + 'squeeze_excite/HardSigmoid')(x)
x = layers.Multiply(name=prefix + 'squeeze_excite/Mul')([inputs, x])
return x
def _inverted_res_block(x,
input_c: int, # input channel
kernel_size: int, # kennel size
exp_c: int, # expanded channel
out_c: int, # out channel
use_att: int, # using SE/CA/none
activation: str, # RE or HS
stride: int,
block_id: int,
alpha: float = 1.0):
bn = partial(layers.BatchNormalization, epsilon=0.001, momentum=0.99)
input_c = _make_divisible(input_c * alpha)
exp_c = _make_divisible(exp_c * alpha)
out_c = _make_divisible(out_c * alpha)
act = layers.ReLU if activation == "RE" else HardSwish
shortcut = x
prefix = 'expanded_conv/'
if block_id:
# expand channel
prefix = 'expanded_conv_{}/'.format(block_id)
x = layers.Conv2D(filters=exp_c,
kernel_size=1,
padding='same',
use_bias=False,
name=prefix + 'expand')(x)
x = bn(name=prefix + 'expand/BatchNorm')(x)
x = act(name=prefix + 'expand/' + act.__name__)(x)
if stride == 2:
input_size = (x.shape[1], x.shape[2]) # height, width
x = layers.ZeroPadding2D(padding=correct_pad(input_size, kernel_size),
name=prefix + 'depthwise/pad')(x)
x = layers.DepthwiseConv2D(kernel_size=kernel_size,
strides=stride,
padding='same' if stride == 1 else 'valid',
use_bias=False,
name=prefix + 'depthwise')(x)
x = bn(name=prefix + 'depthwise/BatchNorm')(x)
x = act(name=prefix + 'depthwise/' + act.__name__)(x)
if use_att==1:
x = _se_block(x, filters=exp_c, prefix=prefix)
elif use_att==2:
x = coordinate(x)
x = layers.Conv2D(filters=out_c,
kernel_size=1,
padding='same',
use_bias=False,
name=prefix + 'project')(x)
x = bn(name=prefix + 'project/BatchNorm')(x)
if stride == 1 and input_c == out_c:
x = layers.Add(name=prefix + 'Add')([shortcut, x])
return x
# large
def mobilenet_v3_large_CA(input_shape=(224, 224, 3),
num_classes=1000,
alpha=1.0,
include_top=True):
bn = partial(layers.BatchNormalization, epsilon=0.001, momentum=0.99)
img_input = layers.Input(shape=input_shape)
x = layers.Conv2D(filters=16,
kernel_size=3,
strides=(2, 2),
padding='same',
use_bias=False,
name="Conv")(img_input)
x = bn(name="Conv/BatchNorm")(x)
x = HardSwish(name="Conv/HardSwish")(x)
inverted_cnf = partial(_inverted_res_block, alpha=alpha)
# input, input_c, k_size, expand_c, use_att, activation, stride, block_id
x = inverted_cnf(x, 16, 3, 16, 16, 0, "RE", 1, 0)
x = inverted_cnf(x, 16, 3, 64, 24, 0, "RE", 2, 1)
x = inverted_cnf(x, 24, 3, 72, 24, 0, "RE", 1, 2)
x = inverted_cnf(x, 24, 5, 72, 40, 2, "RE", 2, 3)
x = inverted_cnf(x, 40, 5, 120, 40, 2, "RE", 1, 4)
x = inverted_cnf(x, 40, 5, 120, 40, 2, "RE", 1, 5)
x = inverted_cnf(x, 40, 3, 240, 80, 0, "HS", 2, 6)
x = inverted_cnf(x, 80, 3, 200, 80, 0, "HS", 1, 7)
x = inverted_cnf(x, 80, 3, 184, 80, 0, "HS", 1, 8)
x = inverted_cnf(x, 80, 3, 184, 80, 0, "HS", 1, 9)
x = inverted_cnf(x, 80, 3, 480, 112, 1, "HS", 1, 10)
x = inverted_cnf(x, 112, 3, 672, 112, 1, "HS", 1, 11)
x = inverted_cnf(x, 112, 5, 672, 160, 2, "HS", 2, 12)
x = inverted_cnf(x, 160, 5, 960, 160, 2, "HS", 1, 13)
x = inverted_cnf(x, 160, 5, 960, 160, 1, "HS", 1, 14)
last_c = _make_divisible(160 * 6 * alpha)
last_point_c = _make_divisible(1280 * alpha)
x = layers.Conv2D(filters=last_c,
kernel_size=1,
padding='same',
use_bias=False,
name="Conv_1")(x)
x = bn(name="Conv_1/BatchNorm")(x)
x = HardSwish(name="Conv_1/HardSwish")(x)
if include_top is True:
x = layers.GlobalAveragePooling2D()(x)
x = layers.Reshape((1, 1, last_c))(x)
# fc1
x = layers.Conv2D(filters=last_point_c,
kernel_size=1,
padding='same',
name="Conv_2")(x)
x = HardSwish(name="Conv_2/HardSwish")(x)
# fc2
x = layers.Conv2D(filters=num_classes,
kernel_size=1,
padding='same',
name='Logits/Conv2d_1c_1x1')(x)
x = layers.Flatten()(x)
x = layers.Softmax(name="Predictions")(x)
model = Model(img_input, x, name="MobilenetV3largeCA")
return model
#small
def mobilenet_v3_small_CA(input_shape=(224, 224, 3),
num_classes=1000,
alpha=1.0,
include_top=True):
bn = partial(layers.BatchNormalization, epsilon=0.001, momentum=0.99)
img_input = layers.Input(shape=input_shape)
x = layers.Conv2D(filters=16,
kernel_size=3,
strides=(2, 2),
padding='same',
use_bias=False,
name="Conv")(img_input)
x = bn(name="Conv/BatchNorm")(x)
x = HardSwish(name="Conv/HardSwish")(x)
inverted_cnf = partial(_inverted_res_block, alpha=alpha)
# input, input_c, k_size, expand_c, use_att, activation, stride, block_id
x = inverted_cnf(x, 16, 3, 16, 16, 1, "RE", 2, 0)
x = inverted_cnf(x, 16, 3, 72, 24, 0, "RE", 2, 1)
x = inverted_cnf(x, 24, 3, 88, 24, 0, "RE", 1, 2)
x = inverted_cnf(x, 24, 5, 96, 40, 2, "HS", 2, 3)
x = inverted_cnf(x, 40, 5, 240, 40, 2, "HS", 1, 4)
x = inverted_cnf(x, 40, 5, 240, 40, 2, "HS", 1, 5)
x = inverted_cnf(x, 40, 5, 120, 48, 2, "HS", 1, 6)
x = inverted_cnf(x, 48, 5, 144, 48, 2, "HS", 1, 7)
x = inverted_cnf(x, 48, 5, 288, 96, 2, "HS", 2, 8)
x = inverted_cnf(x, 96, 5, 576, 96, 2, "HS", 1, 9)
x = inverted_cnf(x, 96, 5, 576, 96, 2, "HS", 1, 10)
last_c = _make_divisible(96 * 6 * alpha)
last_point_c = _make_divisible(1024 * alpha)
x = layers.Conv2D(filters=last_c,
kernel_size=1,
padding='same',
use_bias=False,
name="Conv_1")(x)
x = bn(name="Conv_1/BatchNorm")(x)
x = HardSwish(name="Conv_1/HardSwish")(x)
if include_top is True:
x = layers.GlobalAveragePooling2D()(x)
x = layers.Reshape((1, 1, last_c))(x)
# fc1
x = layers.Conv2D(filters=last_point_c,
kernel_size=1,
padding='same',
name="Conv_2")(x)
x = HardSwish(name="Conv_2/HardSwish")(x)
# fc2
x = layers.Conv2D(filters=num_classes,
kernel_size=1,
padding='same',
name='Logits/Conv2d_1c_1x1')(x)
x = layers.Flatten()(x)
x = layers.Softmax(name="Predictions")(x)
model = Model(img_input, x, name="MobilenetV3smallCA")
return model