blob: edd4c94d029fa67e3a1649e7d00e89808c3a42b1 [file] [log] [blame]
Pablo Tellofea8ec32018-11-16 13:25:30 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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;
100
101 graph.finalize(common_params.target, config);
102
103 return true;
104 }
105 void do_run() override
106 {
107 // Run graph
108 graph.run();
109 }
110
111private:
112 CommandLineParser cmd_parser;
113 CommonGraphOptions common_opts;
114 SimpleOption<int> *keep_topk_opt{ nullptr };
115 CommonGraphParams common_params;
116 Stream graph;
117
118 SimpleOption<std::string> *detection_boxes_opt{ nullptr };
119 SimpleOption<std::string> *detection_classes_opt{ nullptr };
120 SimpleOption<std::string> *detection_scores_opt{ nullptr };
121 SimpleOption<std::string> *num_detections_opt{ nullptr };
122
123 ConcatLayer get_node_A_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
124 unsigned int conv_filt,
125 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
126 {
127 const std::string total_path = param_path + "_";
128 SubStream sg(master_graph);
129
130 sg << DepthwiseConvolutionLayer(
131 3U, 3U,
132 get_weights_accessor(data_path, total_path + "dw_w.npy"),
133 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
134 dwc_pad_stride_info)
135 .set_name(param_path + "/dw")
136 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "dw_bn_mean.npy"),
137 get_weights_accessor(data_path, total_path + "dw_bn_var.npy"),
138 get_weights_accessor(data_path, total_path + "dw_scale_w.npy"),
139 get_weights_accessor(data_path, total_path + "dw_scale_b.npy"), 0.00001f)
140 .set_name(param_path + "/dw/bn")
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "dw/relu")
142
143 << ConvolutionLayer(
144 1U, 1U, conv_filt,
145 get_weights_accessor(data_path, total_path + "w.npy"),
146 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
147 conv_pad_stride_info)
148 .set_name(param_path + "/pw")
149 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "bn_mean.npy"),
150 get_weights_accessor(data_path, total_path + "bn_var.npy"),
151 get_weights_accessor(data_path, total_path + "scale_w.npy"),
152 get_weights_accessor(data_path, total_path + "scale_b.npy"), 0.00001f)
153 .set_name(param_path + "/pw/bn")
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "pw/relu");
155
156 return ConcatLayer(std::move(sg));
157 }
158
159 ConcatLayer get_node_B_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
160 unsigned int conv_filt,
161 PadStrideInfo conv_pad_stride_info_1, PadStrideInfo conv_pad_stride_info_2)
162 {
163 const std::string total_path = param_path + "_";
164 SubStream sg(master_graph);
165
166 sg << ConvolutionLayer(
167 1, 1, conv_filt / 2,
168 get_weights_accessor(data_path, total_path + "1_w.npy"),
169 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
170 conv_pad_stride_info_1)
171 .set_name(total_path + "1/conv")
172 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "1_bn_mean.npy"),
173 get_weights_accessor(data_path, total_path + "1_bn_var.npy"),
174 get_weights_accessor(data_path, total_path + "1_scale_w.npy"),
175 get_weights_accessor(data_path, total_path + "1_scale_b.npy"), 0.00001f)
176 .set_name(total_path + "1/bn")
177 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "1/relu");
178
179 sg << ConvolutionLayer(
180 3, 3, conv_filt,
181 get_weights_accessor(data_path, total_path + "2_w.npy"),
182 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
183 conv_pad_stride_info_2)
184 .set_name(total_path + "2/conv")
185 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "2_bn_mean.npy"),
186 get_weights_accessor(data_path, total_path + "2_bn_var.npy"),
187 get_weights_accessor(data_path, total_path + "2_scale_w.npy"),
188 get_weights_accessor(data_path, total_path + "2_scale_b.npy"), 0.00001f)
189 .set_name(total_path + "2/bn")
190 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "2/relu");
191
192 return ConcatLayer(std::move(sg));
193 }
194
195 ConcatLayer get_node_C_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
196 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info)
197 {
198 const std::string total_path = param_path + "_";
199 SubStream sg(master_graph);
200 sg << ConvolutionLayer(
201 1U, 1U, conv_filt,
202 get_weights_accessor(data_path, total_path + "w.npy"),
203 get_weights_accessor(data_path, total_path + "b.npy"),
204 conv_pad_stride_info)
205 .set_name(param_path + "/conv");
206 if(common_params.data_layout == DataLayout::NCHW)
207 {
208 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC).set_name(param_path + "/perm");
209 }
210 sg << FlattenLayer().set_name(param_path + "/flat");
211
212 return ConcatLayer(std::move(sg));
213 }
214
215 void create_graph_float(TensorDescriptor &input_descriptor)
216 {
Pablo Tellofea8ec32018-11-16 13:25:30 +0000217 // Create a preprocessor object
218 const std::array<float, 3> mean_rgb{ { 127.5f, 127.5f, 127.5f } };
Georgios Pinitasb54c6442019-04-03 13:18:14 +0100219 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb, true, 0.007843f);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000220
221 // Get trainable parameters data path
222 std::string data_path = common_params.data_path;
223
224 // Add model path to data path
225 if(!data_path.empty())
226 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100227 data_path += "/cnn_data/ssd_mobilenet_model/";
Pablo Tellofea8ec32018-11-16 13:25:30 +0000228 }
229
230 graph << InputLayer(input_descriptor,
231 get_input_accessor(common_params, std::move(preprocessor)));
232
233 SubStream conv_11(graph);
234 conv_11 << ConvolutionLayer(
235 3U, 3U, 32U,
236 get_weights_accessor(data_path, "conv0_w.npy"),
237 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
238 PadStrideInfo(2, 2, 1, 1))
239 .set_name("conv0");
240 conv_11 << BatchNormalizationLayer(get_weights_accessor(data_path, "conv0_bn_mean.npy"),
241 get_weights_accessor(data_path, "conv0_bn_var.npy"),
242 get_weights_accessor(data_path, "conv0_scale_w.npy"),
243 get_weights_accessor(data_path, "conv0_scale_b.npy"), 0.00001f)
244 .set_name("conv0/bn")
245 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/relu");
246
Isabella Gottardic755f782019-07-22 17:40:27 +0100247 conv_11 << get_node_A_float(conv_11, data_path, "conv1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
248 conv_11 << get_node_A_float(conv_11, data_path, "conv2", 128, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
249 conv_11 << get_node_A_float(conv_11, data_path, "conv3", 128, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
250 conv_11 << get_node_A_float(conv_11, data_path, "conv4", 256, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
251 conv_11 << get_node_A_float(conv_11, data_path, "conv5", 256, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
252 conv_11 << get_node_A_float(conv_11, data_path, "conv6", 512, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
253 conv_11 << get_node_A_float(conv_11, data_path, "conv7", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
254 conv_11 << get_node_A_float(conv_11, data_path, "conv8", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
255 conv_11 << get_node_A_float(conv_11, data_path, "conv9", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
256 conv_11 << get_node_A_float(conv_11, data_path, "conv10", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
257 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 +0000258
259 SubStream conv_13(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100260 conv_13 << get_node_A_float(conv_11, data_path, "conv12", 1024, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
261 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 +0000262
263 SubStream conv_14(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100264 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 +0000265
266 SubStream conv_15(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100267 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 +0000268
269 SubStream conv_16(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100270 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 +0000271
272 SubStream conv_17(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100273 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 +0000274
275 //mbox_loc
276 SubStream conv_11_mbox_loc(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100277 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 +0000278
279 SubStream conv_13_mbox_loc(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100280 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 +0000281
282 SubStream conv_14_2_mbox_loc(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100283 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 +0000284
285 SubStream conv_15_2_mbox_loc(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100286 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 +0000287
288 SubStream conv_16_2_mbox_loc(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100289 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 +0000290
291 SubStream conv_17_2_mbox_loc(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100292 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 +0000293
294 SubStream mbox_loc(graph);
295 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),
296 std::move(conv_16_2_mbox_loc), std::move(conv_17_2_mbox_loc));
297
Pablo Tellofea8ec32018-11-16 13:25:30 +0000298 //mbox_conf
299 SubStream conv_11_mbox_conf(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100300 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 +0000301
302 SubStream conv_13_mbox_conf(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100303 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 +0000304
305 SubStream conv_14_2_mbox_conf(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100306 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 +0000307
308 SubStream conv_15_2_mbox_conf(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100309 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 +0000310
311 SubStream conv_16_2_mbox_conf(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100312 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 +0000313
314 SubStream conv_17_2_mbox_conf(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100315 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 +0000316
317 SubStream mbox_conf(graph);
318 mbox_conf << ConcatLayer(std::move(conv_11_mbox_conf), std::move(conv_13_mbox_conf), std::move(conv_14_2_mbox_conf),
319 std::move(conv_15_2_mbox_conf), std::move(conv_16_2_mbox_conf), std::move(conv_17_2_mbox_conf));
320 mbox_conf << ReshapeLayer(TensorShape(21U, 1917U)).set_name("mbox_conf/reshape");
321 mbox_conf << SoftmaxLayer().set_name("mbox_conf/softmax");
322 mbox_conf << FlattenLayer().set_name("mbox_conf/flat");
323
Pablo Tellofea8ec32018-11-16 13:25:30 +0000324 const std::vector<float> priorbox_variances = { 0.1f, 0.1f, 0.2f, 0.2f };
325 const float priorbox_offset = 0.5f;
326 const std::vector<float> priorbox_aspect_ratios = { 2.f, 3.f };
327
328 //mbox_priorbox branch
329 SubStream conv_11_mbox_priorbox(conv_11);
330
331 conv_11_mbox_priorbox << PriorBoxLayer(SubStream(graph),
332 PriorBoxLayerInfo({ 60.f }, priorbox_variances, priorbox_offset, true, false, {}, { 2.f }))
333 .set_name("conv11/priorbox");
334
335 SubStream conv_13_mbox_priorbox(conv_13);
336 conv_13_mbox_priorbox << PriorBoxLayer(SubStream(graph),
337 PriorBoxLayerInfo({ 105.f }, priorbox_variances, priorbox_offset, true, false, { 150.f }, priorbox_aspect_ratios))
338 .set_name("conv13/priorbox");
339
340 SubStream conv_14_2_mbox_priorbox(conv_14);
341 conv_14_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
342 PriorBoxLayerInfo({ 150.f }, priorbox_variances, priorbox_offset, true, false, { 195.f }, priorbox_aspect_ratios))
343 .set_name("conv14/priorbox");
344
345 SubStream conv_15_2_mbox_priorbox(conv_15);
346 conv_15_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
347 PriorBoxLayerInfo({ 195.f }, priorbox_variances, priorbox_offset, true, false, { 240.f }, priorbox_aspect_ratios))
348 .set_name("conv15/priorbox");
349
350 SubStream conv_16_2_mbox_priorbox(conv_16);
351 conv_16_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
352 PriorBoxLayerInfo({ 240.f }, priorbox_variances, priorbox_offset, true, false, { 285.f }, priorbox_aspect_ratios))
353 .set_name("conv16/priorbox");
354
355 SubStream conv_17_2_mbox_priorbox(conv_17);
356 conv_17_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
357 PriorBoxLayerInfo({ 285.f }, priorbox_variances, priorbox_offset, true, false, { 300.f }, priorbox_aspect_ratios))
358 .set_name("conv17/priorbox");
359
360 SubStream mbox_priorbox(graph);
361
362 mbox_priorbox << ConcatLayer(
Isabella Gottardic755f782019-07-22 17:40:27 +0100363 (common_params.data_layout == DataLayout::NCHW) ? arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH) : arm_compute::graph::descriptors::ConcatLayerDescriptor(
364 DataLayoutDimension::CHANNEL),
Pablo Tellofea8ec32018-11-16 13:25:30 +0000365 std::move(conv_11_mbox_priorbox), std::move(conv_13_mbox_priorbox), std::move(conv_14_2_mbox_priorbox),
366 std::move(conv_15_2_mbox_priorbox), std::move(conv_16_2_mbox_priorbox), std::move(conv_17_2_mbox_priorbox));
367
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000368 const int num_classes = 21;
369 const bool share_location = true;
370 const DetectionOutputLayerCodeType detection_type = DetectionOutputLayerCodeType::CENTER_SIZE;
371 const int keep_top_k = keep_topk_opt->value();
372 const float nms_threshold = 0.45f;
373 const int label_id_background = 0;
374 const float conf_thrs = 0.25f;
375 const int top_k = 100;
376
377 SubStream detection_ouput(mbox_loc);
378 detection_ouput << DetectionOutputLayer(std::move(mbox_conf), std::move(mbox_priorbox),
379 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 +0100380 detection_ouput << OutputLayer(get_detection_output_accessor(common_params, { input_descriptor.shape }));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000381 }
382
Isabella Gottardic755f782019-07-22 17:40:27 +0100383 ConcatLayer get_node_A_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
384 unsigned int conv_filt,
385 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
386 std::pair<QuantizationInfo, QuantizationInfo> depth_quant_info, std::pair<QuantizationInfo, QuantizationInfo> point_quant_info)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000387 {
388 const std::string total_path = param_path + "_";
389 SubStream sg(master_graph);
390
391 sg << DepthwiseConvolutionLayer(
392 3U, 3U,
393 get_weights_accessor(data_path, total_path + "dw_w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100394 get_weights_accessor(data_path, total_path + "dw_b.npy"),
395 dwc_pad_stride_info, 1, depth_quant_info.first, depth_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000396 .set_name(param_path + "/dw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100397 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/dw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000398
Isabella Gottardic755f782019-07-22 17:40:27 +0100399 sg << ConvolutionLayer(
Pablo Tellofea8ec32018-11-16 13:25:30 +0000400 1U, 1U, conv_filt,
401 get_weights_accessor(data_path, total_path + "w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100402 get_weights_accessor(data_path, total_path + "b.npy"),
403 conv_pad_stride_info, 1, point_quant_info.first, point_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000404 .set_name(param_path + "/pw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100405 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/pw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000406
407 return ConcatLayer(std::move(sg));
408 }
409
Isabella Gottardic755f782019-07-22 17:40:27 +0100410 ConcatLayer get_node_B_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
411 unsigned int conv_filt,
412 PadStrideInfo conv_pad_stride_info_1x1, PadStrideInfo conv_pad_stride_info_3x3,
413 const std::pair<QuantizationInfo, QuantizationInfo> quant_info_1x1, const std::pair<QuantizationInfo, QuantizationInfo> quant_info_3x3)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000414 {
415 const std::string total_path = param_path + "_";
416 SubStream sg(master_graph);
417
418 sg << ConvolutionLayer(
419 1, 1, conv_filt / 2,
Isabella Gottardic755f782019-07-22 17:40:27 +0100420 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
421 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
422 conv_pad_stride_info_1x1, 1, quant_info_1x1.first, quant_info_1x1.second)
423 .set_name(total_path + "1x1/conv")
424 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "1x1/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000425
426 sg << ConvolutionLayer(
427 3, 3, conv_filt,
Isabella Gottardic755f782019-07-22 17:40:27 +0100428 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
429 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
430 conv_pad_stride_info_3x3, 1, quant_info_3x3.first, quant_info_3x3.second)
431 .set_name(total_path + "3x3/conv")
432 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "3x3/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000433
434 return ConcatLayer(std::move(sg));
435 }
436
Isabella Gottardic755f782019-07-22 17:40:27 +0100437 ConcatLayer get_node_C_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
438 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info,
439 const std::pair<QuantizationInfo, QuantizationInfo> quant_info, TensorShape reshape_shape)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000440 {
441 const std::string total_path = param_path + "_";
442 SubStream sg(master_graph);
443 sg << ConvolutionLayer(
444 1U, 1U, conv_filt,
445 get_weights_accessor(data_path, total_path + "w.npy"),
446 get_weights_accessor(data_path, total_path + "b.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100447 conv_pad_stride_info, 1, quant_info.first, quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000448 .set_name(param_path + "/conv");
449 if(common_params.data_layout == DataLayout::NCHW)
450 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100451 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000452 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100453 sg << ReshapeLayer(reshape_shape).set_name(param_path + "/reshape");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000454
455 return ConcatLayer(std::move(sg));
456 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100457
458 void create_graph_qasymm(TensorDescriptor &input_descriptor)
459 {
460 // Get trainable parameters data path
461 std::string data_path = common_params.data_path;
462
463 // Add model path to data path
464 if(!data_path.empty())
465 {
466 data_path += "/cnn_data/ssd_mobilenet_qasymm8_model/";
467 }
468
469 // Quantization info are saved as pair for each (pointwise/depthwise) convolution layer: <weight_quant_info, output_quant_info>
470 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> conv_quant_info =
471 {
472 { QuantizationInfo(0.03624850884079933f, 163), QuantizationInfo(0.22219789028167725f, 113) }, // conv0
473 { QuantizationInfo(0.0028752065263688564f, 113), QuantizationInfo(0.05433657020330429f, 128) }, // conv13_2_1_1
474 { QuantizationInfo(0.0014862528769299388f, 125), QuantizationInfo(0.05037643015384674f, 131) }, // conv13_2_3_3
475 { QuantizationInfo(0.00233650766313076f, 113), QuantizationInfo(0.04468846693634987f, 126) }, // conv13_3_1_1
476 { QuantizationInfo(0.002501056529581547f, 120), QuantizationInfo(0.06026708707213402f, 111) }, // conv13_3_3_3
477 { QuantizationInfo(0.002896666992455721f, 121), QuantizationInfo(0.037775348871946335f, 117) }, // conv13_4_1_1
478 { QuantizationInfo(0.0023875406477600336f, 122), QuantizationInfo(0.03881589323282242f, 108) }, // conv13_4_3_3
479 { QuantizationInfo(0.0022081052884459496f, 77), QuantizationInfo(0.025450613349676132f, 125) }, // conv13_5_1_1
480 { QuantizationInfo(0.00604657270014286f, 121), QuantizationInfo(0.033533502370119095f, 109) } // conv13_5_3_3
481 };
482
483 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> depth_quant_info =
484 {
485 { QuantizationInfo(0.03408717364072f, 131), QuantizationInfo(0.29286590218544006f, 108) }, // dwsc1
486 { QuantizationInfo(0.027518004179000854f, 107), QuantizationInfo(0.20796941220760345, 117) }, // dwsc2
487 { QuantizationInfo(0.052489638328552246f, 85), QuantizationInfo(0.4303881824016571f, 142) }, // dwsc3
488 { QuantizationInfo(0.016570359468460083f, 79), QuantizationInfo(0.10512150079011917f, 116) }, // dwsc4
489 { QuantizationInfo(0.060739465057849884f, 65), QuantizationInfo(0.15331414341926575f, 94) }, // dwsc5
490 { QuantizationInfo(0.01324534136801958f, 124), QuantizationInfo(0.13010895252227783f, 153) }, // dwsc6
491 { QuantizationInfo(0.032326459884643555f, 124), QuantizationInfo(0.11565316468477249, 156) }, // dwsc7
492 { QuantizationInfo(0.029948478564620018f, 155), QuantizationInfo(0.11413891613483429f, 146) }, // dwsc8
493 { QuantizationInfo(0.028054025024175644f, 129), QuantizationInfo(0.1142905130982399f, 140) }, // dwsc9
494 { QuantizationInfo(0.025204822421073914f, 129), QuantizationInfo(0.14668069779872894f, 149) }, // dwsc10
495 { QuantizationInfo(0.019332280382514f, 110), QuantizationInfo(0.1480235457420349f, 91) }, // dwsc11
496 { QuantizationInfo(0.0319712869822979f, 88), QuantizationInfo(0.10424695909023285f, 117) }, // dwsc12
497 { QuantizationInfo(0.04378943517804146f, 164), QuantizationInfo(0.23176774382591248f, 138) } // dwsc13
498 };
499
500 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> point_quant_info =
501 {
502 { QuantizationInfo(0.028777318075299263f, 144), QuantizationInfo(0.2663874328136444f, 121) }, // pw1
503 { QuantizationInfo(0.015796702355146408f, 127), QuantizationInfo(0.1739964485168457f, 111) }, // pw2
504 { QuantizationInfo(0.009349990636110306f, 127), QuantizationInfo(0.1805974692106247f, 104) }, // pw3
505 { QuantizationInfo(0.012920888140797615f, 106), QuantizationInfo(0.1205204650759697f, 100) }, // pw4
506 { QuantizationInfo(0.008119508624076843f, 145), QuantizationInfo(0.12272439152002335f, 97) }, // pw5
507 { QuantizationInfo(0.0070041813887655735f, 115), QuantizationInfo(0.0947074219584465f, 101) }, // pw6
508 { QuantizationInfo(0.004827278666198254f, 115), QuantizationInfo(0.0842885747551918f, 110) }, // pw7
509 { QuantizationInfo(0.004755120258778334f, 128), QuantizationInfo(0.08283159881830215f, 116) }, // pw8
510 { QuantizationInfo(0.007527193054556847f, 142), QuantizationInfo(0.12555131316184998f, 137) }, // pw9
511 { QuantizationInfo(0.006050156895071268f, 109), QuantizationInfo(0.10871313512325287f, 124) }, // pw10
512 { QuantizationInfo(0.00490700313821435f, 127), QuantizationInfo(0.10364262014627457f, 140) }, // pw11
513 { QuantizationInfo(0.006063731852918863, 124), QuantizationInfo(0.11241862177848816f, 125) }, // pw12
514 { QuantizationInfo(0.007901716977357864f, 139), QuantizationInfo(0.49889302253723145f, 141) } // pw13
515 };
516
517 // Quantization info taken from the TfLite SSD MobileNet example
518 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Isabella Gottardic755f782019-07-22 17:40:27 +0100519 // Create core graph
520 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
521 get_weights_accessor(data_path, common_params.image, DataLayout::NHWC));
522 graph << ConvolutionLayer(
523 3U, 3U, 32U,
524 get_weights_accessor(data_path, "conv0_w.npy"),
525 get_weights_accessor(data_path, "conv0_b.npy"),
526 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), 1, conv_quant_info.at(0).first, conv_quant_info.at(0).second)
527 .set_name("conv0");
528 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("conv0/relu");
529 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),
530 point_quant_info.at(0));
531 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),
532 point_quant_info.at(1));
533 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),
534 point_quant_info.at(2));
535 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),
536 point_quant_info.at(3));
537 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),
538 point_quant_info.at(4));
539 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),
540 point_quant_info.at(5));
541 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),
542 point_quant_info.at(6));
543 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),
544 point_quant_info.at(7));
545 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),
546 point_quant_info.at(8));
547 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),
548 point_quant_info.at(9));
549 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),
550 point_quant_info.at(10));
551
552 SubStream conv_13(graph);
553 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),
554 point_quant_info.at(11));
555 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),
556 point_quant_info.at(12));
557 SubStream conv_14(conv_13);
558 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),
559 conv_quant_info.at(2));
560 SubStream conv_15(conv_14);
561 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),
562 conv_quant_info.at(4));
563 SubStream conv_16(conv_15);
564 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),
565 conv_quant_info.at(6));
566 SubStream conv_17(conv_16);
567 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),
568 conv_quant_info.at(8));
569
570 // box_predictor
571 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> box_enc_pred_quant_info =
572 {
573 { QuantizationInfo(0.005202020984143019f, 136), QuantizationInfo(0.08655580133199692f, 183) }, // boxpredictor0_bep
574 { QuantizationInfo(0.003121797926723957f, 132), QuantizationInfo(0.03218776360154152f, 140) }, // boxpredictor1_bep
575 { QuantizationInfo(0.002995674265548587f, 130), QuantizationInfo(0.029072262346744537f, 125) }, // boxpredictor2_bep
576 { QuantizationInfo(0.0023131705820560455f, 130), QuantizationInfo(0.026488754898309708f, 127) }, // boxpredictor3_bep
577 { QuantizationInfo(0.0013905081432312727f, 132), QuantizationInfo(0.0199890099465847f, 137) }, // boxpredictor4_bep
578 { QuantizationInfo(0.00216794665902853f, 121), QuantizationInfo(0.019798893481492996f, 151) } // boxpredictor5_bep
579 };
580
581 const std::vector<TensorShape> box_reshape = // NHWC
582 {
583 TensorShape(4U, 1U, 1083U), // boxpredictor0_bep_reshape
584 TensorShape(4U, 1U, 600U), // boxpredictor1_bep_reshape
585 TensorShape(4U, 1U, 150U), // boxpredictor2_bep_reshape
586 TensorShape(4U, 1U, 54U), // boxpredictor3_bep_reshape
587 TensorShape(4U, 1U, 24U), // boxpredictor4_bep_reshape
588 TensorShape(4U, 1U, 6U) // boxpredictor5_bep_reshape
589 };
590
591 SubStream conv_11_box_enc_pre(graph);
592 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));
593
594 SubStream conv_13_box_enc_pre(conv_13);
595 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));
596
597 SubStream conv_14_2_box_enc_pre(conv_14);
598 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));
599
600 SubStream conv_15_2_box_enc_pre(conv_15);
601 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));
602
603 SubStream conv_16_2_box_enc_pre(conv_16);
604 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));
605
606 SubStream conv_17_2_box_enc_pre(conv_17);
607 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));
608
609 SubStream box_enc_pre(graph);
610 const QuantizationInfo bep_concate_qinfo = QuantizationInfo(0.08655580133199692f, 183);
611 box_enc_pre << ConcatLayer(arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::HEIGHT, bep_concate_qinfo),
612 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),
613 std::move(conv_16_2_box_enc_pre), std::move(conv_17_2_box_enc_pre))
614 .set_name("BoxPredictor/concat");
615 box_enc_pre << ReshapeLayer(TensorShape(4U, 1917U)).set_name("BoxPredictor/reshape");
616
617 // class_predictor
618 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> class_pred_quant_info =
619 {
620 { QuantizationInfo(0.002744135679677129f, 125), QuantizationInfo(0.05746262148022652f, 234) }, // boxpredictor0_cp
621 { QuantizationInfo(0.0024326108396053314f, 80), QuantizationInfo(0.03764628246426582f, 217) }, // boxpredictor1_cp
622 { QuantizationInfo(0.0013898586621508002f, 141), QuantizationInfo(0.034081317484378815f, 214) }, // boxpredictor2_cp
623 { QuantizationInfo(0.0014176908880472183f, 133), QuantizationInfo(0.033889178186655045f, 215) }, // boxpredictor3_cp
624 { QuantizationInfo(0.001090311910957098f, 125), QuantizationInfo(0.02646234817802906f, 230) }, // boxpredictor4_cp
625 { QuantizationInfo(0.001134163816459477f, 115), QuantizationInfo(0.026926767081022263f, 218) } // boxpredictor5_cp
626 };
627
628 const std::vector<TensorShape> class_reshape =
629 {
630 TensorShape(91U, 1083U), // boxpredictor0_cp_reshape
631 TensorShape(91U, 600U), // boxpredictor1_cp_reshape
632 TensorShape(91U, 150U), // boxpredictor2_cp_reshape
633 TensorShape(91U, 54U), // boxpredictor3_cp_reshape
634 TensorShape(91U, 24U), // boxpredictor4_cp_reshape
635 TensorShape(91U, 6U) // boxpredictor5_cp_reshape
636 };
637
638 SubStream conv_11_class_pre(graph);
639 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));
640
641 SubStream conv_13_class_pre(conv_13);
642 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));
643
644 SubStream conv_14_2_class_pre(conv_14);
645 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));
646
647 SubStream conv_15_2_class_pre(conv_15);
648 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));
649
650 SubStream conv_16_2_class_pre(conv_16);
651 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));
652
653 SubStream conv_17_2_class_pre(conv_17);
654 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));
655
656 const QuantizationInfo cp_concate_qinfo = QuantizationInfo(0.0584389753639698f, 230);
657 SubStream class_pred(graph);
658 class_pred << ConcatLayer(
659 arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH, cp_concate_qinfo),
660 std::move(conv_11_class_pre), std::move(conv_13_class_pre), std::move(conv_14_2_class_pre),
661 std::move(conv_15_2_class_pre), std::move(conv_16_2_class_pre), std::move(conv_17_2_class_pre))
662 .set_name("ClassPrediction/concat");
663
664 const QuantizationInfo logistic_out_qinfo = QuantizationInfo(0.00390625f, 0);
665 class_pred << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), logistic_out_qinfo).set_name("ClassPrediction/logistic");
666
667 const int max_detections = 10;
668 const int max_classes_per_detection = 1;
669 const float nms_score_threshold = 0.30000001192092896f;
670 const float nms_iou_threshold = 0.6000000238418579f;
671 const int num_classes = 90;
672 const float x_scale = 10.f;
673 const float y_scale = 10.f;
674 const float h_scale = 5.f;
675 const float w_scale = 5.f;
676 std::array<float, 4> scales = { y_scale, x_scale, w_scale, h_scale };
677 const QuantizationInfo anchors_qinfo = QuantizationInfo(0.006453060545027256f, 0);
678
679 SubStream detection_ouput(box_enc_pre);
680 detection_ouput << DetectionPostProcessLayer(std::move(class_pred),
681 DetectionPostProcessLayerInfo(max_detections, max_classes_per_detection, nms_score_threshold, nms_iou_threshold, num_classes, scales),
682 get_weights_accessor(data_path, "anchors.npy"), anchors_qinfo)
683 .set_name("DetectionPostProcess");
684
685 SubStream ouput_0(detection_ouput);
686 ouput_0 << OutputLayer(get_npy_output_accessor(detection_boxes_opt->value(), TensorShape(4U, 10U), DataType::F32), 0);
687
688 SubStream ouput_1(detection_ouput);
689 ouput_1 << OutputLayer(get_npy_output_accessor(detection_classes_opt->value(), TensorShape(10U), DataType::F32), 1);
690
691 SubStream ouput_2(detection_ouput);
692 ouput_2 << OutputLayer(get_npy_output_accessor(detection_scores_opt->value(), TensorShape(10U), DataType::F32), 2);
693
694 SubStream ouput_3(detection_ouput);
695 ouput_3 << OutputLayer(get_npy_output_accessor(num_detections_opt->value(), TensorShape(1U), DataType::F32), 3);
696 }
Pablo Tellofea8ec32018-11-16 13:25:30 +0000697};
698
699/** Main program for MobileNetSSD
700 *
701 * Model is based on:
702 * http://arxiv.org/abs/1512.02325
703 * SSD: Single Shot MultiBox Detector
704 * Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg
705 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000706 * Provenance: https://github.com/chuanqi305/MobileNet-SSD
707 *
Pablo Tellofea8ec32018-11-16 13:25:30 +0000708 * @note To list all the possible arguments execute the binary appended with the --help option
709 *
710 * @param[in] argc Number of arguments
711 * @param[in] argv Arguments
712 */
713int main(int argc, char **argv)
714{
715 return arm_compute::utils::run_example<GraphSSDMobilenetExample>(argc, argv);
716}