blob: eb74a1aa11c63f472908700be972c8d02a199b26 [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
35/** Example demonstrating how to implement Microsoft's ResNet50 network using the Compute Library's graph API
36 *
37 * @param[in] argc Number of arguments
38 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
39 */
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
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000055 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010056 Target target_hint = set_target_hint(target);
Alex Gilday8913d8d2018-02-15 11:07:18 +000057
Georgios Pinitas28705162018-03-21 20:10:53 +000058 ConvolutionMethod convolution_hint = (target_hint == Target::CL) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
59
Alex Gilday8913d8d2018-02-15 11:07:18 +000060 // Parse arguments
61 if(argc < 2)
62 {
63 // Print help
64 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
65 std::cout << "No data folder provided: using random values\n\n";
66 }
67 else if(argc == 2)
68 {
69 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
70 std::cout << "No data folder provided: using random values\n\n";
71 }
72 else if(argc == 3)
73 {
74 data_path = argv[2];
75 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
76 std::cout << "No image provided: using random values\n\n";
77 }
78 else if(argc == 4)
79 {
80 data_path = argv[2];
81 image = argv[3];
82 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
83 std::cout << "No text file with labels provided: skipping output accessor\n\n";
84 }
85 else
86 {
87 data_path = argv[2];
88 image = argv[3];
89 label = argv[4];
90 }
91
Alex Gilday8913d8d2018-02-15 11:07:18 +000092 graph << target_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +000093 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
94 get_input_accessor(image, std::move(preprocessor), false /* Do not convert to BGR */))
Alex Gilday8913d8d2018-02-15 11:07:18 +000095 << ConvolutionLayer(
96 7U, 7U, 64U,
97 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy"),
98 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
99 PadStrideInfo(2, 2, 3, 3))
Georgios Pinitas28705162018-03-21 20:10:53 +0000100 << convolution_hint
Alex Gilday8913d8d2018-02-15 11:07:18 +0000101 << BatchNormalizationLayer(
102 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
103 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
104 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
105 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
106 0.0000100099996416f)
107 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
108 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)));
109
110 add_residual_block(data_path, "block1", 64, 3, 2);
111 add_residual_block(data_path, "block2", 128, 4, 2);
112 add_residual_block(data_path, "block3", 256, 6, 2);
113 add_residual_block(data_path, "block4", 512, 3, 1);
114
115 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
116 << ConvolutionLayer(
117 1U, 1U, 1000U,
118 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy"),
119 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
120 PadStrideInfo(1, 1, 0, 0))
121 << FlattenLayer()
122 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000123 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000124
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000125 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000126 GraphConfig config;
127 config.use_function_memory_manager = true;
128 config.use_tuner = (target == 2);
129 graph.finalize(target_hint, config);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000130 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000131
Alex Gilday8913d8d2018-02-15 11:07:18 +0000132 void do_run() override
133 {
134 // Run graph
135 graph.run();
136 }
137
138private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000139 Stream graph{ 0, "ResNet50" };
Alex Gilday8913d8d2018-02-15 11:07:18 +0000140
141 void add_residual_block(const std::string &data_path, const std::string &name, unsigned int base_depth, unsigned int num_units, unsigned int stride)
142 {
143 for(unsigned int i = 0; i < num_units; ++i)
144 {
145 std::stringstream unit;
146 unit << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
147 std::string unit_name = unit.str();
148
149 unsigned int middle_stride = 1;
150
151 if(i == (num_units - 1))
152 {
153 middle_stride = stride;
154 }
155
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000156 SubStream right(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000157 right << ConvolutionLayer(
158 1U, 1U, base_depth,
159 get_weights_accessor(data_path, unit_name + "conv1_weights.npy"),
160 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
161 PadStrideInfo(1, 1, 0, 0))
162 << BatchNormalizationLayer(
163 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_moving_mean.npy"),
164 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_moving_variance.npy"),
165 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_gamma.npy"),
166 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_beta.npy"),
167 0.0000100099996416f)
168 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
169
170 << ConvolutionLayer(
171 3U, 3U, base_depth,
172 get_weights_accessor(data_path, unit_name + "conv2_weights.npy"),
173 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
174 PadStrideInfo(middle_stride, middle_stride, 1, 1))
175 << BatchNormalizationLayer(
176 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_moving_mean.npy"),
177 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_moving_variance.npy"),
178 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_gamma.npy"),
179 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_beta.npy"),
180 0.0000100099996416f)
181 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
182
183 << ConvolutionLayer(
184 1U, 1U, base_depth * 4,
185 get_weights_accessor(data_path, unit_name + "conv3_weights.npy"),
186 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
187 PadStrideInfo(1, 1, 0, 0))
188 << BatchNormalizationLayer(
189 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_moving_mean.npy"),
190 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_moving_variance.npy"),
191 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_gamma.npy"),
192 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_beta.npy"),
193 0.0000100099996416f);
194
195 if(i == 0)
196 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000197 SubStream left(graph);
Alex Gilday8913d8d2018-02-15 11:07:18 +0000198 left << ConvolutionLayer(
199 1U, 1U, base_depth * 4,
200 get_weights_accessor(data_path, unit_name + "shortcut_weights.npy"),
201 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
202 PadStrideInfo(1, 1, 0, 0))
203 << BatchNormalizationLayer(
204 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_moving_mean.npy"),
205 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_moving_variance.npy"),
206 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_gamma.npy"),
207 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_beta.npy"),
208 0.0000100099996416f);
209
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000210 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right));
Alex Gilday8913d8d2018-02-15 11:07:18 +0000211 }
212 else if(middle_stride > 1)
213 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000214 SubStream left(graph);
215 left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true));
Alex Gilday8913d8d2018-02-15 11:07:18 +0000216
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000217 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right));
Alex Gilday8913d8d2018-02-15 11:07:18 +0000218 }
219 else
220 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000221 SubStream left(graph);
222 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right));
Alex Gilday8913d8d2018-02-15 11:07:18 +0000223 }
224
225 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
226 }
227 }
228};
229
230/** Main program for ResNet50
231 *
232 * @param[in] argc Number of arguments
233 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
234 */
235int main(int argc, char **argv)
236{
237 return arm_compute::utils::run_example<GraphResNet50Example>(argc, argv);
238}