1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/graph.h"
25*c217d954SCole Faust #include "support/ToolchainSupport.h"
26*c217d954SCole Faust #include "utils/CommonGraphOptions.h"
27*c217d954SCole Faust #include "utils/GraphUtils.h"
28*c217d954SCole Faust #include "utils/Utils.h"
29*c217d954SCole Faust
30*c217d954SCole Faust using namespace arm_compute;
31*c217d954SCole Faust using namespace arm_compute::utils;
32*c217d954SCole Faust using namespace arm_compute::graph::frontend;
33*c217d954SCole Faust using namespace arm_compute::graph_utils;
34*c217d954SCole Faust
35*c217d954SCole Faust /** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API */
36*c217d954SCole Faust class GraphMobilenetExample : public Example
37*c217d954SCole Faust {
38*c217d954SCole Faust public:
GraphMobilenetExample()39*c217d954SCole Faust GraphMobilenetExample()
40*c217d954SCole Faust : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV1")
41*c217d954SCole Faust {
42*c217d954SCole Faust // Add model id option
43*c217d954SCole Faust model_id_opt = cmd_parser.add_option<SimpleOption<int>>("model-id", 0);
44*c217d954SCole Faust model_id_opt->set_help("Mobilenet model id (0: 1.0_224, else: 0.75_160");
45*c217d954SCole Faust }
46*c217d954SCole Faust GraphMobilenetExample(const GraphMobilenetExample &) = delete;
47*c217d954SCole Faust GraphMobilenetExample &operator=(const GraphMobilenetExample &) = delete;
48*c217d954SCole Faust ~GraphMobilenetExample() override = default;
do_setup(int argc,char ** argv)49*c217d954SCole Faust bool do_setup(int argc, char **argv) override
50*c217d954SCole Faust {
51*c217d954SCole Faust // Parse arguments
52*c217d954SCole Faust cmd_parser.parse(argc, argv);
53*c217d954SCole Faust cmd_parser.validate();
54*c217d954SCole Faust
55*c217d954SCole Faust // Consume common parameters
56*c217d954SCole Faust common_params = consume_common_graph_parameters(common_opts);
57*c217d954SCole Faust
58*c217d954SCole Faust // Return when help menu is requested
59*c217d954SCole Faust if(common_params.help)
60*c217d954SCole Faust {
61*c217d954SCole Faust cmd_parser.print_help(argv[0]);
62*c217d954SCole Faust return false;
63*c217d954SCole Faust }
64*c217d954SCole Faust
65*c217d954SCole Faust // Print parameter values
66*c217d954SCole Faust std::cout << common_params << std::endl;
67*c217d954SCole Faust
68*c217d954SCole Faust // Get model parameters
69*c217d954SCole Faust int model_id = model_id_opt->value();
70*c217d954SCole Faust
71*c217d954SCole Faust // Create input descriptor
72*c217d954SCole Faust unsigned int spatial_size = (model_id == 0 || common_params.data_type == DataType::QASYMM8) ? 224 : 160;
73*c217d954SCole Faust
74*c217d954SCole Faust // Create input descriptor
75*c217d954SCole Faust const TensorShape tensor_shape = permute_shape(TensorShape(spatial_size, spatial_size, 3U, common_params.batches), DataLayout::NCHW, common_params.data_layout);
76*c217d954SCole Faust TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
77*c217d954SCole Faust
78*c217d954SCole Faust // Set graph hints
79*c217d954SCole Faust graph << common_params.target
80*c217d954SCole Faust << common_params.fast_math_hint;
81*c217d954SCole Faust
82*c217d954SCole Faust // Create core graph
83*c217d954SCole Faust if(arm_compute::is_data_type_float(common_params.data_type))
84*c217d954SCole Faust {
85*c217d954SCole Faust create_graph_float(input_descriptor, model_id);
86*c217d954SCole Faust }
87*c217d954SCole Faust else
88*c217d954SCole Faust {
89*c217d954SCole Faust create_graph_qasymm(input_descriptor);
90*c217d954SCole Faust }
91*c217d954SCole Faust
92*c217d954SCole Faust // Create common tail
93*c217d954SCole Faust graph << ReshapeLayer(TensorShape(1001U)).set_name("Reshape")
94*c217d954SCole Faust << SoftmaxLayer().set_name("Softmax")
95*c217d954SCole Faust << OutputLayer(get_output_accessor(common_params, 5));
96*c217d954SCole Faust
97*c217d954SCole Faust // Finalize graph
98*c217d954SCole Faust GraphConfig config;
99*c217d954SCole Faust config.num_threads = common_params.threads;
100*c217d954SCole Faust config.use_tuner = common_params.enable_tuner;
101*c217d954SCole Faust config.tuner_mode = common_params.tuner_mode;
102*c217d954SCole Faust config.tuner_file = common_params.tuner_file;
103*c217d954SCole Faust config.mlgo_file = common_params.mlgo_file;
104*c217d954SCole Faust
105*c217d954SCole Faust graph.finalize(common_params.target, config);
106*c217d954SCole Faust
107*c217d954SCole Faust return true;
108*c217d954SCole Faust }
do_run()109*c217d954SCole Faust void do_run() override
110*c217d954SCole Faust {
111*c217d954SCole Faust // Run graph
112*c217d954SCole Faust graph.run();
113*c217d954SCole Faust }
114*c217d954SCole Faust
115*c217d954SCole Faust private:
116*c217d954SCole Faust CommandLineParser cmd_parser;
117*c217d954SCole Faust CommonGraphOptions common_opts;
118*c217d954SCole Faust SimpleOption<int> *model_id_opt{ nullptr };
119*c217d954SCole Faust CommonGraphParams common_params;
120*c217d954SCole Faust Stream graph;
121*c217d954SCole Faust
create_graph_float(TensorDescriptor & input_descriptor,int model_id)122*c217d954SCole Faust void create_graph_float(TensorDescriptor &input_descriptor, int model_id)
123*c217d954SCole Faust {
124*c217d954SCole Faust float depth_scale = (model_id == 0) ? 1.f : 0.75;
125*c217d954SCole Faust std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
126*c217d954SCole Faust
127*c217d954SCole Faust // Create a preprocessor object
128*c217d954SCole Faust std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>();
129*c217d954SCole Faust
130*c217d954SCole Faust // Get trainable parameters data path
131*c217d954SCole Faust std::string data_path = common_params.data_path;
132*c217d954SCole Faust
133*c217d954SCole Faust // Add model path to data path
134*c217d954SCole Faust if(!data_path.empty())
135*c217d954SCole Faust {
136*c217d954SCole Faust data_path += model_path;
137*c217d954SCole Faust }
138*c217d954SCole Faust
139*c217d954SCole Faust graph << InputLayer(input_descriptor,
140*c217d954SCole Faust get_input_accessor(common_params, std::move(preprocessor), false))
141*c217d954SCole Faust << ConvolutionLayer(
142*c217d954SCole Faust 3U, 3U, 32U * depth_scale,
143*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW),
144*c217d954SCole Faust std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
145*c217d954SCole Faust PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
146*c217d954SCole Faust .set_name("Conv2d_0")
147*c217d954SCole Faust << BatchNormalizationLayer(
148*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"),
149*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"),
150*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"),
151*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"),
152*c217d954SCole Faust 0.001f)
153*c217d954SCole Faust .set_name("Conv2d_0/BatchNorm")
154*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6");
155*c217d954SCole Faust graph << get_dwsc_node_float(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
156*c217d954SCole Faust 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));
157*c217d954SCole Faust 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));
158*c217d954SCole Faust 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));
159*c217d954SCole Faust 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));
160*c217d954SCole Faust 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));
161*c217d954SCole Faust 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));
162*c217d954SCole Faust 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));
163*c217d954SCole Faust 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));
164*c217d954SCole Faust 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));
165*c217d954SCole Faust 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));
166*c217d954SCole Faust 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));
167*c217d954SCole Faust 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));
168*c217d954SCole Faust graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
169*c217d954SCole Faust << ConvolutionLayer(
170*c217d954SCole Faust 1U, 1U, 1001U,
171*c217d954SCole Faust get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
172*c217d954SCole Faust get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
173*c217d954SCole Faust PadStrideInfo(1, 1, 0, 0))
174*c217d954SCole Faust .set_name("Logits/Conv2d_1c_1x1");
175*c217d954SCole Faust }
176*c217d954SCole Faust
create_graph_qasymm(TensorDescriptor & input_descriptor)177*c217d954SCole Faust void create_graph_qasymm(TensorDescriptor &input_descriptor)
178*c217d954SCole Faust {
179*c217d954SCole Faust // Get trainable parameters data path
180*c217d954SCole Faust std::string data_path = common_params.data_path;
181*c217d954SCole Faust
182*c217d954SCole Faust // Add model path to data path
183*c217d954SCole Faust if(!data_path.empty())
184*c217d954SCole Faust {
185*c217d954SCole Faust data_path += "/cnn_data/mobilenet_qasymm8_model/";
186*c217d954SCole Faust }
187*c217d954SCole Faust
188*c217d954SCole Faust // Quantization info taken from the AndroidNN QASYMM8 MobileNet example
189*c217d954SCole Faust const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
190*c217d954SCole Faust
191*c217d954SCole Faust const std::vector<QuantizationInfo> conv_weights_quant_info =
192*c217d954SCole Faust {
193*c217d954SCole Faust QuantizationInfo(0.02182667888700962f, 151), // conv0
194*c217d954SCole Faust QuantizationInfo(0.004986600950360298f, 74) // conv14
195*c217d954SCole Faust };
196*c217d954SCole Faust const std::vector<QuantizationInfo> conv_out_quant_info =
197*c217d954SCole Faust {
198*c217d954SCole Faust QuantizationInfo(0.023528477177023888f, 0), // conv0
199*c217d954SCole Faust QuantizationInfo(0.16609922051429749f, 66) // conv14
200*c217d954SCole Faust };
201*c217d954SCole Faust
202*c217d954SCole Faust const std::vector<QuantizationInfo> depth_weights_quant_info =
203*c217d954SCole Faust {
204*c217d954SCole Faust QuantizationInfo(0.29219913482666016f, 110), // dwsc1
205*c217d954SCole Faust QuantizationInfo(0.40277284383773804f, 130), // dwsc2
206*c217d954SCole Faust QuantizationInfo(0.06053730100393295f, 160), // dwsc3
207*c217d954SCole Faust QuantizationInfo(0.01675807684659958f, 123), // dwsc4
208*c217d954SCole Faust QuantizationInfo(0.04105526953935623f, 129), // dwsc5
209*c217d954SCole Faust QuantizationInfo(0.013460792601108551f, 122), // dwsc6
210*c217d954SCole Faust QuantizationInfo(0.036934755742549896f, 132), // dwsc7
211*c217d954SCole Faust QuantizationInfo(0.042609862983226776f, 94), // dwsc8
212*c217d954SCole Faust QuantizationInfo(0.028358859941363335f, 127), // dwsc9
213*c217d954SCole Faust QuantizationInfo(0.024329448118805885f, 134), // dwsc10
214*c217d954SCole Faust QuantizationInfo(0.019366811960935593f, 106), // dwsc11
215*c217d954SCole Faust QuantizationInfo(0.007835594937205315f, 126), // dwsc12
216*c217d954SCole Faust QuantizationInfo(0.12616927921772003f, 211) // dwsc13
217*c217d954SCole Faust };
218*c217d954SCole Faust
219*c217d954SCole Faust const std::vector<QuantizationInfo> point_weights_quant_info =
220*c217d954SCole Faust {
221*c217d954SCole Faust QuantizationInfo(0.030420949682593346f, 121), // dwsc1
222*c217d954SCole Faust QuantizationInfo(0.015148180536925793f, 104), // dwsc2
223*c217d954SCole Faust QuantizationInfo(0.013755458407104015f, 94), // dwsc3
224*c217d954SCole Faust QuantizationInfo(0.007601846940815449f, 151), // dwsc4
225*c217d954SCole Faust QuantizationInfo(0.006431614048779011f, 122), // dwsc5
226*c217d954SCole Faust QuantizationInfo(0.00917122047394514f, 109), // dwsc6
227*c217d954SCole Faust QuantizationInfo(0.005300046876072884f, 140), // dwsc7
228*c217d954SCole Faust QuantizationInfo(0.0049632852897048f, 127), // dwsc8
229*c217d954SCole Faust QuantizationInfo(0.007770895957946777f, 89), // dwsc9
230*c217d954SCole Faust QuantizationInfo(0.009658650495111942f, 99), // dwsc10
231*c217d954SCole Faust QuantizationInfo(0.005446993745863438f, 153), // dwsc11
232*c217d954SCole Faust QuantizationInfo(0.00817922968417406f, 130), // dwsc12
233*c217d954SCole Faust QuantizationInfo(0.018048152327537537f, 95) // dwsc13
234*c217d954SCole Faust };
235*c217d954SCole Faust
236*c217d954SCole Faust graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
237*c217d954SCole Faust get_input_accessor(common_params, nullptr, false))
238*c217d954SCole Faust << ConvolutionLayer(
239*c217d954SCole Faust 3U, 3U, 32U,
240*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_weights.npy"),
241*c217d954SCole Faust get_weights_accessor(data_path, "Conv2d_0_bias.npy"),
242*c217d954SCole Faust PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
243*c217d954SCole Faust 1, conv_weights_quant_info.at(0), conv_out_quant_info.at(0))
244*c217d954SCole Faust .set_name("Conv2d_0")
245*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6");
246*c217d954SCole Faust 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));
247*c217d954SCole Faust 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),
248*c217d954SCole Faust point_weights_quant_info.at(1));
249*c217d954SCole Faust 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),
250*c217d954SCole Faust point_weights_quant_info.at(2));
251*c217d954SCole Faust 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),
252*c217d954SCole Faust point_weights_quant_info.at(3));
253*c217d954SCole Faust 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),
254*c217d954SCole Faust point_weights_quant_info.at(4));
255*c217d954SCole Faust 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),
256*c217d954SCole Faust point_weights_quant_info.at(5));
257*c217d954SCole Faust 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),
258*c217d954SCole Faust point_weights_quant_info.at(6));
259*c217d954SCole Faust 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),
260*c217d954SCole Faust point_weights_quant_info.at(7));
261*c217d954SCole Faust 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),
262*c217d954SCole Faust point_weights_quant_info.at(8));
263*c217d954SCole Faust 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),
264*c217d954SCole Faust point_weights_quant_info.at(9));
265*c217d954SCole Faust 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),
266*c217d954SCole Faust point_weights_quant_info.at(10));
267*c217d954SCole Faust 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),
268*c217d954SCole Faust point_weights_quant_info.at(11));
269*c217d954SCole Faust 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),
270*c217d954SCole Faust point_weights_quant_info.at(12))
271*c217d954SCole Faust << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool_1a")
272*c217d954SCole Faust << ConvolutionLayer(
273*c217d954SCole Faust 1U, 1U, 1001U,
274*c217d954SCole Faust get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
275*c217d954SCole Faust get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_bias.npy"),
276*c217d954SCole Faust PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1), conv_out_quant_info.at(1))
277*c217d954SCole Faust .set_name("Logits/Conv2d_1c_1x1");
278*c217d954SCole Faust }
279*c217d954SCole Faust
get_dwsc_node_float(const std::string & data_path,std::string && param_path,unsigned int conv_filt,PadStrideInfo dwc_pad_stride_info,PadStrideInfo conv_pad_stride_info)280*c217d954SCole Faust ConcatLayer get_dwsc_node_float(const std::string &data_path, std::string &¶m_path,
281*c217d954SCole Faust unsigned int conv_filt,
282*c217d954SCole Faust PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
283*c217d954SCole Faust {
284*c217d954SCole Faust std::string total_path = param_path + "_";
285*c217d954SCole Faust SubStream sg(graph);
286*c217d954SCole Faust sg << DepthwiseConvolutionLayer(
287*c217d954SCole Faust 3U, 3U,
288*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
289*c217d954SCole Faust std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
290*c217d954SCole Faust dwc_pad_stride_info)
291*c217d954SCole Faust .set_name(total_path + "depthwise/depthwise")
292*c217d954SCole Faust << BatchNormalizationLayer(
293*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
294*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
295*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
296*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
297*c217d954SCole Faust 0.001f)
298*c217d954SCole Faust .set_name(total_path + "depthwise/BatchNorm")
299*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6")
300*c217d954SCole Faust << ConvolutionLayer(
301*c217d954SCole Faust 1U, 1U, conv_filt,
302*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW),
303*c217d954SCole Faust std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
304*c217d954SCole Faust conv_pad_stride_info)
305*c217d954SCole Faust .set_name(total_path + "pointwise/Conv2D")
306*c217d954SCole Faust << BatchNormalizationLayer(
307*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
308*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
309*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
310*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
311*c217d954SCole Faust 0.001f)
312*c217d954SCole Faust .set_name(total_path + "pointwise/BatchNorm")
313*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6");
314*c217d954SCole Faust
315*c217d954SCole Faust return ConcatLayer(std::move(sg));
316*c217d954SCole Faust }
317*c217d954SCole Faust
get_dwsc_node_qasymm(const std::string & data_path,std::string && param_path,const unsigned int conv_filt,PadStrideInfo dwc_pad_stride_info,PadStrideInfo conv_pad_stride_info,QuantizationInfo depth_weights_quant_info,QuantizationInfo point_weights_quant_info)318*c217d954SCole Faust ConcatLayer get_dwsc_node_qasymm(const std::string &data_path, std::string &¶m_path,
319*c217d954SCole Faust const unsigned int conv_filt,
320*c217d954SCole Faust PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
321*c217d954SCole Faust QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info)
322*c217d954SCole Faust {
323*c217d954SCole Faust std::string total_path = param_path + "_";
324*c217d954SCole Faust SubStream sg(graph);
325*c217d954SCole Faust
326*c217d954SCole Faust sg << DepthwiseConvolutionLayer(
327*c217d954SCole Faust 3U, 3U,
328*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_weights.npy"),
329*c217d954SCole Faust get_weights_accessor(data_path, total_path + "depthwise_bias.npy"),
330*c217d954SCole Faust dwc_pad_stride_info, 1, std::move(depth_weights_quant_info))
331*c217d954SCole Faust .set_name(total_path + "depthwise/depthwise")
332*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6")
333*c217d954SCole Faust << ConvolutionLayer(
334*c217d954SCole Faust 1U, 1U, conv_filt,
335*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
336*c217d954SCole Faust get_weights_accessor(data_path, total_path + "pointwise_bias.npy"),
337*c217d954SCole Faust conv_pad_stride_info, 1, std::move(point_weights_quant_info))
338*c217d954SCole Faust .set_name(total_path + "pointwise/Conv2D")
339*c217d954SCole Faust << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6");
340*c217d954SCole Faust
341*c217d954SCole Faust return ConcatLayer(std::move(sg));
342*c217d954SCole Faust }
343*c217d954SCole Faust };
344*c217d954SCole Faust
345*c217d954SCole Faust /** Main program for MobileNetV1
346*c217d954SCole Faust *
347*c217d954SCole Faust * Model is based on:
348*c217d954SCole Faust * https://arxiv.org/abs/1704.04861
349*c217d954SCole Faust * "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
350*c217d954SCole Faust * Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam
351*c217d954SCole Faust *
352*c217d954SCole Faust * Provenance: download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz
353*c217d954SCole Faust * download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_0.75_160.tgz
354*c217d954SCole Faust *
355*c217d954SCole Faust * @note To list all the possible arguments execute the binary appended with the --help option
356*c217d954SCole Faust *
357*c217d954SCole Faust * @param[in] argc Number of arguments
358*c217d954SCole Faust * @param[in] argv Arguments
359*c217d954SCole Faust */
main(int argc,char ** argv)360*c217d954SCole Faust int main(int argc, char **argv)
361*c217d954SCole Faust {
362*c217d954SCole Faust return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);
363*c217d954SCole Faust }
364