blob: bb893998beaa9e1132a2daa10b4256f7c59a4a6f [file] [log] [blame]
Georgios Pinitas236bfe72017-11-23 15:59:55 +00001/*
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00002 * Copyright (c) 2017-2020 ARM Limited.
Georgios Pinitas236bfe72017-11-23 15:59:55 +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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Georgios Pinitas236bfe72017-11-23 15:59:55 +000025#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010026#include "utils/CommonGraphOptions.h"
Georgios Pinitas236bfe72017-11-23 15:59:55 +000027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010030using namespace arm_compute;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000031using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032using namespace arm_compute::graph::frontend;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000033using namespace arm_compute::graph_utils;
34
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010035/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API */
Gian Marco Iodice11a7e322018-07-05 15:42:02 +010036class GraphMobilenetExample : public Example
Georgios Pinitas236bfe72017-11-23 15:59:55 +000037{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000038public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010039 GraphMobilenetExample()
40 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 // Add model id option
43 model_id_opt = cmd_parser.add_option<SimpleOption<int>>("model-id", 0);
44 model_id_opt->set_help("Mobilenet model id (0: 1.0_224, else: 0.75_160");
45 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010046 GraphMobilenetExample(const GraphMobilenetExample &) = delete;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010047 GraphMobilenetExample &operator=(const GraphMobilenetExample &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000048 ~GraphMobilenetExample() override = default;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010049 bool do_setup(int argc, char **argv) override
50 {
51 // Parse arguments
52 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010053 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010054
55 // Consume common parameters
56 common_params = consume_common_graph_parameters(common_opts);
57
58 // Return when help menu is requested
59 if(common_params.help)
60 {
61 cmd_parser.print_help(argv[0]);
62 return false;
63 }
64
65 // Print parameter values
66 std::cout << common_params << std::endl;
67
68 // Get model parameters
69 int model_id = model_id_opt->value();
70
71 // Create input descriptor
72 unsigned int spatial_size = (model_id == 0 || common_params.data_type == DataType::QASYMM8) ? 224 : 160;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010073
74 // Create input descriptor
75 const TensorShape tensor_shape = permute_shape(TensorShape(spatial_size, spatial_size, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
76 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010077
78 // Set graph hints
79 graph << common_params.target
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010080 << common_params.fast_math_hint;
81
82 // Create core graph
83 if(arm_compute::is_data_type_float(common_params.data_type))
84 {
85 create_graph_float(input_descriptor, model_id);
86 }
87 else
88 {
89 create_graph_qasymm(input_descriptor);
90 }
91
92 // Create common tail
93 graph << ReshapeLayer(TensorShape(1001U)).set_name("Reshape")
94 << SoftmaxLayer().set_name("Softmax")
95 << OutputLayer(get_output_accessor(common_params, 5));
96
97 // Finalize graph
98 GraphConfig config;
99 config.num_threads = common_params.threads;
100 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100101 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100102 config.tuner_file = common_params.tuner_file;
103
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100104 graph.finalize(common_params.target, config);
105
106 return true;
107 }
108 void do_run() override
109 {
110 // Run graph
111 graph.run();
112 }
113
114private:
115 CommandLineParser cmd_parser;
116 CommonGraphOptions common_opts;
117 SimpleOption<int> *model_id_opt{ nullptr };
118 CommonGraphParams common_params;
119 Stream graph;
120
121 void create_graph_float(TensorDescriptor &input_descriptor, int model_id)
122 {
123 float depth_scale = (model_id == 0) ? 1.f : 0.75;
124 std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000125
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000126 // Create a preprocessor object
127 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000128
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100129 // Get trainable parameters data path
130 std::string data_path = common_params.data_path;
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000131
132 // Add model path to data path
133 if(!data_path.empty())
134 {
135 data_path += model_path;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000136 }
137
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100138 graph << InputLayer(input_descriptor,
139 get_input_accessor(common_params, std::move(preprocessor), false))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000140 << ConvolutionLayer(
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000141 3U, 3U, 32U * depth_scale,
Georgios Pinitascac13b12018-04-27 19:07:19 +0100142 get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000143 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
144 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100145 .set_name("Conv2d_0")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000146 << BatchNormalizationLayer(
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000147 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"),
148 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"),
149 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"),
150 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000151 0.001f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100152 .set_name("Conv2d_0/BatchNorm")
153 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100154 graph << get_dwsc_node_float(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
155 graph << get_dwsc_node_float(data_path, "Conv2d_2", 128 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
156 graph << get_dwsc_node_float(data_path, "Conv2d_3", 128 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
157 graph << get_dwsc_node_float(data_path, "Conv2d_4", 256 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
158 graph << get_dwsc_node_float(data_path, "Conv2d_5", 256 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
159 graph << get_dwsc_node_float(data_path, "Conv2d_6", 512 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
160 graph << get_dwsc_node_float(data_path, "Conv2d_7", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
161 graph << get_dwsc_node_float(data_path, "Conv2d_8", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
162 graph << get_dwsc_node_float(data_path, "Conv2d_9", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
163 graph << get_dwsc_node_float(data_path, "Conv2d_10", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
164 graph << get_dwsc_node_float(data_path, "Conv2d_11", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
165 graph << get_dwsc_node_float(data_path, "Conv2d_12", 1024 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
166 graph << get_dwsc_node_float(data_path, "Conv2d_13", 1024 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000167 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000168 << ConvolutionLayer(
169 1U, 1U, 1001U,
Georgios Pinitascac13b12018-04-27 19:07:19 +0100170 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000171 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000172 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100173 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000174 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100175
176 void create_graph_qasymm(TensorDescriptor &input_descriptor)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000177 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100178 // Get trainable parameters data path
179 std::string data_path = common_params.data_path;
180
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100181 // Add model path to data path
182 if(!data_path.empty())
183 {
184 data_path += "/cnn_data/mobilenet_qasymm8_model/";
185 }
186
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100187 // Quantization info taken from the AndroidNN QASYMM8 MobileNet example
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100188 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100189
190 const std::vector<QuantizationInfo> conv_weights_quant_info =
191 {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100192 QuantizationInfo(0.02182667888700962f, 151), // conv0
193 QuantizationInfo(0.004986600950360298f, 74) // conv14
194 };
195 const std::vector<QuantizationInfo> conv_out_quant_info =
196 {
197 QuantizationInfo(0.023528477177023888f, 0), // conv0
198 QuantizationInfo(0.16609922051429749f, 66) // conv14
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100199 };
200
201 const std::vector<QuantizationInfo> depth_weights_quant_info =
202 {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100203 QuantizationInfo(0.29219913482666016f, 110), // dwsc1
204 QuantizationInfo(0.40277284383773804f, 130), // dwsc2
205 QuantizationInfo(0.06053730100393295f, 160), // dwsc3
206 QuantizationInfo(0.01675807684659958f, 123), // dwsc4
207 QuantizationInfo(0.04105526953935623f, 129), // dwsc5
208 QuantizationInfo(0.013460792601108551f, 122), // dwsc6
209 QuantizationInfo(0.036934755742549896f, 132), // dwsc7
210 QuantizationInfo(0.042609862983226776f, 94), // dwsc8
211 QuantizationInfo(0.028358859941363335f, 127), // dwsc9
212 QuantizationInfo(0.024329448118805885f, 134), // dwsc10
213 QuantizationInfo(0.019366811960935593f, 106), // dwsc11
214 QuantizationInfo(0.007835594937205315f, 126), // dwsc12
215 QuantizationInfo(0.12616927921772003f, 211) // dwsc13
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100216 };
217
218 const std::vector<QuantizationInfo> point_weights_quant_info =
219 {
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100220 QuantizationInfo(0.030420949682593346f, 121), // dwsc1
221 QuantizationInfo(0.015148180536925793f, 104), // dwsc2
222 QuantizationInfo(0.013755458407104015f, 94), // dwsc3
223 QuantizationInfo(0.007601846940815449f, 151), // dwsc4
224 QuantizationInfo(0.006431614048779011f, 122), // dwsc5
225 QuantizationInfo(0.00917122047394514f, 109), // dwsc6
226 QuantizationInfo(0.005300046876072884f, 140), // dwsc7
227 QuantizationInfo(0.0049632852897048f, 127), // dwsc8
228 QuantizationInfo(0.007770895957946777f, 89), // dwsc9
229 QuantizationInfo(0.009658650495111942f, 99), // dwsc10
230 QuantizationInfo(0.005446993745863438f, 153), // dwsc11
231 QuantizationInfo(0.00817922968417406f, 130), // dwsc12
232 QuantizationInfo(0.018048152327537537f, 95) // dwsc13
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100233 };
234
235 graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
Manuel Bottini32527952019-11-05 16:55:57 +0000236 get_input_accessor(common_params, nullptr, false))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100237 << ConvolutionLayer(
238 3U, 3U, 32U,
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100239 get_weights_accessor(data_path, "Conv2d_0_weights.npy"),
240 get_weights_accessor(data_path, "Conv2d_0_bias.npy"),
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100241 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100242 1, conv_weights_quant_info.at(0), conv_out_quant_info.at(0))
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100243 .set_name("Conv2d_0")
244 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100245 graph << get_dwsc_node_qasymm(data_path, "Conv2d_1", 64U, PadStrideInfo(1U, 1U, 1U, 1U), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(0), point_weights_quant_info.at(0));
246 graph << get_dwsc_node_qasymm(data_path, "Conv2d_2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(1),
247 point_weights_quant_info.at(1));
248 graph << get_dwsc_node_qasymm(data_path, "Conv2d_3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(2),
249 point_weights_quant_info.at(2));
250 graph << get_dwsc_node_qasymm(data_path, "Conv2d_4", 256U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(3),
251 point_weights_quant_info.at(3));
252 graph << get_dwsc_node_qasymm(data_path, "Conv2d_5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(4),
253 point_weights_quant_info.at(4));
254 graph << get_dwsc_node_qasymm(data_path, "Conv2d_6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(5),
255 point_weights_quant_info.at(5));
256 graph << get_dwsc_node_qasymm(data_path, "Conv2d_7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(6),
257 point_weights_quant_info.at(6));
258 graph << get_dwsc_node_qasymm(data_path, "Conv2d_8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(7),
259 point_weights_quant_info.at(7));
260 graph << get_dwsc_node_qasymm(data_path, "Conv2d_9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(8),
261 point_weights_quant_info.at(8));
262 graph << get_dwsc_node_qasymm(data_path, "Conv2d_10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(9),
263 point_weights_quant_info.at(9));
264 graph << get_dwsc_node_qasymm(data_path, "Conv2d_11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(10),
265 point_weights_quant_info.at(10));
266 graph << get_dwsc_node_qasymm(data_path, "Conv2d_12", 1024U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(11),
267 point_weights_quant_info.at(11));
268 graph << get_dwsc_node_qasymm(data_path, "Conv2d_13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(12),
269 point_weights_quant_info.at(12))
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000270 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100271 << ConvolutionLayer(
272 1U, 1U, 1001U,
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100273 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
274 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_bias.npy"),
Isabella Gottardi8baaa452019-05-20 18:22:15 +0100275 PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1), conv_out_quant_info.at(1))
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100276 .set_name("Logits/Conv2d_1c_1x1");
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000277 }
278
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100279 ConcatLayer get_dwsc_node_float(const std::string &data_path, std::string &&param_path,
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100280 unsigned int conv_filt,
281 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000282 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000283 std::string total_path = param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000284 SubStream sg(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000285 sg << DepthwiseConvolutionLayer(
286 3U, 3U,
Georgios Pinitascac13b12018-04-27 19:07:19 +0100287 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000288 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000289 dwc_pad_stride_info)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100290 .set_name(total_path + "depthwise/depthwise")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000291 << BatchNormalizationLayer(
292 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
293 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000294 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000295 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000296 0.001f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100297 .set_name(total_path + "depthwise/BatchNorm")
298 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000299 << ConvolutionLayer(
300 1U, 1U, conv_filt,
Georgios Pinitascac13b12018-04-27 19:07:19 +0100301 get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000302 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
303 conv_pad_stride_info)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100304 .set_name(total_path + "pointwise/Conv2D")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000305 << BatchNormalizationLayer(
306 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
307 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000308 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000309 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000310 0.001f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100311 .set_name(total_path + "pointwise/BatchNorm")
312 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6");
Gian Marcobfa3b522017-12-12 10:08:38 +0000313
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100314 return ConcatLayer(std::move(sg));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000315 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100316
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100317 ConcatLayer get_dwsc_node_qasymm(const std::string &data_path, std::string &&param_path,
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100318 const unsigned int conv_filt,
319 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
320 QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info)
321 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100322 std::string total_path = param_path + "_";
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100323 SubStream sg(graph);
324
325 sg << DepthwiseConvolutionLayer(
326 3U, 3U,
327 get_weights_accessor(data_path, total_path + "depthwise_weights.npy"),
328 get_weights_accessor(data_path, total_path + "depthwise_bias.npy"),
Michalis Spyrou6260e192019-06-06 13:47:38 +0100329 dwc_pad_stride_info, 1, std::move(depth_weights_quant_info))
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100330 .set_name(total_path + "depthwise/depthwise")
331 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100332 << ConvolutionLayer(
333 1U, 1U, conv_filt,
334 get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
335 get_weights_accessor(data_path, total_path + "pointwise_bias.npy"),
Michalis Spyrou6260e192019-06-06 13:47:38 +0100336 conv_pad_stride_info, 1, std::move(point_weights_quant_info))
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100337 .set_name(total_path + "pointwise/Conv2D")
338 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100339
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100340 return ConcatLayer(std::move(sg));
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100341 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000342};
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000343
344/** Main program for MobileNetV1
345 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000346 * Model is based on:
347 * https://arxiv.org/abs/1704.04861
348 * "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
349 * Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam
350 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000351 * Provenance: download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz
352 * download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_0.75_160.tgz
353 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100354 * @note To list all the possible arguments execute the binary appended with the --help option
355 *
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000356 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100357 * @param[in] argv Arguments
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000358 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000359int main(int argc, char **argv)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000360{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000361 return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000362}