blob: 12a1507c4c222c5b6cfff4c6ede595af33d1c7c8 [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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Isabella Gottardi88d5b222018-04-06 12:24:55 +010026#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010027#include "utils/CommonGraphOptions.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010028#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
Isabella Gottardi88d5b222018-04-06 12:24:55 +010031using namespace arm_compute::utils;
32using namespace arm_compute::graph::frontend;
33using namespace arm_compute::graph_utils;
34
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010035/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API */
Isabella Gottardi88d5b222018-04-06 12:24:55 +010036class GraphResNeXt50Example : public Example
37{
38public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 GraphResNeXt50Example() : 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type),
60 "QASYMM8 not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010061
62 // Print parameter values
63 std::cout << common_params << std::endl;
64
65 // Get trainable parameters data path
66 std::string data_path = common_params.data_path;
67
Georgios Pinitase2220552018-07-20 13:23:44 +010068 // Create input descriptor
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000069 const auto operation_layout = common_params.data_layout;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 const TensorShape tensor_shape =
71 permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
72 TensorDescriptor input_descriptor =
73 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
Georgios Pinitase2220552018-07-20 13:23:44 +010074
75 // Set weights trained layout
76 const DataLayout weights_layout = DataLayout::NCHW;
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 graph << common_params.target << common_params.fast_math_hint
Georgios Pinitase2220552018-07-20 13:23:44 +010079 << InputLayer(input_descriptor, get_input_accessor(common_params))
Isabella Gottardi88d5b222018-04-06 12:24:55 +010080 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
81 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 .set_name("bn_data/Scale")
Isabella Gottardi88d5b222018-04-06 12:24:55 +010083 << ConvolutionLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084 7U, 7U, 64U,
85 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy", weights_layout),
86 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
87 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
88 .set_name("conv0/Convolution")
89 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
90 .set_name("conv0/Relu")
91 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout,
92 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)))
93 .set_name("pool0");
Isabella Gottardi88d5b222018-04-06 12:24:55 +010094
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 add_residual_block(data_path, weights_layout, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3,
96 /*stride_conv_unit1*/ 1);
Georgios Pinitase2220552018-07-20 13:23:44 +010097 add_residual_block(data_path, weights_layout, 512, 2, 4, 2);
98 add_residual_block(data_path, weights_layout, 1024, 3, 6, 2);
99 add_residual_block(data_path, weights_layout, 2048, 4, 3, 2);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100100
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000101 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("pool1")
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100102 << FlattenLayer().set_name("predictions/Reshape")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100103 << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100104
105 // Finalize graph
106 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100107 config.num_threads = common_params.threads;
108 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100109 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100110 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000111 config.mlgo_file = common_params.mlgo_file;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100112
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100113 graph.finalize(common_params.target, config);
114
115 return true;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100116 }
117
118 void do_run() override
119 {
120 // Run graph
121 graph.run();
122 }
123
124private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100125 CommandLineParser cmd_parser;
126 CommonGraphOptions common_opts;
127 CommonGraphParams common_params;
128 Stream graph;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100129
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 void add_residual_block(const std::string &data_path,
131 DataLayout weights_layout,
132 unsigned int base_depth,
133 unsigned int stage,
134 unsigned int num_units,
135 unsigned int stride_conv_unit1)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100136 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 for (unsigned int i = 0; i < num_units; ++i)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100138 {
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();
142
143 std::stringstream unit_name_ss;
144 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
145 std::string unit_name = unit_name_ss.str();
146
147 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (i == 0)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100149 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 pad_grouped_conv = (stage == 1) ? PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 1, 1)
151 : PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 1, 0, 1,
152 DimensionRoundingType::FLOOR);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100153 }
154
155 SubStream right(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 right << ConvolutionLayer(1U, 1U, base_depth / 2,
157 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
158 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
159 PadStrideInfo(1, 1, 0, 0))
160 .set_name(unit_name + "conv1/convolution")
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
162 .set_name(unit_name + "conv1/Relu")
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100163
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 << ConvolutionLayer(3U, 3U, base_depth / 2,
165 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
166 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), pad_grouped_conv,
167 32)
168 .set_name(unit_name + "conv2/convolution")
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100169 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
170 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 .set_name(unit_name + "conv1/Scale")
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
173 .set_name(unit_name + "conv2/Relu")
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100174
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 << ConvolutionLayer(1U, 1U, base_depth,
176 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
177 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
178 PadStrideInfo(1, 1, 0, 0))
179 .set_name(unit_name + "conv3/convolution");
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100180
181 SubStream left(graph);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182 if (i == 0)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100183 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 left << ConvolutionLayer(1U, 1U, base_depth,
185 get_weights_accessor(data_path, unit_path + "sc_weights.npy", weights_layout),
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")
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100189 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
190 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 .set_name(unit_name + "sc/scale");
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100192 }
193
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100194 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
196 .set_name(unit_name + "Relu");
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100197 }
198 }
199};
200
201/** Main program for ResNeXt50
202 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000203 * Model is based on:
204 * https://arxiv.org/abs/1611.05431
205 * "Aggregated Residual Transformations for Deep Neural Networks"
ramelg01b2eba7f2021-12-23 08:32:08 +0000206 * Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He.
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000207 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100208 * @note To list all the possible arguments execute the binary appended with the --help option
209 *
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100210 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100211 * @param[in] argv Arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100212 */
213int main(int argc, char **argv)
214{
215 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
216}