blob: e3a6ef116dd0a54e9e9adf0da38c543a1a3300bb [file] [log] [blame]
Georgios Pinitas236bfe72017-11-23 15:59:55 +00001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2017-2021 Arm Limited.
Georgios Pinitas236bfe72017-11-23 15:59:55 +00003 *
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Georgios Pinitas236bfe72017-11-23 15:59:55 +000026#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010027#include "utils/CommonGraphOptions.h"
Georgios Pinitas236bfe72017-11-23 15:59:55 +000028#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010031using namespace arm_compute;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033using namespace arm_compute::graph::frontend;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000034using namespace arm_compute::graph_utils;
35
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010036/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API */
Gian Marco Iodice11a7e322018-07-05 15:42:02 +010037class GraphMobilenetExample : public Example
Georgios Pinitas236bfe72017-11-23 15:59:55 +000038{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000039public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 GraphMobilenetExample() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 // Add model id option
43 model_id_opt = cmd_parser.add_option<SimpleOption<int>>("model-id", 0);
44 model_id_opt->set_help("Mobilenet model id (0: 1.0_224, else: 0.75_160");
45 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 GraphMobilenetExample(const GraphMobilenetExample &) = delete;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010047 GraphMobilenetExample &operator=(const GraphMobilenetExample &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000048 ~GraphMobilenetExample() override = default;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010049 bool do_setup(int argc, char **argv) override
50 {
51 // Parse arguments
52 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010053 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010054
55 // Consume common parameters
56 common_params = consume_common_graph_parameters(common_opts);
57
58 // Return when help menu is requested
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 if (common_params.help)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010060 {
61 cmd_parser.print_help(argv[0]);
62 return false;
63 }
64
65 // Print parameter values
66 std::cout << common_params << std::endl;
67
68 // Get model parameters
69 int model_id = model_id_opt->value();
70
71 // Create input descriptor
72 unsigned int spatial_size = (model_id == 0 || common_params.data_type == DataType::QASYMM8) ? 224 : 160;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010073
74 // Create input descriptor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 const TensorShape tensor_shape =
76 permute_shape(TensorShape(spatial_size, spatial_size, 3U, common_params.batches), DataLayout::NCHW,
77 common_params.data_layout);
78 TensorDescriptor input_descriptor =
79 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010080
81 // Set graph hints
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 graph << common_params.target << common_params.fast_math_hint;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010083
84 // Create core graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 if (arm_compute::is_data_type_float(common_params.data_type))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010086 {
87 create_graph_float(input_descriptor, model_id);
88 }
89 else
90 {
91 create_graph_qasymm(input_descriptor);
92 }
93
94 // Create common tail
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 graph << ReshapeLayer(TensorShape(1001U)).set_name("Reshape") << SoftmaxLayer().set_name("Softmax")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010096 << OutputLayer(get_output_accessor(common_params, 5));
97
98 // Finalize graph
99 GraphConfig config;
100 config.num_threads = common_params.threads;
101 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100102 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100103 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000104 config.mlgo_file = common_params.mlgo_file;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100105
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100106 graph.finalize(common_params.target, config);
107
108 return true;
109 }
110 void do_run() override
111 {
112 // Run graph
113 graph.run();
114 }
115
116private:
117 CommandLineParser cmd_parser;
118 CommonGraphOptions common_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 SimpleOption<int> *model_id_opt{nullptr};
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100120 CommonGraphParams common_params;
121 Stream graph;
122
123 void create_graph_float(TensorDescriptor &input_descriptor, int model_id)
124 {
125 float depth_scale = (model_id == 0) ? 1.f : 0.75;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 std::string model_path =
127 (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000128
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000129 // Create a preprocessor object
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000130 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>();
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000131
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100132 // Get trainable parameters data path
133 std::string data_path = common_params.data_path;
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000134
135 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 if (!data_path.empty())
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000137 {
138 data_path += model_path;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000139 }
140
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
142 << ConvolutionLayer(3U, 3U, 32U * depth_scale,
143 get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW),
144 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
145 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
146 .set_name("Conv2d_0")
147 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"),
148 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"),
149 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"),
150 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"), 0.001f)
151 .set_name("Conv2d_0/BatchNorm")
152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
153 .set_name("Conv2d_0/Relu6");
154 graph << get_dwsc_node_float(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1),
155 PadStrideInfo(1, 1, 0, 0));
156 graph << get_dwsc_node_float(data_path, "Conv2d_2", 128 * depth_scale,
157 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
158 PadStrideInfo(1, 1, 0, 0));
159 graph << get_dwsc_node_float(data_path, "Conv2d_3", 128 * depth_scale,
160 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
161 PadStrideInfo(1, 1, 0, 0));
162 graph << get_dwsc_node_float(data_path, "Conv2d_4", 256 * depth_scale,
163 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
164 PadStrideInfo(1, 1, 0, 0));
165 graph << get_dwsc_node_float(data_path, "Conv2d_5", 256 * depth_scale,
166 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
167 PadStrideInfo(1, 1, 0, 0));
168 graph << get_dwsc_node_float(data_path, "Conv2d_6", 512 * depth_scale,
169 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
170 PadStrideInfo(1, 1, 0, 0));
171 graph << get_dwsc_node_float(data_path, "Conv2d_7", 512 * depth_scale,
172 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
173 PadStrideInfo(1, 1, 0, 0));
174 graph << get_dwsc_node_float(data_path, "Conv2d_8", 512 * depth_scale,
175 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
176 PadStrideInfo(1, 1, 0, 0));
177 graph << get_dwsc_node_float(data_path, "Conv2d_9", 512 * depth_scale,
178 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
179 PadStrideInfo(1, 1, 0, 0));
180 graph << get_dwsc_node_float(data_path, "Conv2d_10", 512 * depth_scale,
181 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
182 PadStrideInfo(1, 1, 0, 0));
183 graph << get_dwsc_node_float(data_path, "Conv2d_11", 512 * depth_scale,
184 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
185 PadStrideInfo(1, 1, 0, 0));
186 graph << get_dwsc_node_float(data_path, "Conv2d_12", 1024 * depth_scale,
187 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL),
188 PadStrideInfo(1, 1, 0, 0));
189 graph << get_dwsc_node_float(data_path, "Conv2d_13", 1024 * depth_scale,
190 PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL),
191 PadStrideInfo(1, 1, 0, 0));
192 graph
193 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
194 << ConvolutionLayer(
195 1U, 1U, 1001U, get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
196 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), PadStrideInfo(1, 1, 0, 0))
197 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000198 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100199
200 void create_graph_qasymm(TensorDescriptor &input_descriptor)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000201 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100202 // Get trainable parameters data path
203 std::string data_path = common_params.data_path;
204
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100205 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (!data_path.empty())
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100207 {
208 data_path += "/cnn_data/mobilenet_qasymm8_model/";
209 }
210
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100211 // Quantization info taken from the AndroidNN QASYMM8 MobileNet example
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100212 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100213
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 const std::vector<QuantizationInfo> conv_weights_quant_info = {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100215 QuantizationInfo(0.02182667888700962f, 151), // conv0
216 QuantizationInfo(0.004986600950360298f, 74) // conv14
217 };
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100218 const std::vector<QuantizationInfo> conv_out_quant_info = {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100219 QuantizationInfo(0.023528477177023888f, 0), // conv0
220 QuantizationInfo(0.16609922051429749f, 66) // conv14
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100221 };
222
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 const std::vector<QuantizationInfo> depth_weights_quant_info = {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100224 QuantizationInfo(0.29219913482666016f, 110), // dwsc1
225 QuantizationInfo(0.40277284383773804f, 130), // dwsc2
226 QuantizationInfo(0.06053730100393295f, 160), // dwsc3
227 QuantizationInfo(0.01675807684659958f, 123), // dwsc4
228 QuantizationInfo(0.04105526953935623f, 129), // dwsc5
229 QuantizationInfo(0.013460792601108551f, 122), // dwsc6
230 QuantizationInfo(0.036934755742549896f, 132), // dwsc7
231 QuantizationInfo(0.042609862983226776f, 94), // dwsc8
232 QuantizationInfo(0.028358859941363335f, 127), // dwsc9
233 QuantizationInfo(0.024329448118805885f, 134), // dwsc10
234 QuantizationInfo(0.019366811960935593f, 106), // dwsc11
235 QuantizationInfo(0.007835594937205315f, 126), // dwsc12
236 QuantizationInfo(0.12616927921772003f, 211) // dwsc13
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100237 };
238
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 const std::vector<QuantizationInfo> point_weights_quant_info = {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100240 QuantizationInfo(0.030420949682593346f, 121), // dwsc1
241 QuantizationInfo(0.015148180536925793f, 104), // dwsc2
242 QuantizationInfo(0.013755458407104015f, 94), // dwsc3
243 QuantizationInfo(0.007601846940815449f, 151), // dwsc4
244 QuantizationInfo(0.006431614048779011f, 122), // dwsc5
245 QuantizationInfo(0.00917122047394514f, 109), // dwsc6
246 QuantizationInfo(0.005300046876072884f, 140), // dwsc7
247 QuantizationInfo(0.0049632852897048f, 127), // dwsc8
248 QuantizationInfo(0.007770895957946777f, 89), // dwsc9
249 QuantizationInfo(0.009658650495111942f, 99), // dwsc10
250 QuantizationInfo(0.005446993745863438f, 153), // dwsc11
251 QuantizationInfo(0.00817922968417406f, 130), // dwsc12
252 QuantizationInfo(0.018048152327537537f, 95) // dwsc13
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100253 };
254
255 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
Manuel Bottini32527952019-11-05 16:55:57 +0000256 get_input_accessor(common_params, nullptr, false))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "Conv2d_0_weights.npy"),
258 get_weights_accessor(data_path, "Conv2d_0_bias.npy"),
259 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), 1,
260 conv_weights_quant_info.at(0), conv_out_quant_info.at(0))
261 .set_name("Conv2d_0")
262 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
263 .set_name("Conv2d_0/Relu6");
264 graph << get_dwsc_node_qasymm(data_path, "Conv2d_1", 64U, PadStrideInfo(1U, 1U, 1U, 1U),
265 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(0),
266 point_weights_quant_info.at(0));
267 graph << get_dwsc_node_qasymm(
268 data_path, "Conv2d_2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
269 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(1), point_weights_quant_info.at(1));
270 graph << get_dwsc_node_qasymm(
271 data_path, "Conv2d_3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
272 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(2), point_weights_quant_info.at(2));
273 graph << get_dwsc_node_qasymm(
274 data_path, "Conv2d_4", 256U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
275 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(3), point_weights_quant_info.at(3));
276 graph << get_dwsc_node_qasymm(
277 data_path, "Conv2d_5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
278 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(4), point_weights_quant_info.at(4));
279 graph << get_dwsc_node_qasymm(
280 data_path, "Conv2d_6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
281 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(5), point_weights_quant_info.at(5));
282 graph << get_dwsc_node_qasymm(
283 data_path, "Conv2d_7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
284 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(6), point_weights_quant_info.at(6));
285 graph << get_dwsc_node_qasymm(
286 data_path, "Conv2d_8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
287 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(7), point_weights_quant_info.at(7));
288 graph << get_dwsc_node_qasymm(
289 data_path, "Conv2d_9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
290 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(8), point_weights_quant_info.at(8));
291 graph << get_dwsc_node_qasymm(
292 data_path, "Conv2d_10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
293 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(9), point_weights_quant_info.at(9));
294 graph << get_dwsc_node_qasymm(
295 data_path, "Conv2d_11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
296 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(10), point_weights_quant_info.at(10));
297 graph << get_dwsc_node_qasymm(
298 data_path, "Conv2d_12", 1024U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
299 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(11), point_weights_quant_info.at(11));
300 graph
301 << get_dwsc_node_qasymm(
302 data_path, "Conv2d_13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR),
303 PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(12), point_weights_quant_info.at(12))
304 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
305 << ConvolutionLayer(1U, 1U, 1001U, get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
306 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_bias.npy"),
307 PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1),
308 conv_out_quant_info.at(1))
309 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000310 }
311
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100312 ConcatLayer get_dwsc_node_float(const std::string &data_path,
313 std::string &&param_path,
314 unsigned int conv_filt,
315 PadStrideInfo dwc_pad_stride_info,
316 PadStrideInfo conv_pad_stride_info)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000317 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000318 std::string total_path = param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000319 SubStream sg(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000320 sg << DepthwiseConvolutionLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321 3U, 3U,
322 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
323 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), dwc_pad_stride_info)
324 .set_name(total_path + "depthwise/depthwise")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000325 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100326 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
327 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
328 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
329 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), 0.001f)
330 .set_name(total_path + "depthwise/BatchNorm")
331 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
332 .set_name(total_path + "depthwise/Relu6")
333 << ConvolutionLayer(1U, 1U, conv_filt,
334 get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW),
335 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), conv_pad_stride_info)
336 .set_name(total_path + "pointwise/Conv2D")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000337 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100338 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
339 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
340 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
341 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"), 0.001f)
342 .set_name(total_path + "pointwise/BatchNorm")
343 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
344 .set_name(total_path + "pointwise/Relu6");
Gian Marcobfa3b522017-12-12 10:08:38 +0000345
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100346 return ConcatLayer(std::move(sg));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000347 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100348
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100349 ConcatLayer get_dwsc_node_qasymm(const std::string &data_path,
350 std::string &&param_path,
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100351 const unsigned int conv_filt,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 PadStrideInfo dwc_pad_stride_info,
353 PadStrideInfo conv_pad_stride_info,
354 QuantizationInfo depth_weights_quant_info,
355 QuantizationInfo point_weights_quant_info)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100356 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100357 std::string total_path = param_path + "_";
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100358 SubStream sg(graph);
359
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 sg << DepthwiseConvolutionLayer(3U, 3U, get_weights_accessor(data_path, total_path + "depthwise_weights.npy"),
361 get_weights_accessor(data_path, total_path + "depthwise_bias.npy"),
362 dwc_pad_stride_info, 1, std::move(depth_weights_quant_info))
363 .set_name(total_path + "depthwise/depthwise")
364 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
365 .set_name(total_path + "depthwise/Relu6")
366 << ConvolutionLayer(1U, 1U, conv_filt, get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
367 get_weights_accessor(data_path, total_path + "pointwise_bias.npy"), conv_pad_stride_info,
368 1, std::move(point_weights_quant_info))
369 .set_name(total_path + "pointwise/Conv2D")
370 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
371 .set_name(total_path + "pointwise/Relu6");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100372
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100373 return ConcatLayer(std::move(sg));
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100374 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000375};
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000376
377/** Main program for MobileNetV1
378 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000379 * Model is based on:
380 * https://arxiv.org/abs/1704.04861
381 * "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
382 * Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam
383 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000384 * Provenance: download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz
385 * download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_0.75_160.tgz
386 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100387 * @note To list all the possible arguments execute the binary appended with the --help option
388 *
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000389 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100390 * @param[in] argv Arguments
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000391 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000392int main(int argc, char **argv)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000393{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000394 return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000395}