blob: 704e0e420e0ada689b7ccbef61100e213aa4a9c1 [file] [log] [blame]
Georgios Pinitas7b2f0262018-08-14 16:40:18 +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::utils;
31using namespace arm_compute::graph::frontend;
32using namespace arm_compute::graph_utils;
33
34/** Example demonstrating how to implement ResNetV2_50 network using the Compute Library's graph API
35 *
36 * @param[in] argc Number of arguments
37 * @param[in] argv Arguments
38 */
39class GraphResNetV2_50Example : public Example
40{
41public:
42 GraphResNetV2_50Example()
43 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNetV2_50")
44 {
45 }
46 bool do_setup(int argc, char **argv) override
47 {
48 // Parse arguments
49 cmd_parser.parse(argc, argv);
50
51 // Consume common parameters
52 common_params = consume_common_graph_parameters(common_opts);
53
54 // Return when help menu is requested
55 if(common_params.help)
56 {
57 cmd_parser.print_help(argv[0]);
58 return false;
59 }
60
61 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010062 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
63 ARM_COMPUTE_EXIT_ON_MSG(common_params.data_type == DataType::F16 && common_params.target == Target::NEON, "F16 NEON not supported for this graph");
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010064
65 // Print parameter values
66 std::cout << common_params << std::endl;
67
68 // Get trainable parameters data path
69 std::string data_path = common_params.data_path;
70 std::string model_path = "/cnn_data/resnet_v2_50_model/";
71 if(!data_path.empty())
72 {
73 data_path += model_path;
74 }
75
76 // Create a preprocessor object
77 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
78
79 // Create input descriptor
80 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
81 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
82
83 // Set weights trained layout
84 const DataLayout weights_layout = DataLayout::NCHW;
85
86 graph << common_params.target
87 << common_params.fast_math_hint
88 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
89 << ConvolutionLayer(
90 7U, 7U, 64U,
91 get_weights_accessor(data_path, "conv1_weights.npy", weights_layout),
92 get_weights_accessor(data_path, "conv1_biases.npy", weights_layout),
93 PadStrideInfo(2, 2, 3, 3))
94 .set_name("conv1/convolution")
95 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool1/MaxPool");
96
97 add_residual_block(data_path, "block1", weights_layout, 64, 3, 2);
98 add_residual_block(data_path, "block2", weights_layout, 128, 4, 2);
99 add_residual_block(data_path, "block3", weights_layout, 256, 6, 2);
100 add_residual_block(data_path, "block4", weights_layout, 512, 3, 1);
101
102 graph << BatchNormalizationLayer(
103 get_weights_accessor(data_path, "postnorm_moving_mean.npy"),
104 get_weights_accessor(data_path, "postnorm_moving_variance.npy"),
105 get_weights_accessor(data_path, "postnorm_gamma.npy"),
106 get_weights_accessor(data_path, "postnorm_beta.npy"),
107 0.000009999999747378752f)
108 .set_name("postnorm/BatchNorm")
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("postnorm/Relu")
110 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
111 << ConvolutionLayer(
112 1U, 1U, 1001U,
113 get_weights_accessor(data_path, "logits_weights.npy", weights_layout),
114 get_weights_accessor(data_path, "logits_biases.npy"),
115 PadStrideInfo(1, 1, 0, 0))
116 .set_name("logits/convolution")
117 << FlattenLayer().set_name("predictions/Reshape")
118 << SoftmaxLayer().set_name("predictions/Softmax")
119 << OutputLayer(get_output_accessor(common_params, 5));
120
121 // Finalize graph
122 GraphConfig config;
123 config.num_threads = common_params.threads;
124 config.use_tuner = common_params.enable_tuner;
125 graph.finalize(common_params.target, config);
126
127 return true;
128 }
129
130 void do_run() override
131 {
132 // Run graph
133 graph.run();
134 }
135
136private:
137 CommandLineParser cmd_parser;
138 CommonGraphOptions common_opts;
139 CommonGraphParams common_params;
140 Stream graph;
141
142 void add_residual_block(const std::string &data_path, const std::string &name, DataLayout weights_layout,
143 unsigned int base_depth, unsigned int num_units, unsigned int stride)
144 {
145 for(unsigned int i = 0; i < num_units; ++i)
146 {
147 // Generate unit names
148 std::stringstream unit_path_ss;
149 unit_path_ss << name << "_unit_" << (i + 1) << "_bottleneck_v2_";
150 std::stringstream unit_name_ss;
151 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v2/";
152
153 std::string unit_path = unit_path_ss.str();
154 std::string unit_name = unit_name_ss.str();
155
156 const TensorShape last_shape = graph.graph().node(graph.tail_node())->output(0)->desc().shape;
157 unsigned int depth_in = last_shape[arm_compute::get_data_layout_dimension_index(common_params.data_layout, DataLayoutDimension::CHANNEL)];
158 unsigned int depth_out = base_depth * 4;
159
160 // All units have stride 1 apart from last one
161 unsigned int middle_stride = (i == (num_units - 1)) ? stride : 1;
162
163 // Preact
164 SubStream preact(graph);
165 preact << BatchNormalizationLayer(
166 get_weights_accessor(data_path, unit_path + "preact_moving_mean.npy"),
167 get_weights_accessor(data_path, unit_path + "preact_moving_variance.npy"),
168 get_weights_accessor(data_path, unit_path + "preact_gamma.npy"),
169 get_weights_accessor(data_path, unit_path + "preact_beta.npy"),
170 0.000009999999747378752f)
171 .set_name(unit_name + "preact/BatchNorm")
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "preact/Relu");
173
174 // Create bottleneck path
175 SubStream shortcut(graph);
176 if(depth_in == depth_out)
177 {
178 if(middle_stride != 1)
179 {
180 shortcut << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true)).set_name(unit_name + "shortcut/MaxPool");
181 }
182 }
183 else
184 {
185 shortcut.forward_tail(preact.tail_node());
186 shortcut << ConvolutionLayer(
187 1U, 1U, depth_out,
188 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout),
189 get_weights_accessor(data_path, unit_path + "shortcut_biases.npy", weights_layout),
190 PadStrideInfo(1, 1, 0, 0))
191 .set_name(unit_name + "shortcut/convolution");
192 }
193
194 // Create residual path
195 SubStream residual(preact);
196 residual << ConvolutionLayer(
197 1U, 1U, base_depth,
198 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
199 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
200 PadStrideInfo(1, 1, 0, 0))
201 .set_name(unit_name + "conv1/convolution")
202 << BatchNormalizationLayer(
203 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
204 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
205 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
206 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
207 0.000009999999747378752f)
208 .set_name(unit_name + "conv1/BatchNorm")
209 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
210 << ConvolutionLayer(
211 3U, 3U, base_depth,
212 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
213 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
214 PadStrideInfo(middle_stride, middle_stride, 1, 1))
215 .set_name(unit_name + "conv2/convolution")
216 << BatchNormalizationLayer(
217 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
218 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
219 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
220 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
221 0.000009999999747378752f)
222 .set_name(unit_name + "conv2/BatchNorm")
223 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
224 << ConvolutionLayer(
225 1U, 1U, depth_out,
226 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
227 get_weights_accessor(data_path, unit_path + "conv3_biases.npy", weights_layout),
228 PadStrideInfo(1, 1, 0, 0))
229 .set_name(unit_name + "conv3/convolution");
230
231 graph << BranchLayer(BranchMergeMethod::ADD, std::move(shortcut), std::move(residual)).set_name(unit_name + "add");
232 }
233 }
234};
235
236/** Main program for ResNetV2_50
237 *
238 * @note To list all the possible arguments execute the binary appended with the --help option
239 *
240 * @param[in] argc Number of arguments
241 * @param[in] argv Arguments
242 */
243int main(int argc, char **argv)
244{
245 return arm_compute::utils::run_example<GraphResNetV2_50Example>(argc, argv);
246}