blob: 6378f6c74188a398342ffd4e714741b7ca5eaa28 [file] [log] [blame]
Isabella Gottardi88d5b222018-04-06 12:24:55 +01001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2018-2021 Arm Limited.
Isabella Gottardi88d5b222018-04-06 12:24:55 +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"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010026#include "utils/CommonGraphOptions.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Isabella Gottardi88d5b222018-04-06 12:24:55 +010030using namespace arm_compute::utils;
31using namespace arm_compute::graph::frontend;
32using namespace arm_compute::graph_utils;
33
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010034/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API */
Isabella Gottardi88d5b222018-04-06 12:24:55 +010035class GraphResNeXt50Example : public Example
36{
37public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010038 GraphResNeXt50Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNeXt50")
Isabella Gottardi88d5b222018-04-06 12:24:55 +010040 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010041 }
42 bool do_setup(int argc, char **argv) override
43 {
Isabella Gottardi88d5b222018-04-06 12:24:55 +010044 // Parse arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010045 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010046 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010047
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)
Isabella Gottardi88d5b222018-04-06 12:24:55 +010053 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010054 cmd_parser.print_help(argv[0]);
55 return false;
Isabella Gottardi88d5b222018-04-06 12:24:55 +010056 }
57
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010059 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010060
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
Georgios Pinitase2220552018-07-20 13:23:44 +010067 // Create input descriptor
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000068 const auto operation_layout = common_params.data_layout;
Georgios Pinitas450dfb12021-06-15 10:11:47 +010069 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000070 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
Georgios Pinitase2220552018-07-20 13:23:44 +010071
72 // Set weights trained layout
73 const DataLayout weights_layout = DataLayout::NCHW;
74
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010075 graph << common_params.target
76 << common_params.fast_math_hint
Georgios Pinitase2220552018-07-20 13:23:44 +010077 << InputLayer(input_descriptor, get_input_accessor(common_params))
Isabella Gottardi88d5b222018-04-06 12:24:55 +010078 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
79 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
80 .set_name("bn_data/Scale")
81 << ConvolutionLayer(
82 7U, 7U, 64U,
Georgios Pinitase2220552018-07-20 13:23:44 +010083 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +010084 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
85 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
86 .set_name("conv0/Convolution")
87 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000088 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
Isabella Gottardi88d5b222018-04-06 12:24:55 +010089
Georgios Pinitase2220552018-07-20 13:23:44 +010090 add_residual_block(data_path, weights_layout, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
91 add_residual_block(data_path, weights_layout, 512, 2, 4, 2);
92 add_residual_block(data_path, weights_layout, 1024, 3, 6, 2);
93 add_residual_block(data_path, weights_layout, 2048, 4, 3, 2);
Isabella Gottardi88d5b222018-04-06 12:24:55 +010094
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000095 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("pool1")
Isabella Gottardi88d5b222018-04-06 12:24:55 +010096 << FlattenLayer().set_name("predictions/Reshape")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010097 << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32));
Isabella Gottardi88d5b222018-04-06 12:24:55 +010098
99 // Finalize graph
100 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100101 config.num_threads = common_params.threads;
102 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100103 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100104 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000105 config.mlgo_file = common_params.mlgo_file;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100106
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100107 graph.finalize(common_params.target, config);
108
109 return true;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100110 }
111
112 void do_run() override
113 {
114 // Run graph
115 graph.run();
116 }
117
118private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100119 CommandLineParser cmd_parser;
120 CommonGraphOptions common_opts;
121 CommonGraphParams common_params;
122 Stream graph;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100123
Georgios Pinitase2220552018-07-20 13:23:44 +0100124 void add_residual_block(const std::string &data_path, DataLayout weights_layout,
125 unsigned int base_depth, unsigned int stage, unsigned int num_units, unsigned int stride_conv_unit1)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100126 {
127 for(unsigned int i = 0; i < num_units; ++i)
128 {
129 std::stringstream unit_path_ss;
130 unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_";
131 std::string unit_path = unit_path_ss.str();
132
133 std::stringstream unit_name_ss;
134 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
135 std::string unit_name = unit_name_ss.str();
136
137 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
138 if(i == 0)
139 {
140 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);
141 }
142
143 SubStream right(graph);
144 right << ConvolutionLayer(
145 1U, 1U, base_depth / 2,
Georgios Pinitase2220552018-07-20 13:23:44 +0100146 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100147 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
148 PadStrideInfo(1, 1, 0, 0))
149 .set_name(unit_name + "conv1/convolution")
150 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
151
152 << ConvolutionLayer(
153 3U, 3U, base_depth / 2,
Georgios Pinitase2220552018-07-20 13:23:44 +0100154 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100155 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
156 pad_grouped_conv, 32)
157 .set_name(unit_name + "conv2/convolution")
158 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
159 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
160 .set_name(unit_name + "conv1/Scale")
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
162
163 << ConvolutionLayer(
164 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100165 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100166 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
167 PadStrideInfo(1, 1, 0, 0))
168 .set_name(unit_name + "conv3/convolution");
169
170 SubStream left(graph);
171 if(i == 0)
172 {
173 left << ConvolutionLayer(
174 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100175 get_weights_accessor(data_path, unit_path + "sc_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100176 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
177 PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0))
178 .set_name(unit_name + "sc/convolution")
179 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
180 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
181 .set_name(unit_name + "sc/scale");
182 }
183
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100184 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100185 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
186 }
187 }
188};
189
190/** Main program for ResNeXt50
191 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000192 * Model is based on:
193 * https://arxiv.org/abs/1611.05431
194 * "Aggregated Residual Transformations for Deep Neural Networks"
ramelg01b2eba7f2021-12-23 08:32:08 +0000195 * Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He.
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000196 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100197 * @note To list all the possible arguments execute the binary appended with the --help option
198 *
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100199 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100200 * @param[in] argv Arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100201 */
202int main(int argc, char **argv)
203{
204 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
205}