2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph/Graph.h"
25 #include "arm_compute/graph/Nodes.h"
26 #include "support/ToolchainSupport.h"
27 #include "utils/GraphUtils.h"
28 #include "utils/Utils.h"
32 using namespace arm_compute::utils;
33 using namespace arm_compute::graph;
34 using namespace arm_compute::graph_utils;
36 /** Example demonstrating how to implement Microsoft's ResNet50 network using the Compute Library's graph API
38 * @param[in] argc Number of arguments
39 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
41 class GraphResNet50Example : public Example
44 void do_setup(int argc, char **argv) override
46 std::string data_path; /* Path to the trainable data */
47 std::string image; /* Image data */
48 std::string label; /* Label data */
50 // Create a preprocessor object
51 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
52 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb,
53 false /* Do not convert to BGR */);
55 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
56 const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
57 TargetHint target_hint = set_target_hint(int_target_hint);
63 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
64 std::cout << "No data folder provided: using random values\n\n";
68 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
69 std::cout << "No data folder provided: using random values\n\n";
74 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
75 std::cout << "No image provided: using random values\n\n";
81 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
82 std::cout << "No text file with labels provided: skipping output accessor\n\n";
92 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
93 get_input_accessor(image, std::move(preprocessor), false /* Do not convert to BGR */))
96 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy"),
97 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
98 PadStrideInfo(2, 2, 3, 3))
99 << BatchNormalizationLayer(
100 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"),
101 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"),
102 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"),
103 get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"),
105 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
106 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)));
108 add_residual_block(data_path, "block1", 64, 3, 2);
109 add_residual_block(data_path, "block2", 128, 4, 2);
110 add_residual_block(data_path, "block3", 256, 6, 2);
111 add_residual_block(data_path, "block4", 512, 3, 1);
113 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
116 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy"),
117 get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"),
118 PadStrideInfo(1, 1, 0, 0))
121 << Tensor(get_output_accessor(label, 5));
123 // In order to enable the OpenCL tuner, graph_init() has to be called only when all nodes have been instantiated
124 graph.graph_init(int_target_hint == 2);
126 void do_run() override
135 void add_residual_block(const std::string &data_path, const std::string &name, unsigned int base_depth, unsigned int num_units, unsigned int stride)
137 for(unsigned int i = 0; i < num_units; ++i)
139 std::stringstream unit;
140 unit << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_";
141 std::string unit_name = unit.str();
143 unsigned int middle_stride = 1;
145 if(i == (num_units - 1))
147 middle_stride = stride;
151 right << ConvolutionLayer(
153 get_weights_accessor(data_path, unit_name + "conv1_weights.npy"),
154 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
155 PadStrideInfo(1, 1, 0, 0))
156 << BatchNormalizationLayer(
157 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_moving_mean.npy"),
158 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_moving_variance.npy"),
159 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_gamma.npy"),
160 get_weights_accessor(data_path, unit_name + "conv1_BatchNorm_beta.npy"),
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
166 get_weights_accessor(data_path, unit_name + "conv2_weights.npy"),
167 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
168 PadStrideInfo(middle_stride, middle_stride, 1, 1))
169 << BatchNormalizationLayer(
170 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_moving_mean.npy"),
171 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_moving_variance.npy"),
172 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_gamma.npy"),
173 get_weights_accessor(data_path, unit_name + "conv2_BatchNorm_beta.npy"),
175 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
178 1U, 1U, base_depth * 4,
179 get_weights_accessor(data_path, unit_name + "conv3_weights.npy"),
180 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
181 PadStrideInfo(1, 1, 0, 0))
182 << BatchNormalizationLayer(
183 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_moving_mean.npy"),
184 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_moving_variance.npy"),
185 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_gamma.npy"),
186 get_weights_accessor(data_path, unit_name + "conv3_BatchNorm_beta.npy"),
187 0.0000100099996416f);
192 left << ConvolutionLayer(
193 1U, 1U, base_depth * 4,
194 get_weights_accessor(data_path, unit_name + "shortcut_weights.npy"),
195 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
196 PadStrideInfo(1, 1, 0, 0))
197 << BatchNormalizationLayer(
198 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_moving_mean.npy"),
199 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_moving_variance.npy"),
200 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_gamma.npy"),
201 get_weights_accessor(data_path, unit_name + "shortcut_BatchNorm_beta.npy"),
202 0.0000100099996416f);
204 graph << ResidualLayer(std::move(left), std::move(right));
206 else if(middle_stride > 1)
209 left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true))
210 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, 1.f, 0.f));
212 graph << ResidualLayer(std::move(left), std::move(right));
216 graph << ResidualLayer(std::move(right));
219 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
224 /** Main program for ResNet50
226 * @param[in] argc Number of arguments
227 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
229 int main(int argc, char **argv)
231 return arm_compute::utils::run_example<GraphResNet50Example>(argc, argv);