blob: 92abd6a21394a9384f28daa10dcd17aa008fe1d0 [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);
44 keep_topk_opt->set_help("Top k detections results per image.");
Pablo Tellofea8ec32018-11-16 13:25:30 +000045 }
46 GraphSSDMobilenetExample(const GraphSSDMobilenetExample &) = delete;
47 GraphSSDMobilenetExample &operator=(const GraphSSDMobilenetExample &) = delete;
48 GraphSSDMobilenetExample(GraphSSDMobilenetExample &&) = default; // NOLINT
49 GraphSSDMobilenetExample &operator=(GraphSSDMobilenetExample &&) = default; // NOLINT
50 ~GraphSSDMobilenetExample() override = default;
51 bool do_setup(int argc, char **argv) override
52 {
53 // Parse arguments
54 cmd_parser.parse(argc, argv);
55
56 // Consume common parameters
57 common_params = consume_common_graph_parameters(common_opts);
58
59 // Return when help menu is requested
60 if(common_params.help)
61 {
62 cmd_parser.print_help(argv[0]);
63 return false;
64 }
65
66 // Print parameter values
67 std::cout << common_params << std::endl;
68
69 // Create input descriptor
70 const TensorShape tensor_shape = permute_shape(TensorShape(300, 300, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
71 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
72
73 // Set graph hints
74 graph << common_params.target
Georgios Pinitasf52cd782019-03-25 14:06:14 +000075 << DepthwiseConvolutionMethod::Optimized3x3 // TODO(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method
Pablo Tellofea8ec32018-11-16 13:25:30 +000076 << common_params.fast_math_hint;
77
78 // Create core graph
79 std::string model_path = "/cnn_data/ssd_mobilenet_model/";
80
81 // Create a preprocessor object
82 const std::array<float, 3> mean_rgb{ { 127.5f, 127.5f, 127.5f } };
83 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb, 0.007843f);
84
85 // Get trainable parameters data path
86 std::string data_path = common_params.data_path;
87
88 // Add model path to data path
89 if(!data_path.empty())
90 {
91 data_path += model_path;
92 }
93
94 graph << InputLayer(input_descriptor,
95 get_input_accessor(common_params, std::move(preprocessor)));
96
97 SubStream conv_11(graph);
98 conv_11 << ConvolutionLayer(
99 3U, 3U, 32U,
100 get_weights_accessor(data_path, "conv0_w.npy"),
101 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
102 PadStrideInfo(2, 2, 1, 1))
103 .set_name("conv0");
104 conv_11 << BatchNormalizationLayer(get_weights_accessor(data_path, "conv0_bn_mean.npy"),
105 get_weights_accessor(data_path, "conv0_bn_var.npy"),
106 get_weights_accessor(data_path, "conv0_scale_w.npy"),
107 get_weights_accessor(data_path, "conv0_scale_b.npy"), 0.00001f)
108 .set_name("conv0/bn")
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/relu");
110
111 conv_11 << get_node_A(conv_11, data_path, "conv1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
112 conv_11 << get_node_A(conv_11, data_path, "conv2", 128, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
113 conv_11 << get_node_A(conv_11, data_path, "conv3", 128, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
114 conv_11 << get_node_A(conv_11, data_path, "conv4", 256, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
115 conv_11 << get_node_A(conv_11, data_path, "conv5", 256, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
116 conv_11 << get_node_A(conv_11, data_path, "conv6", 512, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
117 conv_11 << get_node_A(conv_11, data_path, "conv7", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
118 conv_11 << get_node_A(conv_11, data_path, "conv8", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
119 conv_11 << get_node_A(conv_11, data_path, "conv9", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
120 conv_11 << get_node_A(conv_11, data_path, "conv10", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
121 conv_11 << get_node_A(conv_11, data_path, "conv11", 512, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
122
123 SubStream conv_13(conv_11);
124 conv_13 << get_node_A(conv_11, data_path, "conv12", 1024, PadStrideInfo(2, 2, 1, 1), PadStrideInfo(1, 1, 0, 0));
125 conv_13 << get_node_A(conv_13, data_path, "conv13", 1024, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
126
127 SubStream conv_14(conv_13);
128 conv_14 << get_node_B(conv_13, data_path, "conv14", 512, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
129
130 SubStream conv_15(conv_14);
131 conv_15 << get_node_B(conv_14, data_path, "conv15", 256, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
132
133 SubStream conv_16(conv_15);
134 conv_16 << get_node_B(conv_15, data_path, "conv16", 256, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
135
136 SubStream conv_17(conv_16);
137 conv_17 << get_node_B(conv_16, data_path, "conv17", 128, PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 2, 1, 1));
138
139 //mbox_loc
140 SubStream conv_11_mbox_loc(conv_11);
141 conv_11_mbox_loc << get_node_C(conv_11, data_path, "conv11_mbox_loc", 12, PadStrideInfo(1, 1, 0, 0));
142
143 SubStream conv_13_mbox_loc(conv_13);
144 conv_13_mbox_loc << get_node_C(conv_13, data_path, "conv13_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
145
146 SubStream conv_14_2_mbox_loc(conv_14);
147 conv_14_2_mbox_loc << get_node_C(conv_14, data_path, "conv14_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
148
149 SubStream conv_15_2_mbox_loc(conv_15);
150 conv_15_2_mbox_loc << get_node_C(conv_15, data_path, "conv15_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
151
152 SubStream conv_16_2_mbox_loc(conv_16);
153 conv_16_2_mbox_loc << get_node_C(conv_16, data_path, "conv16_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
154
155 SubStream conv_17_2_mbox_loc(conv_17);
156 conv_17_2_mbox_loc << get_node_C(conv_17, data_path, "conv17_2_mbox_loc", 24, PadStrideInfo(1, 1, 0, 0));
157
158 SubStream mbox_loc(graph);
159 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),
160 std::move(conv_16_2_mbox_loc), std::move(conv_17_2_mbox_loc));
161
Pablo Tellofea8ec32018-11-16 13:25:30 +0000162 //mbox_conf
163 SubStream conv_11_mbox_conf(conv_11);
164 conv_11_mbox_conf << get_node_C(conv_11, data_path, "conv11_mbox_conf", 63, PadStrideInfo(1, 1, 0, 0));
165
166 SubStream conv_13_mbox_conf(conv_13);
167 conv_13_mbox_conf << get_node_C(conv_13, data_path, "conv13_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
168
169 SubStream conv_14_2_mbox_conf(conv_14);
170 conv_14_2_mbox_conf << get_node_C(conv_14, data_path, "conv14_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
171
172 SubStream conv_15_2_mbox_conf(conv_15);
173 conv_15_2_mbox_conf << get_node_C(conv_15, data_path, "conv15_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
174
175 SubStream conv_16_2_mbox_conf(conv_16);
176 conv_16_2_mbox_conf << get_node_C(conv_16, data_path, "conv16_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
177
178 SubStream conv_17_2_mbox_conf(conv_17);
179 conv_17_2_mbox_conf << get_node_C(conv_17, data_path, "conv17_2_mbox_conf", 126, PadStrideInfo(1, 1, 0, 0));
180
181 SubStream mbox_conf(graph);
182 mbox_conf << ConcatLayer(std::move(conv_11_mbox_conf), std::move(conv_13_mbox_conf), std::move(conv_14_2_mbox_conf),
183 std::move(conv_15_2_mbox_conf), std::move(conv_16_2_mbox_conf), std::move(conv_17_2_mbox_conf));
184 mbox_conf << ReshapeLayer(TensorShape(21U, 1917U)).set_name("mbox_conf/reshape");
185 mbox_conf << SoftmaxLayer().set_name("mbox_conf/softmax");
186 mbox_conf << FlattenLayer().set_name("mbox_conf/flat");
187
Pablo Tellofea8ec32018-11-16 13:25:30 +0000188 const std::vector<float> priorbox_variances = { 0.1f, 0.1f, 0.2f, 0.2f };
189 const float priorbox_offset = 0.5f;
190 const std::vector<float> priorbox_aspect_ratios = { 2.f, 3.f };
191
192 //mbox_priorbox branch
193 SubStream conv_11_mbox_priorbox(conv_11);
194
195 conv_11_mbox_priorbox << PriorBoxLayer(SubStream(graph),
196 PriorBoxLayerInfo({ 60.f }, priorbox_variances, priorbox_offset, true, false, {}, { 2.f }))
197 .set_name("conv11/priorbox");
198
199 SubStream conv_13_mbox_priorbox(conv_13);
200 conv_13_mbox_priorbox << PriorBoxLayer(SubStream(graph),
201 PriorBoxLayerInfo({ 105.f }, priorbox_variances, priorbox_offset, true, false, { 150.f }, priorbox_aspect_ratios))
202 .set_name("conv13/priorbox");
203
204 SubStream conv_14_2_mbox_priorbox(conv_14);
205 conv_14_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
206 PriorBoxLayerInfo({ 150.f }, priorbox_variances, priorbox_offset, true, false, { 195.f }, priorbox_aspect_ratios))
207 .set_name("conv14/priorbox");
208
209 SubStream conv_15_2_mbox_priorbox(conv_15);
210 conv_15_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
211 PriorBoxLayerInfo({ 195.f }, priorbox_variances, priorbox_offset, true, false, { 240.f }, priorbox_aspect_ratios))
212 .set_name("conv15/priorbox");
213
214 SubStream conv_16_2_mbox_priorbox(conv_16);
215 conv_16_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
216 PriorBoxLayerInfo({ 240.f }, priorbox_variances, priorbox_offset, true, false, { 285.f }, priorbox_aspect_ratios))
217 .set_name("conv16/priorbox");
218
219 SubStream conv_17_2_mbox_priorbox(conv_17);
220 conv_17_2_mbox_priorbox << PriorBoxLayer(SubStream(graph),
221 PriorBoxLayerInfo({ 285.f }, priorbox_variances, priorbox_offset, true, false, { 300.f }, priorbox_aspect_ratios))
222 .set_name("conv17/priorbox");
223
224 SubStream mbox_priorbox(graph);
225
226 mbox_priorbox << ConcatLayer(
227 (common_params.data_layout == DataLayout::NCHW) ? DataLayoutDimension::WIDTH : DataLayoutDimension::CHANNEL,
228 std::move(conv_11_mbox_priorbox), std::move(conv_13_mbox_priorbox), std::move(conv_14_2_mbox_priorbox),
229 std::move(conv_15_2_mbox_priorbox), std::move(conv_16_2_mbox_priorbox), std::move(conv_17_2_mbox_priorbox));
230
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000231 const int num_classes = 21;
232 const bool share_location = true;
233 const DetectionOutputLayerCodeType detection_type = DetectionOutputLayerCodeType::CENTER_SIZE;
234 const int keep_top_k = keep_topk_opt->value();
235 const float nms_threshold = 0.45f;
236 const int label_id_background = 0;
237 const float conf_thrs = 0.25f;
238 const int top_k = 100;
239
240 SubStream detection_ouput(mbox_loc);
241 detection_ouput << DetectionOutputLayer(std::move(mbox_conf), std::move(mbox_priorbox),
242 DetectionOutputLayerInfo(num_classes, share_location, detection_type, keep_top_k, nms_threshold, top_k, label_id_background, conf_thrs));
243 detection_ouput << OutputLayer(get_detection_output_accessor(common_params, { tensor_shape }));
Pablo Tellofea8ec32018-11-16 13:25:30 +0000244
245 // Finalize graph
246 GraphConfig config;
247 config.num_threads = common_params.threads;
248 config.use_tuner = common_params.enable_tuner;
249 config.tuner_file = common_params.tuner_file;
250
251 graph.finalize(common_params.target, config);
252
253 return true;
254 }
255 void do_run() override
256 {
257 // Run graph
258 graph.run();
259 }
260
261private:
262 CommandLineParser cmd_parser;
263 CommonGraphOptions common_opts;
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000264 SimpleOption<int> *keep_topk_opt{ nullptr };
265 CommonGraphParams common_params;
266 Stream graph;
Pablo Tellofea8ec32018-11-16 13:25:30 +0000267
268 ConcatLayer get_node_A(IStream &master_graph, const std::string &data_path, std::string &&param_path,
269 unsigned int conv_filt,
270 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
271 {
272 const std::string total_path = param_path + "_";
273 SubStream sg(master_graph);
274
275 sg << DepthwiseConvolutionLayer(
276 3U, 3U,
277 get_weights_accessor(data_path, total_path + "dw_w.npy"),
278 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
279 dwc_pad_stride_info)
280 .set_name(param_path + "/dw")
281 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "dw_bn_mean.npy"),
282 get_weights_accessor(data_path, total_path + "dw_bn_var.npy"),
283 get_weights_accessor(data_path, total_path + "dw_scale_w.npy"),
284 get_weights_accessor(data_path, total_path + "dw_scale_b.npy"), 0.00001f)
285 .set_name(param_path + "/dw/bn")
286 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "dw/relu")
287
288 << ConvolutionLayer(
289 1U, 1U, conv_filt,
290 get_weights_accessor(data_path, total_path + "w.npy"),
291 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
292 conv_pad_stride_info)
293 .set_name(param_path + "/pw")
294 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "bn_mean.npy"),
295 get_weights_accessor(data_path, total_path + "bn_var.npy"),
296 get_weights_accessor(data_path, total_path + "scale_w.npy"),
297 get_weights_accessor(data_path, total_path + "scale_b.npy"), 0.00001f)
298 .set_name(param_path + "/pw/bn")
299 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(param_path + "pw/relu");
300
301 return ConcatLayer(std::move(sg));
302 }
303
304 ConcatLayer get_node_B(IStream &master_graph, const std::string &data_path, std::string &&param_path,
305 unsigned int conv_filt,
306 PadStrideInfo conv_pad_stride_info_1, PadStrideInfo conv_pad_stride_info_2)
307 {
308 const std::string total_path = param_path + "_";
309 SubStream sg(master_graph);
310
311 sg << ConvolutionLayer(
312 1, 1, conv_filt / 2,
313 get_weights_accessor(data_path, total_path + "1_w.npy"),
314 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
315 conv_pad_stride_info_1)
316 .set_name(total_path + "1/conv")
317 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "1_bn_mean.npy"),
318 get_weights_accessor(data_path, total_path + "1_bn_var.npy"),
319 get_weights_accessor(data_path, total_path + "1_scale_w.npy"),
320 get_weights_accessor(data_path, total_path + "1_scale_b.npy"), 0.00001f)
321 .set_name(total_path + "1/bn")
322 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "1/relu");
323
324 sg << ConvolutionLayer(
325 3, 3, conv_filt,
326 get_weights_accessor(data_path, total_path + "2_w.npy"),
327 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
328 conv_pad_stride_info_2)
329 .set_name(total_path + "2/conv")
330 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "2_bn_mean.npy"),
331 get_weights_accessor(data_path, total_path + "2_bn_var.npy"),
332 get_weights_accessor(data_path, total_path + "2_scale_w.npy"),
333 get_weights_accessor(data_path, total_path + "2_scale_b.npy"), 0.00001f)
334 .set_name(total_path + "2/bn")
335 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(total_path + "2/relu");
336
337 return ConcatLayer(std::move(sg));
338 }
339
340 ConcatLayer get_node_C(IStream &master_graph, const std::string &data_path, std::string &&param_path,
341 unsigned int conv_filt, PadStrideInfo conv_pad_stride_info)
342 {
343 const std::string total_path = param_path + "_";
344 SubStream sg(master_graph);
345 sg << ConvolutionLayer(
346 1U, 1U, conv_filt,
347 get_weights_accessor(data_path, total_path + "w.npy"),
348 get_weights_accessor(data_path, total_path + "b.npy"),
349 conv_pad_stride_info)
350 .set_name(param_path + "/conv");
351 if(common_params.data_layout == DataLayout::NCHW)
352 {
353 sg << PermuteLayer(PermutationVector(2U, 0U, 1U), DataLayout::NHWC).set_name(param_path + "/perm");
354 }
355 sg << FlattenLayer().set_name(param_path + "/flat");
356
357 return ConcatLayer(std::move(sg));
358 }
359};
360
361/** Main program for MobileNetSSD
362 *
363 * Model is based on:
364 * http://arxiv.org/abs/1512.02325
365 * SSD: Single Shot MultiBox Detector
366 * Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg
367 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000368 * Provenance: https://github.com/chuanqi305/MobileNet-SSD
369 *
Pablo Tellofea8ec32018-11-16 13:25:30 +0000370 * @note To list all the possible arguments execute the binary appended with the --help option
371 *
372 * @param[in] argc Number of arguments
373 * @param[in] argv Arguments
374 */
375int main(int argc, char **argv)
376{
377 return arm_compute::utils::run_example<GraphSSDMobilenetExample>(argc, argv);
378}