2 * Copyright (c) 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.h"
25 #include "support/ToolchainSupport.h"
26 #include "utils/GraphUtils.h"
27 #include "utils/Utils.h"
31 using namespace arm_compute::utils;
32 using namespace arm_compute::graph::frontend;
33 using namespace arm_compute::graph_utils;
35 /** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API
37 * @param[in] argc Number of arguments
38 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] npy_in, [optional] npy_out, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
40 class GraphResNeXt50Example : public Example
43 void do_setup(int argc, char **argv) override
45 std::string data_path; /* Path to the trainable data */
46 std::string npy_in; /* Input npy data */
47 std::string npy_out; /* Output npy data */
49 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
50 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
51 Target target_hint = set_target_hint(target);
52 FastMathHint fast_math_hint = FastMathHint::DISABLED;
58 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [npy_in] [npy_out] [fast_math_hint]\n\n";
59 std::cout << "No data folder provided: using random values\n\n";
63 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [npy_in] [npy_out] [fast_math_hint]\n\n";
64 std::cout << "No data folder provided: using random values\n\n";
69 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [npy_in] [npy_out] [fast_math_hint]\n\n";
70 std::cout << "No input npy file provided: using random values\n\n";
76 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [npy_out] [fast_math_hint]\n\n";
77 std::cout << "No output npy file provided: skipping output accessor\n\n";
84 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
85 std::cout << "No fast math info provided: disabling fast math\n\n";
92 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
97 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
98 get_input_accessor(npy_in))
99 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
100 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
101 .set_name("bn_data/Scale")
104 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy"),
105 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
106 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
107 .set_name("conv0/Convolution")
108 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
109 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
111 add_residual_block(data_path, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
112 add_residual_block(data_path, 512, 2, 4, 2);
113 add_residual_block(data_path, 1024, 3, 6, 2);
114 add_residual_block(data_path, 2048, 4, 3, 2);
116 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1")
117 << FlattenLayer().set_name("predictions/Reshape")
118 << OutputLayer(get_npy_output_accessor(npy_out, TensorShape(2048U), DataType::F32));
122 config.use_tuner = (target == 2);
123 graph.finalize(target_hint, config);
126 void do_run() override
133 Stream graph{ 0, "ResNeXt50" };
135 void add_residual_block(const std::string &data_path, unsigned int base_depth, unsigned int stage, unsigned int num_units, unsigned int stride_conv_unit1)
137 for(unsigned int i = 0; i < num_units; ++i)
139 std::stringstream unit_path_ss;
140 unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_";
141 std::string unit_path = unit_path_ss.str();
143 std::stringstream unit_name_ss;
144 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
145 std::string unit_name = unit_name_ss.str();
147 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
150 pad_grouped_conv = (stage == 1) ? PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 1, 1) : PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 1, 0, 1, DimensionRoundingType::FLOOR);
153 SubStream right(graph);
154 right << ConvolutionLayer(
155 1U, 1U, base_depth / 2,
156 get_weights_accessor(data_path, unit_path + "conv1_weights.npy"),
157 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
158 PadStrideInfo(1, 1, 0, 0))
159 .set_name(unit_name + "conv1/convolution")
160 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
163 3U, 3U, base_depth / 2,
164 get_weights_accessor(data_path, unit_path + "conv2_weights.npy"),
165 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
166 pad_grouped_conv, 32)
167 .set_name(unit_name + "conv2/convolution")
168 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
169 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
170 .set_name(unit_name + "conv1/Scale")
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
175 get_weights_accessor(data_path, unit_path + "conv3_weights.npy"),
176 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
177 PadStrideInfo(1, 1, 0, 0))
178 .set_name(unit_name + "conv3/convolution");
180 SubStream left(graph);
183 left << ConvolutionLayer(
185 get_weights_accessor(data_path, unit_path + "sc_weights.npy"),
186 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
187 PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0))
188 .set_name(unit_name + "sc/convolution")
189 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
190 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
191 .set_name(unit_name + "sc/scale");
194 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
195 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
200 /** Main program for ResNeXt50
202 * @param[in] argc Number of arguments
203 * @param[in] argv Arguments ( [[optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] npy_in, [optional] npy_out )
205 int main(int argc, char **argv)
207 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);