-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileNetV1.py
115 lines (83 loc) · 3.43 KB
/
MobileNetV1.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
import tensorflow
class MobileNetV1Block(tensorflow.keras.layers.Layer):
def __init__(self, stride, output_channle):
super(MobileNetV1Block, self).__init__()
self.stride = stride
self.output_channle = output_channle
self.DepthwiseConv2D_1 = tensorflow.keras.layers.DepthwiseConv2D(
kernel_size=3,
strides=self.stride,
padding='same')
self.BatchNormalization_1 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_1 = tensorflow.keras.layers.ReLU()
self.Conv2D_2 = tensorflow.keras.layers.Conv2D(
filters=self.output_channle,
kernel_size=1,
strides=1,
padding='same')
self.BatchNormalization_2 = tensorflow.keras.layers.BatchNormalization()
self.ReLU_2 = tensorflow.keras.layers.ReLU()
def call(self, x):
y = self.DepthwiseConv2D_1(x)
y = self.BatchNormalization_1(y)
y = self.ReLU_1(y)
y = self.Conv2D_2(y)
y = self.BatchNormalization_2(y)
y = self.ReLU_2(y)
return y
class MobileNetV1(tensorflow.keras.Model):
def __init__(self, num_classes=1000):
super(MobileNetV1, self).__init__(name='MobileNetV1')
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()
self.MobileNetV1Block_2 = MobileNetV1Block(1, 64)
self.MobileNetV1Block_3 = MobileNetV1Block(2, 128)
self.MobileNetV1Block_4 = MobileNetV1Block(1, 128)
self.MobileNetV1Block_5 = MobileNetV1Block(2, 256)
self.MobileNetV1Block_6 = MobileNetV1Block(1, 256)
self.MobileNetV1Block_7 = MobileNetV1Block(2, 512)
self.MobileNetV1Block_8 = MobileNetV1Block(1, 512)
self.MobileNetV1Block_9 = MobileNetV1Block(1, 512)
self.MobileNetV1Block_10 = MobileNetV1Block(1, 512)
self.MobileNetV1Block_11 = MobileNetV1Block(1, 512)
self.MobileNetV1Block_12 = MobileNetV1Block(1, 512)
self.MobileNetV1Block_13 = MobileNetV1Block(2, 1024)
self.MobileNetV1Block_14 = MobileNetV1Block(1, 1024)
self.AveragePooling2D_15 = tensorflow.keras.layers.AveragePooling2D(
pool_size=7,
strides=1)
self.Conv2D_16 = 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.MobileNetV1Block_2(x)
x = self.MobileNetV1Block_3(x)
x = self.MobileNetV1Block_4(x)
x = self.MobileNetV1Block_5(x)
x = self.MobileNetV1Block_6(x)
x = self.MobileNetV1Block_7(x)
x = self.MobileNetV1Block_8(x)
x = self.MobileNetV1Block_9(x)
x = self.MobileNetV1Block_10(x)
x = self.MobileNetV1Block_11(x)
x = self.MobileNetV1Block_12(x)
x = self.MobileNetV1Block_13(x)
x = self.MobileNetV1Block_14(x)
x = self.AveragePooling2D_15(x)
x = self.Conv2D_16(x)
return x
if __name__ == '__main__':
tensorflow.enable_eager_execution()
data = tensorflow.random.uniform([128, 224, 224, 3])
model = MobileNetV1()
y = model(data)