Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 1 | /* |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/CommonGraphOptions.h" |
| 28 | #include "utils/GraphUtils.h" |
| 29 | #include "utils/Utils.h" |
| 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 SRCNN 9-5-5 network using the Compute Library's graph API */ |
| 36 | class GraphSRCNN955Example : public Example |
| 37 | { |
| 38 | public: |
| 39 | GraphSRCNN955Example() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | : 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 Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 46 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | GraphSRCNN955Example(const GraphSRCNN955Example &) = delete; |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 55 | GraphSRCNN955Example &operator=(const GraphSRCNN955Example &) = delete; |
Matthew Bentham | f5f2391 | 2020-03-05 22:32:16 +0000 | [diff] [blame] | 56 | ~GraphSRCNN955Example() override = default; |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 57 | bool do_setup(int argc, char **argv) override |
| 58 | { |
| 59 | // Parse arguments |
| 60 | cmd_parser.parse(argc, argv); |
Georgios Pinitas | cd60a5f | 2019-08-21 17:06:54 +0100 | [diff] [blame] | 61 | cmd_parser.validate(); |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 62 | |
| 63 | // Consume common parameters |
| 64 | common_params = consume_common_graph_parameters(common_opts); |
| 65 | |
| 66 | // Return when help menu is requested |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | if (common_params.help) |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 68 | { |
| 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 Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 82 | // 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 87 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(); |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 88 | |
| 89 | // Create input descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | 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 Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 95 | |
| 96 | // Set weights trained layout |
| 97 | const DataLayout weights_layout = DataLayout::NCHW; |
| 98 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 117 | << OutputLayer(std::make_unique<DummyAccessor>(0)); |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 118 | |
| 119 | // Finalize graph |
| 120 | GraphConfig config; |
SiCongLi | f466d75 | 2021-03-01 15:26:18 +0000 | [diff] [blame] | 121 | 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 Giorgio | 1df9cca | 2019-01-17 13:20:32 +0000 | [diff] [blame] | 128 | |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 129 | 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 | |
| 140 | private: |
| 141 | CommandLineParser cmd_parser; |
| 142 | CommonGraphOptions common_opts; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | SimpleOption<unsigned int> *model_input_width{nullptr}; |
| 144 | SimpleOption<unsigned int> *model_input_height{nullptr}; |
Georgios Pinitas | 8fe103c | 2018-12-04 14:26:31 +0000 | [diff] [blame] | 145 | 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 | */ |
| 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | return arm_compute::utils::run_example<GraphSRCNN955Example>(argc, argv); |
| 164 | } |