blob: c369fa91bdea36376b1bad026ab6b6d5891d99fc [file] [log] [blame]
Isabella Gottardi88d5b222018-04-06 12:24:55 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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
34/** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API
35 *
36 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010037 * @param[in] argv Arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +010038 */
39class GraphResNeXt50Example : public Example
40{
41public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 GraphResNeXt50Example()
43 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNeXt50")
Isabella Gottardi88d5b222018-04-06 12:24:55 +010044 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010045 }
46 bool do_setup(int argc, char **argv) override
47 {
Isabella Gottardi88d5b222018-04-06 12:24:55 +010048 // Parse arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010049 cmd_parser.parse(argc, argv);
50
51 // Consume common parameters
52 common_params = consume_common_graph_parameters(common_opts);
53
54 // Return when help menu is requested
55 if(common_params.help)
Isabella Gottardi88d5b222018-04-06 12:24:55 +010056 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010057 cmd_parser.print_help(argv[0]);
58 return false;
Isabella Gottardi88d5b222018-04-06 12:24:55 +010059 }
60
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010061 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010062 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
63 ARM_COMPUTE_EXIT_ON_MSG(common_params.data_type == DataType::F16 && common_params.target == Target::NEON, "F16 NEON not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010064
65 // Print parameter values
66 std::cout << common_params << std::endl;
67
68 // Get trainable parameters data path
69 std::string data_path = common_params.data_path;
70
Georgios Pinitase2220552018-07-20 13:23:44 +010071 // Create input descriptor
72 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
73 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
74
75 // Set weights trained layout
76 const DataLayout weights_layout = DataLayout::NCHW;
77
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010078 graph << common_params.target
79 << common_params.fast_math_hint
Georgios Pinitase2220552018-07-20 13:23:44 +010080 << InputLayer(input_descriptor, get_input_accessor(common_params))
Isabella Gottardi88d5b222018-04-06 12:24:55 +010081 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
82 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
83 .set_name("bn_data/Scale")
84 << ConvolutionLayer(
85 7U, 7U, 64U,
Georgios Pinitase2220552018-07-20 13:23:44 +010086 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +010087 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
88 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
89 .set_name("conv0/Convolution")
90 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
91 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
92
Georgios Pinitase2220552018-07-20 13:23:44 +010093 add_residual_block(data_path, weights_layout, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
94 add_residual_block(data_path, weights_layout, 512, 2, 4, 2);
95 add_residual_block(data_path, weights_layout, 1024, 3, 6, 2);
96 add_residual_block(data_path, weights_layout, 2048, 4, 3, 2);
Isabella Gottardi88d5b222018-04-06 12:24:55 +010097
98 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1")
99 << FlattenLayer().set_name("predictions/Reshape")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100100 << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100101
102 // Finalize graph
103 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100104 config.num_threads = common_params.threads;
105 config.use_tuner = common_params.enable_tuner;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100106 config.tuner_file = common_params.tuner_file;
107
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100108 graph.finalize(common_params.target, config);
109
110 return true;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100111 }
112
113 void do_run() override
114 {
115 // Run graph
116 graph.run();
117 }
118
119private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100120 CommandLineParser cmd_parser;
121 CommonGraphOptions common_opts;
122 CommonGraphParams common_params;
123 Stream graph;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100124
Georgios Pinitase2220552018-07-20 13:23:44 +0100125 void add_residual_block(const std::string &data_path, DataLayout weights_layout,
126 unsigned int base_depth, unsigned int stage, unsigned int num_units, unsigned int stride_conv_unit1)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100127 {
128 for(unsigned int i = 0; i < num_units; ++i)
129 {
130 std::stringstream unit_path_ss;
131 unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_";
132 std::string unit_path = unit_path_ss.str();
133
134 std::stringstream unit_name_ss;
135 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
136 std::string unit_name = unit_name_ss.str();
137
138 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
139 if(i == 0)
140 {
141 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);
142 }
143
144 SubStream right(graph);
145 right << ConvolutionLayer(
146 1U, 1U, base_depth / 2,
Georgios Pinitase2220552018-07-20 13:23:44 +0100147 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100148 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
149 PadStrideInfo(1, 1, 0, 0))
150 .set_name(unit_name + "conv1/convolution")
151 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
152
153 << ConvolutionLayer(
154 3U, 3U, base_depth / 2,
Georgios Pinitase2220552018-07-20 13:23:44 +0100155 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100156 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
157 pad_grouped_conv, 32)
158 .set_name(unit_name + "conv2/convolution")
159 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
160 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
161 .set_name(unit_name + "conv1/Scale")
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
163
164 << ConvolutionLayer(
165 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100166 get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100167 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
168 PadStrideInfo(1, 1, 0, 0))
169 .set_name(unit_name + "conv3/convolution");
170
171 SubStream left(graph);
172 if(i == 0)
173 {
174 left << ConvolutionLayer(
175 1U, 1U, base_depth,
Georgios Pinitase2220552018-07-20 13:23:44 +0100176 get_weights_accessor(data_path, unit_path + "sc_weights.npy", weights_layout),
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100177 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
178 PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0))
179 .set_name(unit_name + "sc/convolution")
180 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
181 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
182 .set_name(unit_name + "sc/scale");
183 }
184
Georgios Pinitas427bbbf2018-08-28 13:32:02 +0100185 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100186 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
187 }
188 }
189};
190
191/** Main program for ResNeXt50
192 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100193 * @note To list all the possible arguments execute the binary appended with the --help option
194 *
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100195 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100196 * @param[in] argv Arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100197 */
198int main(int argc, char **argv)
199{
200 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
201}