Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [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 | 7b2f026 | 2018-08-14 16:40:18 +0100 | [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 | |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 35 | /** Example demonstrating how to implement ResNetV2_50 network using the Compute Library's graph API */ |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 36 | class GraphResNetV2_50Example : public Example |
| 37 | { |
| 38 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | GraphResNetV2_50Example() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNetV2_50") |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | bool do_setup(int argc, char **argv) override |
| 43 | { |
| 44 | // Parse arguments |
| 45 | cmd_parser.parse(argc, argv); |
Georgios Pinitas | cd60a5f | 2019-08-21 17:06:54 +0100 | [diff] [blame] | 46 | cmd_parser.validate(); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 47 | |
| 48 | // Consume common parameters |
| 49 | common_params = consume_common_graph_parameters(common_opts); |
| 50 | |
| 51 | // Return when help menu is requested |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | if (common_params.help) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 53 | { |
| 54 | cmd_parser.print_help(argv[0]); |
| 55 | return false; |
| 56 | } |
| 57 | |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 58 | // Print parameter values |
| 59 | std::cout << common_params << std::endl; |
| 60 | |
| 61 | // Get trainable parameters data path |
| 62 | std::string data_path = common_params.data_path; |
| 63 | std::string model_path = "/cnn_data/resnet_v2_50_model/"; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | if (!data_path.empty()) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 65 | { |
| 66 | data_path += model_path; |
| 67 | } |
| 68 | |
| 69 | // Create a preprocessor object |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 70 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 71 | |
| 72 | // Create input descriptor |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 73 | const auto operation_layout = common_params.data_layout; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | const TensorShape tensor_shape = |
| 75 | permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout); |
| 76 | TensorDescriptor input_descriptor = |
| 77 | TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 78 | |
| 79 | // Set weights trained layout |
| 80 | const DataLayout weights_layout = DataLayout::NCHW; |
| 81 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | graph << common_params.target << common_params.fast_math_hint |
| 83 | << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), |
| 84 | false /* Do not convert to BGR */)) |
| 85 | << ConvolutionLayer(7U, 7U, 64U, get_weights_accessor(data_path, "conv1_weights.npy", weights_layout), |
| 86 | get_weights_accessor(data_path, "conv1_biases.npy", weights_layout), |
| 87 | PadStrideInfo(2, 2, 3, 3)) |
| 88 | .set_name("conv1/convolution") |
| 89 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout, |
| 90 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))) |
| 91 | .set_name("pool1/MaxPool"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 92 | |
| 93 | add_residual_block(data_path, "block1", weights_layout, 64, 3, 2); |
| 94 | add_residual_block(data_path, "block2", weights_layout, 128, 4, 2); |
| 95 | add_residual_block(data_path, "block3", weights_layout, 256, 6, 2); |
| 96 | add_residual_block(data_path, "block4", weights_layout, 512, 3, 1); |
| 97 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | graph << BatchNormalizationLayer(get_weights_accessor(data_path, "postnorm_moving_mean.npy"), |
| 99 | get_weights_accessor(data_path, "postnorm_moving_variance.npy"), |
| 100 | get_weights_accessor(data_path, "postnorm_gamma.npy"), |
| 101 | get_weights_accessor(data_path, "postnorm_beta.npy"), 0.000009999999747378752f) |
| 102 | .set_name("postnorm/BatchNorm") |
| 103 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 104 | .set_name("postnorm/Relu") |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 105 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("pool5") |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 106 | << ConvolutionLayer(1U, 1U, 1001U, get_weights_accessor(data_path, "logits_weights.npy", weights_layout), |
| 107 | get_weights_accessor(data_path, "logits_biases.npy"), PadStrideInfo(1, 1, 0, 0)) |
| 108 | .set_name("logits/convolution") |
| 109 | << FlattenLayer().set_name("predictions/Reshape") << SoftmaxLayer().set_name("predictions/Softmax") |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 110 | << OutputLayer(get_output_accessor(common_params, 5)); |
| 111 | |
| 112 | // Finalize graph |
| 113 | GraphConfig config; |
SiCongLi | f466d75 | 2021-03-01 15:26:18 +0000 | [diff] [blame] | 114 | config.num_threads = common_params.threads; |
| 115 | config.use_tuner = common_params.enable_tuner; |
| 116 | config.tuner_mode = common_params.tuner_mode; |
| 117 | config.tuner_file = common_params.tuner_file; |
| 118 | config.mlgo_file = common_params.mlgo_file; |
| 119 | config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type); |
| 120 | config.synthetic_type = common_params.data_type; |
Michele Di Giorgio | 1df9cca | 2019-01-17 13:20:32 +0000 | [diff] [blame] | 121 | |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 122 | graph.finalize(common_params.target, config); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void do_run() override |
| 128 | { |
| 129 | // Run graph |
| 130 | graph.run(); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | CommandLineParser cmd_parser; |
| 135 | CommonGraphOptions common_opts; |
| 136 | CommonGraphParams common_params; |
| 137 | Stream graph; |
| 138 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | void add_residual_block(const std::string &data_path, |
| 140 | const std::string &name, |
| 141 | DataLayout weights_layout, |
| 142 | unsigned int base_depth, |
| 143 | unsigned int num_units, |
| 144 | unsigned int stride) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 145 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 146 | for (unsigned int i = 0; i < num_units; ++i) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 147 | { |
| 148 | // Generate unit names |
| 149 | std::stringstream unit_path_ss; |
| 150 | unit_path_ss << name << "_unit_" << (i + 1) << "_bottleneck_v2_"; |
| 151 | std::stringstream unit_name_ss; |
| 152 | unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v2/"; |
| 153 | |
| 154 | std::string unit_path = unit_path_ss.str(); |
| 155 | std::string unit_name = unit_name_ss.str(); |
| 156 | |
| 157 | const TensorShape last_shape = graph.graph().node(graph.tail_node())->output(0)->desc().shape; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | unsigned int depth_in = last_shape[arm_compute::get_data_layout_dimension_index( |
| 159 | common_params.data_layout, DataLayoutDimension::CHANNEL)]; |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 160 | unsigned int depth_out = base_depth * 4; |
| 161 | |
| 162 | // All units have stride 1 apart from last one |
| 163 | unsigned int middle_stride = (i == (num_units - 1)) ? stride : 1; |
| 164 | |
| 165 | // Preact |
| 166 | SubStream preact(graph); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 167 | preact << BatchNormalizationLayer(get_weights_accessor(data_path, unit_path + "preact_moving_mean.npy"), |
| 168 | get_weights_accessor(data_path, unit_path + "preact_moving_variance.npy"), |
| 169 | get_weights_accessor(data_path, unit_path + "preact_gamma.npy"), |
| 170 | get_weights_accessor(data_path, unit_path + "preact_beta.npy"), |
| 171 | 0.000009999999747378752f) |
| 172 | .set_name(unit_name + "preact/BatchNorm") |
| 173 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 174 | .set_name(unit_name + "preact/Relu"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 175 | |
| 176 | // Create bottleneck path |
| 177 | SubStream shortcut(graph); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | if (depth_in == depth_out) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 179 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 180 | if (middle_stride != 1) |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 181 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | shortcut << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, common_params.data_layout, |
| 183 | PadStrideInfo(middle_stride, middle_stride, 0, 0), true)) |
| 184 | .set_name(unit_name + "shortcut/MaxPool"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | shortcut.forward_tail(preact.tail_node()); |
| 190 | shortcut << ConvolutionLayer( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 191 | 1U, 1U, depth_out, |
| 192 | get_weights_accessor(data_path, unit_path + "shortcut_weights.npy", weights_layout), |
| 193 | get_weights_accessor(data_path, unit_path + "shortcut_biases.npy", weights_layout), |
| 194 | PadStrideInfo(1, 1, 0, 0)) |
| 195 | .set_name(unit_name + "shortcut/convolution"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Create residual path |
| 199 | SubStream residual(preact); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 200 | residual |
| 201 | << ConvolutionLayer(1U, 1U, base_depth, |
| 202 | get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout), |
| 203 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 204 | PadStrideInfo(1, 1, 0, 0)) |
| 205 | .set_name(unit_name + "conv1/convolution") |
| 206 | << BatchNormalizationLayer( |
| 207 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"), |
| 208 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"), |
| 209 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"), |
| 210 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"), |
| 211 | 0.000009999999747378752f) |
| 212 | .set_name(unit_name + "conv1/BatchNorm") |
| 213 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 214 | .set_name(unit_name + "conv1/Relu") |
| 215 | << ConvolutionLayer(3U, 3U, base_depth, |
| 216 | get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout), |
| 217 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 218 | PadStrideInfo(middle_stride, middle_stride, 1, 1)) |
| 219 | .set_name(unit_name + "conv2/convolution") |
| 220 | << BatchNormalizationLayer( |
| 221 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"), |
| 222 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"), |
| 223 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"), |
| 224 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"), |
| 225 | 0.000009999999747378752f) |
| 226 | .set_name(unit_name + "conv2/BatchNorm") |
| 227 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 228 | .set_name(unit_name + "conv1/Relu") |
| 229 | << ConvolutionLayer(1U, 1U, depth_out, |
| 230 | get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout), |
| 231 | get_weights_accessor(data_path, unit_path + "conv3_biases.npy", weights_layout), |
| 232 | PadStrideInfo(1, 1, 0, 0)) |
| 233 | .set_name(unit_name + "conv3/convolution"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 234 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 235 | graph << EltwiseLayer(std::move(shortcut), std::move(residual), EltwiseOperation::Add) |
| 236 | .set_name(unit_name + "add"); |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | /** Main program for ResNetV2_50 |
| 242 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 243 | * Model is based on: |
| 244 | * https://arxiv.org/abs/1603.05027 |
| 245 | * "Identity Mappings in Deep Residual Networks" |
| 246 | * Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun |
| 247 | * |
Georgios Pinitas | 588ebc5 | 2018-12-21 13:39:07 +0000 | [diff] [blame] | 248 | * Provenance: download.tensorflow.org/models/resnet_v2_50_2017_04_14.tar.gz |
| 249 | * |
Georgios Pinitas | 7b2f026 | 2018-08-14 16:40:18 +0100 | [diff] [blame] | 250 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 251 | * |
| 252 | * @param[in] argc Number of arguments |
| 253 | * @param[in] argv Arguments |
| 254 | */ |
| 255 | int main(int argc, char **argv) |
| 256 | { |
| 257 | return arm_compute::utils::run_example<GraphResNetV2_50Example>(argc, argv); |
| 258 | } |