blob: 9bc21c42c54f7bef053f34e239a329dc8de3f3f5 [file] [log] [blame]
Georgios Pinitas766b70c2018-08-13 17:50:34 +01001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2018-2021 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Georgios Pinitas766b70c2018-08-13 17:50:34 +010026#include "support/ToolchainSupport.h"
27#include "utils/CommonGraphOptions.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31using namespace arm_compute;
32using namespace arm_compute::utils;
33using namespace arm_compute::graph::frontend;
34using namespace arm_compute::graph_utils;
35
36/** Example demonstrating how to implement MobileNetV2's network using the Compute Library's graph API */
37class GraphMobilenetV2Example : public Example
38{
39public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 GraphMobilenetV2Example() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV2")
Georgios Pinitas766b70c2018-08-13 17:50:34 +010041 {
42 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 GraphMobilenetV2Example(const GraphMobilenetV2Example &) = delete;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010044 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 if (common_params.help)
Georgios Pinitas766b70c2018-08-13 17:50:34 +010058 {
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, common_params.batches),
68 DataLayout::NCHW, common_params.data_layout);
69 TensorDescriptor input_descriptor =
70 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Georgios Pinitas766b70c2018-08-13 17:50:34 +010071
Georgios Pinitasf60d6712018-11-29 13:21:54 +000072 // Set graph hints
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 graph << common_params.target << common_params.fast_math_hint;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010074
Georgios Pinitasf60d6712018-11-29 13:21:54 +000075 // Create core graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 if (arm_compute::is_data_type_float(common_params.data_type))
Georgios Pinitasf60d6712018-11-29 13:21:54 +000077 {
78 create_graph_float(input_descriptor);
79 }
80 else
81 {
82 create_graph_qasymm8(input_descriptor);
83 }
84 // Create common tail
85 graph << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape")
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 << SoftmaxLayer().set_name("Predictions/Softmax") << OutputLayer(get_output_accessor(common_params, 5));
Georgios Pinitas766b70c2018-08-13 17:50:34 +010087
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;
SiCong Li4841c972021-02-03 12:17:35 +000094 config.mlgo_file = common_params.mlgo_file;
Georgios Pinitas766b70c2018-08-13 17:50:34 +010095
96 graph.finalize(common_params.target, config);
97
98 return true;
99 }
100
101 void do_run() override
102 {
103 // Run graph
104 graph.run();
105 }
106
107private:
108 CommandLineParser cmd_parser;
109 CommonGraphOptions common_opts;
110 CommonGraphParams common_params;
111 Stream graph;
112
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000113private:
114 enum class IsResidual
115 {
116 Yes,
117 No
118 };
119
120 enum class HasExpand
121 {
122 Yes,
123 No
124 };
125
126private:
127 void create_graph_float(TensorDescriptor &input_descriptor)
128 {
129 // Create model path
130 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/";
131
132 // Create a preprocessor object
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000133 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>();
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000134
135 // Get trainable parameters data path
136 std::string data_path = common_params.data_path;
137
138 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139 if (!data_path.empty())
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000140 {
141 data_path += model_path;
142 }
143
144 graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW),
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000146 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
147 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 .set_name("Conv")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000149 << 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)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 .set_name("Conv/BatchNorm")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 .set_name("Conv/Relu6");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000157
158 get_expanded_conv_float(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 get_expanded_conv_float(data_path, "expanded_conv_1", 16U, 24U,
160 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
161 get_expanded_conv_float(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
162 IsResidual::Yes);
163 get_expanded_conv_float(data_path, "expanded_conv_3", 24U, 32U,
164 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
165 get_expanded_conv_float(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
166 IsResidual::Yes);
167 get_expanded_conv_float(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
168 IsResidual::Yes);
169 get_expanded_conv_float(data_path, "expanded_conv_6", 32U, 64U,
170 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
171 get_expanded_conv_float(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
172 IsResidual::Yes);
173 get_expanded_conv_float(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
174 IsResidual::Yes);
175 get_expanded_conv_float(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
176 IsResidual::Yes);
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000177 get_expanded_conv_float(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 get_expanded_conv_float(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
179 IsResidual::Yes);
180 get_expanded_conv_float(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
181 IsResidual::Yes);
182 get_expanded_conv_float(data_path, "expanded_conv_13", 96U, 160U,
183 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes);
184 get_expanded_conv_float(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
185 IsResidual::Yes);
186 get_expanded_conv_float(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes,
187 IsResidual::Yes);
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000188 get_expanded_conv_float(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes);
189
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190 graph << ConvolutionLayer(
191 1U, 1U, 1280U, get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW),
192 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
193 .set_name("Conv_1")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000194 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"),
195 get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"),
196 get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"),
197 get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"),
198 0.0010000000474974513f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 .set_name("Conv_1/BatchNorm")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 .set_name("Conv_1/Relu6")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000202 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000203 << ConvolutionLayer(1U, 1U, 1001U,
204 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
205 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
206 PadStrideInfo(1, 1, 0, 0))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000208 }
209
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 void get_expanded_conv_float(const std::string &data_path,
211 std::string &&param_path,
212 unsigned int input_channels,
213 unsigned int output_channels,
214 PadStrideInfo dwc_pad_stride_info,
215 HasExpand has_expand = HasExpand::No,
216 IsResidual is_residual = IsResidual::No,
217 unsigned int expansion_size = 6)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100218 {
219 std::string total_path = param_path + "_";
220 SubStream left(graph);
221
222 // Add expand node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 if (has_expand == HasExpand::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100224 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 left << ConvolutionLayer(
226 1U, 1U, input_channels * expansion_size,
227 get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW),
228 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
229 .set_name(param_path + "/expand/Conv2D")
230 << BatchNormalizationLayer(
231 get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"),
232 get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"),
233 get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"),
234 get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"),
235 0.0010000000474974513f)
236 .set_name(param_path + "/expand/BatchNorm")
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100237 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238 .set_name(param_path + "/expand/Relu6");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100239 }
240
241 // Add depthwise node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100242 left << DepthwiseConvolutionLayer(
243 3U, 3U,
244 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
245 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), dwc_pad_stride_info)
246 .set_name(param_path + "/depthwise/depthwise")
247 << BatchNormalizationLayer(
248 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
249 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
250 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
251 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
252 0.0010000000474974513f)
253 .set_name(param_path + "/depthwise/BatchNorm")
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100254 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100255 .set_name(param_path + "/depthwise/Relu6");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100256
257 // Add project node
258 left << ConvolutionLayer(1U, 1U, output_channels,
259 get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100260 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
261 PadStrideInfo(1, 1, 0, 0))
262 .set_name(param_path + "/project/Conv2D")
263 << BatchNormalizationLayer(
264 get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"),
265 get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"),
266 get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"),
267 get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"), 0.0010000000474974513)
268 .set_name(param_path + "/project/BatchNorm");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100269
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100270 if (is_residual == IsResidual::Yes)
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000271 {
272 // Add residual node
273 SubStream right(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274 graph
275 << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000276 }
277 else
278 {
279 graph.forward_tail(left.tail_node());
280 }
281 }
282
283 void create_graph_qasymm8(TensorDescriptor &input_descriptor)
284 {
285 // Create model path
Pablo Telloadc21862019-03-22 16:47:59 +0000286 const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_quantized_model/";
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000287
288 // Get trainable parameters data path
289 std::string data_path = common_params.data_path;
290
291 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 if (!data_path.empty())
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000293 {
294 data_path += model_path;
295 }
296
297 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
298 const QuantizationInfo mid_quant_info = QuantizationInfo(0.023528477177023888f, 128);
299
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 const std::vector<QuantizationInfo> conv_weights_quant_info = {
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000301 QuantizationInfo(0.03396892547607422f, 122), // Conv
302 QuantizationInfo(0.005167067516595125f, 125), // Conv1
303 QuantizationInfo(0.0016910821432247758f, 113) // Conv2d_1c_1x1
304 };
305
306 // Pointwise expand convolution quantization info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100307 const std::vector<QuantizationInfo> pwc_q = {
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000308 QuantizationInfo(0.254282623529f, 129), // expand_0 (Dummy)
309 QuantizationInfo(0.009758507832884789f, 127), // expand_1
310 QuantizationInfo(0.0036556976847350597f, 144), // expand_2
311 QuantizationInfo(0.0029988749884068966f, 104), // expand_3
312 QuantizationInfo(0.0019244228024035692f, 128), // expand_4
313 QuantizationInfo(0.0013649158645421267f, 135), // expand_5
314 QuantizationInfo(0.0019170437008142471f, 127), // expand_6
315 QuantizationInfo(0.0015538912266492844f, 125), // expand_7
316 QuantizationInfo(0.0014702979242429137f, 134), // expand_8
317 QuantizationInfo(0.0013733493397012353f, 127), // expand_9
318 QuantizationInfo(0.0016282502328976989f, 131), // expand_10
319 QuantizationInfo(0.0016309921629726887f, 134), // expand_11
320 QuantizationInfo(0.0018258779309689999f, 138), // expand_12
321 QuantizationInfo(0.0013828007504343987f, 123), // expand_13
322 QuantizationInfo(0.0020222084131091833f, 135), // expand_14
323 QuantizationInfo(0.04281935095787048f, 102), // expand_15
324 QuantizationInfo(0.002046825597062707f, 135) // expand_16
325 };
326 // Depthwise expand convolution quantization info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327 const std::vector<QuantizationInfo> dwc_q = {
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000328 QuantizationInfo(0.3436955213546753f, 165), // expand_0
329 QuantizationInfo(0.020969120785593987f, 109), // expand_1
330 QuantizationInfo(0.16981913149356842f, 52), // expand_2
331 QuantizationInfo(0.017202870920300484f, 143), // expand_3
332 QuantizationInfo(0.06525065749883652f, 118), // expand_4
333 QuantizationInfo(0.07909784466028214f, 95), // expand_5
334 QuantizationInfo(0.010087885893881321f, 127), // expand_6
335 QuantizationInfo(0.06092711538076401f, 110), // expand_7
336 QuantizationInfo(0.052407849580049515f, 133), // expand_8
337 QuantizationInfo(0.04077887907624245f, 155), // expand_9
338 QuantizationInfo(0.031107846647500992f, 143), // expand_10
339 QuantizationInfo(0.07080810517072678f, 66), // expand_11
340 QuantizationInfo(0.07448793947696686f, 159), // expand_12
341 QuantizationInfo(0.01525793131440878f, 92), // expand_13
342 QuantizationInfo(0.04166752099990845f, 147), // expand_14
343 QuantizationInfo(0.04281935095787048f, 102), // expand_15
344 QuantizationInfo(0.16456253826618195, 201) // expand_16
345 };
346 // Project convolution quantization info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100347 const std::vector<QuantizationInfo> prwc_q = {
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000348 QuantizationInfo(0.03737175464630127f, 140), // expand_0
349 QuantizationInfo(0.0225360207259655f, 156), // expand_1
350 QuantizationInfo(0.02740888111293316f, 122), // expand_2
351 QuantizationInfo(0.016844693571329117f, 111), // expand_3
352 QuantizationInfo(0.019062912091612816f, 146), // expand_4
353 QuantizationInfo(0.018293123692274094f, 128), // expand_5
354 QuantizationInfo(0.014601286500692368f, 147), // expand_6
355 QuantizationInfo(0.016782939434051514f, 124), // expand_7
356 QuantizationInfo(0.012898261658847332f, 125), // expand_8
357 QuantizationInfo(0.019561484456062317f, 144), // expand_9
358 QuantizationInfo(0.007436311338096857f, 129), // expand_10
359 QuantizationInfo(0.00838223285973072f, 136), // expand_11
360 QuantizationInfo(0.023982593789696693f, 154), // expand_12
361 QuantizationInfo(0.009447949007153511f, 140), // expand_13
362 QuantizationInfo(0.00789870135486126f, 139), // expand_14
363 QuantizationInfo(0.03697410225868225f, 131), // expand_15
364 QuantizationInfo(0.008009289391338825f, 111) // expand_16
365 };
366
367 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
368 get_weights_accessor(data_path, common_params.image))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100369 << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "Conv_weights.npy"),
370 get_weights_accessor(data_path, "Conv_bias.npy"),
371 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), 1,
372 conv_weights_quant_info.at(0), mid_quant_info)
373 .set_name("Conv")
374 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
375 .set_name("Conv/Relu6")
376 << DepthwiseConvolutionLayer(
377 3U, 3U, get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_weights.npy"),
378 get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_biases.npy"),
379 PadStrideInfo(1, 1, 1, 1), 1, dwc_q.at(0))
380 .set_name("expanded_conv/depthwise/depthwise")
381 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
382 .set_name("expanded_conv/depthwise/Relu6")
383 << ConvolutionLayer(1U, 1U, 16U, get_weights_accessor(data_path, "expanded_conv_project_weights.npy"),
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000384 get_weights_accessor(data_path, "expanded_conv_project_biases.npy"),
385 PadStrideInfo(1, 1, 0, 0), 1, prwc_q.at(0))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100386 .set_name("expanded_conv/project/Conv2D");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000387
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100388 get_expanded_conv_qasymm8(data_path, "expanded_conv_1", IsResidual::No, 96U, 24U,
389 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(1),
390 dwc_q.at(1), prwc_q.at(1));
391 get_expanded_conv_qasymm8(data_path, "expanded_conv_2", IsResidual::Yes, 144U, 24U, PadStrideInfo(1, 1, 1, 1),
392 pwc_q.at(2), dwc_q.at(2), prwc_q.at(2));
393 get_expanded_conv_qasymm8(data_path, "expanded_conv_3", IsResidual::No, 144U, 32U,
394 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(3),
395 dwc_q.at(3), prwc_q.at(3));
396 get_expanded_conv_qasymm8(data_path, "expanded_conv_4", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1),
397 pwc_q.at(4), dwc_q.at(4), prwc_q.at(4));
398 get_expanded_conv_qasymm8(data_path, "expanded_conv_5", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1),
399 pwc_q.at(5), dwc_q.at(5), prwc_q.at(5));
400 get_expanded_conv_qasymm8(data_path, "expanded_conv_6", IsResidual::No, 192U, 64U,
401 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(6),
402 dwc_q.at(6), prwc_q.at(6));
403 get_expanded_conv_qasymm8(data_path, "expanded_conv_7", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1),
404 pwc_q.at(7), dwc_q.at(7), prwc_q.at(7));
405 get_expanded_conv_qasymm8(data_path, "expanded_conv_8", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1),
406 pwc_q.at(8), dwc_q.at(8), prwc_q.at(8));
407 get_expanded_conv_qasymm8(data_path, "expanded_conv_9", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1),
408 pwc_q.at(9), dwc_q.at(9), prwc_q.at(9));
409 get_expanded_conv_qasymm8(data_path, "expanded_conv_10", IsResidual::No, 384U, 96U, PadStrideInfo(1, 1, 1, 1),
410 pwc_q.at(10), dwc_q.at(10), prwc_q.at(10));
411 get_expanded_conv_qasymm8(data_path, "expanded_conv_11", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1),
412 pwc_q.at(11), dwc_q.at(11), prwc_q.at(11));
413 get_expanded_conv_qasymm8(data_path, "expanded_conv_12", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1),
414 pwc_q.at(12), dwc_q.at(12), prwc_q.at(12));
415 get_expanded_conv_qasymm8(data_path, "expanded_conv_13", IsResidual::No, 576U, 160U,
416 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(13),
417 dwc_q.at(13), prwc_q.at(13));
418 get_expanded_conv_qasymm8(data_path, "expanded_conv_14", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1),
419 pwc_q.at(14), dwc_q.at(14), prwc_q.at(14));
420 get_expanded_conv_qasymm8(data_path, "expanded_conv_15", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1),
421 pwc_q.at(15), dwc_q.at(15), prwc_q.at(15));
422 get_expanded_conv_qasymm8(data_path, "expanded_conv_16", IsResidual::No, 960U, 320U, PadStrideInfo(1, 1, 1, 1),
423 pwc_q.at(16), dwc_q.at(16), prwc_q.at(16));
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000424
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100425 graph << ConvolutionLayer(1U, 1U, 1280U, get_weights_accessor(data_path, "Conv_1_weights.npy"),
426 get_weights_accessor(data_path, "Conv_1_biases.npy"), PadStrideInfo(1, 1, 0, 0), 1,
427 conv_weights_quant_info.at(1))
428 .set_name("Conv_1")
429 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
430 .set_name("Conv_1/Relu6")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000431 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool")
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100432 << ConvolutionLayer(1U, 1U, 1001U, get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000433 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
434 PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(2))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100435 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000436 }
437
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100438 void get_expanded_conv_qasymm8(const std::string &data_path,
439 std::string &&param_path,
440 IsResidual is_residual,
441 unsigned int input_channels,
442 unsigned int output_channels,
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000443 PadStrideInfo dwc_pad_stride_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100444 const QuantizationInfo &pwi,
445 const QuantizationInfo &dwi,
446 const QuantizationInfo &pji)
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000447 {
448 std::string total_path = param_path + "_";
449
450 SubStream left(graph);
451 left << ConvolutionLayer(1U, 1U, input_channels,
452 get_weights_accessor(data_path, total_path + "project_weights.npy"),
453 get_weights_accessor(data_path, total_path + "project_biases.npy"),
454 PadStrideInfo(1, 1, 0, 0), 1, pwi)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100455 .set_name(param_path + "/Conv2D")
456 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
457 .set_name(param_path + "/Conv2D/Relu6")
458 << DepthwiseConvolutionLayer(
459 3U, 3U, get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
460 get_weights_accessor(data_path, total_path + "depthwise_depthwise_biases.npy"), dwc_pad_stride_info,
461 1, dwi)
462 .set_name(param_path + "/depthwise/depthwise")
463 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
464 .set_name(param_path + "/depthwise/Relu6")
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000465 << ConvolutionLayer(1U, 1U, output_channels,
466 get_weights_accessor(data_path, total_path + "project_weights.npy"),
467 get_weights_accessor(data_path, total_path + "project_biases.npy"),
468 PadStrideInfo(1, 1, 0, 0), 1, pji)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100469 .set_name(param_path + "/project/Conv2D");
Georgios Pinitasf60d6712018-11-29 13:21:54 +0000470
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100471 if (is_residual == IsResidual::Yes)
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100472 {
473 // Add residual node
474 SubStream right(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100475 graph
476 << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add");
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100477 }
478 else
479 {
480 graph.forward_tail(left.tail_node());
481 }
482 }
483};
484
485/** Main program for MobileNetV2
486 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000487 * Model is based on:
488 * https://arxiv.org/abs/1801.04381
489 * "MobileNetV2: Inverted Residuals and Linear Bottlenecks"
490 * Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen
491 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000492 * Provenance: https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz
493 *
Georgios Pinitas766b70c2018-08-13 17:50:34 +0100494 * @note To list all the possible arguments execute the binary appended with the --help option
495 *
496 * @param[in] argc Number of arguments
497 * @param[in] argv Arguments
498 */
499int main(int argc, char **argv)
500{
501 return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv);
502}