blob: 9fe7f5b454a6138f8594b9f4ed10ff55bdf26791 [file] [log] [blame]
Pablo Tellofea8ec32018-11-16 13:25:30 +00001/*
ramelg01b4cd2dc2022-03-30 18:42:23 +01002 * Copyright (c) 2018-2022 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 */
24#include "arm_compute/graph.h"
25#include "support/ToolchainSupport.h"
26#include "utils/CommonGraphOptions.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30using namespace arm_compute;
31using namespace arm_compute::utils;
32using namespace arm_compute::graph::frontend;
33using namespace arm_compute::graph_utils;
34
35/** Example demonstrating how to implement MobileNetSSD's network using the Compute Library's graph API */
36class GraphSSDMobilenetExample : public Example
37{
38public:
39 GraphSSDMobilenetExample()
40 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetSSD")
41 {
Isabella Gottardi7234ed82018-11-27 08:51:10 +000042 // Add topk option
43 keep_topk_opt = cmd_parser.add_option<SimpleOption<int>>("topk", 100);
Isabella Gottardic755f782019-07-22 17:40:27 +010044 keep_topk_opt->set_help("Top k detections results per image. Used for data type F32.");
45 // Add output option
46 detection_boxes_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_boxes_opt", "");
47 detection_boxes_opt->set_help("Filename containing the reference values for the graph output detection_boxes. Used for data type QASYMM8.");
48 detection_classes_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_classes_opt", "");
49 detection_classes_opt->set_help("Filename containing the reference values for the output detection_classes. Used for data type QASYMM8.");
50 detection_scores_opt = cmd_parser.add_option<SimpleOption<std::string>>("detection_scores_opt", "");
51 detection_scores_opt->set_help("Filename containing the reference values for the output detection_scores. Used for data type QASYMM8.");
52 num_detections_opt = cmd_parser.add_option<SimpleOption<std::string>>("num_detections_opt", "");
53 num_detections_opt->set_help("Filename containing the reference values for the output num_detections. Used with datatype QASYMM8.");
Pablo Tellofea8ec32018-11-16 13:25:30 +000054 }
55 GraphSSDMobilenetExample(const GraphSSDMobilenetExample &) = delete;
56 GraphSSDMobilenetExample &operator=(const GraphSSDMobilenetExample &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000057 ~GraphSSDMobilenetExample() override = default;
Pablo Tellofea8ec32018-11-16 13:25:30 +000058 bool do_setup(int argc, char **argv) override
59 {
60 // Parse arguments
61 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010062 cmd_parser.validate();
Pablo Tellofea8ec32018-11-16 13:25:30 +000063
64 // Consume common parameters
65 common_params = consume_common_graph_parameters(common_opts);
66
67 // Return when help menu is requested
68 if(common_params.help)
69 {
70 cmd_parser.print_help(argv[0]);
71 return false;
72 }
73
74 // Print parameter values
75 std::cout << common_params << std::endl;
76
77 // Create input descriptor
78 const TensorShape tensor_shape = permute_shape(TensorShape(300, 300, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
79 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
80
81 // Set graph hints
82 graph << common_params.target
Pablo Tellofea8ec32018-11-16 13:25:30 +000083 << common_params.fast_math_hint;
84
85 // Create core graph
Isabella Gottardic755f782019-07-22 17:40:27 +010086 if(arm_compute::is_data_type_float(common_params.data_type))
87 {
88 create_graph_float(input_descriptor);
89 }
90 else
91 {
92 create_graph_qasymm(input_descriptor);
93 }
Pablo Tellofea8ec32018-11-16 13:25:30 +000094
Isabella Gottardic755f782019-07-22 17:40:27 +010095 // Finalize graph
96 GraphConfig config;
97 config.num_threads = common_params.threads;
98 config.use_tuner = common_params.enable_tuner;
99 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000100 config.mlgo_file = common_params.mlgo_file;
Isabella Gottardic755f782019-07-22 17:40:27 +0100101
102 graph.finalize(common_params.target, config);
103
104 return true;
105 }
106 void do_run() override
107 {
108 // Run graph
109 graph.run();
110 }
111
112private:
113 CommandLineParser cmd_parser;
114 CommonGraphOptions common_opts;
115 SimpleOption<int> *keep_topk_opt{ nullptr };
116 CommonGraphParams common_params;
117 Stream graph;
118
119 SimpleOption<std::string> *detection_boxes_opt{ nullptr };
120 SimpleOption<std::string> *detection_classes_opt{ nullptr };
121 SimpleOption<std::string> *detection_scores_opt{ nullptr };
122 SimpleOption<std::string> *num_detections_opt{ nullptr };
123
ramelg01b4cd2dc2022-03-30 18:42:23 +0100124 ConcatLayer get_node_A_float(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100125 unsigned int conv_filt,
126 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
127 {
128 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100129 SubStream sg(main_graph);
Isabella Gottardic755f782019-07-22 17:40:27 +0100130
131 sg << DepthwiseConvolutionLayer(
132 3U, 3U,
133 get_weights_accessor(data_path, total_path + "dw_w.npy"),
134 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
135 dwc_pad_stride_info)
136 .set_name(param_path + "/dw")
137 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "dw_bn_mean.npy"),
138 get_weights_accessor(data_path, total_path + "dw_bn_var.npy"),
139 get_weights_accessor(data_path, total_path + "dw_scale_w.npy"),
140 get_weights_accessor(data_path, total_path + "dw_scale_b.npy"), 0.00001f)
141 .set_name(param_path + "/dw/bn")
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "dw/relu")
143
144 << ConvolutionLayer(
145 1U, 1U, conv_filt,
146 get_weights_accessor(data_path, total_path + "w.npy"),
147 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
148 conv_pad_stride_info)
149 .set_name(param_path + "/pw")
150 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "bn_mean.npy"),
151 get_weights_accessor(data_path, total_path + "bn_var.npy"),
152 get_weights_accessor(data_path, total_path + "scale_w.npy"),
153 get_weights_accessor(data_path, total_path + "scale_b.npy"), 0.00001f)
154 .set_name(param_path + "/pw/bn")
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "pw/relu");
156
157 return ConcatLayer(std::move(sg));
158 }
159
ramelg01b4cd2dc2022-03-30 18:42:23 +0100160 ConcatLayer get_node_B_float(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100161 unsigned int conv_filt,
162 PadStrideInfo conv_pad_stride_info_1, PadStrideInfo conv_pad_stride_info_2)
163 {
164 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100165 SubStream sg(main_graph);
Isabella Gottardic755f782019-07-22 17:40:27 +0100166
167 sg << ConvolutionLayer(
168 1, 1, conv_filt / 2,
169 get_weights_accessor(data_path, total_path + "1_w.npy"),
170 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
171 conv_pad_stride_info_1)
172 .set_name(total_path + "1/conv")
173 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "1_bn_mean.npy"),
174 get_weights_accessor(data_path, total_path + "1_bn_var.npy"),
175 get_weights_accessor(data_path, total_path + "1_scale_w.npy"),
176 get_weights_accessor(data_path, total_path + "1_scale_b.npy"), 0.00001f)
177 .set_name(total_path + "1/bn")
178 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "1/relu");
179
180 sg << ConvolutionLayer(
181 3, 3, conv_filt,
182 get_weights_accessor(data_path, total_path + "2_w.npy"),
183 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
184 conv_pad_stride_info_2)
185 .set_name(total_path + "2/conv")
186 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "2_bn_mean.npy"),
187 get_weights_accessor(data_path, total_path + "2_bn_var.npy"),
188 get_weights_accessor(data_path, total_path + "2_scale_w.npy"),
189 get_weights_accessor(data_path, total_path + "2_scale_b.npy"), 0.00001f)
190 .set_name(total_path + "2/bn")
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "2/relu");
192
193 return ConcatLayer(std::move(sg));
194 }
195
ramelg01b4cd2dc2022-03-30 18:42:23 +0100196 ConcatLayer get_node_C_float(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100197 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info)
198 {
199 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100200 SubStream sg(main_graph);
Isabella Gottardic755f782019-07-22 17:40:27 +0100201 sg << ConvolutionLayer(
202 1U, 1U, conv_filt,
203 get_weights_accessor(data_path, total_path + "w.npy"),
204 get_weights_accessor(data_path, total_path + "b.npy"),
205 conv_pad_stride_info)
206 .set_name(param_path + "/conv");
207 if(common_params.data_layout == DataLayout::NCHW)
208 {
209 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC).set_name(param_path + "/perm");
210 }
211 sg << FlattenLayer().set_name(param_path + "/flat");
212
213 return ConcatLayer(std::move(sg));
214 }
215
216 void create_graph_float(TensorDescriptor &input_descriptor)
217 {
Pablo Tellofea8ec32018-11-16 13:25:30 +0000218 // Create a preprocessor object
219 const std::array<float, 3> mean_rgb{ { 127.5f, 127.5f, 127.5f } };
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000220 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<CaffePreproccessor>(mean_rgb, true, 0.007843f);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000221
222 // Get trainable parameters data path
223 std::string data_path = common_params.data_path;
224
225 // Add model path to data path
226 if(!data_path.empty())
227 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100228 data_path += "/cnn_data/ssd_mobilenet_model/";
Pablo Tellofea8ec32018-11-16 13:25:30 +0000229 }
230
231 graph << InputLayer(input_descriptor,
232 get_input_accessor(common_params, std::move(preprocessor)));
233
234 SubStream conv_11(graph);
235 conv_11 << ConvolutionLayer(
236 3U, 3U, 32U,
237 get_weights_accessor(data_path, "conv0_w.npy"),
238 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
239 PadStrideInfo(2, 2, 1, 1))
240 .set_name("conv0");
241 conv_11 << BatchNormalizationLayer(get_weights_accessor(data_path, "conv0_bn_mean.npy"),
242 get_weights_accessor(data_path, "conv0_bn_var.npy"),
243 get_weights_accessor(data_path, "conv0_scale_w.npy"),
244 get_weights_accessor(data_path, "conv0_scale_b.npy"), 0.00001f)
245 .set_name("conv0/bn")
246 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/relu");
247
Isabella Gottardic755f782019-07-22 17:40:27 +0100248 conv_11 << get_node_A_float(conv_11, data_path, "conv1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
249 conv_11 << get_node_A_float(conv_11, data_path, "conv2", 128, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
250 conv_11 << get_node_A_float(conv_11, data_path, "conv3", 128, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
251 conv_11 << get_node_A_float(conv_11, data_path, "conv4", 256, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
252 conv_11 << get_node_A_float(conv_11, data_path, "conv5", 256, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
253 conv_11 << get_node_A_float(conv_11, data_path, "conv6", 512, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
254 conv_11 << get_node_A_float(conv_11, data_path, "conv7", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
255 conv_11 << get_node_A_float(conv_11, data_path, "conv8", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
256 conv_11 << get_node_A_float(conv_11, data_path, "conv9", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
257 conv_11 << get_node_A_float(conv_11, data_path, "conv10", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
258 conv_11 << get_node_A_float(conv_11, data_path, "conv11", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000259
260 SubStream conv_13(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100261 conv_13 << get_node_A_float(conv_11, data_path, "conv12", 1024, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
262 conv_13 << get_node_A_float(conv_13, data_path, "conv13", 1024, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000263
264 SubStream conv_14(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100265 conv_14 << get_node_B_float(conv_13, data_path, "conv14", 512, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000266
267 SubStream conv_15(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100268 conv_15 << get_node_B_float(conv_14, data_path, "conv15", 256, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000269
270 SubStream conv_16(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100271 conv_16 << get_node_B_float(conv_15, data_path, "conv16", 256, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000272
273 SubStream conv_17(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100274 conv_17 << get_node_B_float(conv_16, data_path, "conv17", 128, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000275
276 //mbox_loc
277 SubStream conv_11_mbox_loc(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100278 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 +0000279
280 SubStream conv_13_mbox_loc(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100281 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 +0000282
283 SubStream conv_14_2_mbox_loc(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100284 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 +0000285
286 SubStream conv_15_2_mbox_loc(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100287 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 +0000288
289 SubStream conv_16_2_mbox_loc(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100290 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 +0000291
292 SubStream conv_17_2_mbox_loc(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100293 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 +0000294
295 SubStream mbox_loc(graph);
296 mbox_loc << ConcatLayer(std::move(conv_11_mbox_loc), std::move(conv_13_mbox_loc), conv_14_2_mbox_loc, std::move(conv_15_2_mbox_loc),
297 std::move(conv_16_2_mbox_loc), std::move(conv_17_2_mbox_loc));
298
Pablo Tellofea8ec32018-11-16 13:25:30 +0000299 //mbox_conf
300 SubStream conv_11_mbox_conf(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100301 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 +0000302
303 SubStream conv_13_mbox_conf(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100304 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 +0000305
306 SubStream conv_14_2_mbox_conf(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100307 conv_14_2_mbox_conf << get_node_C_float(conv_14, data_path, "conv14_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000308
309 SubStream conv_15_2_mbox_conf(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100310 conv_15_2_mbox_conf << get_node_C_float(conv_15, data_path, "conv15_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000311
312 SubStream conv_16_2_mbox_conf(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100313 conv_16_2_mbox_conf << get_node_C_float(conv_16, data_path, "conv16_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000314
315 SubStream conv_17_2_mbox_conf(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100316 conv_17_2_mbox_conf << get_node_C_float(conv_17, data_path, "conv17_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000317
318 SubStream mbox_conf(graph);
319 mbox_conf << ConcatLayer(std::move(conv_11_mbox_conf), std::move(conv_13_mbox_conf), std::move(conv_14_2_mbox_conf),
320 std::move(conv_15_2_mbox_conf), std::move(conv_16_2_mbox_conf), std::move(conv_17_2_mbox_conf));
321 mbox_conf << ReshapeLayer(TensorShape(21U, 1917U)).set_name("mbox_conf/reshape");
322 mbox_conf << SoftmaxLayer().set_name("mbox_conf/softmax");
323 mbox_conf << FlattenLayer().set_name("mbox_conf/flat");
324
Pablo Tellofea8ec32018-11-16 13:25:30 +0000325 const std::vector<float> priorbox_variances = { 0.1f, 0.1f, 0.2f, 0.2f };
326 const float priorbox_offset = 0.5f;
327 const std::vector<float> priorbox_aspect_ratios = { 2.f, 3.f };
328
329 //mbox_priorbox branch
330 SubStream conv_11_mbox_priorbox(conv_11);
331
332 conv_11_mbox_priorbox << PriorBoxLayer(SubStream(graph),
333 PriorBoxLayerInfo({ 60.f }, priorbox_variances, priorbox_offset, true, false, {}, { 2.f }))
334 .set_name("conv11/priorbox");
335
336 SubStream conv_13_mbox_priorbox(conv_13);
337 conv_13_mbox_priorbox << PriorBoxLayer(SubStream(graph),
338 PriorBoxLayerInfo({ 105.f }, priorbox_variances, priorbox_offset, true, false, { 150.f }, priorbox_aspect_ratios))
339 .set_name("conv13/priorbox");
340
341 SubStream conv_14_2_mbox_priorbox(conv_14);
342 conv_14_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
343 PriorBoxLayerInfo({ 150.f }, priorbox_variances, priorbox_offset, true, false, { 195.f }, priorbox_aspect_ratios))
344 .set_name("conv14/priorbox");
345
346 SubStream conv_15_2_mbox_priorbox(conv_15);
347 conv_15_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
348 PriorBoxLayerInfo({ 195.f }, priorbox_variances, priorbox_offset, true, false, { 240.f }, priorbox_aspect_ratios))
349 .set_name("conv15/priorbox");
350
351 SubStream conv_16_2_mbox_priorbox(conv_16);
352 conv_16_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
353 PriorBoxLayerInfo({ 240.f }, priorbox_variances, priorbox_offset, true, false, { 285.f }, priorbox_aspect_ratios))
354 .set_name("conv16/priorbox");
355
356 SubStream conv_17_2_mbox_priorbox(conv_17);
357 conv_17_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
358 PriorBoxLayerInfo({ 285.f }, priorbox_variances, priorbox_offset, true, false, { 300.f }, priorbox_aspect_ratios))
359 .set_name("conv17/priorbox");
360
361 SubStream mbox_priorbox(graph);
362
363 mbox_priorbox << ConcatLayer(
Isabella Gottardic755f782019-07-22 17:40:27 +0100364 (common_params.data_layout == DataLayout::NCHW) ? arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH) : arm_compute::graph::descriptors::ConcatLayerDescriptor(
365 DataLayoutDimension::CHANNEL),
Pablo Tellofea8ec32018-11-16 13:25:30 +0000366 std::move(conv_11_mbox_priorbox), std::move(conv_13_mbox_priorbox), std::move(conv_14_2_mbox_priorbox),
367 std::move(conv_15_2_mbox_priorbox), std::move(conv_16_2_mbox_priorbox), std::move(conv_17_2_mbox_priorbox));
368
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000369 const int num_classes = 21;
370 const bool share_location = true;
371 const DetectionOutputLayerCodeType detection_type = DetectionOutputLayerCodeType::CENTER_SIZE;
372 const int keep_top_k = keep_topk_opt->value();
373 const float nms_threshold = 0.45f;
374 const int label_id_background = 0;
375 const float conf_thrs = 0.25f;
376 const int top_k = 100;
377
378 SubStream detection_ouput(mbox_loc);
379 detection_ouput << DetectionOutputLayer(std::move(mbox_conf), std::move(mbox_priorbox),
380 DetectionOutputLayerInfo(num_classes, share_location, detection_type, keep_top_k, nms_threshold, top_k, label_id_background, conf_thrs));
Isabella Gottardic755f782019-07-22 17:40:27 +0100381 detection_ouput << OutputLayer(get_detection_output_accessor(common_params, { input_descriptor.shape }));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000382 }
383
ramelg01b4cd2dc2022-03-30 18:42:23 +0100384 ConcatLayer get_node_A_qasymm(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100385 unsigned int conv_filt,
386 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
387 std::pair<QuantizationInfo, QuantizationInfo> depth_quant_info, std::pair<QuantizationInfo, QuantizationInfo> point_quant_info)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000388 {
389 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100390 SubStream sg(main_graph);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000391
392 sg << DepthwiseConvolutionLayer(
393 3U, 3U,
394 get_weights_accessor(data_path, total_path + "dw_w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100395 get_weights_accessor(data_path, total_path + "dw_b.npy"),
396 dwc_pad_stride_info, 1, depth_quant_info.first, depth_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000397 .set_name(param_path + "/dw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100398 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/dw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000399
Isabella Gottardic755f782019-07-22 17:40:27 +0100400 sg << ConvolutionLayer(
Pablo Tellofea8ec32018-11-16 13:25:30 +0000401 1U, 1U, conv_filt,
402 get_weights_accessor(data_path, total_path + "w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100403 get_weights_accessor(data_path, total_path + "b.npy"),
404 conv_pad_stride_info, 1, point_quant_info.first, point_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000405 .set_name(param_path + "/pw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100406 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/pw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000407
408 return ConcatLayer(std::move(sg));
409 }
410
ramelg01b4cd2dc2022-03-30 18:42:23 +0100411 ConcatLayer get_node_B_qasymm(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100412 unsigned int conv_filt,
413 PadStrideInfo conv_pad_stride_info_1x1, PadStrideInfo conv_pad_stride_info_3x3,
414 const std::pair<QuantizationInfo, QuantizationInfo> quant_info_1x1, const std::pair<QuantizationInfo, QuantizationInfo> quant_info_3x3)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000415 {
416 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100417 SubStream sg(main_graph);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000418
419 sg << ConvolutionLayer(
420 1, 1, conv_filt / 2,
Isabella Gottardic755f782019-07-22 17:40:27 +0100421 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
422 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
423 conv_pad_stride_info_1x1, 1, quant_info_1x1.first, quant_info_1x1.second)
424 .set_name(total_path + "1x1/conv")
425 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "1x1/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000426
427 sg << ConvolutionLayer(
428 3, 3, conv_filt,
Isabella Gottardic755f782019-07-22 17:40:27 +0100429 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
430 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
431 conv_pad_stride_info_3x3, 1, quant_info_3x3.first, quant_info_3x3.second)
432 .set_name(total_path + "3x3/conv")
433 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "3x3/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000434
435 return ConcatLayer(std::move(sg));
436 }
437
ramelg01b4cd2dc2022-03-30 18:42:23 +0100438 ConcatLayer get_node_C_qasymm(IStream &main_graph, const std::string &data_path, std::string &&param_path,
Isabella Gottardic755f782019-07-22 17:40:27 +0100439 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info,
440 const std::pair<QuantizationInfo, QuantizationInfo> quant_info, TensorShape reshape_shape)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000441 {
442 const std::string total_path = param_path + "_";
ramelg01b4cd2dc2022-03-30 18:42:23 +0100443 SubStream sg(main_graph);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000444 sg << ConvolutionLayer(
445 1U, 1U, conv_filt,
446 get_weights_accessor(data_path, total_path + "w.npy"),
447 get_weights_accessor(data_path, total_path + "b.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100448 conv_pad_stride_info, 1, quant_info.first, quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000449 .set_name(param_path + "/conv");
450 if(common_params.data_layout == DataLayout::NCHW)
451 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100452 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000453 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100454 sg << ReshapeLayer(reshape_shape).set_name(param_path + "/reshape");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000455
456 return ConcatLayer(std::move(sg));
457 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100458
459 void create_graph_qasymm(TensorDescriptor &input_descriptor)
460 {
461 // Get trainable parameters data path
462 std::string data_path = common_params.data_path;
463
464 // Add model path to data path
465 if(!data_path.empty())
466 {
467 data_path += "/cnn_data/ssd_mobilenet_qasymm8_model/";
468 }
469
470 // Quantization info are saved as pair for each (pointwise/depthwise) convolution layer: <weight_quant_info, output_quant_info>
471 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> conv_quant_info =
472 {
473 { QuantizationInfo(0.03624850884079933f, 163), QuantizationInfo(0.22219789028167725f, 113) }, // conv0
474 { QuantizationInfo(0.0028752065263688564f, 113), QuantizationInfo(0.05433657020330429f, 128) }, // conv13_2_1_1
475 { QuantizationInfo(0.0014862528769299388f, 125), QuantizationInfo(0.05037643015384674f, 131) }, // conv13_2_3_3
476 { QuantizationInfo(0.00233650766313076f, 113), QuantizationInfo(0.04468846693634987f, 126) }, // conv13_3_1_1
477 { QuantizationInfo(0.002501056529581547f, 120), QuantizationInfo(0.06026708707213402f, 111) }, // conv13_3_3_3
478 { QuantizationInfo(0.002896666992455721f, 121), QuantizationInfo(0.037775348871946335f, 117) }, // conv13_4_1_1
479 { QuantizationInfo(0.0023875406477600336f, 122), QuantizationInfo(0.03881589323282242f, 108) }, // conv13_4_3_3
480 { QuantizationInfo(0.0022081052884459496f, 77), QuantizationInfo(0.025450613349676132f, 125) }, // conv13_5_1_1
481 { QuantizationInfo(0.00604657270014286f, 121), QuantizationInfo(0.033533502370119095f, 109) } // conv13_5_3_3
482 };
483
484 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> depth_quant_info =
485 {
486 { QuantizationInfo(0.03408717364072f, 131), QuantizationInfo(0.29286590218544006f, 108) }, // dwsc1
487 { QuantizationInfo(0.027518004179000854f, 107), QuantizationInfo(0.20796941220760345, 117) }, // dwsc2
488 { QuantizationInfo(0.052489638328552246f, 85), QuantizationInfo(0.4303881824016571f, 142) }, // dwsc3
489 { QuantizationInfo(0.016570359468460083f, 79), QuantizationInfo(0.10512150079011917f, 116) }, // dwsc4
490 { QuantizationInfo(0.060739465057849884f, 65), QuantizationInfo(0.15331414341926575f, 94) }, // dwsc5
491 { QuantizationInfo(0.01324534136801958f, 124), QuantizationInfo(0.13010895252227783f, 153) }, // dwsc6
492 { QuantizationInfo(0.032326459884643555f, 124), QuantizationInfo(0.11565316468477249, 156) }, // dwsc7
493 { QuantizationInfo(0.029948478564620018f, 155), QuantizationInfo(0.11413891613483429f, 146) }, // dwsc8
494 { QuantizationInfo(0.028054025024175644f, 129), QuantizationInfo(0.1142905130982399f, 140) }, // dwsc9
495 { QuantizationInfo(0.025204822421073914f, 129), QuantizationInfo(0.14668069779872894f, 149) }, // dwsc10
496 { QuantizationInfo(0.019332280382514f, 110), QuantizationInfo(0.1480235457420349f, 91) }, // dwsc11
497 { QuantizationInfo(0.0319712869822979f, 88), QuantizationInfo(0.10424695909023285f, 117) }, // dwsc12
498 { QuantizationInfo(0.04378943517804146f, 164), QuantizationInfo(0.23176774382591248f, 138) } // dwsc13
499 };
500
501 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> point_quant_info =
502 {
503 { QuantizationInfo(0.028777318075299263f, 144), QuantizationInfo(0.2663874328136444f, 121) }, // pw1
504 { QuantizationInfo(0.015796702355146408f, 127), QuantizationInfo(0.1739964485168457f, 111) }, // pw2
505 { QuantizationInfo(0.009349990636110306f, 127), QuantizationInfo(0.1805974692106247f, 104) }, // pw3
506 { QuantizationInfo(0.012920888140797615f, 106), QuantizationInfo(0.1205204650759697f, 100) }, // pw4
507 { QuantizationInfo(0.008119508624076843f, 145), QuantizationInfo(0.12272439152002335f, 97) }, // pw5
508 { QuantizationInfo(0.0070041813887655735f, 115), QuantizationInfo(0.0947074219584465f, 101) }, // pw6
509 { QuantizationInfo(0.004827278666198254f, 115), QuantizationInfo(0.0842885747551918f, 110) }, // pw7
510 { QuantizationInfo(0.004755120258778334f, 128), QuantizationInfo(0.08283159881830215f, 116) }, // pw8
511 { QuantizationInfo(0.007527193054556847f, 142), QuantizationInfo(0.12555131316184998f, 137) }, // pw9
512 { QuantizationInfo(0.006050156895071268f, 109), QuantizationInfo(0.10871313512325287f, 124) }, // pw10
513 { QuantizationInfo(0.00490700313821435f, 127), QuantizationInfo(0.10364262014627457f, 140) }, // pw11
514 { QuantizationInfo(0.006063731852918863, 124), QuantizationInfo(0.11241862177848816f, 125) }, // pw12
515 { QuantizationInfo(0.007901716977357864f, 139), QuantizationInfo(0.49889302253723145f, 141) } // pw13
516 };
517
518 // Quantization info taken from the TfLite SSD MobileNet example
519 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Isabella Gottardic755f782019-07-22 17:40:27 +0100520 // Create core graph
521 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
522 get_weights_accessor(data_path, common_params.image, DataLayout::NHWC));
523 graph << ConvolutionLayer(
524 3U, 3U, 32U,
525 get_weights_accessor(data_path, "conv0_w.npy"),
526 get_weights_accessor(data_path, "conv0_b.npy"),
527 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), 1, conv_quant_info.at(0).first, conv_quant_info.at(0).second)
528 .set_name("conv0");
529 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("conv0/relu");
530 graph << get_node_A_qasymm(graph, data_path, "conv1", 64U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(0),
531 point_quant_info.at(0));
532 graph << get_node_A_qasymm(graph, data_path, "conv2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(1),
533 point_quant_info.at(1));
534 graph << get_node_A_qasymm(graph, data_path, "conv3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(2),
535 point_quant_info.at(2));
536 graph << get_node_A_qasymm(graph, data_path, "conv4", 256U, PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(3),
537 point_quant_info.at(3));
538 graph << get_node_A_qasymm(graph, data_path, "conv5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(4),
539 point_quant_info.at(4));
540 graph << get_node_A_qasymm(graph, data_path, "conv6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(5),
541 point_quant_info.at(5));
542 graph << get_node_A_qasymm(graph, data_path, "conv7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(6),
543 point_quant_info.at(6));
544 graph << get_node_A_qasymm(graph, data_path, "conv8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(7),
545 point_quant_info.at(7));
546 graph << get_node_A_qasymm(graph, data_path, "conv9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(8),
547 point_quant_info.at(8));
548 graph << get_node_A_qasymm(graph, data_path, "conv10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(9),
549 point_quant_info.at(9));
550 graph << get_node_A_qasymm(graph, data_path, "conv11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(10),
551 point_quant_info.at(10));
552
553 SubStream conv_13(graph);
554 conv_13 << get_node_A_qasymm(graph, data_path, "conv12", 1024U, PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(11),
555 point_quant_info.at(11));
556 conv_13 << get_node_A_qasymm(conv_13, data_path, "conv13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), PadStrideInfo(1U, 1U, 0U, 0U), depth_quant_info.at(12),
557 point_quant_info.at(12));
558 SubStream conv_14(conv_13);
559 conv_14 << get_node_B_qasymm(conv_13, data_path, "conv13_2", 512U, PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), conv_quant_info.at(1),
560 conv_quant_info.at(2));
561 SubStream conv_15(conv_14);
562 conv_15 << get_node_B_qasymm(conv_14, data_path, "conv13_3", 256U, PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), conv_quant_info.at(3),
563 conv_quant_info.at(4));
564 SubStream conv_16(conv_15);
565 conv_16 << get_node_B_qasymm(conv_15, data_path, "conv13_4", 256U, PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(2U, 2U, 1U, 1U, 1U, 1U, DimensionRoundingType::CEIL), conv_quant_info.at(5),
566 conv_quant_info.at(6));
567 SubStream conv_17(conv_16);
568 conv_17 << get_node_B_qasymm(conv_16, data_path, "conv13_5", 128U, PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), conv_quant_info.at(7),
569 conv_quant_info.at(8));
570
571 // box_predictor
572 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> box_enc_pred_quant_info =
573 {
574 { QuantizationInfo(0.005202020984143019f, 136), QuantizationInfo(0.08655580133199692f, 183) }, // boxpredictor0_bep
575 { QuantizationInfo(0.003121797926723957f, 132), QuantizationInfo(0.03218776360154152f, 140) }, // boxpredictor1_bep
576 { QuantizationInfo(0.002995674265548587f, 130), QuantizationInfo(0.029072262346744537f, 125) }, // boxpredictor2_bep
577 { QuantizationInfo(0.0023131705820560455f, 130), QuantizationInfo(0.026488754898309708f, 127) }, // boxpredictor3_bep
578 { QuantizationInfo(0.0013905081432312727f, 132), QuantizationInfo(0.0199890099465847f, 137) }, // boxpredictor4_bep
579 { QuantizationInfo(0.00216794665902853f, 121), QuantizationInfo(0.019798893481492996f, 151) } // boxpredictor5_bep
580 };
581
582 const std::vector<TensorShape> box_reshape = // NHWC
583 {
584 TensorShape(4U, 1U, 1083U), // boxpredictor0_bep_reshape
585 TensorShape(4U, 1U, 600U), // boxpredictor1_bep_reshape
586 TensorShape(4U, 1U, 150U), // boxpredictor2_bep_reshape
587 TensorShape(4U, 1U, 54U), // boxpredictor3_bep_reshape
588 TensorShape(4U, 1U, 24U), // boxpredictor4_bep_reshape
589 TensorShape(4U, 1U, 6U) // boxpredictor5_bep_reshape
590 };
591
592 SubStream conv_11_box_enc_pre(graph);
593 conv_11_box_enc_pre << get_node_C_qasymm(graph, data_path, "BoxPredictor_0_BEP", 12U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(0), box_reshape.at(0));
594
595 SubStream conv_13_box_enc_pre(conv_13);
596 conv_13_box_enc_pre << get_node_C_qasymm(conv_13, data_path, "BoxPredictor_1_BEP", 24U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(1), box_reshape.at(1));
597
598 SubStream conv_14_2_box_enc_pre(conv_14);
599 conv_14_2_box_enc_pre << get_node_C_qasymm(conv_14, data_path, "BoxPredictor_2_BEP", 24U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(2), box_reshape.at(2));
600
601 SubStream conv_15_2_box_enc_pre(conv_15);
602 conv_15_2_box_enc_pre << get_node_C_qasymm(conv_15, data_path, "BoxPredictor_3_BEP", 24U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(3), box_reshape.at(3));
603
604 SubStream conv_16_2_box_enc_pre(conv_16);
605 conv_16_2_box_enc_pre << get_node_C_qasymm(conv_16, data_path, "BoxPredictor_4_BEP", 24U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(4), box_reshape.at(4));
606
607 SubStream conv_17_2_box_enc_pre(conv_17);
608 conv_17_2_box_enc_pre << get_node_C_qasymm(conv_17, data_path, "BoxPredictor_5_BEP", 24U, PadStrideInfo(1U, 1U, 0U, 0U), box_enc_pred_quant_info.at(5), box_reshape.at(5));
609
610 SubStream box_enc_pre(graph);
611 const QuantizationInfo bep_concate_qinfo = QuantizationInfo(0.08655580133199692f, 183);
612 box_enc_pre << ConcatLayer(arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::HEIGHT, bep_concate_qinfo),
613 std::move(conv_11_box_enc_pre), std::move(conv_13_box_enc_pre), conv_14_2_box_enc_pre, std::move(conv_15_2_box_enc_pre),
614 std::move(conv_16_2_box_enc_pre), std::move(conv_17_2_box_enc_pre))
615 .set_name("BoxPredictor/concat");
616 box_enc_pre << ReshapeLayer(TensorShape(4U, 1917U)).set_name("BoxPredictor/reshape");
617
618 // class_predictor
619 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> class_pred_quant_info =
620 {
621 { QuantizationInfo(0.002744135679677129f, 125), QuantizationInfo(0.05746262148022652f, 234) }, // boxpredictor0_cp
622 { QuantizationInfo(0.0024326108396053314f, 80), QuantizationInfo(0.03764628246426582f, 217) }, // boxpredictor1_cp
623 { QuantizationInfo(0.0013898586621508002f, 141), QuantizationInfo(0.034081317484378815f, 214) }, // boxpredictor2_cp
624 { QuantizationInfo(0.0014176908880472183f, 133), QuantizationInfo(0.033889178186655045f, 215) }, // boxpredictor3_cp
625 { QuantizationInfo(0.001090311910957098f, 125), QuantizationInfo(0.02646234817802906f, 230) }, // boxpredictor4_cp
626 { QuantizationInfo(0.001134163816459477f, 115), QuantizationInfo(0.026926767081022263f, 218) } // boxpredictor5_cp
627 };
628
629 const std::vector<TensorShape> class_reshape =
630 {
631 TensorShape(91U, 1083U), // boxpredictor0_cp_reshape
632 TensorShape(91U, 600U), // boxpredictor1_cp_reshape
633 TensorShape(91U, 150U), // boxpredictor2_cp_reshape
634 TensorShape(91U, 54U), // boxpredictor3_cp_reshape
635 TensorShape(91U, 24U), // boxpredictor4_cp_reshape
636 TensorShape(91U, 6U) // boxpredictor5_cp_reshape
637 };
638
639 SubStream conv_11_class_pre(graph);
640 conv_11_class_pre << get_node_C_qasymm(graph, data_path, "BoxPredictor_0_CP", 273U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(0), class_reshape.at(0));
641
642 SubStream conv_13_class_pre(conv_13);
643 conv_13_class_pre << get_node_C_qasymm(conv_13, data_path, "BoxPredictor_1_CP", 546U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(1), class_reshape.at(1));
644
645 SubStream conv_14_2_class_pre(conv_14);
646 conv_14_2_class_pre << get_node_C_qasymm(conv_14, data_path, "BoxPredictor_2_CP", 546U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(2), class_reshape.at(2));
647
648 SubStream conv_15_2_class_pre(conv_15);
649 conv_15_2_class_pre << get_node_C_qasymm(conv_15, data_path, "BoxPredictor_3_CP", 546U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(3), class_reshape.at(3));
650
651 SubStream conv_16_2_class_pre(conv_16);
652 conv_16_2_class_pre << get_node_C_qasymm(conv_16, data_path, "BoxPredictor_4_CP", 546U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(4), class_reshape.at(4));
653
654 SubStream conv_17_2_class_pre(conv_17);
655 conv_17_2_class_pre << get_node_C_qasymm(conv_17, data_path, "BoxPredictor_5_CP", 546U, PadStrideInfo(1U, 1U, 0U, 0U), class_pred_quant_info.at(5), class_reshape.at(5));
656
657 const QuantizationInfo cp_concate_qinfo = QuantizationInfo(0.0584389753639698f, 230);
658 SubStream class_pred(graph);
659 class_pred << ConcatLayer(
660 arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH, cp_concate_qinfo),
661 std::move(conv_11_class_pre), std::move(conv_13_class_pre), std::move(conv_14_2_class_pre),
662 std::move(conv_15_2_class_pre), std::move(conv_16_2_class_pre), std::move(conv_17_2_class_pre))
663 .set_name("ClassPrediction/concat");
664
665 const QuantizationInfo logistic_out_qinfo = QuantizationInfo(0.00390625f, 0);
666 class_pred << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), logistic_out_qinfo).set_name("ClassPrediction/logistic");
667
668 const int max_detections = 10;
669 const int max_classes_per_detection = 1;
670 const float nms_score_threshold = 0.30000001192092896f;
671 const float nms_iou_threshold = 0.6000000238418579f;
672 const int num_classes = 90;
673 const float x_scale = 10.f;
674 const float y_scale = 10.f;
675 const float h_scale = 5.f;
676 const float w_scale = 5.f;
677 std::array<float, 4> scales = { y_scale, x_scale, w_scale, h_scale };
678 const QuantizationInfo anchors_qinfo = QuantizationInfo(0.006453060545027256f, 0);
679
680 SubStream detection_ouput(box_enc_pre);
681 detection_ouput << DetectionPostProcessLayer(std::move(class_pred),
682 DetectionPostProcessLayerInfo(max_detections, max_classes_per_detection, nms_score_threshold, nms_iou_threshold, num_classes, scales),
683 get_weights_accessor(data_path, "anchors.npy"), anchors_qinfo)
684 .set_name("DetectionPostProcess");
685
686 SubStream ouput_0(detection_ouput);
687 ouput_0 << OutputLayer(get_npy_output_accessor(detection_boxes_opt->value(), TensorShape(4U, 10U), DataType::F32), 0);
688
689 SubStream ouput_1(detection_ouput);
690 ouput_1 << OutputLayer(get_npy_output_accessor(detection_classes_opt->value(), TensorShape(10U), DataType::F32), 1);
691
692 SubStream ouput_2(detection_ouput);
693 ouput_2 << OutputLayer(get_npy_output_accessor(detection_scores_opt->value(), TensorShape(10U), DataType::F32), 2);
694
695 SubStream ouput_3(detection_ouput);
696 ouput_3 << OutputLayer(get_npy_output_accessor(num_detections_opt->value(), TensorShape(1U), DataType::F32), 3);
697 }
Pablo Tellofea8ec32018-11-16 13:25:30 +0000698};
699
700/** Main program for MobileNetSSD
701 *
702 * Model is based on:
703 * http://arxiv.org/abs/1512.02325
704 * SSD: Single Shot MultiBox Detector
705 * Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg
706 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000707 * Provenance: https://github.com/chuanqi305/MobileNet-SSD
708 *
Pablo Tellofea8ec32018-11-16 13:25:30 +0000709 * @note To list all the possible arguments execute the binary appended with the --help option
710 *
711 * @param[in] argc Number of arguments
712 * @param[in] argv Arguments
713 */
714int main(int argc, char **argv)
715{
716 return arm_compute::utils::run_example<GraphSSDMobilenetExample>(argc, argv);
717}