blob: 58f36f6ae4e2be8b79e9089a59b5781d0200d05d [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
Isabella Gottardi88d5b222018-04-06 12:24:55 +010034/** Example demonstrating how to implement ResNet50 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 */
39class GraphResNet50Example : public Example
40{
41public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 GraphResNet50Example()
43 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNet50")
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
Georgios Pinitas6ed43b52018-07-12 17:34:22 +010062 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "Unsupported data type!");
63 ARM_COMPUTE_EXIT_ON_MSG(common_params.data_layout == DataLayout::NHWC, "Unsupported data layout!");
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 Pinitas12be7ab2018-07-03 12:06:23 +010075 graph << common_params.target
76 << common_params.fast_math_hint
77 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), common_params.data_type),
78 get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +000079 << ConvolutionLayer(
80 7U, 7U, 64U,
81 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy"),
82 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
83 PadStrideInfo(2, 2, 3, 3))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010084 .set_name("conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +000085 << BatchNormalizationLayer(
86 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
87 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
88 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
89 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
90 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010091 .set_name("conv1/BatchNorm")
92 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu")
93 << 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 +000094
95 add_residual_block(data_path, "block1", 64, 3, 2);
96 add_residual_block(data_path, "block2", 128, 4, 2);
97 add_residual_block(data_path, "block3", 256, 6, 2);
98 add_residual_block(data_path, "block4", 512, 3, 1);
99
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100100 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000101 << ConvolutionLayer(
102 1U, 1U, 1000U,
103 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy"),
104 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
105 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100106 .set_name("logits/convolution")
107 << FlattenLayer().set_name("predictions/Reshape")
108 << SoftmaxLayer().set_name("predictions/Softmax")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100109 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000110
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000111 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000112 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100113 config.num_threads = common_params.threads;
114 config.use_tuner = common_params.enable_tuner;
115 graph.finalize(common_params.target, config);
116
117 return true;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000118 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119
Alex Gilday8913d8d2018-02-15 11:07:18 +0000120 void do_run() override
121 {
122 // Run graph
123 graph.run();
124 }
125
126private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100127 CommandLineParser cmd_parser;
128 CommonGraphOptions common_opts;
129 CommonGraphParams common_params;
130 Stream graph;
Alex Gilday8913d8d2018-02-15 11:07:18 +0000131
132 void add_residual_block(const std::string &data_path, const std::string &name, unsigned int base_depth, unsigned int num_units, unsigned int stride)
133 {
134 for(unsigned int i = 0; i < num_units; ++i)
135 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100136 std::stringstream unit_path_ss;
137 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
138 std::stringstream unit_name_ss;
139 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
140
141 std::string unit_path = unit_path_ss.str();
142 std::string unit_name = unit_name_ss.str();
Alex Gilday8913d8d2018-02-15 11:07:18 +0000143
144 unsigned int middle_stride = 1;
145
146 if(i == (num_units - 1))
147 {
148 middle_stride = stride;
149 }
150
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000151 SubStream right(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000152 right << ConvolutionLayer(
153 1U, 1U, base_depth,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100154 get_weights_accessor(data_path, unit_path + "conv1_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000155 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
156 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100157 .set_name(unit_name + "conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000158 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100159 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
160 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
161 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
162 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000163 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100164 .set_name(unit_name + "conv1/BatchNorm")
165 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000166
167 << ConvolutionLayer(
168 3U, 3U, base_depth,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100169 get_weights_accessor(data_path, unit_path + "conv2_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000170 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
171 PadStrideInfo(middle_stride, middle_stride, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100172 .set_name(unit_name + "conv2/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000173 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100174 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
175 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
176 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
177 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000178 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100179 .set_name(unit_name + "conv2/BatchNorm")
180 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000181
182 << ConvolutionLayer(
183 1U, 1U, base_depth * 4,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100184 get_weights_accessor(data_path, unit_path + "conv3_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000185 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
186 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100187 .set_name(unit_name + "conv3/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000188 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100189 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
190 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
191 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
192 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"),
193 0.0000100099996416f)
194 .set_name(unit_name + "conv2/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000195
196 if(i == 0)
197 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000198 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000199 left << ConvolutionLayer(
200 1U, 1U, base_depth * 4,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100201 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000202 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
203 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100204 .set_name(unit_name + "shortcut/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000205 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100206 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
207 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
208 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
209 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
210 0.0000100099996416f)
211 .set_name(unit_name + "shortcut/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000212
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100213 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000214 }
215 else if(middle_stride > 1)
216 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000217 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100218 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 +0000219
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100220 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000221 }
222 else
223 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000224 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100225 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000226 }
227
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100228 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000229 }
230 }
231};
232
233/** Main program for ResNet50
234 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100235 * @note To list all the possible arguments execute the binary appended with the --help option
236 *
Alex Gilday8913d8d2018-02-15 11:07:18 +0000237 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100238 * @param[in] argv Arguments
Alex Gilday8913d8d2018-02-15 11:07:18 +0000239 */
240int main(int argc, char **argv)
241{
242 return arm_compute::utils::run_example<GraphResNet50Example>(argc, argv);
243}