blob: 5ee1f7e52aac749e5362fc05be76729316b770cb [file] [log] [blame]
Georgios Pinitas766b70c2018-08-13 17:50:34 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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;
Matthew Benthamf5f23912020-03-05 22:32:16 +000045 ~GraphMobilenetV2Example() override = default;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010046
47 bool do_setup(int argc, char **argv) override
48 {
49 // Parse arguments
50 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010051 cmd_parser.validate();
Georgios Pinitas766b70c2018-08-13 17:50:34 +010052
53 // Consume common parameters
54 common_params = consume_common_graph_parameters(common_opts);
55
56 // Return when help menu is requested
57 if(common_params.help)
58 {
59 cmd_parser.print_help(argv[0]);
60 return false;
61 }
62
63 // Print parameter values
64 std::cout << common_params << std::endl;
65
Georgios Pinitas766b70c2018-08-13 17:50:34 +010066 // Create input descriptor
67 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
68 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
69
Georgios Pinitasf60d6712018-11-29 13:21:54 +000070 // Set graph hints
Georgios Pinitas766b70c2018-08-13 17:50:34 +010071 graph << common_params.target
Georgios Pinitasf60d6712018-11-29 13:21:54 +000072 << common_params.fast_math_hint;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010073
Georgios Pinitasf60d6712018-11-29 13:21:54 +000074 // Create core graph
75 if(arm_compute::is_data_type_float(common_params.data_type))
76 {
77 create_graph_float(input_descriptor);
78 }
79 else
80 {
81 create_graph_qasymm8(input_descriptor);
82 }
83 // Create common tail
84 graph << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape")
Georgios Pinitas766b70c2018-08-13 17:50:34 +010085 << SoftmaxLayer().set_name("Predictions/Softmax")
86 << OutputLayer(get_output_accessor(common_params, 5));
87
88 // Finalize graph
89 GraphConfig config;
90 config.num_threads = common_params.threads;
91 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010092 config.tuner_mode = common_params.tuner_mode;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010093 config.tuner_file = common_params.tuner_file;
94
95 graph.finalize(common_params.target, config);
96
97 return true;
98 }
99
100 void do_run() override
101 {
102 // Run graph
103 graph.run();
104 }
105
106private:
107 CommandLineParser cmd_parser;
108 CommonGraphOptions common_opts;
109 CommonGraphParams common_params;
110 Stream graph;
111
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000112private:
113 enum class IsResidual
114 {
115 Yes,
116 No
117 };
118
119 enum class HasExpand
120 {
121 Yes,
122 No
123 };
124
125private:
126 void create_graph_float(TensorDescriptor &input_descriptor)
127 {
128 // Create model path
129 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/";
130
131 // Create a preprocessor object
132 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
133
134 // Get trainable parameters data path
135 std::string data_path = common_params.data_path;
136
137 // Add model path to data path
138 if(!data_path.empty())
139 {
140 data_path += model_path;
141 }
142
143 graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
144 << ConvolutionLayer(3U, 3U, 32U,
145 get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW),
146 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
147 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL))
148 .set_name("Conv")
149 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_BatchNorm_moving_mean.npy"),
150 get_weights_accessor(data_path, "Conv_BatchNorm_moving_variance.npy"),
151 get_weights_accessor(data_path, "Conv_BatchNorm_gamma.npy"),
152 get_weights_accessor(data_path, "Conv_BatchNorm_beta.npy"),
153 0.0010000000474974513f)
154 .set_name("Conv/BatchNorm")
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
156 .set_name("Conv/Relu6");
157
158 get_expanded_conv_float(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1));
159 get_expanded_conv_float(data_path, "expanded_conv_1", 16U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
160 get_expanded_conv_float(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
161 get_expanded_conv_float(data_path, "expanded_conv_3", 24U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
162 get_expanded_conv_float(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
163 get_expanded_conv_float(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
164 get_expanded_conv_float(data_path, "expanded_conv_6", 32U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
165 get_expanded_conv_float(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
166 get_expanded_conv_float(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
167 get_expanded_conv_float(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
168 get_expanded_conv_float(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
169 get_expanded_conv_float(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
170 get_expanded_conv_float(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
171 get_expanded_conv_float(data_path, "expanded_conv_13", 96U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
172 get_expanded_conv_float(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
173 get_expanded_conv_float(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, IsResidual::Yes);
174 get_expanded_conv_float(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
175
176 graph << ConvolutionLayer(1U, 1U, 1280U,
177 get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW),
178 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
179 PadStrideInfo(1, 1, 0, 0))
180 .set_name("Conv_1")
181 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"),
182 get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"),
183 get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"),
184 get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"),
185 0.0010000000474974513f)
186 .set_name("Conv_1/BatchNorm")
187 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
188 .set_name("Conv_1/Relu6")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000189 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000190 << ConvolutionLayer(1U, 1U, 1001U,
191 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
192 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
193 PadStrideInfo(1, 1, 0, 0))
194 .set_name("Logits/Conv2d_1c_1x1");
195 }
196
197 void get_expanded_conv_float(const std::string &data_path, std::string &&param_path,
198 unsigned int input_channels, unsigned int output_channels,
199 PadStrideInfo dwc_pad_stride_info,
200 HasExpand has_expand = HasExpand::No, IsResidual is_residual = IsResidual::No,
201 unsigned int expansion_size = 6)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100202 {
203 std::string total_path = param_path + "_";
204 SubStream left(graph);
205
206 // Add expand node
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000207 if(has_expand == HasExpand::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100208 {
209 left << ConvolutionLayer(1U, 1U, input_channels * expansion_size,
210 get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW),
211 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
212 .set_name(param_path + "/expand/Conv2D")
213 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"),
214 get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"),
215 get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"),
216 get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"),
217 0.0010000000474974513f)
218 .set_name(param_path + "/expand/BatchNorm")
219 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
220 .set_name(param_path + "/expand/Relu6");
221 }
222
223 // Add depthwise node
224 left << DepthwiseConvolutionLayer(3U, 3U,
225 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
226 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
227 dwc_pad_stride_info)
228 .set_name(param_path + "/depthwise/depthwise")
229 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
230 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
231 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
232 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
233 0.0010000000474974513f)
234 .set_name(param_path + "/depthwise/BatchNorm")
235 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
236 .set_name(param_path + "/depthwise/Relu6");
237
238 // Add project node
239 left << ConvolutionLayer(1U, 1U, output_channels,
240 get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW),
241 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
242 .set_name(param_path + "/project/Conv2D")
243 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"),
244 get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"),
245 get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"),
246 get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"),
247 0.0010000000474974513)
248 .set_name(param_path + "/project/BatchNorm");
249
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000250 if(is_residual == IsResidual::Yes)
251 {
252 // Add residual node
253 SubStream right(graph);
254 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
255 }
256 else
257 {
258 graph.forward_tail(left.tail_node());
259 }
260 }
261
262 void create_graph_qasymm8(TensorDescriptor &input_descriptor)
263 {
264 // Create model path
Pablo Telloadc21862019-03-22 16:47:59 +0000265 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_quantized_model/";
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000266
267 // Get trainable parameters data path
268 std::string data_path = common_params.data_path;
269
270 // Add model path to data path
271 if(!data_path.empty())
272 {
273 data_path += model_path;
274 }
275
276 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
277 const QuantizationInfo mid_quant_info = QuantizationInfo(0.023528477177023888f, 128);
278
279 const std::vector<QuantizationInfo> conv_weights_quant_info =
280 {
281 QuantizationInfo(0.03396892547607422f, 122), // Conv
282 QuantizationInfo(0.005167067516595125f, 125), // Conv1
283 QuantizationInfo(0.0016910821432247758f, 113) // Conv2d_1c_1x1
284 };
285
286 // Pointwise expand convolution quantization info
287 const std::vector<QuantizationInfo> pwc_q =
288 {
289 QuantizationInfo(0.254282623529f, 129), // expand_0 (Dummy)
290 QuantizationInfo(0.009758507832884789f, 127), // expand_1
291 QuantizationInfo(0.0036556976847350597f, 144), // expand_2
292 QuantizationInfo(0.0029988749884068966f, 104), // expand_3
293 QuantizationInfo(0.0019244228024035692f, 128), // expand_4
294 QuantizationInfo(0.0013649158645421267f, 135), // expand_5
295 QuantizationInfo(0.0019170437008142471f, 127), // expand_6
296 QuantizationInfo(0.0015538912266492844f, 125), // expand_7
297 QuantizationInfo(0.0014702979242429137f, 134), // expand_8
298 QuantizationInfo(0.0013733493397012353f, 127), // expand_9
299 QuantizationInfo(0.0016282502328976989f, 131), // expand_10
300 QuantizationInfo(0.0016309921629726887f, 134), // expand_11
301 QuantizationInfo(0.0018258779309689999f, 138), // expand_12
302 QuantizationInfo(0.0013828007504343987f, 123), // expand_13
303 QuantizationInfo(0.0020222084131091833f, 135), // expand_14
304 QuantizationInfo(0.04281935095787048f, 102), // expand_15
305 QuantizationInfo(0.002046825597062707f, 135) // expand_16
306 };
307 // Depthwise expand convolution quantization info
308 const std::vector<QuantizationInfo> dwc_q =
309 {
310 QuantizationInfo(0.3436955213546753f, 165), // expand_0
311 QuantizationInfo(0.020969120785593987f, 109), // expand_1
312 QuantizationInfo(0.16981913149356842f, 52), // expand_2
313 QuantizationInfo(0.017202870920300484f, 143), // expand_3
314 QuantizationInfo(0.06525065749883652f, 118), // expand_4
315 QuantizationInfo(0.07909784466028214f, 95), // expand_5
316 QuantizationInfo(0.010087885893881321f, 127), // expand_6
317 QuantizationInfo(0.06092711538076401f, 110), // expand_7
318 QuantizationInfo(0.052407849580049515f, 133), // expand_8
319 QuantizationInfo(0.04077887907624245f, 155), // expand_9
320 QuantizationInfo(0.031107846647500992f, 143), // expand_10
321 QuantizationInfo(0.07080810517072678f, 66), // expand_11
322 QuantizationInfo(0.07448793947696686f, 159), // expand_12
323 QuantizationInfo(0.01525793131440878f, 92), // expand_13
324 QuantizationInfo(0.04166752099990845f, 147), // expand_14
325 QuantizationInfo(0.04281935095787048f, 102), // expand_15
326 QuantizationInfo(0.16456253826618195, 201) // expand_16
327 };
328 // Project convolution quantization info
329 const std::vector<QuantizationInfo> prwc_q =
330 {
331 QuantizationInfo(0.03737175464630127f, 140), // expand_0
332 QuantizationInfo(0.0225360207259655f, 156), // expand_1
333 QuantizationInfo(0.02740888111293316f, 122), // expand_2
334 QuantizationInfo(0.016844693571329117f, 111), // expand_3
335 QuantizationInfo(0.019062912091612816f, 146), // expand_4
336 QuantizationInfo(0.018293123692274094f, 128), // expand_5
337 QuantizationInfo(0.014601286500692368f, 147), // expand_6
338 QuantizationInfo(0.016782939434051514f, 124), // expand_7
339 QuantizationInfo(0.012898261658847332f, 125), // expand_8
340 QuantizationInfo(0.019561484456062317f, 144), // expand_9
341 QuantizationInfo(0.007436311338096857f, 129), // expand_10
342 QuantizationInfo(0.00838223285973072f, 136), // expand_11
343 QuantizationInfo(0.023982593789696693f, 154), // expand_12
344 QuantizationInfo(0.009447949007153511f, 140), // expand_13
345 QuantizationInfo(0.00789870135486126f, 139), // expand_14
346 QuantizationInfo(0.03697410225868225f, 131), // expand_15
347 QuantizationInfo(0.008009289391338825f, 111) // expand_16
348 };
349
350 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
351 get_weights_accessor(data_path, common_params.image))
352 << ConvolutionLayer(
353 3U, 3U, 32U,
354 get_weights_accessor(data_path, "Conv_weights.npy"),
355 get_weights_accessor(data_path, "Conv_bias.npy"),
356 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
357 1, conv_weights_quant_info.at(0), mid_quant_info)
358 .set_name("Conv")
359 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv/Relu6")
360 << DepthwiseConvolutionLayer(3U, 3U,
361 get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_weights.npy"),
362 get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_biases.npy"),
Georgios Pinitas05045c12018-12-07 18:31:47 +0000363 PadStrideInfo(1, 1, 1, 1), 1, dwc_q.at(0))
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000364 .set_name("expanded_conv/depthwise/depthwise")
365 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("expanded_conv/depthwise/Relu6")
366 << ConvolutionLayer(1U, 1U, 16U,
367 get_weights_accessor(data_path, "expanded_conv_project_weights.npy"),
368 get_weights_accessor(data_path, "expanded_conv_project_biases.npy"),
369 PadStrideInfo(1, 1, 0, 0), 1, prwc_q.at(0))
370 .set_name("expanded_conv/project/Conv2D");
371
372 get_expanded_conv_qasymm8(data_path, "expanded_conv_1", IsResidual::No, 96U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
373 pwc_q.at(1), dwc_q.at(1), prwc_q.at(1));
374 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));
375 get_expanded_conv_qasymm8(data_path, "expanded_conv_3", IsResidual::No, 144U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
376 pwc_q.at(3), dwc_q.at(3), prwc_q.at(3));
377 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));
378 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));
379 get_expanded_conv_qasymm8(data_path, "expanded_conv_6", IsResidual::No, 192U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
380 pwc_q.at(6), dwc_q.at(6), prwc_q.at(6));
381 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));
382 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));
383 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));
384 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));
385 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));
386 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));
387 get_expanded_conv_qasymm8(data_path, "expanded_conv_13", IsResidual::No, 576U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
388 pwc_q.at(13), dwc_q.at(13), prwc_q.at(13));
389 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));
390 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));
391 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));
392
393 graph << ConvolutionLayer(1U, 1U, 1280U,
394 get_weights_accessor(data_path, "Conv_1_weights.npy"),
395 get_weights_accessor(data_path, "Conv_1_biases.npy"),
396 PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(1))
397 .set_name("Conv_1")
398 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv_1/Relu6")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000399 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000400 << ConvolutionLayer(1U, 1U, 1001U,
401 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
402 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
403 PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(2))
404 .set_name("Logits/Conv2d_1c_1x1");
405 }
406
407 void get_expanded_conv_qasymm8(const std::string &data_path, std::string &&param_path, IsResidual is_residual,
408 unsigned int input_channels, unsigned int output_channels,
409 PadStrideInfo dwc_pad_stride_info,
410 const QuantizationInfo &pwi, const QuantizationInfo &dwi, const QuantizationInfo &pji)
411 {
412 std::string total_path = param_path + "_";
413
414 SubStream left(graph);
415 left << ConvolutionLayer(1U, 1U, input_channels,
416 get_weights_accessor(data_path, total_path + "project_weights.npy"),
417 get_weights_accessor(data_path, total_path + "project_biases.npy"),
418 PadStrideInfo(1, 1, 0, 0), 1, pwi)
419 .set_name(param_path + "/Conv2D")
420 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(param_path + "/Conv2D/Relu6")
421 << DepthwiseConvolutionLayer(3U, 3U,
422 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
423 get_weights_accessor(data_path, total_path + "depthwise_depthwise_biases.npy"),
Georgios Pinitas05045c12018-12-07 18:31:47 +0000424 dwc_pad_stride_info, 1, dwi)
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000425 .set_name(param_path + "/depthwise/depthwise")
426 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(param_path + "/depthwise/Relu6")
427 << ConvolutionLayer(1U, 1U, output_channels,
428 get_weights_accessor(data_path, total_path + "project_weights.npy"),
429 get_weights_accessor(data_path, total_path + "project_biases.npy"),
430 PadStrideInfo(1, 1, 0, 0), 1, pji)
431 .set_name(param_path + "/project/Conv2D");
432
433 if(is_residual == IsResidual::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100434 {
435 // Add residual node
436 SubStream right(graph);
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100437 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100438 }
439 else
440 {
441 graph.forward_tail(left.tail_node());
442 }
443 }
444};
445
446/** Main program for MobileNetV2
447 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000448 * Model is based on:
449 * https://arxiv.org/abs/1801.04381
450 * "MobileNetV2: Inverted Residuals and Linear Bottlenecks"
451 * Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen
452 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000453 * Provenance: https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz
454 *
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100455 * @note To list all the possible arguments execute the binary appended with the --help option
456 *
457 * @param[in] argc Number of arguments
458 * @param[in] argv Arguments
459 */
460int main(int argc, char **argv)
461{
462 return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv);
463}