blob: 3704be7ac17dbe7708df0d815f96f164ed7eb9e3 [file] [log] [blame]
Georgios Pinitas108ab0b2018-09-14 18:35:11 +01001/*
Gian Marco Iodicea74923c2019-01-31 17:06:54 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitas108ab0b2018-09-14 18:35:11 +01003 *
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::utils;
31using namespace arm_compute::graph::frontend;
32using namespace arm_compute::graph_utils;
33
34/** Example demonstrating how to implement ShuffleNet network using the Compute Library's graph API */
35class ShuffleNetExample : public Example
36{
37public:
38 ShuffleNetExample()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ShuffleNet")
40 {
41 }
42 bool do_setup(int argc, char **argv) override
43 {
44 // Parse arguments
45 cmd_parser.parse(argc, argv);
46
47 // Consume common parameters
48 common_params = consume_common_graph_parameters(common_opts);
49
50 // Return when help menu is requested
51 if(common_params.help)
52 {
53 cmd_parser.print_help(argv[0]);
54 return false;
55 }
56
57 // Set default layout if needed (Single kernel grouped convolution not yet supported int NHWC)
58 if(!common_opts.data_layout->is_set())
59 {
Gian Marco Iodicea74923c2019-01-31 17:06:54 +000060 common_params.data_layout = DataLayout::NHWC;
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010061 }
62
63 // Checks
64 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010065
66 // Print parameter values
67 std::cout << common_params << std::endl;
68 std::cout << "Model: Shufflenet_1_g4" << std::endl;
69
70 // Create model path
71 std::string model_path = "/cnn_data/shufflenet_model/";
72
73 // Get trainable parameters data path
74 std::string data_path = common_params.data_path;
75
76 // Add model path to data path
77 if(!data_path.empty())
78 {
79 data_path += model_path;
80 }
81
82 // Create input descriptor
83 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
84 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
85
86 // Set weights trained layout
87 const DataLayout weights_layout = DataLayout::NCHW;
88
89 // Create preprocessor
90 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>(0);
91
92 graph << common_params.target
93 << common_params.fast_math_hint
94 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
95 << ConvolutionLayer(
96 3U, 3U, 24U,
97 get_weights_accessor(data_path, "conv3_0_w_0.npy", weights_layout),
98 get_weights_accessor(data_path, "conv3_0_b_0.npy", weights_layout),
99 PadStrideInfo(2, 2, 1, 1))
100 .set_name("Conv1/convolution")
101 << BatchNormalizationLayer(
102 get_weights_accessor(data_path, "conv3_0_bn_rm_0.npy"),
103 get_weights_accessor(data_path, "conv3_0_bn_riv_0.npy"),
104 get_weights_accessor(data_path, "conv3_0_bn_s_0.npy"),
105 get_weights_accessor(data_path, "conv3_0_bn_b_0.npy"),
106 1e-5f)
107 .set_name("Conv1/BatchNorm")
108 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv1/Relu")
109 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 1, 1))).set_name("pool1/MaxPool");
110
111 // Stage 2
112 add_residual_block(data_path, DataLayout::NCHW, 0U /* unit */, 112U /* depth */, 2U /* stride */);
113 add_residual_block(data_path, DataLayout::NCHW, 1U /* unit */, 136U /* depth */, 1U /* stride */);
114 add_residual_block(data_path, DataLayout::NCHW, 2U /* unit */, 136U /* depth */, 1U /* stride */);
115 add_residual_block(data_path, DataLayout::NCHW, 3U /* unit */, 136U /* depth */, 1U /* stride */);
116
117 // Stage 3
118 add_residual_block(data_path, DataLayout::NCHW, 4U /* unit */, 136U /* depth */, 2U /* stride */);
119 add_residual_block(data_path, DataLayout::NCHW, 5U /* unit */, 272U /* depth */, 1U /* stride */);
120 add_residual_block(data_path, DataLayout::NCHW, 6U /* unit */, 272U /* depth */, 1U /* stride */);
121 add_residual_block(data_path, DataLayout::NCHW, 7U /* unit */, 272U /* depth */, 1U /* stride */);
122 add_residual_block(data_path, DataLayout::NCHW, 8U /* unit */, 272U /* depth */, 1U /* stride */);
123 add_residual_block(data_path, DataLayout::NCHW, 9U /* unit */, 272U /* depth */, 1U /* stride */);
124 add_residual_block(data_path, DataLayout::NCHW, 10U /* unit */, 272U /* depth */, 1U /* stride */);
125 add_residual_block(data_path, DataLayout::NCHW, 11U /* unit */, 272U /* depth */, 1U /* stride */);
126
127 // Stage 4
128 add_residual_block(data_path, DataLayout::NCHW, 12U /* unit */, 272U /* depth */, 2U /* stride */);
129 add_residual_block(data_path, DataLayout::NCHW, 13U /* unit */, 544U /* depth */, 1U /* stride */);
130 add_residual_block(data_path, DataLayout::NCHW, 14U /* unit */, 544U /* depth */, 1U /* stride */);
131 add_residual_block(data_path, DataLayout::NCHW, 15U /* unit */, 544U /* depth */, 1U /* stride */);
132
133 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("predictions/AvgPool")
134 << FlattenLayer().set_name("predictions/Reshape")
135 << FullyConnectedLayer(
136 1000U,
137 get_weights_accessor(data_path, "pred_w_0.npy", weights_layout),
138 get_weights_accessor(data_path, "pred_b_0.npy"))
139 .set_name("predictions/FC")
140 << SoftmaxLayer().set_name("predictions/Softmax")
141 << OutputLayer(get_output_accessor(common_params, 5));
142
143 // Finalize graph
144 GraphConfig config;
145 config.num_threads = common_params.threads;
146 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100147 config.tuner_mode = common_params.tuner_mode;
Georgios Pinitas108ab0b2018-09-14 18:35:11 +0100148 config.tuner_file = common_params.tuner_file;
149
150 graph.finalize(common_params.target, config);
151
152 return true;
153 }
154
155 void do_run() override
156 {
157 // Run graph
158 graph.run();
159 }
160
161private:
162 CommandLineParser cmd_parser;
163 CommonGraphOptions common_opts;
164 CommonGraphParams common_params;
165 Stream graph;
166
167 void add_residual_block(const std::string &data_path, DataLayout weights_layout,
168 unsigned int unit, unsigned int depth, unsigned int stride)
169 {
170 PadStrideInfo dwc_info = PadStrideInfo(1, 1, 1, 1);
171 const unsigned int gconv_id = unit * 2;
172 const unsigned int num_groups = 4;
173 const std::string unit_id_name = arm_compute::support::cpp11::to_string(unit);
174 const std::string gconv_id_name = arm_compute::support::cpp11::to_string(gconv_id);
175 const std::string gconv_id_1_name = arm_compute::support::cpp11::to_string(gconv_id + 1);
176 const std::string unit_name = "unit" + unit_id_name;
177
178 SubStream left_ss(graph);
179 SubStream right_ss(graph);
180
181 if(stride == 2)
182 {
183 right_ss << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(2, 2, 1, 1))).set_name(unit_name + "/pool_1/AveragePool");
184 dwc_info = PadStrideInfo(2, 2, 1, 1);
185 }
186
187 left_ss << ConvolutionLayer(
188 1U, 1U, depth,
189 get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_w_0.npy", weights_layout),
190 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
191 PadStrideInfo(1, 1, 0, 0), num_groups)
192 .set_name(unit_name + "/gconv1_" + gconv_id_name + "/convolution")
193 << BatchNormalizationLayer(
194 get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_rm_0.npy"),
195 get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_riv_0.npy"),
196 get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_s_0.npy"),
197 get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_b_0.npy"),
198 1e-5f)
199 .set_name(unit_name + "/gconv1_" + gconv_id_name + "/BatchNorm")
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/gconv1_" + gconv_id_name + "/Relu")
201 << ChannelShuffleLayer(num_groups).set_name(unit_name + "/shuffle_0/ChannelShufle")
202 << DepthwiseConvolutionLayer(
203 3U, 3U,
204 get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_w_0.npy", weights_layout),
205 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
206 dwc_info)
207 .set_name(unit_name + "/gconv3_" + unit_id_name + "/depthwise")
208 << BatchNormalizationLayer(
209 get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_rm_0.npy"),
210 get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_riv_0.npy"),
211 get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_s_0.npy"),
212 get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_b_0.npy"),
213 1e-5f)
214 .set_name(unit_name + "/gconv3_" + unit_id_name + "/BatchNorm")
215 << ConvolutionLayer(
216 1U, 1U, depth,
217 get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_w_0.npy", weights_layout),
218 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
219 PadStrideInfo(1, 1, 0, 0), num_groups)
220 .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/convolution")
221 << BatchNormalizationLayer(
222 get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_rm_0.npy"),
223 get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_riv_0.npy"),
224 get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_s_0.npy"),
225 get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_b_0.npy"),
226 1e-5f)
227 .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/BatchNorm");
228
229 if(stride == 2)
230 {
231 graph << ConcatLayer(std::move(left_ss), std::move(right_ss)).set_name(unit_name + "/Concat");
232 }
233 else
234 {
235 graph << EltwiseLayer(std::move(left_ss), std::move(right_ss), EltwiseOperation::Add).set_name(unit_name + "/Add");
236 }
237 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/Relu");
238 }
239};
240
241/** Main program for ShuffleNet
242 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000243 * Model is based on:
244 * https://arxiv.org/abs/1707.01083
245 * "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices"
246 * Xiangyu Zhang, Xinyu Zhou, Mengxiao Lin, Jian Sun
247 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000248 * Provenance: https://s3.amazonaws.com/download.onnx/models/opset_9/shufflenet.tar.gz
249 *
Georgios Pinitas108ab0b2018-09-14 18:35:11 +0100250 * @note To list all the possible arguments execute the binary appended with the --help option
251 *
252 * @param[in] argc Number of arguments
253 * @param[in] argv Arguments
254 */
255int main(int argc, char **argv)
256{
257 return arm_compute::utils::run_example<ShuffleNetExample>(argc, argv);
258}