blob: bafa9a58527540c878da9be555c5d44cb0920277 [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"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30
31using 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
Isabella Gottardi88d5b222018-04-06 12:24:55 +010035/** Example demonstrating how to implement ResNet50 network using the Compute Library's graph API
Alex Gilday8913d8d2018-02-15 11:07:18 +000036 *
37 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +010038 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Alex Gilday8913d8d2018-02-15 11:07:18 +000039 */
40class GraphResNet50Example : public Example
41{
42public:
43 void do_setup(int argc, char **argv) override
44 {
45 std::string data_path; /* Path to the trainable data */
46 std::string image; /* Image data */
47 std::string label; /* Label data */
48
49 // Create a preprocessor object
50 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
51 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb,
52 false /* Do not convert to BGR */);
53
54 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Gian Marco Iodicea8aef292018-05-14 14:21:39 +010055 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
56 Target target_hint = set_target_hint(target);
57 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Georgios Pinitas28705162018-03-21 20:10:53 +000058
Alex Gilday8913d8d2018-02-15 11:07:18 +000059 // Parse arguments
60 if(argc < 2)
61 {
62 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +010063 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Alex Gilday8913d8d2018-02-15 11:07:18 +000064 std::cout << "No data folder provided: using random values\n\n";
65 }
66 else if(argc == 2)
67 {
Giorgio Arena59631a12018-05-02 13:59:04 +010068 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Alex Gilday8913d8d2018-02-15 11:07:18 +000069 std::cout << "No data folder provided: using random values\n\n";
70 }
71 else if(argc == 3)
72 {
73 data_path = argv[2];
Giorgio Arena59631a12018-05-02 13:59:04 +010074 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
Alex Gilday8913d8d2018-02-15 11:07:18 +000075 std::cout << "No image provided: using random values\n\n";
76 }
77 else if(argc == 4)
78 {
79 data_path = argv[2];
80 image = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +010081 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Alex Gilday8913d8d2018-02-15 11:07:18 +000082 std::cout << "No text file with labels provided: skipping output accessor\n\n";
83 }
Giorgio Arena59631a12018-05-02 13:59:04 +010084 else if(argc == 5)
Alex Gilday8913d8d2018-02-15 11:07:18 +000085 {
86 data_path = argv[2];
87 image = argv[3];
88 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +010089 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
90 std::cout << "No fast math info provided: disabling fast math\n\n";
91 }
92 else
93 {
94 data_path = argv[2];
95 image = argv[3];
96 label = argv[4];
97 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Alex Gilday8913d8d2018-02-15 11:07:18 +000098 }
99
Alex Gilday8913d8d2018-02-15 11:07:18 +0000100 graph << target_hint
Giorgio Arena59631a12018-05-02 13:59:04 +0100101 << fast_math_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000102 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
103 get_input_accessor(image, std::move(preprocessor), false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +0000104 << ConvolutionLayer(
105 7U, 7U, 64U,
106 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy"),
107 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
108 PadStrideInfo(2, 2, 3, 3))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100109 .set_name("conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000110 << BatchNormalizationLayer(
111 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
112 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
113 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
114 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
115 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100116 .set_name("conv1/BatchNorm")
117 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu")
118 << 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 +0000119
120 add_residual_block(data_path, "block1", 64, 3, 2);
121 add_residual_block(data_path, "block2", 128, 4, 2);
122 add_residual_block(data_path, "block3", 256, 6, 2);
123 add_residual_block(data_path, "block4", 512, 3, 1);
124
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100125 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000126 << ConvolutionLayer(
127 1U, 1U, 1000U,
128 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy"),
129 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
130 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100131 .set_name("logits/convolution")
132 << FlattenLayer().set_name("predictions/Reshape")
133 << SoftmaxLayer().set_name("predictions/Softmax")
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000135
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000136 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000137 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100138 config.use_tuner = (target == 2);
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000139 graph.finalize(target_hint, config);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000140 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000141
Alex Gilday8913d8d2018-02-15 11:07:18 +0000142 void do_run() override
143 {
144 // Run graph
145 graph.run();
146 }
147
148private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000149 Stream graph{ 0, "ResNet50" };
Alex Gilday8913d8d2018-02-15 11:07:18 +0000150
151 void add_residual_block(const std::string &data_path, const std::string &name, unsigned int base_depth, unsigned int num_units, unsigned int stride)
152 {
153 for(unsigned int i = 0; i < num_units; ++i)
154 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100155 std::stringstream unit_path_ss;
156 unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
157 std::stringstream unit_name_ss;
158 unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/";
159
160 std::string unit_path = unit_path_ss.str();
161 std::string unit_name = unit_name_ss.str();
Alex Gilday8913d8d2018-02-15 11:07:18 +0000162
163 unsigned int middle_stride = 1;
164
165 if(i == (num_units - 1))
166 {
167 middle_stride = stride;
168 }
169
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 SubStream right(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000171 right << ConvolutionLayer(
172 1U, 1U, base_depth,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100173 get_weights_accessor(data_path, unit_path + "conv1_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000174 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
175 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100176 .set_name(unit_name + "conv1/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000177 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100178 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
179 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
180 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
181 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000182 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100183 .set_name(unit_name + "conv1/BatchNorm")
184 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000185
186 << ConvolutionLayer(
187 3U, 3U, base_depth,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100188 get_weights_accessor(data_path, unit_path + "conv2_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000189 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
190 PadStrideInfo(middle_stride, middle_stride, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100191 .set_name(unit_name + "conv2/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000192 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100193 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
194 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
195 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
196 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000197 0.0000100099996416f)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100198 .set_name(unit_name + "conv2/BatchNorm")
199 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000200
201 << ConvolutionLayer(
202 1U, 1U, base_depth * 4,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100203 get_weights_accessor(data_path, unit_path + "conv3_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000204 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
205 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100206 .set_name(unit_name + "conv3/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000207 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100208 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"),
209 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"),
210 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"),
211 get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"),
212 0.0000100099996416f)
213 .set_name(unit_name + "conv2/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000214
215 if(i == 0)
216 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000217 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000218 left << ConvolutionLayer(
219 1U, 1U, base_depth * 4,
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100220 get_weights_accessor(data_path, unit_path + "shortcut_weights.npy"),
Alex Gilday8913d8d2018-02-15 11:07:18 +0000221 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
222 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100223 .set_name(unit_name + "shortcut/convolution")
Alex Gilday8913d8d2018-02-15 11:07:18 +0000224 << BatchNormalizationLayer(
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100225 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"),
226 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"),
227 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"),
228 get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"),
229 0.0000100099996416f)
230 .set_name(unit_name + "shortcut/BatchNorm");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000231
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100232 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000233 }
234 else if(middle_stride > 1)
235 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000236 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100237 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 +0000238
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100239 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000240 }
241 else
242 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000243 SubStream left(graph);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100244 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000245 }
246
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100247 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
Alex Gilday8913d8d2018-02-15 11:07:18 +0000248 }
249 }
250};
251
252/** Main program for ResNet50
253 *
254 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100255 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Alex Gilday8913d8d2018-02-15 11:07:18 +0000256 */
257int main(int argc, char **argv)
258{
259 return arm_compute::utils::run_example<GraphResNet50Example>(argc, argv);
260}