-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileNetV2.py
146 lines (114 loc) · 5.07 KB
/
MobileNetV2.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
import tensorflow
class MobileNetV2Block(tensorflow.keras.layers.Layer):
def __init__(self, stride, t, input_channle, output_channle):
super(MobileNetV2Block, self).__init__()
self.stride = stride
self.t = t
self.input_channle = input_channle
self.output_channle = output_channle
self.Conv2D_1 = tensorflow.keras.layers.Conv2D(
filters=self.t * self.input_channle,
kernel_size=1,
strides=1,
padding='same')
self.BatchNormalization_1 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_1 = tensorflow.keras.layers.ReLU(max_value=6)
self.DepthwiseConv2D_2 = tensorflow.keras.layers.DepthwiseConv2D(
kernel_size=3,
strides=self.stride,
padding='same',
activation=tensorflow.keras.layers.ReLU(max_value=6))
self.BatchNormalization_2 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_2 = tensorflow.keras.layers.ReLU(max_value=6)
self.Conv2D_3 = tensorflow.keras.layers.Conv2D(
filters=self.output_channle,
kernel_size=1,
strides=1,
padding='same')
self.BatchNormalization_3 = tensorflow.keras.layers.BatchNormalization()
def call(self, x):
y = self.Conv2D_1(x)
y = self.BatchNormalization_1(y)
y = self.ReLU_1(y)
y = self.DepthwiseConv2D_2(y)
y = self.BatchNormalization_2(y)
y = self.ReLU_2(y)
y = self.Conv2D_3(y)
y = self.BatchNormalization_3(y)
if self.input_channle == self.output_channle:
y = x + y
return y
class MobileNetV2(tensorflow.keras.Model):
def __init__(self, num_classes=1000):
super(MobileNetV2, self).__init__(name='MobileNetV2')
self.Conv2D_1 = tensorflow.keras.layers.Conv2D(
filters=32,
kernel_size=3,
strides=2,
padding='same')
self.BatchNormalization_1 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_1 = tensorflow.keras.layers.ReLU(max_value=6)
self.MobileNetV2Block_2 = MobileNetV2Block(1, 1, 32, 16)
self.MobileNetV2Block_3 = MobileNetV2Block(2, 6, 16, 24)
self.MobileNetV2Block_4 = MobileNetV2Block(1, 6, 24, 24)
self.MobileNetV2Block_5 = MobileNetV2Block(2, 6, 24, 32)
self.MobileNetV2Block_6 = MobileNetV2Block(1, 6, 32, 32)
self.MobileNetV2Block_7 = MobileNetV2Block(1, 6, 32, 32)
self.MobileNetV2Block_8 = MobileNetV2Block(2, 6, 32, 64)
self.MobileNetV2Block_9 = MobileNetV2Block(1, 6, 64, 64)
self.MobileNetV2Block_10 = MobileNetV2Block(1, 6, 64, 64)
self.MobileNetV2Block_11 = MobileNetV2Block(1, 6, 64, 64)
self.MobileNetV2Block_12 = MobileNetV2Block(1, 6, 64, 96)
self.MobileNetV2Block_13 = MobileNetV2Block(1, 6, 96, 96)
self.MobileNetV2Block_14 = MobileNetV2Block(1, 6, 96, 96)
self.MobileNetV2Block_15 = MobileNetV2Block(2, 6, 96, 160)
self.MobileNetV2Block_16 = MobileNetV2Block(1, 6, 160, 160)
self.MobileNetV2Block_17 = MobileNetV2Block(1, 6, 160, 160)
self.MobileNetV2Block_18 = MobileNetV2Block(1, 6, 160, 320)
self.Conv2D_19 = tensorflow.keras.layers.Conv2D(
filters=1280,
kernel_size=1,
strides=1,
padding='same')
self.BatchNormalization_19 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_19 = tensorflow.keras.layers.ReLU(max_value=6)
self.AveragePooling2D_20 = tensorflow.keras.layers.AveragePooling2D(
pool_size=7,
strides=1)
self.Conv2D_21 = tensorflow.keras.layers.Conv2D(
filters=num_classes,
kernel_size=1,
strides=1,
padding='same')
def call(self, x):
x = self.Conv2D_1(x)
x = self.BatchNormalization_1(x)
x = self.ReLU_1(x)
x = self.MobileNetV2Block_2(x)
x = self.MobileNetV2Block_3(x)
x = self.MobileNetV2Block_4(x)
x = self.MobileNetV2Block_5(x)
x = self.MobileNetV2Block_6(x)
x = self.MobileNetV2Block_7(x)
x = self.MobileNetV2Block_8(x)
x = self.MobileNetV2Block_9(x)
x = self.MobileNetV2Block_10(x)
x = self.MobileNetV2Block_11(x)
x = self.MobileNetV2Block_12(x)
x = self.MobileNetV2Block_13(x)
x = self.MobileNetV2Block_14(x)
x = self.MobileNetV2Block_15(x)
x = self.MobileNetV2Block_16(x)
x = self.MobileNetV2Block_17(x)
x = self.MobileNetV2Block_18(x)
x = self.Conv2D_19(x)
x = self.BatchNormalization_19(x)
x = self.ReLU_19(x)
x = self.AveragePooling2D_20(x)
x = self.Conv2D_21(x)
return x
if __name__ == '__main__':
tensorflow.enable_eager_execution(config=tensorflow.ConfigProto(allow_soft_placement=True, gpu_options=tensorflow.GPUOptions(allow_growth=True), inter_op_parallelism_threads=0, intra_op_parallelism_threads=0))
data = tensorflow.random.uniform([128, 224, 224, 3])
model = MobileNetV2()
y = model(data)