blob: 0fce3a1b0451c9233ce30b96780dc41664653a49 [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
Georgios Pinitas6ed43b52018-07-12 17:34:22 +010062 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "Unsupported data type!");
63 ARM_COMPUTE_EXIT_ON_MSG(common_params.data_layout == DataLayout::NHWC, "Unsupported data layout!");
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
71 graph << common_params.target
72 << common_params.fast_math_hint
73 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), common_params.data_type),
74 get_input_accessor(common_params))
Isabella Gottardi88d5b222018-04-06 12:24:55 +010075 << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
76 get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
77 .set_name("bn_data/Scale")
78 << ConvolutionLayer(
79 7U, 7U, 64U,
80 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy"),
81 get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
82 PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
83 .set_name("conv0/Convolution")
84 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
85 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
86
87 add_residual_block(data_path, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
88 add_residual_block(data_path, 512, 2, 4, 2);
89 add_residual_block(data_path, 1024, 3, 6, 2);
90 add_residual_block(data_path, 2048, 4, 3, 2);
91
92 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1")
93 << FlattenLayer().set_name("predictions/Reshape")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010094 << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32));
Isabella Gottardi88d5b222018-04-06 12:24:55 +010095
96 // Finalize graph
97 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010098 config.num_threads = common_params.threads;
99 config.use_tuner = common_params.enable_tuner;
100 graph.finalize(common_params.target, config);
101
102 return true;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100103 }
104
105 void do_run() override
106 {
107 // Run graph
108 graph.run();
109 }
110
111private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100112 CommandLineParser cmd_parser;
113 CommonGraphOptions common_opts;
114 CommonGraphParams common_params;
115 Stream graph;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100116
117 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)
118 {
119 for(unsigned int i = 0; i < num_units; ++i)
120 {
121 std::stringstream unit_path_ss;
122 unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_";
123 std::string unit_path = unit_path_ss.str();
124
125 std::stringstream unit_name_ss;
126 unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/";
127 std::string unit_name = unit_name_ss.str();
128
129 PadStrideInfo pad_grouped_conv(1, 1, 1, 1);
130 if(i == 0)
131 {
132 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);
133 }
134
135 SubStream right(graph);
136 right << ConvolutionLayer(
137 1U, 1U, base_depth / 2,
138 get_weights_accessor(data_path, unit_path + "conv1_weights.npy"),
139 get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
140 PadStrideInfo(1, 1, 0, 0))
141 .set_name(unit_name + "conv1/convolution")
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
143
144 << ConvolutionLayer(
145 3U, 3U, base_depth / 2,
146 get_weights_accessor(data_path, unit_path + "conv2_weights.npy"),
147 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
148 pad_grouped_conv, 32)
149 .set_name(unit_name + "conv2/convolution")
150 << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
151 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
152 .set_name(unit_name + "conv1/Scale")
153 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
154
155 << ConvolutionLayer(
156 1U, 1U, base_depth,
157 get_weights_accessor(data_path, unit_path + "conv3_weights.npy"),
158 get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
159 PadStrideInfo(1, 1, 0, 0))
160 .set_name(unit_name + "conv3/convolution");
161
162 SubStream left(graph);
163 if(i == 0)
164 {
165 left << ConvolutionLayer(
166 1U, 1U, base_depth,
167 get_weights_accessor(data_path, unit_path + "sc_weights.npy"),
168 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
169 PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0))
170 .set_name(unit_name + "sc/convolution")
171 << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
172 get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
173 .set_name(unit_name + "sc/scale");
174 }
175
176 graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
177 graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
178 }
179 }
180};
181
182/** Main program for ResNeXt50
183 *
184 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100185 * @param[in] argv Arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100186 */
187int main(int argc, char **argv)
188{
189 return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
190}