blob: 63dcba69505311aeaaa2cac1e8e2f942b08c4ae2 [file] [log] [blame]
Georgios Pinitas766b70c2018-08-13 17:50:34 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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 MobileNetV2's network using the Compute Library's graph API */
36class GraphMobilenetV2Example : public Example
37{
38public:
39 GraphMobilenetV2Example()
40 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV2")
41 {
42 }
43 GraphMobilenetV2Example(const GraphMobilenetV2Example &) = delete;
44 GraphMobilenetV2Example &operator=(const GraphMobilenetV2Example &) = delete;
45 GraphMobilenetV2Example(GraphMobilenetV2Example &&) = default; // NOLINT
46 GraphMobilenetV2Example &operator=(GraphMobilenetV2Example &&) = default; // NOLINT
47 ~GraphMobilenetV2Example() override = default;
48
49 bool do_setup(int argc, char **argv) override
50 {
51 // Parse arguments
52 cmd_parser.parse(argc, argv);
53
54 // Consume common parameters
55 common_params = consume_common_graph_parameters(common_opts);
56
57 // Return when help menu is requested
58 if(common_params.help)
59 {
60 cmd_parser.print_help(argv[0]);
61 return false;
62 }
63
64 // Print parameter values
65 std::cout << common_params << std::endl;
66
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010067 // Create model path
68 std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/";
Georgios Pinitas766b70c2018-08-13 17:50:34 +010069
70 // Create input descriptor
71 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
72 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
73
74 // Create a preprocessor object
75 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
76
77 // Get trainable parameters data path
78 std::string data_path = common_params.data_path;
79
80 // Add model path to data path
81 if(!data_path.empty())
82 {
83 data_path += model_path;
84 }
85
86 // Create graph
87 graph << common_params.target
88 << DepthwiseConvolutionMethod::Optimized3x3 // FIXME(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method
89 << common_params.fast_math_hint
90 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false))
91 << ConvolutionLayer(3U, 3U, 32U,
92 get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW),
93 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
94 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL))
95 .set_name("Conv")
96 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_BatchNorm_moving_mean.npy"),
97 get_weights_accessor(data_path, "Conv_BatchNorm_moving_variance.npy"),
98 get_weights_accessor(data_path, "Conv_BatchNorm_gamma.npy"),
99 get_weights_accessor(data_path, "Conv_BatchNorm_beta.npy"),
100 0.0010000000474974513f)
101 .set_name("Conv/BatchNorm")
102 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
103 .set_name("Conv/Relu6");
104
105 get_expanded_conv(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1));
106 get_expanded_conv(data_path, "expanded_conv_1", 16U, 24U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true);
107 get_expanded_conv(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), true, true);
108 get_expanded_conv(data_path, "expanded_conv_3", 24U, 32U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true);
109 get_expanded_conv(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), true, true);
110 get_expanded_conv(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), true, true);
111 get_expanded_conv(data_path, "expanded_conv_6", 32U, 64U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true);
112 get_expanded_conv(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true);
113 get_expanded_conv(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true);
114 get_expanded_conv(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), true, true);
115 get_expanded_conv(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), true);
116 get_expanded_conv(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), true, true);
117 get_expanded_conv(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), true, true);
118 get_expanded_conv(data_path, "expanded_conv_13", 96U, 160U, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), true);
119 get_expanded_conv(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), true, true);
120 get_expanded_conv(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), true, true);
121 get_expanded_conv(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), true);
122
123 graph << ConvolutionLayer(1U, 1U, 1280U,
124 get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW),
125 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
126 PadStrideInfo(1, 1, 0, 0))
127 .set_name("Conv_1")
128 << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"),
129 get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"),
130 get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"),
131 get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"),
132 0.0010000000474974513f)
133 .set_name("Conv_1/BatchNorm")
134 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
135 .set_name("Conv_1/Relu6")
136 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool")
137 << ConvolutionLayer(1U, 1U, 1001U,
138 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
139 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
140 PadStrideInfo(1, 1, 0, 0))
141 .set_name("Logits/Conv2d_1c_1x1")
142 << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape")
143 << SoftmaxLayer().set_name("Predictions/Softmax")
144 << OutputLayer(get_output_accessor(common_params, 5));
145
146 // Finalize graph
147 GraphConfig config;
148 config.num_threads = common_params.threads;
149 config.use_tuner = common_params.enable_tuner;
150 config.tuner_file = common_params.tuner_file;
151
152 graph.finalize(common_params.target, config);
153
154 return true;
155 }
156
157 void do_run() override
158 {
159 // Run graph
160 graph.run();
161 }
162
163private:
164 CommandLineParser cmd_parser;
165 CommonGraphOptions common_opts;
166 CommonGraphParams common_params;
167 Stream graph;
168
169 void get_expanded_conv(const std::string &data_path, std::string &&param_path,
170 unsigned int input_channels, unsigned int output_channels,
171 PadStrideInfo dwc_pad_stride_info,
172 bool has_expand = false, bool is_residual = false, unsigned int expansion_size = 6)
173 {
174 std::string total_path = param_path + "_";
175 SubStream left(graph);
176
177 // Add expand node
178 if(has_expand)
179 {
180 left << ConvolutionLayer(1U, 1U, input_channels * expansion_size,
181 get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW),
182 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
183 .set_name(param_path + "/expand/Conv2D")
184 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"),
185 get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"),
186 get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"),
187 get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"),
188 0.0010000000474974513f)
189 .set_name(param_path + "/expand/BatchNorm")
190 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
191 .set_name(param_path + "/expand/Relu6");
192 }
193
194 // Add depthwise node
195 left << DepthwiseConvolutionLayer(3U, 3U,
196 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
197 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
198 dwc_pad_stride_info)
199 .set_name(param_path + "/depthwise/depthwise")
200 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
201 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
202 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
203 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
204 0.0010000000474974513f)
205 .set_name(param_path + "/depthwise/BatchNorm")
206 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
207 .set_name(param_path + "/depthwise/Relu6");
208
209 // Add project node
210 left << ConvolutionLayer(1U, 1U, output_channels,
211 get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW),
212 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
213 .set_name(param_path + "/project/Conv2D")
214 << BatchNormalizationLayer(get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"),
215 get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"),
216 get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"),
217 get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"),
218 0.0010000000474974513)
219 .set_name(param_path + "/project/BatchNorm");
220
221 if(is_residual)
222 {
223 // Add residual node
224 SubStream right(graph);
225 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(param_path + "/add");
226 }
227 else
228 {
229 graph.forward_tail(left.tail_node());
230 }
231 }
232};
233
234/** Main program for MobileNetV2
235 *
236 * @note To list all the possible arguments execute the binary appended with the --help option
237 *
238 * @param[in] argc Number of arguments
239 * @param[in] argv Arguments
240 */
241int main(int argc, char **argv)
242{
243 return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv);
244}