blob: 15a8b5d8ec6ff040445d3426ea7aafe03185fa59 [file] [log] [blame]
Georgios Pinitas8fe103c2018-12-04 14:26:31 +00001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitas8fe103c2018-12-04 14:26:31 +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 Pinitas8fe103c2018-12-04 14:26:31 +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 SRCNN 9-5-5 network using the Compute Library's graph API */
36class GraphSRCNN955Example : public Example
37{
38public:
39 GraphSRCNN955Example()
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, "SRCNN955")
Georgios Pinitas8fe103c2018-12-04 14:26:31 +000046 {
47 model_input_width = cmd_parser.add_option<SimpleOption<unsigned int>>("image-width", 300);
48 model_input_height = cmd_parser.add_option<SimpleOption<unsigned int>>("image-height", 300);
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 GraphSRCNN955Example(const GraphSRCNN955Example &) = delete;
Georgios Pinitas8fe103c2018-12-04 14:26:31 +000055 GraphSRCNN955Example &operator=(const GraphSRCNN955Example &) = delete;
Matthew Benthamf5f23912020-03-05 22:32:16 +000056 ~GraphSRCNN955Example() override = default;
Georgios Pinitas8fe103c2018-12-04 14:26:31 +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 Pinitas8fe103c2018-12-04 14:26:31 +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 Pinitas8fe103c2018-12-04 14:26:31 +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 // Print parameter values
78 std::cout << common_params << std::endl;
79 std::cout << "Image width: " << image_width << std::endl;
80 std::cout << "Image height: " << image_height << std::endl;
81
Georgios Pinitas8fe103c2018-12-04 14:26:31 +000082 // Get trainable parameters data path
83 const std::string data_path = common_params.data_path;
84 const std::string model_path = "/cnn_data/srcnn955_model/";
85
86 // Create a preprocessor object
Georgios Pinitas40f51a62020-11-21 03:04:18 +000087 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>();
Georgios Pinitas8fe103c2018-12-04 14:26:31 +000088
89 // Create input descriptor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 const TensorShape tensor_shape =
91 permute_shape(TensorShape(image_width, image_height, 3U, common_params.batches), DataLayout::NCHW,
92 common_params.data_layout);
93 TensorDescriptor input_descriptor =
94 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Georgios Pinitas8fe103c2018-12-04 14:26:31 +000095
96 // Set weights trained layout
97 const DataLayout weights_layout = DataLayout::NCHW;
98
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 graph << common_params.target << common_params.fast_math_hint
100 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor),
101 false /* Do not convert to BGR */))
102 << ConvolutionLayer(9U, 9U, 64U, get_weights_accessor(data_path, "conv1_weights.npy", weights_layout),
103 get_weights_accessor(data_path, "conv1_biases.npy"), PadStrideInfo(1, 1, 4, 4))
104 .set_name("conv1/convolution")
105 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
106 .set_name("conv1/Relu")
107 << ConvolutionLayer(5U, 5U, 32U, get_weights_accessor(data_path, "conv2_weights.npy", weights_layout),
108 get_weights_accessor(data_path, "conv2_biases.npy"), PadStrideInfo(1, 1, 2, 2))
109 .set_name("conv2/convolution")
110 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
111 .set_name("conv2/Relu")
112 << ConvolutionLayer(5U, 5U, 3U, get_weights_accessor(data_path, "conv3_weights.npy", weights_layout),
113 get_weights_accessor(data_path, "conv3_biases.npy"), PadStrideInfo(1, 1, 2, 2))
114 .set_name("conv3/convolution")
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
116 .set_name("conv3/Relu")
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000117 << OutputLayer(std::make_unique<DummyAccessor>(0));
Georgios Pinitas8fe103c2018-12-04 14:26:31 +0000118
119 // Finalize graph
120 GraphConfig config;
SiCongLif466d752021-03-01 15:26:18 +0000121 config.num_threads = common_params.threads;
122 config.use_tuner = common_params.enable_tuner;
123 config.tuner_mode = common_params.tuner_mode;
124 config.tuner_file = common_params.tuner_file;
125 config.mlgo_file = common_params.mlgo_file;
126 config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type);
127 config.synthetic_type = common_params.data_type;
Michele Di Giorgio1df9cca2019-01-17 13:20:32 +0000128
Georgios Pinitas8fe103c2018-12-04 14:26:31 +0000129 graph.finalize(common_params.target, config);
130
131 return true;
132 }
133
134 void do_run() override
135 {
136 // Run graph
137 graph.run();
138 }
139
140private:
141 CommandLineParser cmd_parser;
142 CommonGraphOptions common_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 SimpleOption<unsigned int> *model_input_width{nullptr};
144 SimpleOption<unsigned int> *model_input_height{nullptr};
Georgios Pinitas8fe103c2018-12-04 14:26:31 +0000145 CommonGraphParams common_params;
146 Stream graph;
147};
148
149/** Main program for SRCNN 9-5-5
150 *
151 * Model is based on:
152 * http://mmlab.ie.cuhk.edu.hk/projects/SRCNN.html
153 * "Image Super-Resolution Using Deep Convolutional Networks"
154 * Chao Dong, Chen Change Loy, Kaiming He, Xiaoou Tang
155 *
156 * @note To list all the possible arguments execute the binary appended with the --help option
157 *
158 * @param[in] argc Number of arguments
159 * @param[in] argv Arguments
160 */
161int main(int argc, char **argv)
162{
163 return arm_compute::utils::run_example<GraphSRCNN955Example>(argc, argv);
164}