blob: 25690aa6fcf3036bd78914c542b0c88558c110e7 [file] [log] [blame]
Georgios Pinitas766b70c2018-08-13 17:50:34 +01001/*
Pablo Telloadc21862019-03-22 16:47:59 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitas766b70c2018-08-13 17:50:34 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/graph.h"
25#include "support/ToolchainSupport.h"
26#include "utils/CommonGraphOptions.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30using namespace arm_compute;
31using namespace arm_compute::utils;
32using namespace arm_compute::graph::frontend;
33using namespace arm_compute::graph_utils;
34
35/** Example demonstrating how to implement MobileNetV2's network using the Compute Library's graph API */
36class GraphMobilenetV2Example : public Example
37{
38public:
39 GraphMobilenetV2Example()
40 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV2")
41 {
42 }
43 GraphMobilenetV2Example(const GraphMobilenetV2Example &) = delete;
44 GraphMobilenetV2Example &operator=(const GraphMobilenetV2Example &) = delete;
45 GraphMobilenetV2Example(GraphMobilenetV2Example &&) = default; // NOLINT
46 GraphMobilenetV2Example &operator=(GraphMobilenetV2Example &&) = default; // NOLINT
47 ~GraphMobilenetV2Example() override = default;
48
49 bool do_setup(int argc, char **argv) override
50 {
51 // Parse arguments
52 cmd_parser.parse(argc, argv);
53
54 // Consume common parameters
55 common_params = consume_common_graph_parameters(common_opts);
56
57 // Return when help menu is requested
58 if(common_params.help)
59 {
60 cmd_parser.print_help(argv[0]);
61 return false;
62 }
63
64 // Print parameter values
65 std::cout << common_params << std::endl;
66
Georgios Pinitas766b70c2018-08-13 17:50:34 +010067 // Create input descriptor
68 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
69 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
70
Georgios Pinitasf60d6712018-11-29 13:21:54 +000071 // Set graph hints
Georgios Pinitas766b70c2018-08-13 17:50:34 +010072 graph << common_params.target
Georgios Pinitasf52cd782019-03-25 14:06:14 +000073 << DepthwiseConvolutionMethod::Optimized3x3 // TODO(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method
Georgios Pinitasf60d6712018-11-29 13:21:54 +000074 << common_params.fast_math_hint;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010075
Georgios Pinitasf60d6712018-11-29 13:21:54 +000076 // Create core graph
77 if(arm_compute::is_data_type_float(common_params.data_type))
78 {
79 create_graph_float(input_descriptor);
80 }
81 else
82 {
83 create_graph_qasymm8(input_descriptor);
84 }
85 // Create common tail
86 graph << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape")
Georgios Pinitas766b70c2018-08-13 17:50:34 +010087 << SoftmaxLayer().set_name("Predictions/Softmax")
88 << OutputLayer(get_output_accessor(common_params, 5));
89
90 // Finalize graph
91 GraphConfig config;
92 config.num_threads = common_params.threads;
93 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010094 config.tuner_mode = common_params.tuner_mode;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010095 config.tuner_file = common_params.tuner_file;
96
97 graph.finalize(common_params.target, config);
98
99 return true;
100 }
101
102 void do_run() override
103 {
104 // Run graph
105 graph.run();
106 }
107
108private:
109 CommandLineParser cmd_parser;
110 CommonGraphOptions common_opts;
111 CommonGraphParams common_params;
112 Stream graph;
113
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000114private:
115 enum class IsResidual
116 {
117 Yes,
118 No
119 };
120
121 enum class HasExpand
122 {
123 Yes,
124 No
125 };
126
127private:
128 void create_graph_float(TensorDescriptor &input_descriptor)
129 {
130 // Create model path
131 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/";
132
133 // Create a preprocessor object
134 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
135
136 // Get trainable parameters data path
137 std::string data_path = common_params.data_path;
138
139 // Add model path to data path
140 if(!data_path.empty())
141 {
142 data_path += model_path;
143 }
144
145 graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
146 << ConvolutionLayer(3U, 3U, 32U,
147 get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW),
148 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
149 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL))
150 .set_name("Conv")
151 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_BatchNorm_moving_mean.npy"),
152 get_weights_accessor(data_path, "Conv_BatchNorm_moving_variance.npy"),
153 get_weights_accessor(data_path, "Conv_BatchNorm_gamma.npy"),
154 get_weights_accessor(data_path, "Conv_BatchNorm_beta.npy"),
155 0.0010000000474974513f)
156 .set_name("Conv/BatchNorm")
157 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
158 .set_name("Conv/Relu6");
159
160 get_expanded_conv_float(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1));
161 get_expanded_conv_float(data_path, "expanded_conv_1", 16U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
162 get_expanded_conv_float(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
163 get_expanded_conv_float(data_path, "expanded_conv_3", 24U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
164 get_expanded_conv_float(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
165 get_expanded_conv_float(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
166 get_expanded_conv_float(data_path, "expanded_conv_6", 32U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
167 get_expanded_conv_float(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
168 get_expanded_conv_float(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
169 get_expanded_conv_float(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
170 get_expanded_conv_float(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
171 get_expanded_conv_float(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
172 get_expanded_conv_float(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
173 get_expanded_conv_float(data_path, "expanded_conv_13", 96U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
174 get_expanded_conv_float(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
175 get_expanded_conv_float(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
176 get_expanded_conv_float(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
177
178 graph << ConvolutionLayer(1U, 1U, 1280U,
179 get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW),
180 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
181 PadStrideInfo(1, 1, 0, 0))
182 .set_name("Conv_1")
183 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"),
184 get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"),
185 get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"),
186 get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"),
187 0.0010000000474974513f)
188 .set_name("Conv_1/BatchNorm")
189 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
190 .set_name("Conv_1/Relu6")
191 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool")
192 << ConvolutionLayer(1U, 1U, 1001U,
193 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
194 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
195 PadStrideInfo(1, 1, 0, 0))
196 .set_name("Logits/Conv2d_1c_1x1");
197 }
198
199 void get_expanded_conv_float(const std::string &data_path, std::string &&param_path,
200 unsigned int input_channels, unsigned int output_channels,
201 PadStrideInfo dwc_pad_stride_info,
202 HasExpand has_expand = HasExpand::No, IsResidual is_residual = IsResidual::No,
203 unsigned int expansion_size = 6)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100204 {
205 std::string total_path = param_path + "_";
206 SubStream left(graph);
207
208 // Add expand node
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000209 if(has_expand == HasExpand::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100210 {
211 left << ConvolutionLayer(1U, 1U, input_channels * expansion_size,
212 get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW),
213 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
214 .set_name(param_path + "/expand/Conv2D")
215 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"),
216 get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"),
217 get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"),
218 get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"),
219 0.0010000000474974513f)
220 .set_name(param_path + "/expand/BatchNorm")
221 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
222 .set_name(param_path + "/expand/Relu6");
223 }
224
225 // Add depthwise node
226 left << DepthwiseConvolutionLayer(3U, 3U,
227 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
228 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
229 dwc_pad_stride_info)
230 .set_name(param_path + "/depthwise/depthwise")
231 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
232 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
233 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
234 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
235 0.0010000000474974513f)
236 .set_name(param_path + "/depthwise/BatchNorm")
237 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
238 .set_name(param_path + "/depthwise/Relu6");
239
240 // Add project node
241 left << ConvolutionLayer(1U, 1U, output_channels,
242 get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW),
243 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
244 .set_name(param_path + "/project/Conv2D")
245 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"),
246 get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"),
247 get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"),
248 get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"),
249 0.0010000000474974513)
250 .set_name(param_path + "/project/BatchNorm");
251
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000252 if(is_residual == IsResidual::Yes)
253 {
254 // Add residual node
255 SubStream right(graph);
256 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
257 }
258 else
259 {
260 graph.forward_tail(left.tail_node());
261 }
262 }
263
264 void create_graph_qasymm8(TensorDescriptor &input_descriptor)
265 {
266 // Create model path
Pablo Telloadc21862019-03-22 16:47:59 +0000267 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_quantized_model/";
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000268
269 // Get trainable parameters data path
270 std::string data_path = common_params.data_path;
271
272 // Add model path to data path
273 if(!data_path.empty())
274 {
275 data_path += model_path;
276 }
277
278 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
279 const QuantizationInfo mid_quant_info = QuantizationInfo(0.023528477177023888f, 128);
280
281 const std::vector<QuantizationInfo> conv_weights_quant_info =
282 {
283 QuantizationInfo(0.03396892547607422f, 122), // Conv
284 QuantizationInfo(0.005167067516595125f, 125), // Conv1
285 QuantizationInfo(0.0016910821432247758f, 113) // Conv2d_1c_1x1
286 };
287
288 // Pointwise expand convolution quantization info
289 const std::vector<QuantizationInfo> pwc_q =
290 {
291 QuantizationInfo(0.254282623529f, 129), // expand_0 (Dummy)
292 QuantizationInfo(0.009758507832884789f, 127), // expand_1
293 QuantizationInfo(0.0036556976847350597f, 144), // expand_2
294 QuantizationInfo(0.0029988749884068966f, 104), // expand_3
295 QuantizationInfo(0.0019244228024035692f, 128), // expand_4
296 QuantizationInfo(0.0013649158645421267f, 135), // expand_5
297 QuantizationInfo(0.0019170437008142471f, 127), // expand_6
298 QuantizationInfo(0.0015538912266492844f, 125), // expand_7
299 QuantizationInfo(0.0014702979242429137f, 134), // expand_8
300 QuantizationInfo(0.0013733493397012353f, 127), // expand_9
301 QuantizationInfo(0.0016282502328976989f, 131), // expand_10
302 QuantizationInfo(0.0016309921629726887f, 134), // expand_11
303 QuantizationInfo(0.0018258779309689999f, 138), // expand_12
304 QuantizationInfo(0.0013828007504343987f, 123), // expand_13
305 QuantizationInfo(0.0020222084131091833f, 135), // expand_14
306 QuantizationInfo(0.04281935095787048f, 102), // expand_15
307 QuantizationInfo(0.002046825597062707f, 135) // expand_16
308 };
309 // Depthwise expand convolution quantization info
310 const std::vector<QuantizationInfo> dwc_q =
311 {
312 QuantizationInfo(0.3436955213546753f, 165), // expand_0
313 QuantizationInfo(0.020969120785593987f, 109), // expand_1
314 QuantizationInfo(0.16981913149356842f, 52), // expand_2
315 QuantizationInfo(0.017202870920300484f, 143), // expand_3
316 QuantizationInfo(0.06525065749883652f, 118), // expand_4
317 QuantizationInfo(0.07909784466028214f, 95), // expand_5
318 QuantizationInfo(0.010087885893881321f, 127), // expand_6
319 QuantizationInfo(0.06092711538076401f, 110), // expand_7
320 QuantizationInfo(0.052407849580049515f, 133), // expand_8
321 QuantizationInfo(0.04077887907624245f, 155), // expand_9
322 QuantizationInfo(0.031107846647500992f, 143), // expand_10
323 QuantizationInfo(0.07080810517072678f, 66), // expand_11
324 QuantizationInfo(0.07448793947696686f, 159), // expand_12
325 QuantizationInfo(0.01525793131440878f, 92), // expand_13
326 QuantizationInfo(0.04166752099990845f, 147), // expand_14
327 QuantizationInfo(0.04281935095787048f, 102), // expand_15
328 QuantizationInfo(0.16456253826618195, 201) // expand_16
329 };
330 // Project convolution quantization info
331 const std::vector<QuantizationInfo> prwc_q =
332 {
333 QuantizationInfo(0.03737175464630127f, 140), // expand_0
334 QuantizationInfo(0.0225360207259655f, 156), // expand_1
335 QuantizationInfo(0.02740888111293316f, 122), // expand_2
336 QuantizationInfo(0.016844693571329117f, 111), // expand_3
337 QuantizationInfo(0.019062912091612816f, 146), // expand_4
338 QuantizationInfo(0.018293123692274094f, 128), // expand_5
339 QuantizationInfo(0.014601286500692368f, 147), // expand_6
340 QuantizationInfo(0.016782939434051514f, 124), // expand_7
341 QuantizationInfo(0.012898261658847332f, 125), // expand_8
342 QuantizationInfo(0.019561484456062317f, 144), // expand_9
343 QuantizationInfo(0.007436311338096857f, 129), // expand_10
344 QuantizationInfo(0.00838223285973072f, 136), // expand_11
345 QuantizationInfo(0.023982593789696693f, 154), // expand_12
346 QuantizationInfo(0.009447949007153511f, 140), // expand_13
347 QuantizationInfo(0.00789870135486126f, 139), // expand_14
348 QuantizationInfo(0.03697410225868225f, 131), // expand_15
349 QuantizationInfo(0.008009289391338825f, 111) // expand_16
350 };
351
352 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
353 get_weights_accessor(data_path, common_params.image))
354 << ConvolutionLayer(
355 3U, 3U, 32U,
356 get_weights_accessor(data_path, "Conv_weights.npy"),
357 get_weights_accessor(data_path, "Conv_bias.npy"),
358 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
359 1, conv_weights_quant_info.at(0), mid_quant_info)
360 .set_name("Conv")
361 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv/Relu6")
362 << DepthwiseConvolutionLayer(3U, 3U,
363 get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_weights.npy"),
364 get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_biases.npy"),
Georgios Pinitas05045c12018-12-07 18:31:47 +0000365 PadStrideInfo(1, 1, 1, 1), 1, dwc_q.at(0))
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000366 .set_name("expanded_conv/depthwise/depthwise")
367 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("expanded_conv/depthwise/Relu6")
368 << ConvolutionLayer(1U, 1U, 16U,
369 get_weights_accessor(data_path, "expanded_conv_project_weights.npy"),
370 get_weights_accessor(data_path, "expanded_conv_project_biases.npy"),
371 PadStrideInfo(1, 1, 0, 0), 1, prwc_q.at(0))
372 .set_name("expanded_conv/project/Conv2D");
373
374 get_expanded_conv_qasymm8(data_path, "expanded_conv_1", IsResidual::No, 96U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
375 pwc_q.at(1), dwc_q.at(1), prwc_q.at(1));
376 get_expanded_conv_qasymm8(data_path, "expanded_conv_2", IsResidual::Yes, 144U, 24U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(2), dwc_q.at(2), prwc_q.at(2));
377 get_expanded_conv_qasymm8(data_path, "expanded_conv_3", IsResidual::No, 144U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
378 pwc_q.at(3), dwc_q.at(3), prwc_q.at(3));
379 get_expanded_conv_qasymm8(data_path, "expanded_conv_4", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(4), dwc_q.at(4), prwc_q.at(4));
380 get_expanded_conv_qasymm8(data_path, "expanded_conv_5", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(5), dwc_q.at(5), prwc_q.at(5));
381 get_expanded_conv_qasymm8(data_path, "expanded_conv_6", IsResidual::No, 192U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
382 pwc_q.at(6), dwc_q.at(6), prwc_q.at(6));
383 get_expanded_conv_qasymm8(data_path, "expanded_conv_7", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(7), dwc_q.at(7), prwc_q.at(7));
384 get_expanded_conv_qasymm8(data_path, "expanded_conv_8", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(8), dwc_q.at(8), prwc_q.at(8));
385 get_expanded_conv_qasymm8(data_path, "expanded_conv_9", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(9), dwc_q.at(9), prwc_q.at(9));
386 get_expanded_conv_qasymm8(data_path, "expanded_conv_10", IsResidual::No, 384U, 96U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(10), dwc_q.at(10), prwc_q.at(10));
387 get_expanded_conv_qasymm8(data_path, "expanded_conv_11", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(11), dwc_q.at(11), prwc_q.at(11));
388 get_expanded_conv_qasymm8(data_path, "expanded_conv_12", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(12), dwc_q.at(12), prwc_q.at(12));
389 get_expanded_conv_qasymm8(data_path, "expanded_conv_13", IsResidual::No, 576U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
390 pwc_q.at(13), dwc_q.at(13), prwc_q.at(13));
391 get_expanded_conv_qasymm8(data_path, "expanded_conv_14", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(14), dwc_q.at(14), prwc_q.at(14));
392 get_expanded_conv_qasymm8(data_path, "expanded_conv_15", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(15), dwc_q.at(15), prwc_q.at(15));
393 get_expanded_conv_qasymm8(data_path, "expanded_conv_16", IsResidual::No, 960U, 320U, PadStrideInfo(1, 1, 1, 1), pwc_q.at(16), dwc_q.at(16), prwc_q.at(16));
394
395 graph << ConvolutionLayer(1U, 1U, 1280U,
396 get_weights_accessor(data_path, "Conv_1_weights.npy"),
397 get_weights_accessor(data_path, "Conv_1_biases.npy"),
398 PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(1))
399 .set_name("Conv_1")
400 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv_1/Relu6")
401 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool")
402 << ConvolutionLayer(1U, 1U, 1001U,
403 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
404 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
405 PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(2))
406 .set_name("Logits/Conv2d_1c_1x1");
407 }
408
409 void get_expanded_conv_qasymm8(const std::string &data_path, std::string &&param_path, IsResidual is_residual,
410 unsigned int input_channels, unsigned int output_channels,
411 PadStrideInfo dwc_pad_stride_info,
412 const QuantizationInfo &pwi, const QuantizationInfo &dwi, const QuantizationInfo &pji)
413 {
414 std::string total_path = param_path + "_";
415
416 SubStream left(graph);
417 left << ConvolutionLayer(1U, 1U, input_channels,
418 get_weights_accessor(data_path, total_path + "project_weights.npy"),
419 get_weights_accessor(data_path, total_path + "project_biases.npy"),
420 PadStrideInfo(1, 1, 0, 0), 1, pwi)
421 .set_name(param_path + "/Conv2D")
422 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(param_path + "/Conv2D/Relu6")
423 << DepthwiseConvolutionLayer(3U, 3U,
424 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
425 get_weights_accessor(data_path, total_path + "depthwise_depthwise_biases.npy"),
Georgios Pinitas05045c12018-12-07 18:31:47 +0000426 dwc_pad_stride_info, 1, dwi)
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000427 .set_name(param_path + "/depthwise/depthwise")
428 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(param_path + "/depthwise/Relu6")
429 << ConvolutionLayer(1U, 1U, output_channels,
430 get_weights_accessor(data_path, total_path + "project_weights.npy"),
431 get_weights_accessor(data_path, total_path + "project_biases.npy"),
432 PadStrideInfo(1, 1, 0, 0), 1, pji)
433 .set_name(param_path + "/project/Conv2D");
434
435 if(is_residual == IsResidual::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100436 {
437 // Add residual node
438 SubStream right(graph);
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100439 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100440 }
441 else
442 {
443 graph.forward_tail(left.tail_node());
444 }
445 }
446};
447
448/** Main program for MobileNetV2
449 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000450 * Model is based on:
451 * https://arxiv.org/abs/1801.04381
452 * "MobileNetV2: Inverted Residuals and Linear Bottlenecks"
453 * Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen
454 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000455 * Provenance: https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz
456 *
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100457 * @note To list all the possible arguments execute the binary appended with the --help option
458 *
459 * @param[in] argc Number of arguments
460 * @param[in] argv Arguments
461 */
462int main(int argc, char **argv)
463{
464 return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv);
465}