blob: 80db826be531b7fc708573cb6e0b62af5575ef65 [file] [log] [blame]
Georgios Pinitasf554be72018-12-03 16:02:47 +00001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasf554be72018-12-03 16:02:47 +00003 *
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
Georgios Pinitasf554be72018-12-03 16:02:47 +000026#include "support/ToolchainSupport.h"
27#include "utils/CommonGraphOptions.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31using namespace arm_compute::utils;
32using namespace arm_compute::graph::frontend;
33using namespace arm_compute::graph_utils;
34
35/** Example demonstrating how to implement ResNet12 network using the Compute Library's graph API */
36class GraphResNet12Example : public Example
37{
38public:
39 GraphResNet12Example()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 : cmd_parser(),
41 common_opts(cmd_parser),
42 model_input_width(nullptr),
43 model_input_height(nullptr),
44 common_params(),
45 graph(0, "ResNet12")
Georgios Pinitasf554be72018-12-03 16:02:47 +000046 {
47 model_input_width = cmd_parser.add_option<SimpleOption<unsigned int>>("image-width", 192);
48 model_input_height = cmd_parser.add_option<SimpleOption<unsigned int>>("image-height", 128);
49
50 // Add model id option
51 model_input_width->set_help("Input image width.");
52 model_input_height->set_help("Input image height.");
53 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 GraphResNet12Example(const GraphResNet12Example &) = delete;
Georgios Pinitasf554be72018-12-03 16:02:47 +000055 GraphResNet12Example &operator=(const GraphResNet12Example &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000056 ~GraphResNet12Example() override = default;
Georgios Pinitasf554be72018-12-03 16:02:47 +000057 bool do_setup(int argc, char **argv) override
58 {
59 // Parse arguments
60 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010061 cmd_parser.validate();
Georgios Pinitasf554be72018-12-03 16:02:47 +000062
63 // Consume common parameters
64 common_params = consume_common_graph_parameters(common_opts);
65
66 // Return when help menu is requested
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (common_params.help)
Georgios Pinitasf554be72018-12-03 16:02:47 +000068 {
69 cmd_parser.print_help(argv[0]);
70 return false;
71 }
72
73 // Get input image width and height
74 const unsigned int image_width = model_input_width->value();
75 const unsigned int image_height = model_input_height->value();
76
77 // Checks
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type),
79 "QASYMM8 not supported for this graph");
Georgios Pinitasf554be72018-12-03 16:02:47 +000080
81 // Print parameter values
82 std::cout << common_params << std::endl;
83 std::cout << "Image width: " << image_width << std::endl;
84 std::cout << "Image height: " << image_height << std::endl;
85
86 // Get trainable parameters data path
87 const std::string data_path = common_params.data_path;
88 const std::string model_path = "/cnn_data/resnet12_model/";
89
90 // Create a preprocessor object
Georgios Pinitas40f51a62020-11-21 03:04:18 +000091 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>();
Georgios Pinitasf554be72018-12-03 16:02:47 +000092
93 // Create input descriptor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 const TensorShape tensor_shape =
95 permute_shape(TensorShape(image_width, image_height, 3U, common_params.batches), DataLayout::NCHW,
96 common_params.data_layout);
97 TensorDescriptor input_descriptor =
98 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Georgios Pinitasf554be72018-12-03 16:02:47 +000099
100 // Set weights trained layout
101 const DataLayout weights_layout = DataLayout::NCHW;
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 graph << common_params.target << common_params.fast_math_hint
104 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor),
105 false /* Do not convert to BGR */))
106 << ConvolutionLayer(9U, 9U, 64U, get_weights_accessor(data_path, "conv1_weights.npy", weights_layout),
107 get_weights_accessor(data_path, "conv1_biases.npy", weights_layout),
108 PadStrideInfo(1, 1, 4, 4))
109 .set_name("conv1/convolution")
110 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
111 .set_name("conv1/Relu");
Georgios Pinitasf554be72018-12-03 16:02:47 +0000112
113 add_residual_block(data_path, "block1", weights_layout);
114 add_residual_block(data_path, "block2", weights_layout);
115 add_residual_block(data_path, "block3", weights_layout);
116 add_residual_block(data_path, "block4", weights_layout);
117
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 graph << ConvolutionLayer(3U, 3U, 64U, get_weights_accessor(data_path, "conv10_weights.npy", weights_layout),
119 get_weights_accessor(data_path, "conv10_biases.npy"), PadStrideInfo(1, 1, 1, 1))
120 .set_name("conv10/convolution")
121 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
122 .set_name("conv10/Relu")
123 << ConvolutionLayer(3U, 3U, 64U, get_weights_accessor(data_path, "conv11_weights.npy", weights_layout),
124 get_weights_accessor(data_path, "conv11_biases.npy"), PadStrideInfo(1, 1, 1, 1))
125 .set_name("conv11/convolution")
126 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
127 .set_name("conv11/Relu")
128 << ConvolutionLayer(9U, 9U, 3U, get_weights_accessor(data_path, "conv12_weights.npy", weights_layout),
129 get_weights_accessor(data_path, "conv12_biases.npy"), PadStrideInfo(1, 1, 4, 4))
130 .set_name("conv12/convolution")
131 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH))
132 .set_name("conv12/Tanh")
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR, 0.58f, 0.5f))
134 .set_name("conv12/Linear")
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000135 << OutputLayer(std::make_unique<DummyAccessor>(0));
Georgios Pinitasf554be72018-12-03 16:02:47 +0000136
137 // Finalize graph
138 GraphConfig config;
139 config.num_threads = common_params.threads;
140 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100141 config.tuner_mode = common_params.tuner_mode;
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +0000142 config.tuner_file = common_params.tuner_file;
SiCong Li4841c972021-02-03 12:17:35 +0000143 config.mlgo_file = common_params.mlgo_file;
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +0000144
Georgios Pinitasf554be72018-12-03 16:02:47 +0000145 graph.finalize(common_params.target, config);
146
147 return true;
148 }
149
150 void do_run() override
151 {
152 // Run graph
153 graph.run();
154 }
155
156private:
157 CommandLineParser cmd_parser;
158 CommonGraphOptions common_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 SimpleOption<unsigned int> *model_input_width{nullptr};
160 SimpleOption<unsigned int> *model_input_height{nullptr};
Georgios Pinitasf554be72018-12-03 16:02:47 +0000161 CommonGraphParams common_params;
162 Stream graph;
163
164 void add_residual_block(const std::string &data_path, const std::string &name, DataLayout weights_layout)
165 {
166 std::stringstream unit_path_ss;
167 unit_path_ss << data_path << name << "_";
168 std::stringstream unit_name_ss;
169 unit_name_ss << name << "/";
170
171 std::string unit_path = unit_path_ss.str();
172 std::string unit_name = unit_name_ss.str();
173
174 SubStream left(graph);
175 SubStream right(graph);
176
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 right << ConvolutionLayer(3U, 3U, 64U,
178 get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout),
179 get_weights_accessor(data_path, unit_path + "conv1_biases.npy", weights_layout),
180 PadStrideInfo(1, 1, 1, 1))
181 .set_name(unit_name + "conv1/convolution")
Georgios Pinitasf554be72018-12-03 16:02:47 +0000182 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"),
184 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"),
185 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"),
186 get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"), 0.0000100099996416f)
187 .set_name(unit_name + "conv1/BatchNorm")
188 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
189 .set_name(unit_name + "conv1/Relu")
Georgios Pinitasf554be72018-12-03 16:02:47 +0000190
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 << ConvolutionLayer(3U, 3U, 64U,
192 get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout),
193 get_weights_accessor(data_path, unit_path + "conv2_biases.npy", weights_layout),
194 PadStrideInfo(1, 1, 1, 1))
195 .set_name(unit_name + "conv2/convolution")
Georgios Pinitasf554be72018-12-03 16:02:47 +0000196 << BatchNormalizationLayer(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"),
198 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"),
199 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"),
200 get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"), 0.0000100099996416f)
201 .set_name(unit_name + "conv2/BatchNorm")
202 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
203 .set_name(unit_name + "conv2/Relu");
Georgios Pinitasf554be72018-12-03 16:02:47 +0000204
205 graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add");
206 }
207};
208
209/** Main program for ResNet12
210 *
211 * Model is based on:
212 * https://arxiv.org/pdf/1709.01118.pdf
213 * "WESPE: Weakly Supervised Photo Enhancer for Digital Cameras"
214 * Andrey Ignatov, Nikolay Kobyshev, Kenneth Vanhoey, Radu Timofte, Luc Van Gool
215 *
216 * @note To list all the possible arguments execute the binary appended with the --help option
217 *
218 * @param[in] argc Number of arguments
219 * @param[in] argv Arguments
220 */
221int main(int argc, char **argv)
222{
223 return arm_compute::utils::run_example<GraphResNet12Example>(argc, argv);
224}