blob: 17506dcf7181554d03e3f643dd15a0b005f7b10c [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier06ea0482018-02-22 15:45:35 +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 */
Jenkinsb3a371b2018-05-23 11:36:53 +010024#include "arm_compute/graph.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000025#include "support/ToolchainSupport.h"
Jenkins52ba29e2018-08-29 15:32:11 +000026#include "utils/CommonGraphOptions.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Anthony Barbier06ea0482018-02-22 15:45:35 +000030using namespace arm_compute::utils;
Jenkinsb3a371b2018-05-23 11:36:53 +010031using namespace arm_compute::graph::frontend;
Anthony Barbier06ea0482018-02-22 15:45:35 +000032using namespace arm_compute::graph_utils;
33
Jenkinsb9abeae2018-11-22 11:58:08 +000034/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API */
35class GraphResNetV1_50Example : public Example
Anthony Barbier06ea0482018-02-22 15:45:35 +000036{
37public:
Jenkinsb9abeae2018-11-22 11:58:08 +000038 GraphResNetV1_50Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNetV1_50")
Anthony Barbier06ea0482018-02-22 15:45:35 +000040 {
Jenkins52ba29e2018-08-29 15:32:11 +000041 }
42 bool do_setup(int argc, char **argv) override
43 {
44 // Parse arguments
45 cmd_parser.parse(argc, argv);
Jenkins0e205f72019-11-28 16:53:35 +000046 cmd_parser.validate();
Jenkins52ba29e2018-08-29 15:32:11 +000047
48 // Consume common parameters
49 common_params = consume_common_graph_parameters(common_opts);
50
51 // Return when help menu is requested
52 if(common_params.help)
53 {
54 cmd_parser.print_help(argv[0]);
55 return false;
56 }
57
58 // Checks
59 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Jenkins52ba29e2018-08-29 15:32:11 +000060
61 // Print parameter values
62 std::cout << common_params << std::endl;
63
64 // Get trainable parameters data path
65 std::string data_path = common_params.data_path;
Anthony Barbier06ea0482018-02-22 15:45:35 +000066
67 // Create a preprocessor object
68 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
69 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb,
70 false /* Do not convert to BGR */);
71
Jenkins52ba29e2018-08-29 15:32:11 +000072 // Create input descriptor
73 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
74 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Anthony Barbier06ea0482018-02-22 15:45:35 +000075
Jenkins52ba29e2018-08-29 15:32:11 +000076 // Set weights trained layout
77 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbier06ea0482018-02-22 15:45:35 +000078
Jenkins52ba29e2018-08-29 15:32:11 +000079 graph << common_params.target
80 << common_params.fast_math_hint
81 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
Anthony Barbier06ea0482018-02-22 15:45:35 +000082 << ConvolutionLayer(
83 7U, 7U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +000084 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +000085 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
86 PadStrideInfo(2, 2, 3, 3))
Jenkinsb3a371b2018-05-23 11:36:53 +010087 .set_name("conv1/convolution")
Anthony Barbier06ea0482018-02-22 15:45:35 +000088 << BatchNormalizationLayer(
89 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
90 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
91 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
92 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
93 0.0000100099996416f)
Jenkinsb3a371b2018-05-23 11:36:53 +010094 .set_name("conv1/BatchNorm")
95 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu")
96 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool1/MaxPool");
Anthony Barbier06ea0482018-02-22 15:45:35 +000097
Jenkins52ba29e2018-08-29 15:32:11 +000098 add_residual_block(data_path, "block1", weights_layout, 64, 3, 2);
99 add_residual_block(data_path, "block2", weights_layout, 128, 4, 2);
100 add_residual_block(data_path, "block3", weights_layout, 256, 6, 2);
101 add_residual_block(data_path, "block4", weights_layout, 512, 3, 1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102
Jenkinsb3a371b2018-05-23 11:36:53 +0100103 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000104 << ConvolutionLayer(
105 1U, 1U, 1000U,
Jenkins52ba29e2018-08-29 15:32:11 +0000106 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000107 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
108 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +0100109 .set_name("logits/convolution")
110 << FlattenLayer().set_name("predictions/Reshape")
111 << SoftmaxLayer().set_name("predictions/Softmax")
Jenkins52ba29e2018-08-29 15:32:11 +0000112 << OutputLayer(get_output_accessor(common_params, 5));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000113
Jenkinsb3a371b2018-05-23 11:36:53 +0100114 // Finalize graph
115 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000116 config.num_threads = common_params.threads;
117 config.use_tuner = common_params.enable_tuner;
Jenkins4ba87db2019-05-23 17:11:51 +0100118 config.tuner_mode = common_params.tuner_mode;
Jenkins514be652019-02-28 12:25:18 +0000119 config.tuner_file = common_params.tuner_file;
120
Jenkins52ba29e2018-08-29 15:32:11 +0000121 graph.finalize(common_params.target, config);
122
123 return true;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000124 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100125
Anthony Barbier06ea0482018-02-22 15:45:35 +0000126 void do_run() override
127 {
128 // Run graph
129 graph.run();
130 }
131
132private:
Jenkins52ba29e2018-08-29 15:32:11 +0000133 CommandLineParser cmd_parser;
134 CommonGraphOptions common_opts;
135 CommonGraphParams common_params;
136 Stream graph;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000137
Jenkins52ba29e2018-08-29 15:32:11 +0000138 void add_residual_block(const std::string &data_path, const std::string &name, DataLayout weights_layout,
139 unsigned int base_depth, unsigned int num_units, unsigned int stride)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000140 {
141 for(unsigned int i = 0; i < num_units; ++i)
142 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100143 std::stringstream unit_path_ss;
144 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
145 std::stringstream unit_name_ss;
146 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
147
148 std::string unit_path = unit_path_ss.str();
149 std::string unit_name = unit_name_ss.str();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000150
151 unsigned int middle_stride = 1;
152
153 if(i == (num_units - 1))
154 {
155 middle_stride = stride;
156 }
157
Jenkinsb3a371b2018-05-23 11:36:53 +0100158 SubStream right(graph);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000159 right << ConvolutionLayer(
160 1U, 1U, base_depth,
Jenkins52ba29e2018-08-29 15:32:11 +0000161 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000162 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
163 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 .set_name(unit_name + "conv1/convolution")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000165 << BatchNormalizationLayer(
Jenkinsb3a371b2018-05-23 11:36:53 +0100166 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
167 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
168 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
169 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000170 0.0000100099996416f)
Jenkinsb3a371b2018-05-23 11:36:53 +0100171 .set_name(unit_name + "conv1/BatchNorm")
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000173
174 << ConvolutionLayer(
175 3U, 3U, base_depth,
Jenkins52ba29e2018-08-29 15:32:11 +0000176 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000177 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
178 PadStrideInfo(middle_stride, middle_stride, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100179 .set_name(unit_name + "conv2/convolution")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000180 << BatchNormalizationLayer(
Jenkinsb3a371b2018-05-23 11:36:53 +0100181 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
182 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
183 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
184 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000185 0.0000100099996416f)
Jenkinsb3a371b2018-05-23 11:36:53 +0100186 .set_name(unit_name + "conv2/BatchNorm")
187 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000188
189 << ConvolutionLayer(
190 1U, 1U, base_depth * 4,
Jenkins52ba29e2018-08-29 15:32:11 +0000191 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000192 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
193 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +0100194 .set_name(unit_name + "conv3/convolution")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000195 << BatchNormalizationLayer(
Jenkinsb3a371b2018-05-23 11:36:53 +0100196 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
197 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
198 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
199 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"),
200 0.0000100099996416f)
201 .set_name(unit_name + "conv2/BatchNorm");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000202
203 if(i == 0)
204 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100205 SubStream left(graph);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000206 left << ConvolutionLayer(
207 1U, 1U, base_depth * 4,
Jenkins52ba29e2018-08-29 15:32:11 +0000208 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout),
Anthony Barbier06ea0482018-02-22 15:45:35 +0000209 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
210 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +0100211 .set_name(unit_name + "shortcut/convolution")
Anthony Barbier06ea0482018-02-22 15:45:35 +0000212 << BatchNormalizationLayer(
Jenkinsb3a371b2018-05-23 11:36:53 +0100213 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
214 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
215 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
216 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
217 0.0000100099996416f)
218 .set_name(unit_name + "shortcut/BatchNorm");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000219
Jenkinsb9abeae2018-11-22 11:58:08 +0000220 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000221 }
222 else if(middle_stride > 1)
223 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100224 SubStream left(graph);
225 left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true)).set_name(unit_name + "shortcut/MaxPool");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000226
Jenkinsb9abeae2018-11-22 11:58:08 +0000227 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000228 }
229 else
230 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100231 SubStream left(graph);
Jenkinsb9abeae2018-11-22 11:58:08 +0000232 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000233 }
234
Jenkinsb3a371b2018-05-23 11:36:53 +0100235 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000236 }
237 }
238};
239
Jenkinsb9abeae2018-11-22 11:58:08 +0000240/** Main program for ResNetV1_50
241 *
242 * Model is based on:
243 * https://arxiv.org/abs/1512.03385
244 * "Deep Residual Learning for Image Recognition"
245 * Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Anthony Barbier06ea0482018-02-22 15:45:35 +0000246 *
Jenkins514be652019-02-28 12:25:18 +0000247 * Provenance: download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz
248 *
Jenkins52ba29e2018-08-29 15:32:11 +0000249 * @note To list all the possible arguments execute the binary appended with the --help option
250 *
Anthony Barbier06ea0482018-02-22 15:45:35 +0000251 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000252 * @param[in] argv Arguments
Anthony Barbier06ea0482018-02-22 15:45:35 +0000253 */
254int main(int argc, char **argv)
255{
Jenkinsb9abeae2018-11-22 11:58:08 +0000256 return arm_compute::utils::run_example<GraphResNetV1_50Example>(argc, argv);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000257}