-
Notifications
You must be signed in to change notification settings - Fork 1
/
mobilenetv2Layers.m
258 lines (233 loc) · 14 KB
/
mobilenetv2Layers.m
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
function lgraph = mobilenetv2Layers()
% mobilenetv2Layers MobileNet-v2 layer graph
%
% lgraph = mobilenetv2Layers creates a layer graph with the network
% architecture of MobileNet-v2. The layer graph contains no weights.
lgraph = layerGraph();
%% Add Layer Branches
% Add the branches of the network to the layer graph. Each branch is a linear
% array of layers.
tempLayers = [
imageInputLayer([224 224 3],"Name","input_1","Normalization","zscore")
convolution2dLayer([3 3],32,"Name","Conv1","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","bn_Conv1","Epsilon",0.001)
clippedReluLayer(6,"Name","Conv1_relu")
groupedConvolution2dLayer([3 3],1,32,"Name","expanded_conv_depthwise","Padding","same")
batchNormalizationLayer("Name","expanded_conv_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","expanded_conv_depthwise_relu")
convolution2dLayer([1 1],16,"Name","expanded_conv_project","Padding","same")
batchNormalizationLayer("Name","expanded_conv_project_BN","Epsilon",0.001)
convolution2dLayer([1 1],96,"Name","block_1_expand","Padding","same")
batchNormalizationLayer("Name","block_1_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_1_expand_relu")
groupedConvolution2dLayer([3 3],1,96,"Name","block_1_depthwise","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","block_1_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_1_depthwise_relu")
convolution2dLayer([1 1],24,"Name","block_1_project","Padding","same")
batchNormalizationLayer("Name","block_1_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],144,"Name","block_2_expand","Padding","same")
batchNormalizationLayer("Name","block_2_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_2_expand_relu")
groupedConvolution2dLayer([3 3],1,144,"Name","block_2_depthwise","Padding","same")
batchNormalizationLayer("Name","block_2_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_2_depthwise_relu")
convolution2dLayer([1 1],24,"Name","block_2_project","Padding","same")
batchNormalizationLayer("Name","block_2_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","block_2_add")
convolution2dLayer([1 1],144,"Name","block_3_expand","Padding","same")
batchNormalizationLayer("Name","block_3_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_3_expand_relu")
groupedConvolution2dLayer([3 3],1,144,"Name","block_3_depthwise","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","block_3_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_3_depthwise_relu")
convolution2dLayer([1 1],32,"Name","block_3_project","Padding","same")
batchNormalizationLayer("Name","block_3_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],192,"Name","block_4_expand","Padding","same")
batchNormalizationLayer("Name","block_4_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_4_expand_relu")
groupedConvolution2dLayer([3 3],1,192,"Name","block_4_depthwise","Padding","same")
batchNormalizationLayer("Name","block_4_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_4_depthwise_relu")
convolution2dLayer([1 1],32,"Name","block_4_project","Padding","same")
batchNormalizationLayer("Name","block_4_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = additionLayer(2,"Name","block_4_add");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],192,"Name","block_5_expand","Padding","same")
batchNormalizationLayer("Name","block_5_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_5_expand_relu")
groupedConvolution2dLayer([3 3],1,192,"Name","block_5_depthwise","Padding","same")
batchNormalizationLayer("Name","block_5_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_5_depthwise_relu")
convolution2dLayer([1 1],32,"Name","block_5_project","Padding","same")
batchNormalizationLayer("Name","block_5_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","block_5_add")
convolution2dLayer([1 1],192,"Name","block_6_expand","Padding","same")
batchNormalizationLayer("Name","block_6_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_6_expand_relu")
groupedConvolution2dLayer([3 3],1,192,"Name","block_6_depthwise","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","block_6_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_6_depthwise_relu")
convolution2dLayer([1 1],64,"Name","block_6_project","Padding","same")
batchNormalizationLayer("Name","block_6_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],384,"Name","block_7_expand","Padding","same")
batchNormalizationLayer("Name","block_7_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_7_expand_relu")
groupedConvolution2dLayer([3 3],1,384,"Name","block_7_depthwise","Padding","same")
batchNormalizationLayer("Name","block_7_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_7_depthwise_relu")
convolution2dLayer([1 1],64,"Name","block_7_project","Padding","same")
batchNormalizationLayer("Name","block_7_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = additionLayer(2,"Name","block_7_add");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],384,"Name","block_8_expand","Padding","same")
batchNormalizationLayer("Name","block_8_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_8_expand_relu")
groupedConvolution2dLayer([3 3],1,384,"Name","block_8_depthwise","Padding","same")
batchNormalizationLayer("Name","block_8_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_8_depthwise_relu")
convolution2dLayer([1 1],64,"Name","block_8_project","Padding","same")
batchNormalizationLayer("Name","block_8_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = additionLayer(2,"Name","block_8_add");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],384,"Name","block_9_expand","Padding","same")
batchNormalizationLayer("Name","block_9_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_9_expand_relu")
groupedConvolution2dLayer([3 3],1,384,"Name","block_9_depthwise","Padding","same")
batchNormalizationLayer("Name","block_9_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_9_depthwise_relu")
convolution2dLayer([1 1],64,"Name","block_9_project","Padding","same")
batchNormalizationLayer("Name","block_9_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","block_9_add")
convolution2dLayer([1 1],384,"Name","block_10_expand","Padding","same")
batchNormalizationLayer("Name","block_10_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_10_expand_relu")
groupedConvolution2dLayer([3 3],1,384,"Name","block_10_depthwise","Padding","same")
batchNormalizationLayer("Name","block_10_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_10_depthwise_relu")
convolution2dLayer([1 1],96,"Name","block_10_project","Padding","same")
batchNormalizationLayer("Name","block_10_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],576,"Name","block_11_expand","Padding","same")
batchNormalizationLayer("Name","block_11_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_11_expand_relu")
groupedConvolution2dLayer([3 3],1,576,"Name","block_11_depthwise","Padding","same")
batchNormalizationLayer("Name","block_11_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_11_depthwise_relu")
convolution2dLayer([1 1],96,"Name","block_11_project","Padding","same")
batchNormalizationLayer("Name","block_11_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = additionLayer(2,"Name","block_11_add");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],576,"Name","block_12_expand","Padding","same")
batchNormalizationLayer("Name","block_12_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_12_expand_relu")
groupedConvolution2dLayer([3 3],1,576,"Name","block_12_depthwise","Padding","same")
batchNormalizationLayer("Name","block_12_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_12_depthwise_relu")
convolution2dLayer([1 1],96,"Name","block_12_project","Padding","same")
batchNormalizationLayer("Name","block_12_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","block_12_add")
convolution2dLayer([1 1],576,"Name","block_13_expand","Padding","same")
batchNormalizationLayer("Name","block_13_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_13_expand_relu")
groupedConvolution2dLayer([3 3],1,576,"Name","block_13_depthwise","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","block_13_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_13_depthwise_relu")
convolution2dLayer([1 1],160,"Name","block_13_project","Padding","same")
batchNormalizationLayer("Name","block_13_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],960,"Name","block_14_expand","Padding","same")
batchNormalizationLayer("Name","block_14_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_14_expand_relu")
groupedConvolution2dLayer([3 3],1,960,"Name","block_14_depthwise","Padding","same")
batchNormalizationLayer("Name","block_14_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_14_depthwise_relu")
convolution2dLayer([1 1],160,"Name","block_14_project","Padding","same")
batchNormalizationLayer("Name","block_14_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = additionLayer(2,"Name","block_14_add");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],960,"Name","block_15_expand","Padding","same")
batchNormalizationLayer("Name","block_15_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_15_expand_relu")
groupedConvolution2dLayer([3 3],1,960,"Name","block_15_depthwise","Padding","same")
batchNormalizationLayer("Name","block_15_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_15_depthwise_relu")
convolution2dLayer([1 1],160,"Name","block_15_project","Padding","same")
batchNormalizationLayer("Name","block_15_project_BN","Epsilon",0.001)];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","block_15_add")
convolution2dLayer([1 1],960,"Name","block_16_expand","Padding","same")
batchNormalizationLayer("Name","block_16_expand_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_16_expand_relu")
groupedConvolution2dLayer([3 3],1,960,"Name","block_16_depthwise","Padding","same")
batchNormalizationLayer("Name","block_16_depthwise_BN","Epsilon",0.001)
clippedReluLayer(6,"Name","block_16_depthwise_relu")
convolution2dLayer([1 1],320,"Name","block_16_project","Padding","same")
batchNormalizationLayer("Name","block_16_project_BN","Epsilon",0.001)
convolution2dLayer([1 1],1280,"Name","Conv_1")
batchNormalizationLayer("Name","Conv_1_bn","Epsilon",0.001)
clippedReluLayer(6,"Name","out_relu")
globalAveragePooling2dLayer("Name","global_average_pooling2d_1")
fullyConnectedLayer(1000,"Name","Logits")
softmaxLayer("Name","Logits_softmax")
classificationLayer("Name","ClassificationLayer_Logits")];
lgraph = addLayers(lgraph,tempLayers);
%% Connect Layer Branches
% Connect all the branches of the network to create the network graph.
lgraph = connectLayers(lgraph,"block_1_project_BN","block_2_expand");
lgraph = connectLayers(lgraph,"block_1_project_BN","block_2_add/in2");
lgraph = connectLayers(lgraph,"block_2_project_BN","block_2_add/in1");
lgraph = connectLayers(lgraph,"block_3_project_BN","block_4_expand");
lgraph = connectLayers(lgraph,"block_3_project_BN","block_4_add/in2");
lgraph = connectLayers(lgraph,"block_4_project_BN","block_4_add/in1");
lgraph = connectLayers(lgraph,"block_4_add","block_5_expand");
lgraph = connectLayers(lgraph,"block_4_add","block_5_add/in2");
lgraph = connectLayers(lgraph,"block_5_project_BN","block_5_add/in1");
lgraph = connectLayers(lgraph,"block_6_project_BN","block_7_expand");
lgraph = connectLayers(lgraph,"block_6_project_BN","block_7_add/in2");
lgraph = connectLayers(lgraph,"block_7_project_BN","block_7_add/in1");
lgraph = connectLayers(lgraph,"block_7_add","block_8_expand");
lgraph = connectLayers(lgraph,"block_7_add","block_8_add/in2");
lgraph = connectLayers(lgraph,"block_8_project_BN","block_8_add/in1");
lgraph = connectLayers(lgraph,"block_8_add","block_9_expand");
lgraph = connectLayers(lgraph,"block_8_add","block_9_add/in2");
lgraph = connectLayers(lgraph,"block_9_project_BN","block_9_add/in1");
lgraph = connectLayers(lgraph,"block_10_project_BN","block_11_expand");
lgraph = connectLayers(lgraph,"block_10_project_BN","block_11_add/in2");
lgraph = connectLayers(lgraph,"block_11_project_BN","block_11_add/in1");
lgraph = connectLayers(lgraph,"block_11_add","block_12_expand");
lgraph = connectLayers(lgraph,"block_11_add","block_12_add/in2");
lgraph = connectLayers(lgraph,"block_12_project_BN","block_12_add/in1");
lgraph = connectLayers(lgraph,"block_13_project_BN","block_14_expand");
lgraph = connectLayers(lgraph,"block_13_project_BN","block_14_add/in2");
lgraph = connectLayers(lgraph,"block_14_project_BN","block_14_add/in1");
lgraph = connectLayers(lgraph,"block_14_add","block_15_expand");
lgraph = connectLayers(lgraph,"block_14_add","block_15_add/in2");
lgraph = connectLayers(lgraph,"block_15_project_BN","block_15_add/in1");
end