-
Notifications
You must be signed in to change notification settings - Fork 21
/
mobilenet_v3_small.py
76 lines (65 loc) · 3.65 KB
/
mobilenet_v3_small.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
import tensorflow as tf
from mobilenet_v3_block import BottleNeck, h_swish
NUM_CLASSES = 10
class MobileNetV3Small(tf.keras.Model):
def __init__(self):
super(MobileNetV3Small, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(filters=16,
kernel_size=(3, 3),
strides=2,
padding="same")
self.bn1 = tf.keras.layers.BatchNormalization()
self.bneck1 = BottleNeck(in_size=16, exp_size=16, out_size=16, s=2, is_se_existing=True, NL="RE", k=3)
self.bneck2 = BottleNeck(in_size=16, exp_size=72, out_size=24, s=2, is_se_existing=False, NL="RE", k=3)
self.bneck3 = BottleNeck(in_size=24, exp_size=88, out_size=24, s=1, is_se_existing=False, NL="RE", k=3)
self.bneck4 = BottleNeck(in_size=24, exp_size=96, out_size=40, s=2, is_se_existing=True, NL="HS", k=5)
self.bneck5 = BottleNeck(in_size=40, exp_size=240, out_size=40, s=1, is_se_existing=True, NL="HS", k=5)
self.bneck6 = BottleNeck(in_size=40, exp_size=240, out_size=40, s=1, is_se_existing=True, NL="HS", k=5)
self.bneck7 = BottleNeck(in_size=40, exp_size=120, out_size=48, s=1, is_se_existing=True, NL="HS", k=5)
self.bneck8 = BottleNeck(in_size=48, exp_size=144, out_size=48, s=1, is_se_existing=True, NL="HS", k=5)
self.bneck9 = BottleNeck(in_size=48, exp_size=288, out_size=96, s=2, is_se_existing=True, NL="HS", k=5)
self.bneck10 = BottleNeck(in_size=96, exp_size=576, out_size=96, s=1, is_se_existing=True, NL="HS", k=5)
self.bneck11 = BottleNeck(in_size=96, exp_size=576, out_size=96, s=1, is_se_existing=True, NL="HS", k=5)
self.conv2 = tf.keras.layers.Conv2D(filters=576,
kernel_size=(1, 1),
strides=1,
padding="same")
self.bn2 = tf.keras.layers.BatchNormalization()
self.avgpool = tf.keras.layers.AveragePooling2D(pool_size=(7, 7),
strides=1)
self.conv3 = tf.keras.layers.Conv2D(filters=1280,
kernel_size=(1, 1),
strides=1,
padding="same")
self.conv4 = tf.keras.layers.Conv2D(filters=NUM_CLASSES,
kernel_size=(1, 1),
strides=1,
padding="same",
activation=tf.keras.activations.softmax)
def call(self, inputs, training=None, mask=None):
x = self.conv1(inputs)
x = self.bn1(x, training=training)
x = h_swish(x)
x = self.bneck1(x, training=training)
x = self.bneck2(x, training=training)
x = self.bneck3(x, training=training)
x = self.bneck4(x, training=training)
x = self.bneck5(x, training=training)
x = self.bneck6(x, training=training)
x = self.bneck7(x, training=training)
x = self.bneck8(x, training=training)
x = self.bneck9(x, training=training)
x = self.bneck10(x, training=training)
x = self.bneck11(x, training=training)
x = self.conv2(x)
x = self.bn2(x, training=training)
x = h_swish(x)
x = self.avgpool(x)
x = self.conv3(x)
x = h_swish(x)
x = self.conv4(x)
return x
if __name__ == '__main__':
model = MobileNetV3Small()
model.build(input_shape=(None, 224, 224, 3))
model.summary()