blob: 7c9b95e47e95db16a5106febd51f46ed14b63826 [file] [log] [blame]
Alex Gilday8913d8d2018-02-15 11:07:18 +00001/*
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +00002 * Copyright (c) 2017-2019 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"
Alex Gilday8913d8d2018-02-15 11:07:18 +000025#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010026#include "utils/CommonGraphOptions.h"
Alex Gilday8913d8d2018-02-15 11:07:18 +000027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Alex Gilday8913d8d2018-02-15 11:07:18 +000030using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031using namespace arm_compute::graph::frontend;
Alex Gilday8913d8d2018-02-15 11:07:18 +000032using namespace arm_compute::graph_utils;
33
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010034/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API */
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010035class GraphResNetV1_50Example : public Example
Alex Gilday8913d8d2018-02-15 11:07:18 +000036{
37public:
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010038 GraphResNetV1_50Example()
39 : 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);
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 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010058 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010059
60 // Print parameter values
61 std::cout << common_params << std::endl;
62
63 // Get trainable parameters data path
64 std::string data_path = common_params.data_path;
Alex Gilday8913d8d2018-02-15 11:07:18 +000065
66 // Create a preprocessor object
67 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
68 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb,
69 false /* Do not convert to BGR */);
Georgios Pinitase2220552018-07-20 13:23:44 +010070
71 // Create input descriptor
72 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
73 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
74
75 // Set weights trained layout
76 const DataLayout weights_layout = DataLayout::NCHW;
77
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010078 graph << common_params.target
79 << common_params.fast_math_hint
Georgios Pinitase2220552018-07-20 13:23:44 +010080 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +000081 << ConvolutionLayer(
82 7U, 7U, 64U,
Georgios Pinitase2220552018-07-20 13:23:44 +010083 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +000084 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
85 PadStrideInfo(2, 2, 3, 3))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010086 .set_name("conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +000087 << BatchNormalizationLayer(
88 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)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010093 .set_name("conv1/BatchNorm")
94 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu")
95 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool1/MaxPool");
Alex Gilday8913d8d2018-02-15 11:07:18 +000096
Georgios Pinitase2220552018-07-20 13:23:44 +010097 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);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000101
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100102 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000103 << ConvolutionLayer(
104 1U, 1U, 1000U,
Georgios Pinitase2220552018-07-20 13:23:44 +0100105 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000106 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
107 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100108 .set_name("logits/convolution")
109 << FlattenLayer().set_name("predictions/Reshape")
110 << SoftmaxLayer().set_name("predictions/Softmax")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100111 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000112
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000113 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000114 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100115 config.num_threads = common_params.threads;
116 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100117 config.tuner_mode = common_params.tuner_mode;
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +0000118 config.tuner_file = common_params.tuner_file;
119
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100120 graph.finalize(common_params.target, config);
121
122 return true;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000123 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000124
Alex Gilday8913d8d2018-02-15 11:07:18 +0000125 void do_run() override
126 {
127 // Run graph
128 graph.run();
129 }
130
131private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100132 CommandLineParser cmd_parser;
133 CommonGraphOptions common_opts;
134 CommonGraphParams common_params;
135 Stream graph;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000136
Georgios Pinitase2220552018-07-20 13:23:44 +0100137 void add_residual_block(const std::string &data_path, const std::string &name, DataLayout weights_layout,
138 unsigned int base_depth, unsigned int num_units, unsigned int stride)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000139 {
140 for(unsigned int i = 0; i < num_units; ++i)
141 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100142 std::stringstream unit_path_ss;
143 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
144 std::stringstream unit_name_ss;
145 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
146
147 std::string unit_path = unit_path_ss.str();
148 std::string unit_name = unit_name_ss.str();
Alex Gilday8913d8d2018-02-15 11:07:18 +0000149
150 unsigned int middle_stride = 1;
151
152 if(i == (num_units - 1))
153 {
154 middle_stride = stride;
155 }
156
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000157 SubStream right(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000158 right << ConvolutionLayer(
159 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100160 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000161 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
162 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100163 .set_name(unit_name + "conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000164 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100165 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
166 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
167 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
168 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000169 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100170 .set_name(unit_name + "conv1/BatchNorm")
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000172
173 << ConvolutionLayer(
174 3U, 3U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100175 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000176 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
177 PadStrideInfo(middle_stride, middle_stride, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100178 .set_name(unit_name + "conv2/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000179 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100180 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
181 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
182 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
183 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000184 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100185 .set_name(unit_name + "conv2/BatchNorm")
186 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000187
188 << ConvolutionLayer(
189 1U, 1U, base_depth * 4,
Georgios Pinitase2220552018-07-20 13:23:44 +0100190 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000191 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
192 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100193 .set_name(unit_name + "conv3/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000194 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100195 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
196 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
197 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
198 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"),
199 0.0000100099996416f)
200 .set_name(unit_name + "conv2/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000201
202 if(i == 0)
203 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000204 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000205 left << ConvolutionLayer(
206 1U, 1U, base_depth * 4,
Georgios Pinitase2220552018-07-20 13:23:44 +0100207 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000208 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
209 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100210 .set_name(unit_name + "shortcut/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000211 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100212 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
213 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
214 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
215 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
216 0.0000100099996416f)
217 .set_name(unit_name + "shortcut/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000218
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100219 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000220 }
221 else if(middle_stride > 1)
222 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000223 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100224 left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true)).set_name(unit_name + "shortcut/MaxPool");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000225
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100226 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000227 }
228 else
229 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000230 SubStream left(graph);
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100231 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000232 }
233
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100234 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000235 }
236 }
237};
238
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100239/** Main program for ResNetV1_50
Alex Gilday8913d8d2018-02-15 11:07:18 +0000240 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000241 * Model is based on:
242 * https://arxiv.org/abs/1512.03385
243 * "Deep Residual Learning for Image Recognition"
244 * Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
245 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000246 * Provenance: download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz
247 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100248 * @note To list all the possible arguments execute the binary appended with the --help option
249 *
Alex Gilday8913d8d2018-02-15 11:07:18 +0000250 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100251 * @param[in] argv Arguments
Alex Gilday8913d8d2018-02-15 11:07:18 +0000252 */
253int main(int argc, char **argv)
254{
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100255 return arm_compute::utils::run_example<GraphResNetV1_50Example>(argc, argv);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000256}