blob: b3476b8e806b10705c187365536a8c1001437378 [file] [log] [blame]
Pablo Tellofea8ec32018-11-16 13:25:30 +00001/*
Georgios Pinitasf52cd782019-03-25 14:06:14 +00002 * Copyright (c) 2018-2019 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;
57 GraphSSDMobilenetExample(GraphSSDMobilenetExample &&) = default; // NOLINT
58 GraphSSDMobilenetExample &operator=(GraphSSDMobilenetExample &&) = default; // NOLINT
59 ~GraphSSDMobilenetExample() override = default;
60 bool do_setup(int argc, char **argv) override
61 {
62 // Parse arguments
63 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010064 cmd_parser.validate();
Pablo Tellofea8ec32018-11-16 13:25:30 +000065
66 // Consume common parameters
67 common_params = consume_common_graph_parameters(common_opts);
68
69 // Return when help menu is requested
70 if(common_params.help)
71 {
72 cmd_parser.print_help(argv[0]);
73 return false;
74 }
75
76 // Print parameter values
77 std::cout << common_params << std::endl;
78
79 // Create input descriptor
80 const TensorShape tensor_shape = permute_shape(TensorShape(300, 300, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
81 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
82
83 // Set graph hints
84 graph << common_params.target
Pablo Tellofea8ec32018-11-16 13:25:30 +000085 << common_params.fast_math_hint;
86
87 // Create core graph
Isabella Gottardic755f782019-07-22 17:40:27 +010088 if(arm_compute::is_data_type_float(common_params.data_type))
89 {
90 create_graph_float(input_descriptor);
91 }
92 else
93 {
94 create_graph_qasymm(input_descriptor);
95 }
Pablo Tellofea8ec32018-11-16 13:25:30 +000096
Isabella Gottardic755f782019-07-22 17:40:27 +010097 // Finalize graph
98 GraphConfig config;
99 config.num_threads = common_params.threads;
100 config.use_tuner = common_params.enable_tuner;
101 config.tuner_file = common_params.tuner_file;
102
103 graph.finalize(common_params.target, config);
104
105 return true;
106 }
107 void do_run() override
108 {
109 // Run graph
110 graph.run();
111 }
112
113private:
114 CommandLineParser cmd_parser;
115 CommonGraphOptions common_opts;
116 SimpleOption<int> *keep_topk_opt{ nullptr };
117 CommonGraphParams common_params;
118 Stream graph;
119
120 SimpleOption<std::string> *detection_boxes_opt{ nullptr };
121 SimpleOption<std::string> *detection_classes_opt{ nullptr };
122 SimpleOption<std::string> *detection_scores_opt{ nullptr };
123 SimpleOption<std::string> *num_detections_opt{ nullptr };
124
125 ConcatLayer get_node_A_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
126 unsigned int conv_filt,
127 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
128 {
129 const std::string total_path = param_path + "_";
130 SubStream sg(master_graph);
131
132 sg << DepthwiseConvolutionLayer(
133 3U, 3U,
134 get_weights_accessor(data_path, total_path + "dw_w.npy"),
135 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
136 dwc_pad_stride_info)
137 .set_name(param_path + "/dw")
138 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "dw_bn_mean.npy"),
139 get_weights_accessor(data_path, total_path + "dw_bn_var.npy"),
140 get_weights_accessor(data_path, total_path + "dw_scale_w.npy"),
141 get_weights_accessor(data_path, total_path + "dw_scale_b.npy"), 0.00001f)
142 .set_name(param_path + "/dw/bn")
143 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "dw/relu")
144
145 << ConvolutionLayer(
146 1U, 1U, conv_filt,
147 get_weights_accessor(data_path, total_path + "w.npy"),
148 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
149 conv_pad_stride_info)
150 .set_name(param_path + "/pw")
151 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "bn_mean.npy"),
152 get_weights_accessor(data_path, total_path + "bn_var.npy"),
153 get_weights_accessor(data_path, total_path + "scale_w.npy"),
154 get_weights_accessor(data_path, total_path + "scale_b.npy"), 0.00001f)
155 .set_name(param_path + "/pw/bn")
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "pw/relu");
157
158 return ConcatLayer(std::move(sg));
159 }
160
161 ConcatLayer get_node_B_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
162 unsigned int conv_filt,
163 PadStrideInfo conv_pad_stride_info_1, PadStrideInfo conv_pad_stride_info_2)
164 {
165 const std::string total_path = param_path + "_";
166 SubStream sg(master_graph);
167
168 sg << ConvolutionLayer(
169 1, 1, conv_filt / 2,
170 get_weights_accessor(data_path, total_path + "1_w.npy"),
171 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
172 conv_pad_stride_info_1)
173 .set_name(total_path + "1/conv")
174 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "1_bn_mean.npy"),
175 get_weights_accessor(data_path, total_path + "1_bn_var.npy"),
176 get_weights_accessor(data_path, total_path + "1_scale_w.npy"),
177 get_weights_accessor(data_path, total_path + "1_scale_b.npy"), 0.00001f)
178 .set_name(total_path + "1/bn")
179 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "1/relu");
180
181 sg << ConvolutionLayer(
182 3, 3, conv_filt,
183 get_weights_accessor(data_path, total_path + "2_w.npy"),
184 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
185 conv_pad_stride_info_2)
186 .set_name(total_path + "2/conv")
187 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "2_bn_mean.npy"),
188 get_weights_accessor(data_path, total_path + "2_bn_var.npy"),
189 get_weights_accessor(data_path, total_path + "2_scale_w.npy"),
190 get_weights_accessor(data_path, total_path + "2_scale_b.npy"), 0.00001f)
191 .set_name(total_path + "2/bn")
192 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "2/relu");
193
194 return ConcatLayer(std::move(sg));
195 }
196
197 ConcatLayer get_node_C_float(IStream &master_graph, const std::string &data_path, std::string &&param_path,
198 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info)
199 {
200 const std::string total_path = param_path + "_";
201 SubStream sg(master_graph);
202 sg << ConvolutionLayer(
203 1U, 1U, conv_filt,
204 get_weights_accessor(data_path, total_path + "w.npy"),
205 get_weights_accessor(data_path, total_path + "b.npy"),
206 conv_pad_stride_info)
207 .set_name(param_path + "/conv");
208 if(common_params.data_layout == DataLayout::NCHW)
209 {
210 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC).set_name(param_path + "/perm");
211 }
212 sg << FlattenLayer().set_name(param_path + "/flat");
213
214 return ConcatLayer(std::move(sg));
215 }
216
217 void create_graph_float(TensorDescriptor &input_descriptor)
218 {
Pablo Tellofea8ec32018-11-16 13:25:30 +0000219 // Create a preprocessor object
220 const std::array<float, 3> mean_rgb{ { 127.5f, 127.5f, 127.5f } };
Georgios Pinitasb54c6442019-04-03 13:18:14 +0100221 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb, true, 0.007843f);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000222
223 // Get trainable parameters data path
224 std::string data_path = common_params.data_path;
225
226 // Add model path to data path
227 if(!data_path.empty())
228 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100229 data_path += "/cnn_data/ssd_mobilenet_model/";
Pablo Tellofea8ec32018-11-16 13:25:30 +0000230 }
231
232 graph << InputLayer(input_descriptor,
233 get_input_accessor(common_params, std::move(preprocessor)));
234
235 SubStream conv_11(graph);
236 conv_11 << ConvolutionLayer(
237 3U, 3U, 32U,
238 get_weights_accessor(data_path, "conv0_w.npy"),
239 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
240 PadStrideInfo(2, 2, 1, 1))
241 .set_name("conv0");
242 conv_11 << BatchNormalizationLayer(get_weights_accessor(data_path, "conv0_bn_mean.npy"),
243 get_weights_accessor(data_path, "conv0_bn_var.npy"),
244 get_weights_accessor(data_path, "conv0_scale_w.npy"),
245 get_weights_accessor(data_path, "conv0_scale_b.npy"), 0.00001f)
246 .set_name("conv0/bn")
247 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/relu");
248
Isabella Gottardic755f782019-07-22 17:40:27 +0100249 conv_11 << get_node_A_float(conv_11, data_path, "conv1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
250 conv_11 << get_node_A_float(conv_11, data_path, "conv2", 128, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
251 conv_11 << get_node_A_float(conv_11, data_path, "conv3", 128, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
252 conv_11 << get_node_A_float(conv_11, data_path, "conv4", 256, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
253 conv_11 << get_node_A_float(conv_11, data_path, "conv5", 256, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
254 conv_11 << get_node_A_float(conv_11, data_path, "conv6", 512, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
255 conv_11 << get_node_A_float(conv_11, data_path, "conv7", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
256 conv_11 << get_node_A_float(conv_11, data_path, "conv8", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
257 conv_11 << get_node_A_float(conv_11, data_path, "conv9", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
258 conv_11 << get_node_A_float(conv_11, data_path, "conv10", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
259 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 +0000260
261 SubStream conv_13(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100262 conv_13 << get_node_A_float(conv_11, data_path, "conv12", 1024, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
263 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 +0000264
265 SubStream conv_14(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100266 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 +0000267
268 SubStream conv_15(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100269 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 +0000270
271 SubStream conv_16(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100272 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 +0000273
274 SubStream conv_17(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100275 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 +0000276
277 //mbox_loc
278 SubStream conv_11_mbox_loc(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100279 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 +0000280
281 SubStream conv_13_mbox_loc(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100282 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 +0000283
284 SubStream conv_14_2_mbox_loc(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100285 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 +0000286
287 SubStream conv_15_2_mbox_loc(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100288 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 +0000289
290 SubStream conv_16_2_mbox_loc(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100291 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 +0000292
293 SubStream conv_17_2_mbox_loc(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100294 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 +0000295
296 SubStream mbox_loc(graph);
297 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),
298 std::move(conv_16_2_mbox_loc), std::move(conv_17_2_mbox_loc));
299
Pablo Tellofea8ec32018-11-16 13:25:30 +0000300 //mbox_conf
301 SubStream conv_11_mbox_conf(conv_11);
Isabella Gottardic755f782019-07-22 17:40:27 +0100302 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 +0000303
304 SubStream conv_13_mbox_conf(conv_13);
Isabella Gottardic755f782019-07-22 17:40:27 +0100305 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 +0000306
307 SubStream conv_14_2_mbox_conf(conv_14);
Isabella Gottardic755f782019-07-22 17:40:27 +0100308 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 +0000309
310 SubStream conv_15_2_mbox_conf(conv_15);
Isabella Gottardic755f782019-07-22 17:40:27 +0100311 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 +0000312
313 SubStream conv_16_2_mbox_conf(conv_16);
Isabella Gottardic755f782019-07-22 17:40:27 +0100314 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 +0000315
316 SubStream conv_17_2_mbox_conf(conv_17);
Isabella Gottardic755f782019-07-22 17:40:27 +0100317 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 +0000318
319 SubStream mbox_conf(graph);
320 mbox_conf << ConcatLayer(std::move(conv_11_mbox_conf), std::move(conv_13_mbox_conf), std::move(conv_14_2_mbox_conf),
321 std::move(conv_15_2_mbox_conf), std::move(conv_16_2_mbox_conf), std::move(conv_17_2_mbox_conf));
322 mbox_conf << ReshapeLayer(TensorShape(21U, 1917U)).set_name("mbox_conf/reshape");
323 mbox_conf << SoftmaxLayer().set_name("mbox_conf/softmax");
324 mbox_conf << FlattenLayer().set_name("mbox_conf/flat");
325
Pablo Tellofea8ec32018-11-16 13:25:30 +0000326 const std::vector<float> priorbox_variances = { 0.1f, 0.1f, 0.2f, 0.2f };
327 const float priorbox_offset = 0.5f;
328 const std::vector<float> priorbox_aspect_ratios = { 2.f, 3.f };
329
330 //mbox_priorbox branch
331 SubStream conv_11_mbox_priorbox(conv_11);
332
333 conv_11_mbox_priorbox << PriorBoxLayer(SubStream(graph),
334 PriorBoxLayerInfo({ 60.f }, priorbox_variances, priorbox_offset, true, false, {}, { 2.f }))
335 .set_name("conv11/priorbox");
336
337 SubStream conv_13_mbox_priorbox(conv_13);
338 conv_13_mbox_priorbox << PriorBoxLayer(SubStream(graph),
339 PriorBoxLayerInfo({ 105.f }, priorbox_variances, priorbox_offset, true, false, { 150.f }, priorbox_aspect_ratios))
340 .set_name("conv13/priorbox");
341
342 SubStream conv_14_2_mbox_priorbox(conv_14);
343 conv_14_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
344 PriorBoxLayerInfo({ 150.f }, priorbox_variances, priorbox_offset, true, false, { 195.f }, priorbox_aspect_ratios))
345 .set_name("conv14/priorbox");
346
347 SubStream conv_15_2_mbox_priorbox(conv_15);
348 conv_15_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
349 PriorBoxLayerInfo({ 195.f }, priorbox_variances, priorbox_offset, true, false, { 240.f }, priorbox_aspect_ratios))
350 .set_name("conv15/priorbox");
351
352 SubStream conv_16_2_mbox_priorbox(conv_16);
353 conv_16_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
354 PriorBoxLayerInfo({ 240.f }, priorbox_variances, priorbox_offset, true, false, { 285.f }, priorbox_aspect_ratios))
355 .set_name("conv16/priorbox");
356
357 SubStream conv_17_2_mbox_priorbox(conv_17);
358 conv_17_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
359 PriorBoxLayerInfo({ 285.f }, priorbox_variances, priorbox_offset, true, false, { 300.f }, priorbox_aspect_ratios))
360 .set_name("conv17/priorbox");
361
362 SubStream mbox_priorbox(graph);
363
364 mbox_priorbox << ConcatLayer(
Isabella Gottardic755f782019-07-22 17:40:27 +0100365 (common_params.data_layout == DataLayout::NCHW) ? arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH) : arm_compute::graph::descriptors::ConcatLayerDescriptor(
366 DataLayoutDimension::CHANNEL),
Pablo Tellofea8ec32018-11-16 13:25:30 +0000367 std::move(conv_11_mbox_priorbox), std::move(conv_13_mbox_priorbox), std::move(conv_14_2_mbox_priorbox),
368 std::move(conv_15_2_mbox_priorbox), std::move(conv_16_2_mbox_priorbox), std::move(conv_17_2_mbox_priorbox));
369
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000370 const int num_classes = 21;
371 const bool share_location = true;
372 const DetectionOutputLayerCodeType detection_type = DetectionOutputLayerCodeType::CENTER_SIZE;
373 const int keep_top_k = keep_topk_opt->value();
374 const float nms_threshold = 0.45f;
375 const int label_id_background = 0;
376 const float conf_thrs = 0.25f;
377 const int top_k = 100;
378
379 SubStream detection_ouput(mbox_loc);
380 detection_ouput << DetectionOutputLayer(std::move(mbox_conf), std::move(mbox_priorbox),
381 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 +0100382 detection_ouput << OutputLayer(get_detection_output_accessor(common_params, { input_descriptor.shape }));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000383 }
384
Isabella Gottardic755f782019-07-22 17:40:27 +0100385 ConcatLayer get_node_A_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
386 unsigned int conv_filt,
387 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
388 std::pair<QuantizationInfo, QuantizationInfo> depth_quant_info, std::pair<QuantizationInfo, QuantizationInfo> point_quant_info)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000389 {
390 const std::string total_path = param_path + "_";
391 SubStream sg(master_graph);
392
393 sg << DepthwiseConvolutionLayer(
394 3U, 3U,
395 get_weights_accessor(data_path, total_path + "dw_w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100396 get_weights_accessor(data_path, total_path + "dw_b.npy"),
397 dwc_pad_stride_info, 1, depth_quant_info.first, depth_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000398 .set_name(param_path + "/dw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100399 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/dw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000400
Isabella Gottardic755f782019-07-22 17:40:27 +0100401 sg << ConvolutionLayer(
Pablo Tellofea8ec32018-11-16 13:25:30 +0000402 1U, 1U, conv_filt,
403 get_weights_accessor(data_path, total_path + "w.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100404 get_weights_accessor(data_path, total_path + "b.npy"),
405 conv_pad_stride_info, 1, point_quant_info.first, point_quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000406 .set_name(param_path + "/pw")
Isabella Gottardic755f782019-07-22 17:40:27 +0100407 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(param_path + "/pw/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000408
409 return ConcatLayer(std::move(sg));
410 }
411
Isabella Gottardic755f782019-07-22 17:40:27 +0100412 ConcatLayer get_node_B_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
413 unsigned int conv_filt,
414 PadStrideInfo conv_pad_stride_info_1x1, PadStrideInfo conv_pad_stride_info_3x3,
415 const std::pair<QuantizationInfo, QuantizationInfo> quant_info_1x1, const std::pair<QuantizationInfo, QuantizationInfo> quant_info_3x3)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000416 {
417 const std::string total_path = param_path + "_";
418 SubStream sg(master_graph);
419
420 sg << ConvolutionLayer(
421 1, 1, conv_filt / 2,
Isabella Gottardic755f782019-07-22 17:40:27 +0100422 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
423 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
424 conv_pad_stride_info_1x1, 1, quant_info_1x1.first, quant_info_1x1.second)
425 .set_name(total_path + "1x1/conv")
426 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "1x1/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000427
428 sg << ConvolutionLayer(
429 3, 3, conv_filt,
Isabella Gottardic755f782019-07-22 17:40:27 +0100430 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
431 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
432 conv_pad_stride_info_3x3, 1, quant_info_3x3.first, quant_info_3x3.second)
433 .set_name(total_path + "3x3/conv")
434 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "3x3/conv/relu6");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000435
436 return ConcatLayer(std::move(sg));
437 }
438
Isabella Gottardic755f782019-07-22 17:40:27 +0100439 ConcatLayer get_node_C_qasymm(IStream &master_graph, const std::string &data_path, std::string &&param_path,
440 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info,
441 const std::pair<QuantizationInfo, QuantizationInfo> quant_info, TensorShape reshape_shape)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000442 {
443 const std::string total_path = param_path + "_";
444 SubStream sg(master_graph);
445 sg << ConvolutionLayer(
446 1U, 1U, conv_filt,
447 get_weights_accessor(data_path, total_path + "w.npy"),
448 get_weights_accessor(data_path, total_path + "b.npy"),
Isabella Gottardic755f782019-07-22 17:40:27 +0100449 conv_pad_stride_info, 1, quant_info.first, quant_info.second)
Pablo Tellofea8ec32018-11-16 13:25:30 +0000450 .set_name(param_path + "/conv");
451 if(common_params.data_layout == DataLayout::NCHW)
452 {
Isabella Gottardic755f782019-07-22 17:40:27 +0100453 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC);
Pablo Tellofea8ec32018-11-16 13:25:30 +0000454 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100455 sg << ReshapeLayer(reshape_shape).set_name(param_path + "/reshape");
Pablo Tellofea8ec32018-11-16 13:25:30 +0000456
457 return ConcatLayer(std::move(sg));
458 }
Isabella Gottardic755f782019-07-22 17:40:27 +0100459
460 void create_graph_qasymm(TensorDescriptor &input_descriptor)
461 {
462 // Get trainable parameters data path
463 std::string data_path = common_params.data_path;
464
465 // Add model path to data path
466 if(!data_path.empty())
467 {
468 data_path += "/cnn_data/ssd_mobilenet_qasymm8_model/";
469 }
470
471 // Quantization info are saved as pair for each (pointwise/depthwise) convolution layer: <weight_quant_info, output_quant_info>
472 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> conv_quant_info =
473 {
474 { QuantizationInfo(0.03624850884079933f, 163), QuantizationInfo(0.22219789028167725f, 113) }, // conv0
475 { QuantizationInfo(0.0028752065263688564f, 113), QuantizationInfo(0.05433657020330429f, 128) }, // conv13_2_1_1
476 { QuantizationInfo(0.0014862528769299388f, 125), QuantizationInfo(0.05037643015384674f, 131) }, // conv13_2_3_3
477 { QuantizationInfo(0.00233650766313076f, 113), QuantizationInfo(0.04468846693634987f, 126) }, // conv13_3_1_1
478 { QuantizationInfo(0.002501056529581547f, 120), QuantizationInfo(0.06026708707213402f, 111) }, // conv13_3_3_3
479 { QuantizationInfo(0.002896666992455721f, 121), QuantizationInfo(0.037775348871946335f, 117) }, // conv13_4_1_1
480 { QuantizationInfo(0.0023875406477600336f, 122), QuantizationInfo(0.03881589323282242f, 108) }, // conv13_4_3_3
481 { QuantizationInfo(0.0022081052884459496f, 77), QuantizationInfo(0.025450613349676132f, 125) }, // conv13_5_1_1
482 { QuantizationInfo(0.00604657270014286f, 121), QuantizationInfo(0.033533502370119095f, 109) } // conv13_5_3_3
483 };
484
485 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> depth_quant_info =
486 {
487 { QuantizationInfo(0.03408717364072f, 131), QuantizationInfo(0.29286590218544006f, 108) }, // dwsc1
488 { QuantizationInfo(0.027518004179000854f, 107), QuantizationInfo(0.20796941220760345, 117) }, // dwsc2
489 { QuantizationInfo(0.052489638328552246f, 85), QuantizationInfo(0.4303881824016571f, 142) }, // dwsc3
490 { QuantizationInfo(0.016570359468460083f, 79), QuantizationInfo(0.10512150079011917f, 116) }, // dwsc4
491 { QuantizationInfo(0.060739465057849884f, 65), QuantizationInfo(0.15331414341926575f, 94) }, // dwsc5
492 { QuantizationInfo(0.01324534136801958f, 124), QuantizationInfo(0.13010895252227783f, 153) }, // dwsc6
493 { QuantizationInfo(0.032326459884643555f, 124), QuantizationInfo(0.11565316468477249, 156) }, // dwsc7
494 { QuantizationInfo(0.029948478564620018f, 155), QuantizationInfo(0.11413891613483429f, 146) }, // dwsc8
495 { QuantizationInfo(0.028054025024175644f, 129), QuantizationInfo(0.1142905130982399f, 140) }, // dwsc9
496 { QuantizationInfo(0.025204822421073914f, 129), QuantizationInfo(0.14668069779872894f, 149) }, // dwsc10
497 { QuantizationInfo(0.019332280382514f, 110), QuantizationInfo(0.1480235457420349f, 91) }, // dwsc11
498 { QuantizationInfo(0.0319712869822979f, 88), QuantizationInfo(0.10424695909023285f, 117) }, // dwsc12
499 { QuantizationInfo(0.04378943517804146f, 164), QuantizationInfo(0.23176774382591248f, 138) } // dwsc13
500 };
501
502 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> point_quant_info =
503 {
504 { QuantizationInfo(0.028777318075299263f, 144), QuantizationInfo(0.2663874328136444f, 121) }, // pw1
505 { QuantizationInfo(0.015796702355146408f, 127), QuantizationInfo(0.1739964485168457f, 111) }, // pw2
506 { QuantizationInfo(0.009349990636110306f, 127), QuantizationInfo(0.1805974692106247f, 104) }, // pw3
507 { QuantizationInfo(0.012920888140797615f, 106), QuantizationInfo(0.1205204650759697f, 100) }, // pw4
508 { QuantizationInfo(0.008119508624076843f, 145), QuantizationInfo(0.12272439152002335f, 97) }, // pw5
509 { QuantizationInfo(0.0070041813887655735f, 115), QuantizationInfo(0.0947074219584465f, 101) }, // pw6
510 { QuantizationInfo(0.004827278666198254f, 115), QuantizationInfo(0.0842885747551918f, 110) }, // pw7
511 { QuantizationInfo(0.004755120258778334f, 128), QuantizationInfo(0.08283159881830215f, 116) }, // pw8
512 { QuantizationInfo(0.007527193054556847f, 142), QuantizationInfo(0.12555131316184998f, 137) }, // pw9
513 { QuantizationInfo(0.006050156895071268f, 109), QuantizationInfo(0.10871313512325287f, 124) }, // pw10
514 { QuantizationInfo(0.00490700313821435f, 127), QuantizationInfo(0.10364262014627457f, 140) }, // pw11
515 { QuantizationInfo(0.006063731852918863, 124), QuantizationInfo(0.11241862177848816f, 125) }, // pw12
516 { QuantizationInfo(0.007901716977357864f, 139), QuantizationInfo(0.49889302253723145f, 141) } // pw13
517 };
518
519 // Quantization info taken from the TfLite SSD MobileNet example
520 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Isabella Gottardic755f782019-07-22 17:40:27 +0100521 // Create core graph
522 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
523 get_weights_accessor(data_path, common_params.image, DataLayout::NHWC));
524 graph << ConvolutionLayer(
525 3U, 3U, 32U,
526 get_weights_accessor(data_path, "conv0_w.npy"),
527 get_weights_accessor(data_path, "conv0_b.npy"),
528 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::CEIL), 1, conv_quant_info.at(0).first, conv_quant_info.at(0).second)
529 .set_name("conv0");
530 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("conv0/relu");
531 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),
532 point_quant_info.at(0));
533 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),
534 point_quant_info.at(1));
535 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),
536 point_quant_info.at(2));
537 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),
538 point_quant_info.at(3));
539 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),
540 point_quant_info.at(4));
541 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),
542 point_quant_info.at(5));
543 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),
544 point_quant_info.at(6));
545 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),
546 point_quant_info.at(7));
547 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),
548 point_quant_info.at(8));
549 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),
550 point_quant_info.at(9));
551 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),
552 point_quant_info.at(10));
553
554 SubStream conv_13(graph);
555 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),
556 point_quant_info.at(11));
557 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),
558 point_quant_info.at(12));
559 SubStream conv_14(conv_13);
560 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),
561 conv_quant_info.at(2));
562 SubStream conv_15(conv_14);
563 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),
564 conv_quant_info.at(4));
565 SubStream conv_16(conv_15);
566 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),
567 conv_quant_info.at(6));
568 SubStream conv_17(conv_16);
569 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),
570 conv_quant_info.at(8));
571
572 // box_predictor
573 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> box_enc_pred_quant_info =
574 {
575 { QuantizationInfo(0.005202020984143019f, 136), QuantizationInfo(0.08655580133199692f, 183) }, // boxpredictor0_bep
576 { QuantizationInfo(0.003121797926723957f, 132), QuantizationInfo(0.03218776360154152f, 140) }, // boxpredictor1_bep
577 { QuantizationInfo(0.002995674265548587f, 130), QuantizationInfo(0.029072262346744537f, 125) }, // boxpredictor2_bep
578 { QuantizationInfo(0.0023131705820560455f, 130), QuantizationInfo(0.026488754898309708f, 127) }, // boxpredictor3_bep
579 { QuantizationInfo(0.0013905081432312727f, 132), QuantizationInfo(0.0199890099465847f, 137) }, // boxpredictor4_bep
580 { QuantizationInfo(0.00216794665902853f, 121), QuantizationInfo(0.019798893481492996f, 151) } // boxpredictor5_bep
581 };
582
583 const std::vector<TensorShape> box_reshape = // NHWC
584 {
585 TensorShape(4U, 1U, 1083U), // boxpredictor0_bep_reshape
586 TensorShape(4U, 1U, 600U), // boxpredictor1_bep_reshape
587 TensorShape(4U, 1U, 150U), // boxpredictor2_bep_reshape
588 TensorShape(4U, 1U, 54U), // boxpredictor3_bep_reshape
589 TensorShape(4U, 1U, 24U), // boxpredictor4_bep_reshape
590 TensorShape(4U, 1U, 6U) // boxpredictor5_bep_reshape
591 };
592
593 SubStream conv_11_box_enc_pre(graph);
594 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));
595
596 SubStream conv_13_box_enc_pre(conv_13);
597 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));
598
599 SubStream conv_14_2_box_enc_pre(conv_14);
600 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));
601
602 SubStream conv_15_2_box_enc_pre(conv_15);
603 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));
604
605 SubStream conv_16_2_box_enc_pre(conv_16);
606 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));
607
608 SubStream conv_17_2_box_enc_pre(conv_17);
609 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));
610
611 SubStream box_enc_pre(graph);
612 const QuantizationInfo bep_concate_qinfo = QuantizationInfo(0.08655580133199692f, 183);
613 box_enc_pre << ConcatLayer(arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::HEIGHT, bep_concate_qinfo),
614 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),
615 std::move(conv_16_2_box_enc_pre), std::move(conv_17_2_box_enc_pre))
616 .set_name("BoxPredictor/concat");
617 box_enc_pre << ReshapeLayer(TensorShape(4U, 1917U)).set_name("BoxPredictor/reshape");
618
619 // class_predictor
620 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> class_pred_quant_info =
621 {
622 { QuantizationInfo(0.002744135679677129f, 125), QuantizationInfo(0.05746262148022652f, 234) }, // boxpredictor0_cp
623 { QuantizationInfo(0.0024326108396053314f, 80), QuantizationInfo(0.03764628246426582f, 217) }, // boxpredictor1_cp
624 { QuantizationInfo(0.0013898586621508002f, 141), QuantizationInfo(0.034081317484378815f, 214) }, // boxpredictor2_cp
625 { QuantizationInfo(0.0014176908880472183f, 133), QuantizationInfo(0.033889178186655045f, 215) }, // boxpredictor3_cp
626 { QuantizationInfo(0.001090311910957098f, 125), QuantizationInfo(0.02646234817802906f, 230) }, // boxpredictor4_cp
627 { QuantizationInfo(0.001134163816459477f, 115), QuantizationInfo(0.026926767081022263f, 218) } // boxpredictor5_cp
628 };
629
630 const std::vector<TensorShape> class_reshape =
631 {
632 TensorShape(91U, 1083U), // boxpredictor0_cp_reshape
633 TensorShape(91U, 600U), // boxpredictor1_cp_reshape
634 TensorShape(91U, 150U), // boxpredictor2_cp_reshape
635 TensorShape(91U, 54U), // boxpredictor3_cp_reshape
636 TensorShape(91U, 24U), // boxpredictor4_cp_reshape
637 TensorShape(91U, 6U) // boxpredictor5_cp_reshape
638 };
639
640 SubStream conv_11_class_pre(graph);
641 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));
642
643 SubStream conv_13_class_pre(conv_13);
644 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));
645
646 SubStream conv_14_2_class_pre(conv_14);
647 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));
648
649 SubStream conv_15_2_class_pre(conv_15);
650 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));
651
652 SubStream conv_16_2_class_pre(conv_16);
653 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));
654
655 SubStream conv_17_2_class_pre(conv_17);
656 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));
657
658 const QuantizationInfo cp_concate_qinfo = QuantizationInfo(0.0584389753639698f, 230);
659 SubStream class_pred(graph);
660 class_pred << ConcatLayer(
661 arm_compute::graph::descriptors::ConcatLayerDescriptor(DataLayoutDimension::WIDTH, cp_concate_qinfo),
662 std::move(conv_11_class_pre), std::move(conv_13_class_pre), std::move(conv_14_2_class_pre),
663 std::move(conv_15_2_class_pre), std::move(conv_16_2_class_pre), std::move(conv_17_2_class_pre))
664 .set_name("ClassPrediction/concat");
665
666 const QuantizationInfo logistic_out_qinfo = QuantizationInfo(0.00390625f, 0);
667 class_pred << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), logistic_out_qinfo).set_name("ClassPrediction/logistic");
668
669 const int max_detections = 10;
670 const int max_classes_per_detection = 1;
671 const float nms_score_threshold = 0.30000001192092896f;
672 const float nms_iou_threshold = 0.6000000238418579f;
673 const int num_classes = 90;
674 const float x_scale = 10.f;
675 const float y_scale = 10.f;
676 const float h_scale = 5.f;
677 const float w_scale = 5.f;
678 std::array<float, 4> scales = { y_scale, x_scale, w_scale, h_scale };
679 const QuantizationInfo anchors_qinfo = QuantizationInfo(0.006453060545027256f, 0);
680
681 SubStream detection_ouput(box_enc_pre);
682 detection_ouput << DetectionPostProcessLayer(std::move(class_pred),
683 DetectionPostProcessLayerInfo(max_detections, max_classes_per_detection, nms_score_threshold, nms_iou_threshold, num_classes, scales),
684 get_weights_accessor(data_path, "anchors.npy"), anchors_qinfo)
685 .set_name("DetectionPostProcess");
686
687 SubStream ouput_0(detection_ouput);
688 ouput_0 << OutputLayer(get_npy_output_accessor(detection_boxes_opt->value(), TensorShape(4U, 10U), DataType::F32), 0);
689
690 SubStream ouput_1(detection_ouput);
691 ouput_1 << OutputLayer(get_npy_output_accessor(detection_classes_opt->value(), TensorShape(10U), DataType::F32), 1);
692
693 SubStream ouput_2(detection_ouput);
694 ouput_2 << OutputLayer(get_npy_output_accessor(detection_scores_opt->value(), TensorShape(10U), DataType::F32), 2);
695
696 SubStream ouput_3(detection_ouput);
697 ouput_3 << OutputLayer(get_npy_output_accessor(num_detections_opt->value(), TensorShape(1U), DataType::F32), 3);
698 }
Pablo Tellofea8ec32018-11-16 13:25:30 +0000699};
700
701/** Main program for MobileNetSSD
702 *
703 * Model is based on:
704 * http://arxiv.org/abs/1512.02325
705 * SSD: Single Shot MultiBox Detector
706 * Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg
707 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000708 * Provenance: https://github.com/chuanqi305/MobileNet-SSD
709 *
Pablo Tellofea8ec32018-11-16 13:25:30 +0000710 * @note To list all the possible arguments execute the binary appended with the --help option
711 *
712 * @param[in] argc Number of arguments
713 * @param[in] argv Arguments
714 */
715int main(int argc, char **argv)
716{
717 return arm_compute::utils::run_example<GraphSSDMobilenetExample>(argc, argv);
718}