forked from Bisonai/mobilenetv3-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
342 lines (289 loc) · 9.4 KB
/
layers.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
# Copyright 2019 Bisonai 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
Layers of MobileNetV3
"""
import tensorflow as tf
from utils import get_layer
class Identity(tf.keras.layers.Layer):
def __init__(self):
super().__init__(name="Identity")
def call(self, input):
return input
class ReLU6(tf.keras.layers.Layer):
def __init__(self):
super().__init__(name="ReLU6")
self.relu6 = tf.keras.layers.ReLU(max_value=6, name="ReLU6")
def call(self, input):
return self.relu6(input)
class HardSigmoid(tf.keras.layers.Layer):
def __init__(self):
super().__init__(name="HardSigmoid")
self.relu6 = ReLU6()
def call(self, input):
return self.relu6(input + 3.0) / 6.0
class HardSwish(tf.keras.layers.Layer):
def __init__(self, name="HardSwish"):
super().__init__(name=name)
self.hard_sigmoid = HardSigmoid()
def call(self, input):
return input * self.hard_sigmoid(input)
class Squeeze(tf.keras.layers.Layer):
"""Squeeze the second and third dimensions of given tensor.
(batch, 1, 1, channels) -> (batch, channels)
"""
def __init__(self):
super().__init__(name="Squeeze")
def call(self, input):
x = tf.keras.backend.squeeze(input, 1)
x = tf.keras.backend.squeeze(x, 1)
return x
class GlobalAveragePooling2D(tf.keras.layers.Layer):
"""Return tensor of output shape (batch_size, rows, cols, channels)
where rows and cols are equal to 1. Output shape of
`tf.keras.layer.GlobalAveragePooling2D` is (batch_size, channels),
"""
def __init__(self):
super().__init__(name="GlobalAveragePooling2D")
def build(self, input_shape):
pool_size = tuple(map(int, input_shape[1:3]))
self.gap = tf.keras.layers.AveragePooling2D(
pool_size=pool_size,
name=f"AvgPool{pool_size[0]}x{pool_size[1]}",
)
super().build(input_shape)
def call(self, input):
return self.gap(input)
class BatchNormalization(tf.keras.layers.Layer):
"""Searching fo MobileNetV3: All our convolutional layers
use batch-normalization layers with average decay of 0.99.
"""
def __init__(
self,
momentum: float=0.99,
name="BatchNormalization",
):
super().__init__(name=name)
self.bn = tf.keras.layers.BatchNormalization(
momentum=0.99,
name="BatchNormalization",
)
def call(self, input):
return self.bn(input)
class ConvNormAct(tf.keras.layers.Layer):
def __init__(
self,
filters: int,
kernel_size: int=3,
stride: int=1,
padding: int=0,
norm_layer: str=None,
act_layer: str="relu",
use_bias: bool=True,
l2_reg: float=1e-5,
name: str="ConvNormAct",
):
super().__init__(name=name)
if padding > 0:
self.pad = tf.keras.layers.ZeroPadding2D(
padding=padding,
name=f"Padding{padding}x{padding}",
)
else:
self.pad = Identity()
self.conv = tf.keras.layers.Conv2D(
filters=filters,
kernel_size=kernel_size,
strides=stride,
name=f"Conv{kernel_size}x{kernel_size}",
kernel_regularizer=tf.keras.regularizers.l2(l2_reg),
use_bias=use_bias,
)
_available_normalization = {
"bn": BatchNormalization(),
}
self.norm = get_layer(norm_layer, _available_normalization, Identity())
_available_activation = {
"relu": tf.keras.layers.ReLU(name="ReLU"),
"relu6": ReLU6(),
"hswish": HardSwish(),
"hsigmoid": HardSigmoid(),
"softmax": tf.keras.layers.Softmax(name="Softmax"),
}
self.act = get_layer(act_layer, _available_activation, Identity())
def call(self, input):
x = self.pad(input)
x = self.conv(x)
x = self.norm(x)
x = self.act(x)
return x
class Bneck(tf.keras.layers.Layer):
def __init__(
self,
out_channels: int,
exp_channels: int,
kernel_size: int,
stride: int,
use_se: bool,
act_layer: str,
l2_reg: float=1e-5,
):
super().__init__(name="Bneck")
self.out_channels = out_channels
self.stride = stride
self.use_se = use_se
# Expand
self.expand = ConvNormAct(
exp_channels,
kernel_size=1,
norm_layer="bn",
act_layer=act_layer,
use_bias=False,
l2_reg=l2_reg,
name="Expand",
)
# Depthwise
dw_padding = (kernel_size - 1) // 2
self.pad = tf.keras.layers.ZeroPadding2D(
padding=dw_padding,
name=f"Depthwise/Padding{dw_padding}x{dw_padding}",
)
self.depthwise = tf.keras.layers.DepthwiseConv2D(
kernel_size=kernel_size,
strides=stride,
name=f"Depthwise/DWConv{kernel_size}x{kernel_size}",
depthwise_regularizer=tf.keras.regularizers.l2(l2_reg),
use_bias=False,
)
self.bn = BatchNormalization(name="Depthwise/BatchNormalization")
if self.use_se:
self.se = SEBottleneck(
l2_reg=l2_reg,
name="Depthwise/SEBottleneck",
)
_available_activation = {
"relu": tf.keras.layers.ReLU(name="Depthwise/ReLU"),
"hswish": HardSwish(name="Depthwise/HardSwish"),
}
self.act = get_layer(act_layer, _available_activation, Identity())
# Project
self.project = ConvNormAct(
out_channels,
kernel_size=1,
norm_layer="bn",
act_layer=None,
use_bias=False,
l2_reg=l2_reg,
name="Project",
)
def build(self, input_shape):
self.in_channels = int(input_shape[3])
super().build(input_shape)
def call(self, input):
x = self.expand(input)
x = self.pad(x)
x = self.depthwise(x)
x = self.bn(x)
if self.use_se:
x = self.se(x)
x = self.act(x)
x = self.project(x)
if self.stride == 1 and self.in_channels == self.out_channels:
return input + x
else:
return x
class SEBottleneck(tf.keras.layers.Layer):
def __init__(
self,
reduction: int=4,
l2_reg: float=0.01,
name: str="SEBottleneck",
):
super().__init__(name=name)
self.reduction = reduction
self.l2_reg = l2_reg
def build(self, input_shape):
input_channels = int(input_shape[3])
self.gap = GlobalAveragePooling2D()
self.conv1 = ConvNormAct(
input_channels // self.reduction,
kernel_size=1,
norm_layer=None,
act_layer="relu",
use_bias=False,
l2_reg=self.l2_reg,
name="Squeeze",
)
self.conv2 = ConvNormAct(
input_channels,
kernel_size=1,
norm_layer=None,
act_layer="hsigmoid",
use_bias=False,
l2_reg=self.l2_reg,
name="Excite",
)
super().build(input_shape)
def call(self, input):
x = self.gap(input)
x = self.conv1(x)
x = self.conv2(x)
return input * x
class LastStage(tf.keras.layers.Layer):
def __init__(
self,
penultimate_channels: int,
last_channels: int,
num_classes: int,
l2_reg: float,
):
super().__init__(name="LastStage")
self.conv1 = ConvNormAct(
penultimate_channels,
kernel_size=1,
stride=1,
norm_layer="bn",
act_layer="hswish",
use_bias=False,
l2_reg=l2_reg,
)
self.gap = GlobalAveragePooling2D()
self.conv2 = ConvNormAct(
last_channels,
kernel_size=1,
norm_layer=None,
act_layer="hswish",
l2_reg=l2_reg,
)
self.dropout = tf.keras.layers.Dropout(
rate=0.2,
name="Dropout",
)
self.conv3 = ConvNormAct(
num_classes,
kernel_size=1,
norm_layer=None,
act_layer="softmax",
l2_reg=l2_reg,
)
self.squeeze = Squeeze()
def call(self, input):
x = self.conv1(input)
x = self.gap(x)
x = self.conv2(x)
x = self.dropout(x)
x = self.conv3(x)
x = self.squeeze(x)
return x