blob: 5b6a4815a1da59d4f47d54ae2508e4cfb1940fb5 [file] [log] [blame]
Alex Gilday8913d8d2018-02-15 11:07:18 +00001/*
2 * Copyright (c) 2017-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 */
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 Pinitas7b2f0262018-08-14 16:40:18 +010034/** Example demonstrating how to implement ResNetV1_50 network using the Compute Library's graph API
Alex Gilday8913d8d2018-02-15 11:07:18 +000035 *
36 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010037 * @param[in] argv Arguments
Alex Gilday8913d8d2018-02-15 11:07:18 +000038 */
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010039class GraphResNetV1_50Example : public Example
Alex Gilday8913d8d2018-02-15 11:07:18 +000040{
41public:
Georgios Pinitas7b2f0262018-08-14 16:40:18 +010042 GraphResNetV1_50Example()
43 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNetV1_50")
Alex Gilday8913d8d2018-02-15 11:07:18 +000044 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010045 }
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 Pinitas12be7ab2018-07-03 12:06:23 +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;
Alex Gilday8913d8d2018-02-15 11:07:18 +000070
71 // Create a preprocessor object
72 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
73 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb,
74 false /* Do not convert to BGR */);
Georgios Pinitase2220552018-07-20 13:23:44 +010075
76 // Create input descriptor
77 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
78 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
79
80 // Set weights trained layout
81 const DataLayout weights_layout = DataLayout::NCHW;
82
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010083 graph << common_params.target
84 << common_params.fast_math_hint
Georgios Pinitase2220552018-07-20 13:23:44 +010085 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +000086 << ConvolutionLayer(
87 7U, 7U, 64U,
Georgios Pinitase2220552018-07-20 13:23:44 +010088 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +000089 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
90 PadStrideInfo(2, 2, 3, 3))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010091 .set_name("conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +000092 << BatchNormalizationLayer(
93 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
94 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
95 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
96 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
97 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010098 .set_name("conv1/BatchNorm")
99 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu")
100 << 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 +0000101
Georgios Pinitase2220552018-07-20 13:23:44 +0100102 add_residual_block(data_path, "block1", weights_layout, 64, 3, 2);
103 add_residual_block(data_path, "block2", weights_layout, 128, 4, 2);
104 add_residual_block(data_path, "block3", weights_layout, 256, 6, 2);
105 add_residual_block(data_path, "block4", weights_layout, 512, 3, 1);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000106
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100107 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000108 << ConvolutionLayer(
109 1U, 1U, 1000U,
Georgios Pinitase2220552018-07-20 13:23:44 +0100110 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000111 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
112 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100113 .set_name("logits/convolution")
114 << FlattenLayer().set_name("predictions/Reshape")
115 << SoftmaxLayer().set_name("predictions/Softmax")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100116 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000117
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000118 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000119 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100120 config.num_threads = common_params.threads;
121 config.use_tuner = common_params.enable_tuner;
122 graph.finalize(common_params.target, config);
123
124 return true;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000125 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000126
Alex Gilday8913d8d2018-02-15 11:07:18 +0000127 void do_run() override
128 {
129 // Run graph
130 graph.run();
131 }
132
133private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100134 CommandLineParser cmd_parser;
135 CommonGraphOptions common_opts;
136 CommonGraphParams common_params;
137 Stream graph;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000138
Georgios Pinitase2220552018-07-20 13:23:44 +0100139 void add_residual_block(const std::string &data_path, const std::string &name, DataLayout weights_layout,
140 unsigned int base_depth, unsigned int num_units, unsigned int stride)
Alex Gilday8913d8d2018-02-15 11:07:18 +0000141 {
142 for(unsigned int i = 0; i < num_units; ++i)
143 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100144 std::stringstream unit_path_ss;
145 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
146 std::stringstream unit_name_ss;
147 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
148
149 std::string unit_path = unit_path_ss.str();
150 std::string unit_name = unit_name_ss.str();
Alex Gilday8913d8d2018-02-15 11:07:18 +0000151
152 unsigned int middle_stride = 1;
153
154 if(i == (num_units - 1))
155 {
156 middle_stride = stride;
157 }
158
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000159 SubStream right(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000160 right << ConvolutionLayer(
161 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100162 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000163 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
164 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100165 .set_name(unit_name + "conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000166 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100167 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
168 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
169 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
170 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000171 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100172 .set_name(unit_name + "conv1/BatchNorm")
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000174
175 << ConvolutionLayer(
176 3U, 3U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100177 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000178 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
179 PadStrideInfo(middle_stride, middle_stride, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100180 .set_name(unit_name + "conv2/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000181 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100182 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
183 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
184 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
185 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000186 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100187 .set_name(unit_name + "conv2/BatchNorm")
188 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000189
190 << ConvolutionLayer(
191 1U, 1U, base_depth * 4,
Georgios Pinitase2220552018-07-20 13:23:44 +0100192 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000193 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
194 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100195 .set_name(unit_name + "conv3/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000196 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100197 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
198 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
199 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
200 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"),
201 0.0000100099996416f)
202 .set_name(unit_name + "conv2/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000203
204 if(i == 0)
205 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000206 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000207 left << ConvolutionLayer(
208 1U, 1U, base_depth * 4,
Georgios Pinitase2220552018-07-20 13:23:44 +0100209 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000210 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
211 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100212 .set_name(unit_name + "shortcut/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000213 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100214 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
215 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
216 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
217 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
218 0.0000100099996416f)
219 .set_name(unit_name + "shortcut/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000220
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100221 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000222 }
223 else if(middle_stride > 1)
224 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000225 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100226 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 +0000227
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100228 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000229 }
230 else
231 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000232 SubStream left(graph);
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100233 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000234 }
235
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100236 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000237 }
238 }
239};
240
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100241/** Main program for ResNetV1_50
Alex Gilday8913d8d2018-02-15 11:07:18 +0000242 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100243 * @note To list all the possible arguments execute the binary appended with the --help option
244 *
Alex Gilday8913d8d2018-02-15 11:07:18 +0000245 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100246 * @param[in] argv Arguments
Alex Gilday8913d8d2018-02-15 11:07:18 +0000247 */
248int main(int argc, char **argv)
249{
Georgios Pinitas7b2f0262018-08-14 16:40:18 +0100250 return arm_compute::utils::run_example<GraphResNetV1_50Example>(argc, argv);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000251}