blob: 4e505a05e54d5d677f72a0dee5b942943b5155bd [file] [log] [blame]
Jenkinsb3a371b2018-05-23 11:36:53 +01001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2018-2019 ARM Limited.
Jenkinsb3a371b2018-05-23 11:36:53 +01003 *
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 */
24#include "arm_compute/graph.h"
25#include "support/ToolchainSupport.h"
Jenkins52ba29e2018-08-29 15:32:11 +000026#include "utils/CommonGraphOptions.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Jenkinsb3a371b2018-05-23 11:36:53 +010030using namespace arm_compute::utils;
31using namespace arm_compute::graph::frontend;
32using namespace arm_compute::graph_utils;
33
Jenkinsb9abeae2018-11-22 11:58:08 +000034/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API */
Jenkinsb3a371b2018-05-23 11:36:53 +010035class GraphResNeXt50Example : public Example
36{
37public:
Jenkins52ba29e2018-08-29 15:32:11 +000038 GraphResNeXt50Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNeXt50")
Jenkinsb3a371b2018-05-23 11:36:53 +010040 {
Jenkins52ba29e2018-08-29 15:32:11 +000041 }
42 bool do_setup(int argc, char **argv) override
43 {
Jenkinsb3a371b2018-05-23 11:36:53 +010044 // Parse arguments
Jenkins52ba29e2018-08-29 15:32:11 +000045 cmd_parser.parse(argc, argv);
Jenkins0e205f72019-11-28 16:53:35 +000046 cmd_parser.validate();
Jenkins52ba29e2018-08-29 15:32:11 +000047
48 // Consume common parameters
49 common_params = consume_common_graph_parameters(common_opts);
50
51 // Return when help menu is requested
52 if(common_params.help)
Jenkinsb3a371b2018-05-23 11:36:53 +010053 {
Jenkins52ba29e2018-08-29 15:32:11 +000054 cmd_parser.print_help(argv[0]);
55 return false;
Jenkinsb3a371b2018-05-23 11:36:53 +010056 }
57
Jenkins52ba29e2018-08-29 15:32:11 +000058 // Checks
59 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Jenkins52ba29e2018-08-29 15:32:11 +000060
61 // Print parameter values
62 std::cout << common_params << std::endl;
63
64 // Get trainable parameters data path
65 std::string data_path = common_params.data_path;
66
67 // Create input descriptor
68 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
69 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
70
71 // Set weights trained layout
72 const DataLayout weights_layout = DataLayout::NCHW;
73
74 graph << common_params.target
75 << common_params.fast_math_hint
76 << InputLayer(input_descriptor, get_input_accessor(common_params))
Jenkinsb3a371b2018-05-23 11:36:53 +010077 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
78 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
79 .set_name("bn_data/Scale")
80 << ConvolutionLayer(
81 7U, 7U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +000082 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy", weights_layout),
Jenkinsb3a371b2018-05-23 11:36:53 +010083 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
84 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
85 .set_name("conv0/Convolution")
86 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
87 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
88
Jenkins52ba29e2018-08-29 15:32:11 +000089 add_residual_block(data_path, weights_layout, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
90 add_residual_block(data_path, weights_layout, 512, 2, 4, 2);
91 add_residual_block(data_path, weights_layout, 1024, 3, 6, 2);
92 add_residual_block(data_path, weights_layout, 2048, 4, 3, 2);
Jenkinsb3a371b2018-05-23 11:36:53 +010093
94 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1")
95 << FlattenLayer().set_name("predictions/Reshape")
Jenkins52ba29e2018-08-29 15:32:11 +000096 << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32));
Jenkinsb3a371b2018-05-23 11:36:53 +010097
98 // Finalize graph
99 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000100 config.num_threads = common_params.threads;
101 config.use_tuner = common_params.enable_tuner;
Jenkins4ba87db2019-05-23 17:11:51 +0100102 config.tuner_mode = common_params.tuner_mode;
Jenkins52ba29e2018-08-29 15:32:11 +0000103 config.tuner_file = common_params.tuner_file;
104
105 graph.finalize(common_params.target, config);
106
107 return true;
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 }
109
110 void do_run() override
111 {
112 // Run graph
113 graph.run();
114 }
115
116private:
Jenkins52ba29e2018-08-29 15:32:11 +0000117 CommandLineParser cmd_parser;
118 CommonGraphOptions common_opts;
119 CommonGraphParams common_params;
120 Stream graph;
Jenkinsb3a371b2018-05-23 11:36:53 +0100121
Jenkins52ba29e2018-08-29 15:32:11 +0000122 void add_residual_block(const std::string &data_path, DataLayout weights_layout,
123 unsigned int base_depth, unsigned int stage, unsigned int num_units, unsigned int stride_conv_unit1)
Jenkinsb3a371b2018-05-23 11:36:53 +0100124 {
125 for(unsigned int i = 0; i < num_units; ++i)
126 {
127 std::stringstream unit_path_ss;
128 unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_";
129 std::string unit_path = unit_path_ss.str();
130
131 std::stringstream unit_name_ss;
132 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
133 std::string unit_name = unit_name_ss.str();
134
135 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
136 if(i == 0)
137 {
138 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);
139 }
140
141 SubStream right(graph);
142 right << ConvolutionLayer(
143 1U, 1U, base_depth / 2,
Jenkins52ba29e2018-08-29 15:32:11 +0000144 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Jenkinsb3a371b2018-05-23 11:36:53 +0100145 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
146 PadStrideInfo(1, 1, 0, 0))
147 .set_name(unit_name + "conv1/convolution")
148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
149
150 << ConvolutionLayer(
151 3U, 3U, base_depth / 2,
Jenkins52ba29e2018-08-29 15:32:11 +0000152 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Jenkinsb3a371b2018-05-23 11:36:53 +0100153 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
154 pad_grouped_conv, 32)
155 .set_name(unit_name + "conv2/convolution")
156 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
157 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
158 .set_name(unit_name + "conv1/Scale")
159 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
160
161 << ConvolutionLayer(
162 1U, 1U, base_depth,
Jenkins52ba29e2018-08-29 15:32:11 +0000163 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
165 PadStrideInfo(1, 1, 0, 0))
166 .set_name(unit_name + "conv3/convolution");
167
168 SubStream left(graph);
169 if(i == 0)
170 {
171 left << ConvolutionLayer(
172 1U, 1U, base_depth,
Jenkins52ba29e2018-08-29 15:32:11 +0000173 get_weights_accessor(data_path, unit_path + "sc_weights.npy", weights_layout),
Jenkinsb3a371b2018-05-23 11:36:53 +0100174 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
175 PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0))
176 .set_name(unit_name + "sc/convolution")
177 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
178 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
179 .set_name(unit_name + "sc/scale");
180 }
181
Jenkinsb9abeae2018-11-22 11:58:08 +0000182 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Jenkinsb3a371b2018-05-23 11:36:53 +0100183 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
184 }
185 }
186};
187
188/** Main program for ResNeXt50
189 *
Jenkinsb9abeae2018-11-22 11:58:08 +0000190 * Model is based on:
191 * https://arxiv.org/abs/1611.05431
192 * "Aggregated Residual Transformations for Deep Neural Networks"
193 * Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He
194 *
Jenkins52ba29e2018-08-29 15:32:11 +0000195 * @note To list all the possible arguments execute the binary appended with the --help option
196 *
Jenkinsb3a371b2018-05-23 11:36:53 +0100197 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000198 * @param[in] argv Arguments
Jenkinsb3a371b2018-05-23 11:36:53 +0100199 */
200int main(int argc, char **argv)
201{
202 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
203}