blob: ba0f0d5fb611f96aa8943bde351afeb7c2850a9d [file] [log] [blame]
Alex Gilday8913d8d2018-02-15 11:07:18 +00001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2017-2021 Arm Limited.
Alex Gilday8913d8d2018-02-15 11:07:18 +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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Alex Gilday8913d8d2018-02-15 11:07:18 +000026#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010027#include "utils/CommonGraphOptions.h"
Alex Gilday8913d8d2018-02-15 11:07:18 +000028#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
Alex Gilday8913d8d2018-02-15 11:07:18 +000031using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032using namespace arm_compute::graph::frontend;
Alex Gilday8913d8d2018-02-15 11:07:18 +000033using namespace arm_compute::graph_utils;
34
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010035/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API */
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010036class GraphResNetV1_50Example : public Example
Alex Gilday8913d8d2018-02-15 11:07:18 +000037{
38public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 GraphResNetV1_50Example() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNetV1_50")
Alex Gilday8913d8d2018-02-15 11:07:18 +000040 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010041 }
42 bool do_setup(int argc, char **argv) override
43 {
44 // Parse arguments
45 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010046 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010047
48 // Consume common parameters
49 common_params = consume_common_graph_parameters(common_opts);
50
51 // Return when help menu is requested
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (common_params.help)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010053 {
54 cmd_parser.print_help(argv[0]);
55 return false;
56 }
57
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058 // Print parameter values
59 std::cout << common_params << std::endl;
60
61 // Get trainable parameters data path
62 std::string data_path = common_params.data_path;
Alex Gilday8913d8d2018-02-15 11:07:18 +000063
64 // Create a preprocessor object
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 const std::array<float, 3> mean_rgb{{122.68f, 116.67f, 104.01f}};
66 std::unique_ptr<IPreprocessor> preprocessor =
67 std::make_unique<CaffePreproccessor>(mean_rgb, false /* Do not convert to BGR */);
Georgios Pinitase2220552018-07-20 13:23:44 +010068
69 // Create input descriptor
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000070 const auto operation_layout = common_params.data_layout;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 const TensorShape tensor_shape =
72 permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
73 TensorDescriptor input_descriptor =
74 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
Georgios Pinitase2220552018-07-20 13:23:44 +010075
76 // Set weights trained layout
77 const DataLayout weights_layout = DataLayout::NCHW;
78
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 graph << common_params.target << common_params.fast_math_hint
80 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor),
81 false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +000082 << ConvolutionLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 7U, 7U, 64U,
84 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy", weights_layout),
85 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(2, 2, 3, 3))
86 .set_name("conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +000087 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
89 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
90 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
91 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
92 0.0000100099996416f)
93 .set_name("conv1/BatchNorm")
94 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
95 .set_name("conv1/Relu")
96 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout,
97 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)))
98 .set_name("pool1/MaxPool");
Alex Gilday8913d8d2018-02-15 11:07:18 +000099
Georgios Pinitase2220552018-07-20 13:23:44 +0100100 add_residual_block(data_path, "block1", weights_layout, 64, 3, 2);
101 add_residual_block(data_path, "block2", weights_layout, 128, 4, 2);
102 add_residual_block(data_path, "block3", weights_layout, 256, 6, 2);
103 add_residual_block(data_path, "block4", weights_layout, 512, 3, 1);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000104
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000105 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("pool5")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000106 << ConvolutionLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 1U, 1U, 1000U,
108 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy", weights_layout),
109 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
110 PadStrideInfo(1, 1, 0, 0))
111 .set_name("logits/convolution")
112 << FlattenLayer().set_name("predictions/Reshape") << SoftmaxLayer().set_name("predictions/Softmax")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100113 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000114
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000115 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000116 GraphConfig config;
SiCongLif466d752021-03-01 15:26:18 +0000117 config.num_threads = common_params.threads;
118 config.use_tuner = common_params.enable_tuner;
119 config.tuner_mode = common_params.tuner_mode;
120 config.tuner_file = common_params.tuner_file;
121 config.mlgo_file = common_params.mlgo_file;
122 config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type);
123 config.synthetic_type = common_params.data_type;
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +0000124
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100125 graph.finalize(common_params.target, config);
126
127 return true;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000128 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000129
Alex Gilday8913d8d2018-02-15 11:07:18 +0000130 void do_run() override
131 {
132 // Run graph
133 graph.run();
134 }
135
136private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100137 CommandLineParser cmd_parser;
138 CommonGraphOptions common_opts;
139 CommonGraphParams common_params;
140 Stream graph;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000141
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 void add_residual_block(const std::string &data_path,
143 const std::string &name,
144 DataLayout weights_layout,
145 unsigned int base_depth,
146 unsigned int num_units,
147 unsigned int stride)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000148 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 for (unsigned int i = 0; i < num_units; ++i)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000150 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100151 std::stringstream unit_path_ss;
152 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
153 std::stringstream unit_name_ss;
154 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
155
156 std::string unit_path = unit_path_ss.str();
157 std::string unit_name = unit_name_ss.str();
Alex Gilday8913d8d2018-02-15 11:07:18 +0000158
159 unsigned int middle_stride = 1;
160
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 if (i == (num_units - 1))
Alex Gilday8913d8d2018-02-15 11:07:18 +0000162 {
163 middle_stride = stride;
164 }
165
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000166 SubStream right(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 right << ConvolutionLayer(1U, 1U, base_depth,
168 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
169 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
170 PadStrideInfo(1, 1, 0, 0))
171 .set_name(unit_name + "conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000172 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
174 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
175 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
176 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"), 0.0000100099996416f)
177 .set_name(unit_name + "conv1/BatchNorm")
178 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
179 .set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000180
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 << ConvolutionLayer(3U, 3U, base_depth,
182 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
183 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
184 PadStrideInfo(middle_stride, middle_stride, 1, 1))
185 .set_name(unit_name + "conv2/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000186 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
188 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
189 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
190 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"), 0.0000100099996416f)
191 .set_name(unit_name + "conv2/BatchNorm")
192 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
193 .set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000194
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 << ConvolutionLayer(1U, 1U, base_depth * 4,
196 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
197 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
198 PadStrideInfo(1, 1, 0, 0))
199 .set_name(unit_name + "conv3/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000200 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
202 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
203 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
204 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"), 0.0000100099996416f)
205 .set_name(unit_name + "conv2/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000206
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 if (i == 0)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000208 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000209 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000210 left << ConvolutionLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 1U, 1U, base_depth * 4,
212 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout),
213 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0))
214 .set_name(unit_name + "shortcut/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000215 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
217 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
218 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
219 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
220 0.0000100099996416f)
221 .set_name(unit_name + "shortcut/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000222
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add)
224 .set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000225 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 else if (middle_stride > 1)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000227 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000228 SubStream left(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, common_params.data_layout,
230 PadStrideInfo(middle_stride, middle_stride, 0, 0), true))
231 .set_name(unit_name + "shortcut/MaxPool");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000232
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add)
234 .set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000235 }
236 else
237 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000238 SubStream left(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add)
240 .set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000241 }
242
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
244 .set_name(unit_name + "Relu");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000245 }
246 }
247};
248
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100249/** Main program for ResNetV1_50
Alex Gilday8913d8d2018-02-15 11:07:18 +0000250 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000251 * Model is based on:
252 * https://arxiv.org/abs/1512.03385
253 * "Deep Residual Learning for Image Recognition"
254 * Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
255 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000256 * Provenance: download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz
257 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100258 * @note To list all the possible arguments execute the binary appended with the --help option
259 *
Alex Gilday8913d8d2018-02-15 11:07:18 +0000260 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100261 * @param[in] argv Arguments
Alex Gilday8913d8d2018-02-15 11:07:18 +0000262 */
263int main(int argc, char **argv)
264{
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100265 return arm_compute::utils::run_example<GraphResNetV1_50Example>(argc, argv);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000266}