Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +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 | f554be7 | 2018-12-03 16:02:47 +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 | f554be7 | 2018-12-03 16:02:47 +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 ResNet12 network using the Compute Library's graph API */ |
| 36 | class GraphResNet12Example : public Example |
| 37 | { |
| 38 | public: |
| 39 | GraphResNet12Example() |
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, "ResNet12") |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 46 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | GraphResNet12Example(const GraphResNet12Example &) = delete; |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 55 | GraphResNet12Example &operator=(const GraphResNet12Example &) = delete; |
Matthew Bentham | f5f2391 | 2020-03-05 22:32:16 +0000 | [diff] [blame] | 56 | ~GraphResNet12Example() override = default; |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +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 | f554be7 | 2018-12-03 16:02:47 +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 | f554be7 | 2018-12-03 16:02:47 +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 | // Checks |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), |
| 79 | "QASYMM8 not supported for this graph"); |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 80 | |
| 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 91 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(); |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 92 | |
| 93 | // Create input descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 99 | |
| 100 | // Set weights trained layout |
| 101 | const DataLayout weights_layout = DataLayout::NCHW; |
| 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 112 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 135 | << OutputLayer(std::make_unique<DummyAccessor>(0)); |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 136 | |
| 137 | // Finalize graph |
| 138 | GraphConfig config; |
| 139 | config.num_threads = common_params.threads; |
| 140 | config.use_tuner = common_params.enable_tuner; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 141 | config.tuner_mode = common_params.tuner_mode; |
Michele Di Giorgio | 1df9cca | 2019-01-17 13:20:32 +0000 | [diff] [blame] | 142 | config.tuner_file = common_params.tuner_file; |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 143 | config.mlgo_file = common_params.mlgo_file; |
Michele Di Giorgio | 1df9cca | 2019-01-17 13:20:32 +0000 | [diff] [blame] | 144 | |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 145 | 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 | |
| 156 | private: |
| 157 | CommandLineParser cmd_parser; |
| 158 | CommonGraphOptions common_opts; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | SimpleOption<unsigned int> *model_input_width{nullptr}; |
| 160 | SimpleOption<unsigned int> *model_input_height{nullptr}; |
Georgios Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 161 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 177 | 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 182 | << BatchNormalizationLayer( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 183 | 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 190 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 191 | << 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 196 | << BatchNormalizationLayer( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 197 | 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 Pinitas | f554be7 | 2018-12-03 16:02:47 +0000 | [diff] [blame] | 204 | |
| 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 | */ |
| 221 | int main(int argc, char **argv) |
| 222 | { |
| 223 | return arm_compute::utils::run_example<GraphResNet12Example>(argc, argv); |
| 224 | } |