arm_compute v18.05
[platform/upstream/armcl.git] / examples / graph_resnext50.cpp
1 /*
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"
26 #include "utils/GraphUtils.h"
27 #include "utils/Utils.h"
28
29 #include <cstdlib>
30
31 using namespace arm_compute::utils;
32 using namespace arm_compute::graph::frontend;
33 using namespace arm_compute::graph_utils;
34
35 /** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API
36  *
37  * @param[in] argc Number of arguments
38  * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] npy_in, [optional] npy_out, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
39  */
40 class GraphResNeXt50Example : public Example
41 {
42 public:
43     void do_setup(int argc, char **argv) override
44     {
45         std::string data_path; /* Path to the trainable data */
46         std::string npy_in;    /* Input npy data */
47         std::string npy_out;   /* Output npy data */
48
49         // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
50         const int    target         = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
51         Target       target_hint    = set_target_hint(target);
52         FastMathHint fast_math_hint = FastMathHint::DISABLED;
53
54         // Parse arguments
55         if(argc < 2)
56         {
57             // Print help
58             std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [npy_in] [npy_out] [fast_math_hint]\n\n";
59             std::cout << "No data folder provided: using random values\n\n";
60         }
61         else if(argc == 2)
62         {
63             std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [npy_in] [npy_out] [fast_math_hint]\n\n";
64             std::cout << "No data folder provided: using random values\n\n";
65         }
66         else if(argc == 3)
67         {
68             data_path = argv[2];
69             std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [npy_in] [npy_out] [fast_math_hint]\n\n";
70             std::cout << "No input npy file provided: using random values\n\n";
71         }
72         else if(argc == 4)
73         {
74             data_path = argv[2];
75             npy_in    = argv[3];
76             std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [npy_out] [fast_math_hint]\n\n";
77             std::cout << "No output npy file provided: skipping output accessor\n\n";
78         }
79         else if(argc == 5)
80         {
81             data_path = argv[2];
82             npy_in    = argv[3];
83             npy_out   = argv[4];
84             std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
85             std::cout << "No fast math info provided: disabling fast math\n\n";
86         }
87         else
88         {
89             data_path      = argv[2];
90             npy_in         = argv[3];
91             npy_out        = argv[4];
92             fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
93         }
94
95         graph << target_hint
96               << fast_math_hint
97               << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
98                             get_input_accessor(npy_in))
99               << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"),
100                             get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy"))
101               .set_name("bn_data/Scale")
102               << ConvolutionLayer(
103                   7U, 7U, 64U,
104                   get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy"),
105                   get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"),
106                   PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR))
107               .set_name("conv0/Convolution")
108               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu")
109               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0");
110
111         add_residual_block(data_path, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1);
112         add_residual_block(data_path, 512, 2, 4, 2);
113         add_residual_block(data_path, 1024, 3, 6, 2);
114         add_residual_block(data_path, 2048, 4, 3, 2);
115
116         graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1")
117               << FlattenLayer().set_name("predictions/Reshape")
118               << OutputLayer(get_npy_output_accessor(npy_out, TensorShape(2048U), DataType::F32));
119
120         // Finalize graph
121         GraphConfig config;
122         config.use_tuner = (target == 2);
123         graph.finalize(target_hint, config);
124     }
125
126     void do_run() override
127     {
128         // Run graph
129         graph.run();
130     }
131
132 private:
133     Stream graph{ 0, "ResNeXt50" };
134
135     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)
136     {
137         for(unsigned int i = 0; i < num_units; ++i)
138         {
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);
148             if(i == 0)
149             {
150                 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);
151             }
152
153             SubStream right(graph);
154             right << ConvolutionLayer(
155                       1U, 1U, base_depth / 2,
156                       get_weights_accessor(data_path, unit_path + "conv1_weights.npy"),
157                       get_weights_accessor(data_path, unit_path + "conv1_biases.npy"),
158                       PadStrideInfo(1, 1, 0, 0))
159                   .set_name(unit_name + "conv1/convolution")
160                   << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu")
161
162                   << ConvolutionLayer(
163                       3U, 3U, base_depth / 2,
164                       get_weights_accessor(data_path, unit_path + "conv2_weights.npy"),
165                       std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
166                       pad_grouped_conv, 32)
167                   .set_name(unit_name + "conv2/convolution")
168                   << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"),
169                                 get_weights_accessor(data_path, unit_path + "bn2_add.npy"))
170                   .set_name(unit_name + "conv1/Scale")
171                   << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu")
172
173                   << ConvolutionLayer(
174                       1U, 1U, base_depth,
175                       get_weights_accessor(data_path, unit_path + "conv3_weights.npy"),
176                       get_weights_accessor(data_path, unit_path + "conv3_biases.npy"),
177                       PadStrideInfo(1, 1, 0, 0))
178                   .set_name(unit_name + "conv3/convolution");
179
180             SubStream left(graph);
181             if(i == 0)
182             {
183                 left << ConvolutionLayer(
184                          1U, 1U, base_depth,
185                          get_weights_accessor(data_path, unit_path + "sc_weights.npy"),
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")
189                      << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"),
190                                    get_weights_accessor(data_path, unit_path + "sc_bn_add.npy"))
191                      .set_name(unit_name + "sc/scale");
192             }
193
194             graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add");
195             graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu");
196         }
197     }
198 };
199
200 /** Main program for ResNeXt50
201  *
202  * @param[in] argc Number of arguments
203  * @param[in] argv Arguments ( [[optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] npy_in, [optional] npy_out )
204  */
205 int main(int argc, char **argv)
206 {
207     return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv);
208 }