blob: 6218d47dd677196410cbc5fd431bcbcf31006897 [file] [log] [blame]
Pablo Tellofea8ec32018-11-16 13:25:30 +00001/*
SiCong Lia4a23542023-12-06 17:24:20 +00002 * Copyright (c) 2018-2023 Arm Limited.
Pablo Tellofea8ec32018-11-16 13:25:30 +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 */
SiCong Lia4a23542023-12-06 17:24:20 +000024#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Pablo Tellofea8ec32018-11-16 13:25:30 +000025#include "arm_compute/graph.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Pablo Tellofea8ec32018-11-16 13:25:30 +000027#include "support/ToolchainSupport.h"
28#include "utils/CommonGraphOptions.h"
29#include "utils/GraphUtils.h"
30#include "utils/Utils.h"
31
32using namespace arm_compute;
33using namespace arm_compute::utils;
34using namespace arm_compute::graph::frontend;
35using namespace arm_compute::graph_utils;
36
37/** Example demonstrating how to implement MobileNetSSD's network using the Compute Library's graph API */
38class GraphSSDMobilenetExample : public Example
39{
40public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041 GraphSSDMobilenetExample() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetSSD")
Pablo Tellofea8ec32018-11-16 13:25:30 +000042 {
Isabella Gottardi7234ed82018-11-27 08:51:10 +000043 // Add topk option
44 keep_topk_opt = cmd_parser.add_option<SimpleOption<int>>("topk", 100);
Isabella Gottardic755f782019-07-22 17:40:27 +010045 keep_topk_opt->set_help("Top k detections results per image. Used for data type F32.");
46 // Add output option
47 detection_boxes_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_boxes_opt", "");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 detection_boxes_opt->set_help("Filename containing the reference values for the graph output detection_boxes. "
49 "Used for data type QASYMM8.");
Isabella Gottardic755f782019-07-22 17:40:27 +010050 detection_classes_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_classes_opt", "");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 detection_classes_opt->set_help(
52 "Filename containing the reference values for the output detection_classes. Used for data type QASYMM8.");
Isabella Gottardic755f782019-07-22 17:40:27 +010053 detection_scores_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_scores_opt", "");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 detection_scores_opt->set_help(
55 "Filename containing the reference values for the output detection_scores. Used for data type QASYMM8.");
Isabella Gottardic755f782019-07-22 17:40:27 +010056 num_detections_opt = cmd_parser.add_option<SimpleOption<std::string>>("num_detections_opt", "");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 num_detections_opt->set_help(
58 "Filename containing the reference values for the output num_detections. Used with datatype QASYMM8.");
Pablo Tellofea8ec32018-11-16 13:25:30 +000059 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 GraphSSDMobilenetExample(const GraphSSDMobilenetExample &) = delete;
Pablo Tellofea8ec32018-11-16 13:25:30 +000061 GraphSSDMobilenetExample &operator=(const GraphSSDMobilenetExample &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000062 ~GraphSSDMobilenetExample() override = default;
Pablo Tellofea8ec32018-11-16 13:25:30 +000063 bool do_setup(int argc, char **argv) override
64 {
65 // Parse arguments
66 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010067 cmd_parser.validate();
Pablo Tellofea8ec32018-11-16 13:25:30 +000068
69 // Consume common parameters
70 common_params = consume_common_graph_parameters(common_opts);
71
72 // Return when help menu is requested
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if (common_params.help)
Pablo Tellofea8ec32018-11-16 13:25:30 +000074 {
75 cmd_parser.print_help(argv[0]);
76 return false;
77 }
78
79 // Print parameter values
80 std::cout << common_params << std::endl;
81
82 // Create input descriptor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 const TensorShape tensor_shape =
84 permute_shape(TensorShape(300, 300, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
85 TensorDescriptor input_descriptor =
86 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Pablo Tellofea8ec32018-11-16 13:25:30 +000087
88 // Set graph hints
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 graph << common_params.target << common_params.fast_math_hint;
Pablo Tellofea8ec32018-11-16 13:25:30 +000090
91 // Create core graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 if (arm_compute::is_data_type_float(common_params.data_type))
Isabella Gottardic755f782019-07-22 17:40:27 +010093 {
94 create_graph_float(input_descriptor);
95 }
96 else
97 {
98 create_graph_qasymm(input_descriptor);
99 }
Pablo Tellofea8ec32018-11-16 13:25:30 +0000100
Isabella Gottardic755f782019-07-22 17:40:27 +0100101 // Finalize graph
102 GraphConfig config;
103 config.num_threads = common_params.threads;
104 config.use_tuner = common_params.enable_tuner;
105 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000106 config.mlgo_file = common_params.mlgo_file;
Isabella Gottardic755f782019-07-22 17:40:27 +0100107
108 graph.finalize(common_params.target, config);
109
110 return true;
111 }
112 void do_run() override
113 {
114 // Run graph
115 graph.run();
116 }
117
118private:
119 CommandLineParser cmd_parser;
120 CommonGraphOptions common_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 SimpleOption<int> *keep_topk_opt{nullptr};
Isabella Gottardic755f782019-07-22 17:40:27 +0100122 CommonGraphParams common_params;
123 Stream graph;
124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 SimpleOption<std::string> *detection_boxes_opt{nullptr};
126 SimpleOption<std::string> *detection_classes_opt{nullptr};
127 SimpleOption<std::string> *detection_scores_opt{nullptr};
128 SimpleOption<std::string> *num_detections_opt{nullptr};
Isabella Gottardic755f782019-07-22 17:40:27 +0100129
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 ConcatLayer get_node_A_float(IStream &main_graph,
131 const std::string &data_path,
132 std::string &&param_path,
133 unsigned int conv_filt,
134 PadStrideInfo dwc_pad_stride_info,
135 PadStrideInfo conv_pad_stride_info)
Isabella Gottardic755f782019-07-22 17:40:27 +0100136 {
137 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100138 SubStream sg(main_graph);
Isabella Gottardic755f782019-07-22 17:40:27 +0100139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 sg << DepthwiseConvolutionLayer(3U, 3U, get_weights_accessor(data_path, total_path + "dw_w.npy"),
141 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
142 dwc_pad_stride_info)
143 .set_name(param_path + "/dw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100144 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "dw_bn_mean.npy"),
145 get_weights_accessor(data_path, total_path + "dw_bn_var.npy"),
146 get_weights_accessor(data_path, total_path + "dw_scale_w.npy"),
147 get_weights_accessor(data_path, total_path + "dw_scale_b.npy"), 0.00001f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 .set_name(param_path + "/dw/bn")
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 .set_name(param_path + "dw/relu")
Isabella Gottardic755f782019-07-22 17:40:27 +0100151
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100152 << ConvolutionLayer(1U, 1U, conv_filt, get_weights_accessor(data_path, total_path + "w.npy"),
153 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), conv_pad_stride_info)
154 .set_name(param_path + "/pw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100155 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "bn_mean.npy"),
156 get_weights_accessor(data_path, total_path + "bn_var.npy"),
157 get_weights_accessor(data_path, total_path + "scale_w.npy"),
158 get_weights_accessor(data_path, total_path + "scale_b.npy"), 0.00001f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 .set_name(param_path + "/pw/bn")
160 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
161 .set_name(param_path + "pw/relu");
Isabella Gottardic755f782019-07-22 17:40:27 +0100162
163 return ConcatLayer(std::move(sg));
164 }
165
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 ConcatLayer get_node_B_float(IStream &main_graph,
167 const std::string &data_path,
168 std::string &&param_path,
169 unsigned int conv_filt,
170 PadStrideInfo conv_pad_stride_info_1,
171 PadStrideInfo conv_pad_stride_info_2)
Isabella Gottardic755f782019-07-22 17:40:27 +0100172 {
173 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100174 SubStream sg(main_graph);
Isabella Gottardic755f782019-07-22 17:40:27 +0100175
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 sg << ConvolutionLayer(1, 1, conv_filt / 2, get_weights_accessor(data_path, total_path + "1_w.npy"),
177 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), conv_pad_stride_info_1)
178 .set_name(total_path + "1/conv")
Isabella Gottardic755f782019-07-22 17:40:27 +0100179 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "1_bn_mean.npy"),
180 get_weights_accessor(data_path, total_path + "1_bn_var.npy"),
181 get_weights_accessor(data_path, total_path + "1_scale_w.npy"),
182 get_weights_accessor(data_path, total_path + "1_scale_b.npy"), 0.00001f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 .set_name(total_path + "1/bn")
184 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
185 .set_name(total_path + "1/relu");
Isabella Gottardic755f782019-07-22 17:40:27 +0100186
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 sg << ConvolutionLayer(3, 3, conv_filt, get_weights_accessor(data_path, total_path + "2_w.npy"),
188 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), conv_pad_stride_info_2)
189 .set_name(total_path + "2/conv")
Isabella Gottardic755f782019-07-22 17:40:27 +0100190 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "2_bn_mean.npy"),
191 get_weights_accessor(data_path, total_path + "2_bn_var.npy"),
192 get_weights_accessor(data_path, total_path + "2_scale_w.npy"),
193 get_weights_accessor(data_path, total_path + "2_scale_b.npy"), 0.00001f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 .set_name(total_path + "2/bn")
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
196 .set_name(total_path + "2/relu");
Isabella Gottardic755f782019-07-22 17:40:27 +0100197
198 return ConcatLayer(std::move(sg));
199 }
200
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 ConcatLayer get_node_C_float(IStream &main_graph,
202 const std::string &data_path,
203 std::string &&param_path,
204 unsigned int conv_filt,
205 PadStrideInfo conv_pad_stride_info)
Isabella Gottardic755f782019-07-22 17:40:27 +0100206 {
207 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100208 SubStream sg(main_graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 sg << ConvolutionLayer(1U, 1U, conv_filt, get_weights_accessor(data_path, total_path + "w.npy"),
210 get_weights_accessor(data_path, total_path + "b.npy"), conv_pad_stride_info)
211 .set_name(param_path + "/conv");
212 if (common_params.data_layout == DataLayout::NCHW)
Isabella Gottardic755f782019-07-22 17:40:27 +0100213 {
214 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC).set_name(param_path + "/perm");
215 }
216 sg << FlattenLayer().set_name(param_path + "/flat");
217
218 return ConcatLayer(std::move(sg));
219 }
220
221 void create_graph_float(TensorDescriptor &input_descriptor)
222 {
Pablo Tellofea8ec32018-11-16 13:25:30 +0000223 // Create a preprocessor object
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 const std::array<float, 3> mean_rgb{{127.5f, 127.5f, 127.5f}};
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000225 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<CaffePreproccessor>(mean_rgb, true, 0.007843f);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000226
227 // Get trainable parameters data path
228 std::string data_path = common_params.data_path;
229
230 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100231 if (!data_path.empty())
Pablo Tellofea8ec32018-11-16 13:25:30 +0000232 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100233 data_path += "/cnn_data/ssd_mobilenet_model/";
Pablo Tellofea8ec32018-11-16 13:25:30 +0000234 }
235
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100236 graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000237
238 SubStream conv_11(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 conv_11 << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "conv0_w.npy"),
240 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
241 PadStrideInfo(2, 2, 1, 1))
242 .set_name("conv0");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000243 conv_11 << BatchNormalizationLayer(get_weights_accessor(data_path, "conv0_bn_mean.npy"),
244 get_weights_accessor(data_path, "conv0_bn_var.npy"),
245 get_weights_accessor(data_path, "conv0_scale_w.npy"),
246 get_weights_accessor(data_path, "conv0_scale_b.npy"), 0.00001f)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 .set_name("conv0/bn")
248 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
249 .set_name("conv0/relu");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000250
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100251 conv_11 << get_node_A_float(conv_11, data_path, "conv1", 64, PadStrideInfo(1, 1, 1, 1),
252 PadStrideInfo(1, 1, 0, 0));
253 conv_11 << get_node_A_float(conv_11, data_path, "conv2", 128, PadStrideInfo(2, 2, 1, 1),
254 PadStrideInfo(1, 1, 0, 0));
255 conv_11 << get_node_A_float(conv_11, data_path, "conv3", 128, PadStrideInfo(1, 1, 1, 1),
256 PadStrideInfo(1, 1, 0, 0));
257 conv_11 << get_node_A_float(conv_11, data_path, "conv4", 256, PadStrideInfo(2, 2, 1, 1),
258 PadStrideInfo(1, 1, 0, 0));
259 conv_11 << get_node_A_float(conv_11, data_path, "conv5", 256, PadStrideInfo(1, 1, 1, 1),
260 PadStrideInfo(1, 1, 0, 0));
261 conv_11 << get_node_A_float(conv_11, data_path, "conv6", 512, PadStrideInfo(2, 2, 1, 1),
262 PadStrideInfo(1, 1, 0, 0));
263 conv_11 << get_node_A_float(conv_11, data_path, "conv7", 512, PadStrideInfo(1, 1, 1, 1),
264 PadStrideInfo(1, 1, 0, 0));
265 conv_11 << get_node_A_float(conv_11, data_path, "conv8", 512, PadStrideInfo(1, 1, 1, 1),
266 PadStrideInfo(1, 1, 0, 0));
267 conv_11 << get_node_A_float(conv_11, data_path, "conv9", 512, PadStrideInfo(1, 1, 1, 1),
268 PadStrideInfo(1, 1, 0, 0));
269 conv_11 << get_node_A_float(conv_11, data_path, "conv10", 512, PadStrideInfo(1, 1, 1, 1),
270 PadStrideInfo(1, 1, 0, 0));
271 conv_11 << get_node_A_float(conv_11, data_path, "conv11", 512, PadStrideInfo(1, 1, 1, 1),
272 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000273
274 SubStream conv_13(conv_11);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100275 conv_13 << get_node_A_float(conv_11, data_path, "conv12", 1024, PadStrideInfo(2, 2, 1, 1),
276 PadStrideInfo(1, 1, 0, 0));
277 conv_13 << get_node_A_float(conv_13, data_path, "conv13", 1024, PadStrideInfo(1, 1, 1, 1),
278 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000279
280 SubStream conv_14(conv_13);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100281 conv_14 << get_node_B_float(conv_13, data_path, "conv14", 512, PadStrideInfo(1, 1, 0, 0),
282 PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000283
284 SubStream conv_15(conv_14);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100285 conv_15 << get_node_B_float(conv_14, data_path, "conv15", 256, PadStrideInfo(1, 1, 0, 0),
286 PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000287
288 SubStream conv_16(conv_15);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100289 conv_16 << get_node_B_float(conv_15, data_path, "conv16", 256, PadStrideInfo(1, 1, 0, 0),
290 PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000291
292 SubStream conv_17(conv_16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100293 conv_17 << get_node_B_float(conv_16, data_path, "conv17", 128, PadStrideInfo(1, 1, 0, 0),
294 PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000295
296 //mbox_loc
297 SubStream conv_11_mbox_loc(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100298 conv_11_mbox_loc << get_node_C_float(conv_11, data_path, "conv11_mbox_loc", 12, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000299
300 SubStream conv_13_mbox_loc(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100301 conv_13_mbox_loc << get_node_C_float(conv_13, data_path, "conv13_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000302
303 SubStream conv_14_2_mbox_loc(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100304 conv_14_2_mbox_loc << get_node_C_float(conv_14, data_path, "conv14_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000305
306 SubStream conv_15_2_mbox_loc(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100307 conv_15_2_mbox_loc << get_node_C_float(conv_15, data_path, "conv15_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000308
309 SubStream conv_16_2_mbox_loc(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100310 conv_16_2_mbox_loc << get_node_C_float(conv_16, data_path, "conv16_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000311
312 SubStream conv_17_2_mbox_loc(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100313 conv_17_2_mbox_loc << get_node_C_float(conv_17, data_path, "conv17_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000314
315 SubStream mbox_loc(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100316 mbox_loc << ConcatLayer(std::move(conv_11_mbox_loc), std::move(conv_13_mbox_loc), conv_14_2_mbox_loc,
317 std::move(conv_15_2_mbox_loc), std::move(conv_16_2_mbox_loc),
318 std::move(conv_17_2_mbox_loc));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000319
Pablo Tellofea8ec32018-11-16 13:25:30 +0000320 //mbox_conf
321 SubStream conv_11_mbox_conf(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100322 conv_11_mbox_conf << get_node_C_float(conv_11, data_path, "conv11_mbox_conf", 63, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000323
324 SubStream conv_13_mbox_conf(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100325 conv_13_mbox_conf << get_node_C_float(conv_13, data_path, "conv13_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000326
327 SubStream conv_14_2_mbox_conf(conv_14);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100328 conv_14_2_mbox_conf << get_node_C_float(conv_14, data_path, "conv14_2_mbox_conf", 126,
329 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000330
331 SubStream conv_15_2_mbox_conf(conv_15);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100332 conv_15_2_mbox_conf << get_node_C_float(conv_15, data_path, "conv15_2_mbox_conf", 126,
333 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000334
335 SubStream conv_16_2_mbox_conf(conv_16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100336 conv_16_2_mbox_conf << get_node_C_float(conv_16, data_path, "conv16_2_mbox_conf", 126,
337 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000338
339 SubStream conv_17_2_mbox_conf(conv_17);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100340 conv_17_2_mbox_conf << get_node_C_float(conv_17, data_path, "conv17_2_mbox_conf", 126,
341 PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000342
343 SubStream mbox_conf(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100344 mbox_conf << ConcatLayer(std::move(conv_11_mbox_conf), std::move(conv_13_mbox_conf),
345 std::move(conv_14_2_mbox_conf), std::move(conv_15_2_mbox_conf),
346 std::move(conv_16_2_mbox_conf), std::move(conv_17_2_mbox_conf));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000347 mbox_conf << ReshapeLayer(TensorShape(21U, 1917U)).set_name("mbox_conf/reshape");
348 mbox_conf << SoftmaxLayer().set_name("mbox_conf/softmax");
349 mbox_conf << FlattenLayer().set_name("mbox_conf/flat");
350
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100351 const std::vector<float> priorbox_variances = {0.1f, 0.1f, 0.2f, 0.2f};
Pablo Tellofea8ec32018-11-16 13:25:30 +0000352 const float priorbox_offset = 0.5f;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100353 const std::vector<float> priorbox_aspect_ratios = {2.f, 3.f};
Pablo Tellofea8ec32018-11-16 13:25:30 +0000354
355 //mbox_priorbox branch
356 SubStream conv_11_mbox_priorbox(conv_11);
357
358 conv_11_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100359 PriorBoxLayerInfo({60.f}, priorbox_variances, priorbox_offset, true,
360 false, {}, {2.f}))
361 .set_name("conv11/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000362
363 SubStream conv_13_mbox_priorbox(conv_13);
364 conv_13_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365 PriorBoxLayerInfo({105.f}, priorbox_variances, priorbox_offset, true,
366 false, {150.f}, priorbox_aspect_ratios))
367 .set_name("conv13/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000368
369 SubStream conv_14_2_mbox_priorbox(conv_14);
370 conv_14_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100371 PriorBoxLayerInfo({150.f}, priorbox_variances, priorbox_offset, true,
372 false, {195.f}, priorbox_aspect_ratios))
373 .set_name("conv14/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000374
375 SubStream conv_15_2_mbox_priorbox(conv_15);
376 conv_15_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100377 PriorBoxLayerInfo({195.f}, priorbox_variances, priorbox_offset, true,
378 false, {240.f}, priorbox_aspect_ratios))
379 .set_name("conv15/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000380
381 SubStream conv_16_2_mbox_priorbox(conv_16);
382 conv_16_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100383 PriorBoxLayerInfo({240.f}, priorbox_variances, priorbox_offset, true,
384 false, {285.f}, priorbox_aspect_ratios))
385 .set_name("conv16/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000386
387 SubStream conv_17_2_mbox_priorbox(conv_17);
388 conv_17_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100389 PriorBoxLayerInfo({285.f}, priorbox_variances, priorbox_offset, true,
390 false, {300.f}, priorbox_aspect_ratios))
391 .set_name("conv17/priorbox");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000392
393 SubStream mbox_priorbox(graph);
394
395 mbox_priorbox << ConcatLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100396 (common_params.data_layout == DataLayout::NCHW)
397 ? arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH)
398 : arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::CHANNEL),
399 std::move(conv_11_mbox_priorbox), std::move(conv_13_mbox_priorbox), std::move(conv_14_2_mbox_priorbox),
400 std::move(conv_15_2_mbox_priorbox), std::move(conv_16_2_mbox_priorbox), std::move(conv_17_2_mbox_priorbox));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000401
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000402 const int num_classes = 21;
403 const bool share_location = true;
404 const DetectionOutputLayerCodeType detection_type = DetectionOutputLayerCodeType::CENTER_SIZE;
405 const int keep_top_k = keep_topk_opt->value();
406 const float nms_threshold = 0.45f;
407 const int label_id_background = 0;
408 const float conf_thrs = 0.25f;
409 const int top_k = 100;
410
411 SubStream detection_ouput(mbox_loc);
412 detection_ouput << DetectionOutputLayer(std::move(mbox_conf), std::move(mbox_priorbox),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100413 DetectionOutputLayerInfo(num_classes, share_location, detection_type,
414 keep_top_k, nms_threshold, top_k,
415 label_id_background, conf_thrs));
416 detection_ouput << OutputLayer(get_detection_output_accessor(common_params, {input_descriptor.shape}));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000417 }
418
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100419 ConcatLayer get_node_A_qasymm(IStream &main_graph,
420 const std::string &data_path,
421 std::string &&param_path,
422 unsigned int conv_filt,
423 PadStrideInfo dwc_pad_stride_info,
424 PadStrideInfo conv_pad_stride_info,
425 std::pair<QuantizationInfo, QuantizationInfo> depth_quant_info,
426 std::pair<QuantizationInfo, QuantizationInfo> point_quant_info)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000427 {
428 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100429 SubStream sg(main_graph);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000430
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100431 sg << DepthwiseConvolutionLayer(3U, 3U, get_weights_accessor(data_path, total_path + "dw_w.npy"),
432 get_weights_accessor(data_path, total_path + "dw_b.npy"), dwc_pad_stride_info,
433 1, depth_quant_info.first, depth_quant_info.second)
434 .set_name(param_path + "/dw")
435 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
436 .set_name(param_path + "/dw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000437
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100438 sg << ConvolutionLayer(1U, 1U, conv_filt, get_weights_accessor(data_path, total_path + "w.npy"),
439 get_weights_accessor(data_path, total_path + "b.npy"), conv_pad_stride_info, 1,
440 point_quant_info.first, point_quant_info.second)
441 .set_name(param_path + "/pw")
442 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
443 .set_name(param_path + "/pw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000444
445 return ConcatLayer(std::move(sg));
446 }
447
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100448 ConcatLayer get_node_B_qasymm(IStream &main_graph,
449 const std::string &data_path,
450 std::string &&param_path,
451 unsigned int conv_filt,
452 PadStrideInfo conv_pad_stride_info_1x1,
453 PadStrideInfo conv_pad_stride_info_3x3,
454 const std::pair<QuantizationInfo, QuantizationInfo> quant_info_1x1,
455 const std::pair<QuantizationInfo, QuantizationInfo> quant_info_3x3)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000456 {
457 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100458 SubStream sg(main_graph);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000459
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100460 sg << ConvolutionLayer(1, 1, conv_filt / 2, get_weights_accessor(data_path, total_path + "1x1_w.npy"),
461 get_weights_accessor(data_path, total_path + "1x1_b.npy"), conv_pad_stride_info_1x1, 1,
462 quant_info_1x1.first, quant_info_1x1.second)
463 .set_name(total_path + "1x1/conv")
464 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
465 .set_name(total_path + "1x1/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000466
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467 sg << ConvolutionLayer(3, 3, conv_filt, get_weights_accessor(data_path, total_path + "3x3_w.npy"),
468 get_weights_accessor(data_path, total_path + "3x3_b.npy"), conv_pad_stride_info_3x3, 1,
469 quant_info_3x3.first, quant_info_3x3.second)
470 .set_name(total_path + "3x3/conv")
471 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
472 .set_name(total_path + "3x3/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000473
474 return ConcatLayer(std::move(sg));
475 }
476
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100477 ConcatLayer get_node_C_qasymm(IStream &main_graph,
478 const std::string &data_path,
479 std::string &&param_path,
480 unsigned int conv_filt,
481 PadStrideInfo conv_pad_stride_info,
482 const std::pair<QuantizationInfo, QuantizationInfo> quant_info,
483 TensorShape reshape_shape)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000484 {
485 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100486 SubStream sg(main_graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100487 sg << ConvolutionLayer(1U, 1U, conv_filt, get_weights_accessor(data_path, total_path + "w.npy"),
488 get_weights_accessor(data_path, total_path + "b.npy"), conv_pad_stride_info, 1,
489 quant_info.first, quant_info.second)
490 .set_name(param_path + "/conv");
491 if (common_params.data_layout == DataLayout::NCHW)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000492 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100493 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000494 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100495 sg << ReshapeLayer(reshape_shape).set_name(param_path + "/reshape");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000496
497 return ConcatLayer(std::move(sg));
498 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100499
500 void create_graph_qasymm(TensorDescriptor &input_descriptor)
501 {
502 // Get trainable parameters data path
503 std::string data_path = common_params.data_path;
504
505 // Add model path to data path
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100506 if (!data_path.empty())
Isabella Gottardic755f782019-07-22 17:40:27 +0100507 {
508 data_path += "/cnn_data/ssd_mobilenet_qasymm8_model/";
509 }
510
511 // Quantization info are saved as pair for each (pointwise/depthwise) convolution layer: <weight_quant_info, output_quant_info>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100512 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> conv_quant_info = {
513 {QuantizationInfo(0.03624850884079933f, 163), QuantizationInfo(0.22219789028167725f, 113)}, // conv0
514 {QuantizationInfo(0.0028752065263688564f, 113),
515 QuantizationInfo(0.05433657020330429f, 128)}, // conv13_2_1_1
516 {QuantizationInfo(0.0014862528769299388f, 125),
517 QuantizationInfo(0.05037643015384674f, 131)}, // conv13_2_3_3
518 {QuantizationInfo(0.00233650766313076f, 113), QuantizationInfo(0.04468846693634987f, 126)}, // conv13_3_1_1
519 {QuantizationInfo(0.002501056529581547f, 120), QuantizationInfo(0.06026708707213402f, 111)}, // conv13_3_3_3
520 {QuantizationInfo(0.002896666992455721f, 121),
521 QuantizationInfo(0.037775348871946335f, 117)}, // conv13_4_1_1
522 {QuantizationInfo(0.0023875406477600336f, 122),
523 QuantizationInfo(0.03881589323282242f, 108)}, // conv13_4_3_3
524 {QuantizationInfo(0.0022081052884459496f, 77),
525 QuantizationInfo(0.025450613349676132f, 125)}, // conv13_5_1_1
526 {QuantizationInfo(0.00604657270014286f, 121), QuantizationInfo(0.033533502370119095f, 109)} // conv13_5_3_3
Isabella Gottardic755f782019-07-22 17:40:27 +0100527 };
528
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100529 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> depth_quant_info = {
530 {QuantizationInfo(0.03408717364072f, 131), QuantizationInfo(0.29286590218544006f, 108)}, // dwsc1
531 {QuantizationInfo(0.027518004179000854f, 107), QuantizationInfo(0.20796941220760345, 117)}, // dwsc2
532 {QuantizationInfo(0.052489638328552246f, 85), QuantizationInfo(0.4303881824016571f, 142)}, // dwsc3
533 {QuantizationInfo(0.016570359468460083f, 79), QuantizationInfo(0.10512150079011917f, 116)}, // dwsc4
534 {QuantizationInfo(0.060739465057849884f, 65), QuantizationInfo(0.15331414341926575f, 94)}, // dwsc5
535 {QuantizationInfo(0.01324534136801958f, 124), QuantizationInfo(0.13010895252227783f, 153)}, // dwsc6
536 {QuantizationInfo(0.032326459884643555f, 124), QuantizationInfo(0.11565316468477249, 156)}, // dwsc7
537 {QuantizationInfo(0.029948478564620018f, 155), QuantizationInfo(0.11413891613483429f, 146)}, // dwsc8
538 {QuantizationInfo(0.028054025024175644f, 129), QuantizationInfo(0.1142905130982399f, 140)}, // dwsc9
539 {QuantizationInfo(0.025204822421073914f, 129), QuantizationInfo(0.14668069779872894f, 149)}, // dwsc10
540 {QuantizationInfo(0.019332280382514f, 110), QuantizationInfo(0.1480235457420349f, 91)}, // dwsc11
541 {QuantizationInfo(0.0319712869822979f, 88), QuantizationInfo(0.10424695909023285f, 117)}, // dwsc12
542 {QuantizationInfo(0.04378943517804146f, 164), QuantizationInfo(0.23176774382591248f, 138)} // dwsc13
Isabella Gottardic755f782019-07-22 17:40:27 +0100543 };
544
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100545 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> point_quant_info = {
546 {QuantizationInfo(0.028777318075299263f, 144), QuantizationInfo(0.2663874328136444f, 121)}, // pw1
547 {QuantizationInfo(0.015796702355146408f, 127), QuantizationInfo(0.1739964485168457f, 111)}, // pw2
548 {QuantizationInfo(0.009349990636110306f, 127), QuantizationInfo(0.1805974692106247f, 104)}, // pw3
549 {QuantizationInfo(0.012920888140797615f, 106), QuantizationInfo(0.1205204650759697f, 100)}, // pw4
550 {QuantizationInfo(0.008119508624076843f, 145), QuantizationInfo(0.12272439152002335f, 97)}, // pw5
551 {QuantizationInfo(0.0070041813887655735f, 115), QuantizationInfo(0.0947074219584465f, 101)}, // pw6
552 {QuantizationInfo(0.004827278666198254f, 115), QuantizationInfo(0.0842885747551918f, 110)}, // pw7
553 {QuantizationInfo(0.004755120258778334f, 128), QuantizationInfo(0.08283159881830215f, 116)}, // pw8
554 {QuantizationInfo(0.007527193054556847f, 142), QuantizationInfo(0.12555131316184998f, 137)}, // pw9
555 {QuantizationInfo(0.006050156895071268f, 109), QuantizationInfo(0.10871313512325287f, 124)}, // pw10
556 {QuantizationInfo(0.00490700313821435f, 127), QuantizationInfo(0.10364262014627457f, 140)}, // pw11
557 {QuantizationInfo(0.006063731852918863, 124), QuantizationInfo(0.11241862177848816f, 125)}, // pw12
558 {QuantizationInfo(0.007901716977357864f, 139), QuantizationInfo(0.49889302253723145f, 141)} // pw13
Isabella Gottardic755f782019-07-22 17:40:27 +0100559 };
560
561 // Quantization info taken from the TfLite SSD MobileNet example
562 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Isabella Gottardic755f782019-07-22 17:40:27 +0100563 // Create core graph
564 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
565 get_weights_accessor(data_path, common_params.image, DataLayout::NHWC));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100566 graph << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "conv0_w.npy"),
567 get_weights_accessor(data_path, "conv0_b.npy"),
568 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), 1,
569 conv_quant_info.at(0).first, conv_quant_info.at(0).second)
570 .set_name("conv0");
571 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
572 .set_name("conv0/relu");
573 graph << get_node_A_qasymm(graph, data_path, "conv1", 64U,
574 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
575 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(0), point_quant_info.at(0));
576 graph << get_node_A_qasymm(graph, data_path, "conv2", 128U,
577 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL),
578 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(1), point_quant_info.at(1));
579 graph << get_node_A_qasymm(graph, data_path, "conv3", 128U,
580 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
581 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(2), point_quant_info.at(2));
582 graph << get_node_A_qasymm(graph, data_path, "conv4", 256U,
583 PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
584 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(3), point_quant_info.at(3));
585 graph << get_node_A_qasymm(graph, data_path, "conv5", 256U,
586 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
587 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(4), point_quant_info.at(4));
588 graph << get_node_A_qasymm(graph, data_path, "conv6", 512U,
589 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL),
590 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(5), point_quant_info.at(5));
591 graph << get_node_A_qasymm(graph, data_path, "conv7", 512U,
592 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
593 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(6), point_quant_info.at(6));
594 graph << get_node_A_qasymm(graph, data_path, "conv8", 512U,
595 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
596 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(7), point_quant_info.at(7));
597 graph << get_node_A_qasymm(graph, data_path, "conv9", 512U,
598 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
599 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(8), point_quant_info.at(8));
600 graph << get_node_A_qasymm(graph, data_path, "conv10", 512U,
601 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
602 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(9), point_quant_info.at(9));
603 graph << get_node_A_qasymm(graph, data_path, "conv11", 512U,
604 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
605 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(10), point_quant_info.at(10));
Isabella Gottardic755f782019-07-22 17:40:27 +0100606
607 SubStream conv_13(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100608 conv_13 << get_node_A_qasymm(graph, data_path, "conv12", 1024U,
609 PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
610 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(11), point_quant_info.at(11));
611 conv_13 << get_node_A_qasymm(conv_13, data_path, "conv13", 1024U,
612 PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
613 PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(12), point_quant_info.at(12));
Isabella Gottardic755f782019-07-22 17:40:27 +0100614 SubStream conv_14(conv_13);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100615 conv_14 << get_node_B_qasymm(conv_13, data_path, "conv13_2", 512U, PadStrideInfo(1U, 1U, 0U, 0U),
616 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL),
617 conv_quant_info.at(1), conv_quant_info.at(2));
Isabella Gottardic755f782019-07-22 17:40:27 +0100618 SubStream conv_15(conv_14);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100619 conv_15 << get_node_B_qasymm(conv_14, data_path, "conv13_3", 256U, PadStrideInfo(1U, 1U, 0U, 0U),
620 PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
621 conv_quant_info.at(3), conv_quant_info.at(4));
Isabella Gottardic755f782019-07-22 17:40:27 +0100622 SubStream conv_16(conv_15);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100623 conv_16 << get_node_B_qasymm(conv_15, data_path, "conv13_4", 256U, PadStrideInfo(1U, 1U, 0U, 0U),
624 PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL),
625 conv_quant_info.at(5), conv_quant_info.at(6));
Isabella Gottardic755f782019-07-22 17:40:27 +0100626 SubStream conv_17(conv_16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100627 conv_17 << get_node_B_qasymm(conv_16, data_path, "conv13_5", 128U, PadStrideInfo(1U, 1U, 0U, 0U),
628 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL),
629 conv_quant_info.at(7), conv_quant_info.at(8));
Isabella Gottardic755f782019-07-22 17:40:27 +0100630
631 // box_predictor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100632 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> box_enc_pred_quant_info = {
633 {QuantizationInfo(0.005202020984143019f, 136),
634 QuantizationInfo(0.08655580133199692f, 183)}, // boxpredictor0_bep
635 {QuantizationInfo(0.003121797926723957f, 132),
636 QuantizationInfo(0.03218776360154152f, 140)}, // boxpredictor1_bep
637 {QuantizationInfo(0.002995674265548587f, 130),
638 QuantizationInfo(0.029072262346744537f, 125)}, // boxpredictor2_bep
639 {QuantizationInfo(0.0023131705820560455f, 130),
640 QuantizationInfo(0.026488754898309708f, 127)}, // boxpredictor3_bep
641 {QuantizationInfo(0.0013905081432312727f, 132),
642 QuantizationInfo(0.0199890099465847f, 137)}, // boxpredictor4_bep
643 {QuantizationInfo(0.00216794665902853f, 121),
644 QuantizationInfo(0.019798893481492996f, 151)} // boxpredictor5_bep
Isabella Gottardic755f782019-07-22 17:40:27 +0100645 };
646
647 const std::vector<TensorShape> box_reshape = // NHWC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100648 {
649 TensorShape(4U, 1U, 1083U), // boxpredictor0_bep_reshape
650 TensorShape(4U, 1U, 600U), // boxpredictor1_bep_reshape
651 TensorShape(4U, 1U, 150U), // boxpredictor2_bep_reshape
652 TensorShape(4U, 1U, 54U), // boxpredictor3_bep_reshape
653 TensorShape(4U, 1U, 24U), // boxpredictor4_bep_reshape
654 TensorShape(4U, 1U, 6U) // boxpredictor5_bep_reshape
655 };
Isabella Gottardic755f782019-07-22 17:40:27 +0100656
657 SubStream conv_11_box_enc_pre(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100658 conv_11_box_enc_pre << get_node_C_qasymm(graph, data_path, "BoxPredictor_0_BEP", 12U,
659 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(0),
660 box_reshape.at(0));
Isabella Gottardic755f782019-07-22 17:40:27 +0100661
662 SubStream conv_13_box_enc_pre(conv_13);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100663 conv_13_box_enc_pre << get_node_C_qasymm(conv_13, data_path, "BoxPredictor_1_BEP", 24U,
664 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(1),
665 box_reshape.at(1));
Isabella Gottardic755f782019-07-22 17:40:27 +0100666
667 SubStream conv_14_2_box_enc_pre(conv_14);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100668 conv_14_2_box_enc_pre << get_node_C_qasymm(conv_14, data_path, "BoxPredictor_2_BEP", 24U,
669 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(2),
670 box_reshape.at(2));
Isabella Gottardic755f782019-07-22 17:40:27 +0100671
672 SubStream conv_15_2_box_enc_pre(conv_15);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100673 conv_15_2_box_enc_pre << get_node_C_qasymm(conv_15, data_path, "BoxPredictor_3_BEP", 24U,
674 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(3),
675 box_reshape.at(3));
Isabella Gottardic755f782019-07-22 17:40:27 +0100676
677 SubStream conv_16_2_box_enc_pre(conv_16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100678 conv_16_2_box_enc_pre << get_node_C_qasymm(conv_16, data_path, "BoxPredictor_4_BEP", 24U,
679 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(4),
680 box_reshape.at(4));
Isabella Gottardic755f782019-07-22 17:40:27 +0100681
682 SubStream conv_17_2_box_enc_pre(conv_17);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100683 conv_17_2_box_enc_pre << get_node_C_qasymm(conv_17, data_path, "BoxPredictor_5_BEP", 24U,
684 PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(5),
685 box_reshape.at(5));
Isabella Gottardic755f782019-07-22 17:40:27 +0100686
687 SubStream box_enc_pre(graph);
688 const QuantizationInfo bep_concate_qinfo = QuantizationInfo(0.08655580133199692f, 183);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100689 box_enc_pre << ConcatLayer(arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::HEIGHT,
690 bep_concate_qinfo),
691 std::move(conv_11_box_enc_pre), std::move(conv_13_box_enc_pre),
692 conv_14_2_box_enc_pre, std::move(conv_15_2_box_enc_pre),
Isabella Gottardic755f782019-07-22 17:40:27 +0100693 std::move(conv_16_2_box_enc_pre), std::move(conv_17_2_box_enc_pre))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100694 .set_name("BoxPredictor/concat");
Isabella Gottardic755f782019-07-22 17:40:27 +0100695 box_enc_pre << ReshapeLayer(TensorShape(4U, 1917U)).set_name("BoxPredictor/reshape");
696
697 // class_predictor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100698 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> class_pred_quant_info = {
699 {QuantizationInfo(0.002744135679677129f, 125),
700 QuantizationInfo(0.05746262148022652f, 234)}, // boxpredictor0_cp
701 {QuantizationInfo(0.0024326108396053314f, 80),
702 QuantizationInfo(0.03764628246426582f, 217)}, // boxpredictor1_cp
703 {QuantizationInfo(0.0013898586621508002f, 141),
704 QuantizationInfo(0.034081317484378815f, 214)}, // boxpredictor2_cp
705 {QuantizationInfo(0.0014176908880472183f, 133),
706 QuantizationInfo(0.033889178186655045f, 215)}, // boxpredictor3_cp
707 {QuantizationInfo(0.001090311910957098f, 125),
708 QuantizationInfo(0.02646234817802906f, 230)}, // boxpredictor4_cp
709 {QuantizationInfo(0.001134163816459477f, 115),
710 QuantizationInfo(0.026926767081022263f, 218)} // boxpredictor5_cp
Isabella Gottardic755f782019-07-22 17:40:27 +0100711 };
712
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100713 const std::vector<TensorShape> class_reshape = {
Isabella Gottardic755f782019-07-22 17:40:27 +0100714 TensorShape(91U, 1083U), // boxpredictor0_cp_reshape
715 TensorShape(91U, 600U), // boxpredictor1_cp_reshape
716 TensorShape(91U, 150U), // boxpredictor2_cp_reshape
717 TensorShape(91U, 54U), // boxpredictor3_cp_reshape
718 TensorShape(91U, 24U), // boxpredictor4_cp_reshape
719 TensorShape(91U, 6U) // boxpredictor5_cp_reshape
720 };
721
722 SubStream conv_11_class_pre(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100723 conv_11_class_pre << get_node_C_qasymm(graph, data_path, "BoxPredictor_0_CP", 273U,
724 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(0),
725 class_reshape.at(0));
Isabella Gottardic755f782019-07-22 17:40:27 +0100726
727 SubStream conv_13_class_pre(conv_13);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100728 conv_13_class_pre << get_node_C_qasymm(conv_13, data_path, "BoxPredictor_1_CP", 546U,
729 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(1),
730 class_reshape.at(1));
Isabella Gottardic755f782019-07-22 17:40:27 +0100731
732 SubStream conv_14_2_class_pre(conv_14);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100733 conv_14_2_class_pre << get_node_C_qasymm(conv_14, data_path, "BoxPredictor_2_CP", 546U,
734 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(2),
735 class_reshape.at(2));
Isabella Gottardic755f782019-07-22 17:40:27 +0100736
737 SubStream conv_15_2_class_pre(conv_15);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100738 conv_15_2_class_pre << get_node_C_qasymm(conv_15, data_path, "BoxPredictor_3_CP", 546U,
739 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(3),
740 class_reshape.at(3));
Isabella Gottardic755f782019-07-22 17:40:27 +0100741
742 SubStream conv_16_2_class_pre(conv_16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100743 conv_16_2_class_pre << get_node_C_qasymm(conv_16, data_path, "BoxPredictor_4_CP", 546U,
744 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(4),
745 class_reshape.at(4));
Isabella Gottardic755f782019-07-22 17:40:27 +0100746
747 SubStream conv_17_2_class_pre(conv_17);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100748 conv_17_2_class_pre << get_node_C_qasymm(conv_17, data_path, "BoxPredictor_5_CP", 546U,
749 PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(5),
750 class_reshape.at(5));
Isabella Gottardic755f782019-07-22 17:40:27 +0100751
752 const QuantizationInfo cp_concate_qinfo = QuantizationInfo(0.0584389753639698f, 230);
753 SubStream class_pred(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100754 class_pred << ConcatLayer(arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH,
755 cp_concate_qinfo),
756 std::move(conv_11_class_pre), std::move(conv_13_class_pre),
757 std::move(conv_14_2_class_pre), std::move(conv_15_2_class_pre),
758 std::move(conv_16_2_class_pre), std::move(conv_17_2_class_pre))
759 .set_name("ClassPrediction/concat");
Isabella Gottardic755f782019-07-22 17:40:27 +0100760
SiCong Lia4a23542023-12-06 17:24:20 +0000761 const QuantizationInfo logistic_out_qinfo = QuantizationInfo(
762 0.00390625f, quantization::get_min_max_values_from_quantized_data_type(common_params.data_type).first);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100763 class_pred << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC),
764 logistic_out_qinfo)
765 .set_name("ClassPrediction/logistic");
Isabella Gottardic755f782019-07-22 17:40:27 +0100766
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100767 const int max_detections = 10;
768 const int max_classes_per_detection = 1;
769 const float nms_score_threshold = 0.30000001192092896f;
770 const float nms_iou_threshold = 0.6000000238418579f;
771 const int num_classes = 90;
772 const float x_scale = 10.f;
773 const float y_scale = 10.f;
774 const float h_scale = 5.f;
775 const float w_scale = 5.f;
776 std::array<float, 4> scales = {y_scale, x_scale, w_scale, h_scale};
777 const QuantizationInfo anchors_qinfo = QuantizationInfo(0.006453060545027256f, 0);
Isabella Gottardic755f782019-07-22 17:40:27 +0100778
779 SubStream detection_ouput(box_enc_pre);
780 detection_ouput << DetectionPostProcessLayer(std::move(class_pred),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100781 DetectionPostProcessLayerInfo(
782 max_detections, max_classes_per_detection, nms_score_threshold,
783 nms_iou_threshold, num_classes, scales),
Isabella Gottardic755f782019-07-22 17:40:27 +0100784 get_weights_accessor(data_path, "anchors.npy"), anchors_qinfo)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100785 .set_name("DetectionPostProcess");
Isabella Gottardic755f782019-07-22 17:40:27 +0100786
787 SubStream ouput_0(detection_ouput);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100788 ouput_0 << OutputLayer(
789 get_npy_output_accessor(detection_boxes_opt->value(), TensorShape(4U, 10U), DataType::F32), 0);
Isabella Gottardic755f782019-07-22 17:40:27 +0100790
791 SubStream ouput_1(detection_ouput);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100792 ouput_1 << OutputLayer(get_npy_output_accessor(detection_classes_opt->value(), TensorShape(10U), DataType::F32),
793 1);
Isabella Gottardic755f782019-07-22 17:40:27 +0100794
795 SubStream ouput_2(detection_ouput);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100796 ouput_2 << OutputLayer(get_npy_output_accessor(detection_scores_opt->value(), TensorShape(10U), DataType::F32),
797 2);
Isabella Gottardic755f782019-07-22 17:40:27 +0100798
799 SubStream ouput_3(detection_ouput);
800 ouput_3 << OutputLayer(get_npy_output_accessor(num_detections_opt->value(), TensorShape(1U), DataType::F32), 3);
801 }
Pablo Tellofea8ec32018-11-16 13:25:30 +0000802};
803
804/** Main program for MobileNetSSD
805 *
806 * Model is based on:
807 * http://arxiv.org/abs/1512.02325
808 * SSD: Single Shot MultiBox Detector
809 * Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg
810 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000811 * Provenance: https://github.com/chuanqi305/MobileNet-SSD
812 *
Pablo Tellofea8ec32018-11-16 13:25:30 +0000813 * @note To list all the possible arguments execute the binary appended with the --help option
814 *
815 * @param[in] argc Number of arguments
816 * @param[in] argv Arguments
817 */
818int main(int argc, char **argv)
819{
820 return arm_compute::utils::run_example<GraphSSDMobilenetExample>(argc, argv);
821}