Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +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 | 766b70c | 2018-08-13 17:50:34 +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 | 766b70c | 2018-08-13 17:50:34 +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; |
| 32 | using namespace arm_compute::utils; |
| 33 | using namespace arm_compute::graph::frontend; |
| 34 | using namespace arm_compute::graph_utils; |
| 35 | |
| 36 | /** Example demonstrating how to implement MobileNetV2's network using the Compute Library's graph API */ |
| 37 | class GraphMobilenetV2Example : public Example |
| 38 | { |
| 39 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | GraphMobilenetV2Example() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV2") |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 41 | { |
| 42 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | GraphMobilenetV2Example(const GraphMobilenetV2Example &) = delete; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 44 | GraphMobilenetV2Example &operator=(const GraphMobilenetV2Example &) = delete; |
Matthew Bentham | f5f2391 | 2020-03-05 22:32:16 +0000 | [diff] [blame] | 45 | ~GraphMobilenetV2Example() override = default; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 46 | |
| 47 | bool do_setup(int argc, char **argv) override |
| 48 | { |
| 49 | // Parse arguments |
| 50 | cmd_parser.parse(argc, argv); |
Georgios Pinitas | cd60a5f | 2019-08-21 17:06:54 +0100 | [diff] [blame] | 51 | cmd_parser.validate(); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 52 | |
| 53 | // Consume common parameters |
| 54 | common_params = consume_common_graph_parameters(common_opts); |
| 55 | |
| 56 | // Return when help menu is requested |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (common_params.help) |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 58 | { |
| 59 | cmd_parser.print_help(argv[0]); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Print parameter values |
| 64 | std::cout << common_params << std::endl; |
| 65 | |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 66 | // Create input descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), |
| 68 | DataLayout::NCHW, common_params.data_layout); |
| 69 | TensorDescriptor input_descriptor = |
| 70 | TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 71 | |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 72 | // Set graph hints |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | graph << common_params.target << common_params.fast_math_hint; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 74 | |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 75 | // Create core graph |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | if (arm_compute::is_data_type_float(common_params.data_type)) |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 77 | { |
| 78 | create_graph_float(input_descriptor); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | create_graph_qasymm8(input_descriptor); |
| 83 | } |
| 84 | // Create common tail |
| 85 | graph << ReshapeLayer(TensorShape(1001U)).set_name("Predictions/Reshape") |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | << SoftmaxLayer().set_name("Predictions/Softmax") << OutputLayer(get_output_accessor(common_params, 5)); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 87 | |
| 88 | // Finalize graph |
| 89 | GraphConfig config; |
| 90 | config.num_threads = common_params.threads; |
| 91 | config.use_tuner = common_params.enable_tuner; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 92 | config.tuner_mode = common_params.tuner_mode; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 93 | config.tuner_file = common_params.tuner_file; |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 94 | config.mlgo_file = common_params.mlgo_file; |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 95 | |
| 96 | graph.finalize(common_params.target, config); |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void do_run() override |
| 102 | { |
| 103 | // Run graph |
| 104 | graph.run(); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | CommandLineParser cmd_parser; |
| 109 | CommonGraphOptions common_opts; |
| 110 | CommonGraphParams common_params; |
| 111 | Stream graph; |
| 112 | |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 113 | private: |
| 114 | enum class IsResidual |
| 115 | { |
| 116 | Yes, |
| 117 | No |
| 118 | }; |
| 119 | |
| 120 | enum class HasExpand |
| 121 | { |
| 122 | Yes, |
| 123 | No |
| 124 | }; |
| 125 | |
| 126 | private: |
| 127 | void create_graph_float(TensorDescriptor &input_descriptor) |
| 128 | { |
| 129 | // Create model path |
| 130 | const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_model/"; |
| 131 | |
| 132 | // Create a preprocessor object |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 133 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 134 | |
| 135 | // Get trainable parameters data path |
| 136 | std::string data_path = common_params.data_path; |
| 137 | |
| 138 | // Add model path to data path |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | if (!data_path.empty()) |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 140 | { |
| 141 | data_path += model_path; |
| 142 | } |
| 143 | |
| 144 | graph << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "Conv_weights.npy", DataLayout::NCHW), |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 146 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 147 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 148 | .set_name("Conv") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 149 | << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_BatchNorm_moving_mean.npy"), |
| 150 | get_weights_accessor(data_path, "Conv_BatchNorm_moving_variance.npy"), |
| 151 | get_weights_accessor(data_path, "Conv_BatchNorm_gamma.npy"), |
| 152 | get_weights_accessor(data_path, "Conv_BatchNorm_beta.npy"), |
| 153 | 0.0010000000474974513f) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | .set_name("Conv/BatchNorm") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 155 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | .set_name("Conv/Relu6"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 157 | |
| 158 | get_expanded_conv_float(data_path, "expanded_conv", 32U, 16U, PadStrideInfo(1, 1, 1, 1)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | get_expanded_conv_float(data_path, "expanded_conv_1", 16U, 24U, |
| 160 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes); |
| 161 | get_expanded_conv_float(data_path, "expanded_conv_2", 24U, 24U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 162 | IsResidual::Yes); |
| 163 | get_expanded_conv_float(data_path, "expanded_conv_3", 24U, 32U, |
| 164 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes); |
| 165 | get_expanded_conv_float(data_path, "expanded_conv_4", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 166 | IsResidual::Yes); |
| 167 | get_expanded_conv_float(data_path, "expanded_conv_5", 32U, 32U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 168 | IsResidual::Yes); |
| 169 | get_expanded_conv_float(data_path, "expanded_conv_6", 32U, 64U, |
| 170 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes); |
| 171 | get_expanded_conv_float(data_path, "expanded_conv_7", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 172 | IsResidual::Yes); |
| 173 | get_expanded_conv_float(data_path, "expanded_conv_8", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 174 | IsResidual::Yes); |
| 175 | get_expanded_conv_float(data_path, "expanded_conv_9", 64U, 64U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 176 | IsResidual::Yes); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 177 | get_expanded_conv_float(data_path, "expanded_conv_10", 64U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | get_expanded_conv_float(data_path, "expanded_conv_11", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 179 | IsResidual::Yes); |
| 180 | get_expanded_conv_float(data_path, "expanded_conv_12", 96U, 96U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 181 | IsResidual::Yes); |
| 182 | get_expanded_conv_float(data_path, "expanded_conv_13", 96U, 160U, |
| 183 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), HasExpand::Yes); |
| 184 | get_expanded_conv_float(data_path, "expanded_conv_14", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 185 | IsResidual::Yes); |
| 186 | get_expanded_conv_float(data_path, "expanded_conv_15", 160U, 160U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes, |
| 187 | IsResidual::Yes); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 188 | get_expanded_conv_float(data_path, "expanded_conv_16", 160U, 320U, PadStrideInfo(1, 1, 1, 1), HasExpand::Yes); |
| 189 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 190 | graph << ConvolutionLayer( |
| 191 | 1U, 1U, 1280U, get_weights_accessor(data_path, "Conv_1_weights.npy", DataLayout::NCHW), |
| 192 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0)) |
| 193 | .set_name("Conv_1") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 194 | << BatchNormalizationLayer(get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_mean.npy"), |
| 195 | get_weights_accessor(data_path, "Conv_1_BatchNorm_moving_variance.npy"), |
| 196 | get_weights_accessor(data_path, "Conv_1_BatchNorm_gamma.npy"), |
| 197 | get_weights_accessor(data_path, "Conv_1_BatchNorm_beta.npy"), |
| 198 | 0.0010000000474974513f) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 199 | .set_name("Conv_1/BatchNorm") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 200 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 201 | .set_name("Conv_1/Relu6") |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 202 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 203 | << ConvolutionLayer(1U, 1U, 1001U, |
| 204 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW), |
| 205 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), |
| 206 | PadStrideInfo(1, 1, 0, 0)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 207 | .set_name("Logits/Conv2d_1c_1x1"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 210 | void get_expanded_conv_float(const std::string &data_path, |
| 211 | std::string &¶m_path, |
| 212 | unsigned int input_channels, |
| 213 | unsigned int output_channels, |
| 214 | PadStrideInfo dwc_pad_stride_info, |
| 215 | HasExpand has_expand = HasExpand::No, |
| 216 | IsResidual is_residual = IsResidual::No, |
| 217 | unsigned int expansion_size = 6) |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 218 | { |
| 219 | std::string total_path = param_path + "_"; |
| 220 | SubStream left(graph); |
| 221 | |
| 222 | // Add expand node |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 223 | if (has_expand == HasExpand::Yes) |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 224 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 225 | left << ConvolutionLayer( |
| 226 | 1U, 1U, input_channels * expansion_size, |
| 227 | get_weights_accessor(data_path, total_path + "expand_weights.npy", DataLayout::NCHW), |
| 228 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0)) |
| 229 | .set_name(param_path + "/expand/Conv2D") |
| 230 | << BatchNormalizationLayer( |
| 231 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_mean.npy"), |
| 232 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_moving_variance.npy"), |
| 233 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_gamma.npy"), |
| 234 | get_weights_accessor(data_path, total_path + "expand_BatchNorm_beta.npy"), |
| 235 | 0.0010000000474974513f) |
| 236 | .set_name(param_path + "/expand/BatchNorm") |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 237 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 238 | .set_name(param_path + "/expand/Relu6"); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Add depthwise node |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 242 | left << DepthwiseConvolutionLayer( |
| 243 | 3U, 3U, |
| 244 | get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW), |
| 245 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), dwc_pad_stride_info) |
| 246 | .set_name(param_path + "/depthwise/depthwise") |
| 247 | << BatchNormalizationLayer( |
| 248 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"), |
| 249 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"), |
| 250 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"), |
| 251 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), |
| 252 | 0.0010000000474974513f) |
| 253 | .set_name(param_path + "/depthwise/BatchNorm") |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 254 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 255 | .set_name(param_path + "/depthwise/Relu6"); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 256 | |
| 257 | // Add project node |
| 258 | left << ConvolutionLayer(1U, 1U, output_channels, |
| 259 | get_weights_accessor(data_path, total_path + "project_weights.npy", DataLayout::NCHW), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 260 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 261 | PadStrideInfo(1, 1, 0, 0)) |
| 262 | .set_name(param_path + "/project/Conv2D") |
| 263 | << BatchNormalizationLayer( |
| 264 | get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_mean.npy"), |
| 265 | get_weights_accessor(data_path, total_path + "project_BatchNorm_moving_variance.npy"), |
| 266 | get_weights_accessor(data_path, total_path + "project_BatchNorm_gamma.npy"), |
| 267 | get_weights_accessor(data_path, total_path + "project_BatchNorm_beta.npy"), 0.0010000000474974513) |
| 268 | .set_name(param_path + "/project/BatchNorm"); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 269 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 270 | if (is_residual == IsResidual::Yes) |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 271 | { |
| 272 | // Add residual node |
| 273 | SubStream right(graph); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | graph |
| 275 | << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 276 | } |
| 277 | else |
| 278 | { |
| 279 | graph.forward_tail(left.tail_node()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void create_graph_qasymm8(TensorDescriptor &input_descriptor) |
| 284 | { |
| 285 | // Create model path |
Pablo Tello | adc2186 | 2019-03-22 16:47:59 +0000 | [diff] [blame] | 286 | const std::string model_path = "/cnn_data/mobilenet_v2_1.0_224_quantized_model/"; |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 287 | |
| 288 | // Get trainable parameters data path |
| 289 | std::string data_path = common_params.data_path; |
| 290 | |
| 291 | // Add model path to data path |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 292 | if (!data_path.empty()) |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 293 | { |
| 294 | data_path += model_path; |
| 295 | } |
| 296 | |
| 297 | const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128); |
| 298 | const QuantizationInfo mid_quant_info = QuantizationInfo(0.023528477177023888f, 128); |
| 299 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 300 | const std::vector<QuantizationInfo> conv_weights_quant_info = { |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 301 | QuantizationInfo(0.03396892547607422f, 122), // Conv |
| 302 | QuantizationInfo(0.005167067516595125f, 125), // Conv1 |
| 303 | QuantizationInfo(0.0016910821432247758f, 113) // Conv2d_1c_1x1 |
| 304 | }; |
| 305 | |
| 306 | // Pointwise expand convolution quantization info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 307 | const std::vector<QuantizationInfo> pwc_q = { |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 308 | QuantizationInfo(0.254282623529f, 129), // expand_0 (Dummy) |
| 309 | QuantizationInfo(0.009758507832884789f, 127), // expand_1 |
| 310 | QuantizationInfo(0.0036556976847350597f, 144), // expand_2 |
| 311 | QuantizationInfo(0.0029988749884068966f, 104), // expand_3 |
| 312 | QuantizationInfo(0.0019244228024035692f, 128), // expand_4 |
| 313 | QuantizationInfo(0.0013649158645421267f, 135), // expand_5 |
| 314 | QuantizationInfo(0.0019170437008142471f, 127), // expand_6 |
| 315 | QuantizationInfo(0.0015538912266492844f, 125), // expand_7 |
| 316 | QuantizationInfo(0.0014702979242429137f, 134), // expand_8 |
| 317 | QuantizationInfo(0.0013733493397012353f, 127), // expand_9 |
| 318 | QuantizationInfo(0.0016282502328976989f, 131), // expand_10 |
| 319 | QuantizationInfo(0.0016309921629726887f, 134), // expand_11 |
| 320 | QuantizationInfo(0.0018258779309689999f, 138), // expand_12 |
| 321 | QuantizationInfo(0.0013828007504343987f, 123), // expand_13 |
| 322 | QuantizationInfo(0.0020222084131091833f, 135), // expand_14 |
| 323 | QuantizationInfo(0.04281935095787048f, 102), // expand_15 |
| 324 | QuantizationInfo(0.002046825597062707f, 135) // expand_16 |
| 325 | }; |
| 326 | // Depthwise expand convolution quantization info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 327 | const std::vector<QuantizationInfo> dwc_q = { |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 328 | QuantizationInfo(0.3436955213546753f, 165), // expand_0 |
| 329 | QuantizationInfo(0.020969120785593987f, 109), // expand_1 |
| 330 | QuantizationInfo(0.16981913149356842f, 52), // expand_2 |
| 331 | QuantizationInfo(0.017202870920300484f, 143), // expand_3 |
| 332 | QuantizationInfo(0.06525065749883652f, 118), // expand_4 |
| 333 | QuantizationInfo(0.07909784466028214f, 95), // expand_5 |
| 334 | QuantizationInfo(0.010087885893881321f, 127), // expand_6 |
| 335 | QuantizationInfo(0.06092711538076401f, 110), // expand_7 |
| 336 | QuantizationInfo(0.052407849580049515f, 133), // expand_8 |
| 337 | QuantizationInfo(0.04077887907624245f, 155), // expand_9 |
| 338 | QuantizationInfo(0.031107846647500992f, 143), // expand_10 |
| 339 | QuantizationInfo(0.07080810517072678f, 66), // expand_11 |
| 340 | QuantizationInfo(0.07448793947696686f, 159), // expand_12 |
| 341 | QuantizationInfo(0.01525793131440878f, 92), // expand_13 |
| 342 | QuantizationInfo(0.04166752099990845f, 147), // expand_14 |
| 343 | QuantizationInfo(0.04281935095787048f, 102), // expand_15 |
| 344 | QuantizationInfo(0.16456253826618195, 201) // expand_16 |
| 345 | }; |
| 346 | // Project convolution quantization info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 347 | const std::vector<QuantizationInfo> prwc_q = { |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 348 | QuantizationInfo(0.03737175464630127f, 140), // expand_0 |
| 349 | QuantizationInfo(0.0225360207259655f, 156), // expand_1 |
| 350 | QuantizationInfo(0.02740888111293316f, 122), // expand_2 |
| 351 | QuantizationInfo(0.016844693571329117f, 111), // expand_3 |
| 352 | QuantizationInfo(0.019062912091612816f, 146), // expand_4 |
| 353 | QuantizationInfo(0.018293123692274094f, 128), // expand_5 |
| 354 | QuantizationInfo(0.014601286500692368f, 147), // expand_6 |
| 355 | QuantizationInfo(0.016782939434051514f, 124), // expand_7 |
| 356 | QuantizationInfo(0.012898261658847332f, 125), // expand_8 |
| 357 | QuantizationInfo(0.019561484456062317f, 144), // expand_9 |
| 358 | QuantizationInfo(0.007436311338096857f, 129), // expand_10 |
| 359 | QuantizationInfo(0.00838223285973072f, 136), // expand_11 |
| 360 | QuantizationInfo(0.023982593789696693f, 154), // expand_12 |
| 361 | QuantizationInfo(0.009447949007153511f, 140), // expand_13 |
| 362 | QuantizationInfo(0.00789870135486126f, 139), // expand_14 |
| 363 | QuantizationInfo(0.03697410225868225f, 131), // expand_15 |
| 364 | QuantizationInfo(0.008009289391338825f, 111) // expand_16 |
| 365 | }; |
| 366 | |
| 367 | graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info), |
| 368 | get_weights_accessor(data_path, common_params.image)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 369 | << ConvolutionLayer(3U, 3U, 32U, get_weights_accessor(data_path, "Conv_weights.npy"), |
| 370 | get_weights_accessor(data_path, "Conv_bias.npy"), |
| 371 | PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), 1, |
| 372 | conv_weights_quant_info.at(0), mid_quant_info) |
| 373 | .set_name("Conv") |
| 374 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 375 | .set_name("Conv/Relu6") |
| 376 | << DepthwiseConvolutionLayer( |
| 377 | 3U, 3U, get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_weights.npy"), |
| 378 | get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_biases.npy"), |
| 379 | PadStrideInfo(1, 1, 1, 1), 1, dwc_q.at(0)) |
| 380 | .set_name("expanded_conv/depthwise/depthwise") |
| 381 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 382 | .set_name("expanded_conv/depthwise/Relu6") |
| 383 | << ConvolutionLayer(1U, 1U, 16U, get_weights_accessor(data_path, "expanded_conv_project_weights.npy"), |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 384 | get_weights_accessor(data_path, "expanded_conv_project_biases.npy"), |
| 385 | PadStrideInfo(1, 1, 0, 0), 1, prwc_q.at(0)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 386 | .set_name("expanded_conv/project/Conv2D"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 387 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 388 | get_expanded_conv_qasymm8(data_path, "expanded_conv_1", IsResidual::No, 96U, 24U, |
| 389 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(1), |
| 390 | dwc_q.at(1), prwc_q.at(1)); |
| 391 | get_expanded_conv_qasymm8(data_path, "expanded_conv_2", IsResidual::Yes, 144U, 24U, PadStrideInfo(1, 1, 1, 1), |
| 392 | pwc_q.at(2), dwc_q.at(2), prwc_q.at(2)); |
| 393 | get_expanded_conv_qasymm8(data_path, "expanded_conv_3", IsResidual::No, 144U, 32U, |
| 394 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(3), |
| 395 | dwc_q.at(3), prwc_q.at(3)); |
| 396 | get_expanded_conv_qasymm8(data_path, "expanded_conv_4", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1), |
| 397 | pwc_q.at(4), dwc_q.at(4), prwc_q.at(4)); |
| 398 | get_expanded_conv_qasymm8(data_path, "expanded_conv_5", IsResidual::Yes, 192U, 32U, PadStrideInfo(1, 1, 1, 1), |
| 399 | pwc_q.at(5), dwc_q.at(5), prwc_q.at(5)); |
| 400 | get_expanded_conv_qasymm8(data_path, "expanded_conv_6", IsResidual::No, 192U, 64U, |
| 401 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(6), |
| 402 | dwc_q.at(6), prwc_q.at(6)); |
| 403 | get_expanded_conv_qasymm8(data_path, "expanded_conv_7", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), |
| 404 | pwc_q.at(7), dwc_q.at(7), prwc_q.at(7)); |
| 405 | get_expanded_conv_qasymm8(data_path, "expanded_conv_8", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), |
| 406 | pwc_q.at(8), dwc_q.at(8), prwc_q.at(8)); |
| 407 | get_expanded_conv_qasymm8(data_path, "expanded_conv_9", IsResidual::Yes, 384U, 64U, PadStrideInfo(1, 1, 1, 1), |
| 408 | pwc_q.at(9), dwc_q.at(9), prwc_q.at(9)); |
| 409 | get_expanded_conv_qasymm8(data_path, "expanded_conv_10", IsResidual::No, 384U, 96U, PadStrideInfo(1, 1, 1, 1), |
| 410 | pwc_q.at(10), dwc_q.at(10), prwc_q.at(10)); |
| 411 | get_expanded_conv_qasymm8(data_path, "expanded_conv_11", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1), |
| 412 | pwc_q.at(11), dwc_q.at(11), prwc_q.at(11)); |
| 413 | get_expanded_conv_qasymm8(data_path, "expanded_conv_12", IsResidual::Yes, 576U, 96U, PadStrideInfo(1, 1, 1, 1), |
| 414 | pwc_q.at(12), dwc_q.at(12), prwc_q.at(12)); |
| 415 | get_expanded_conv_qasymm8(data_path, "expanded_conv_13", IsResidual::No, 576U, 160U, |
| 416 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), pwc_q.at(13), |
| 417 | dwc_q.at(13), prwc_q.at(13)); |
| 418 | get_expanded_conv_qasymm8(data_path, "expanded_conv_14", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1), |
| 419 | pwc_q.at(14), dwc_q.at(14), prwc_q.at(14)); |
| 420 | get_expanded_conv_qasymm8(data_path, "expanded_conv_15", IsResidual::Yes, 960U, 160U, PadStrideInfo(1, 1, 1, 1), |
| 421 | pwc_q.at(15), dwc_q.at(15), prwc_q.at(15)); |
| 422 | get_expanded_conv_qasymm8(data_path, "expanded_conv_16", IsResidual::No, 960U, 320U, PadStrideInfo(1, 1, 1, 1), |
| 423 | pwc_q.at(16), dwc_q.at(16), prwc_q.at(16)); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 424 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 425 | graph << ConvolutionLayer(1U, 1U, 1280U, get_weights_accessor(data_path, "Conv_1_weights.npy"), |
| 426 | get_weights_accessor(data_path, "Conv_1_biases.npy"), PadStrideInfo(1, 1, 0, 0), 1, |
| 427 | conv_weights_quant_info.at(1)) |
| 428 | .set_name("Conv_1") |
| 429 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 430 | .set_name("Conv_1/Relu6") |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 431 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, common_params.data_layout)).set_name("Logits/AvgPool") |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 432 | << ConvolutionLayer(1U, 1U, 1001U, get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"), |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 433 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), |
| 434 | PadStrideInfo(1, 1, 0, 0), 1, conv_weights_quant_info.at(2)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 435 | .set_name("Logits/Conv2d_1c_1x1"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 438 | void get_expanded_conv_qasymm8(const std::string &data_path, |
| 439 | std::string &¶m_path, |
| 440 | IsResidual is_residual, |
| 441 | unsigned int input_channels, |
| 442 | unsigned int output_channels, |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 443 | PadStrideInfo dwc_pad_stride_info, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 444 | const QuantizationInfo &pwi, |
| 445 | const QuantizationInfo &dwi, |
| 446 | const QuantizationInfo &pji) |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 447 | { |
| 448 | std::string total_path = param_path + "_"; |
| 449 | |
| 450 | SubStream left(graph); |
| 451 | left << ConvolutionLayer(1U, 1U, input_channels, |
| 452 | get_weights_accessor(data_path, total_path + "project_weights.npy"), |
| 453 | get_weights_accessor(data_path, total_path + "project_biases.npy"), |
| 454 | PadStrideInfo(1, 1, 0, 0), 1, pwi) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 455 | .set_name(param_path + "/Conv2D") |
| 456 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 457 | .set_name(param_path + "/Conv2D/Relu6") |
| 458 | << DepthwiseConvolutionLayer( |
| 459 | 3U, 3U, get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"), |
| 460 | get_weights_accessor(data_path, total_path + "depthwise_depthwise_biases.npy"), dwc_pad_stride_info, |
| 461 | 1, dwi) |
| 462 | .set_name(param_path + "/depthwise/depthwise") |
| 463 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 464 | .set_name(param_path + "/depthwise/Relu6") |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 465 | << ConvolutionLayer(1U, 1U, output_channels, |
| 466 | get_weights_accessor(data_path, total_path + "project_weights.npy"), |
| 467 | get_weights_accessor(data_path, total_path + "project_biases.npy"), |
| 468 | PadStrideInfo(1, 1, 0, 0), 1, pji) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 469 | .set_name(param_path + "/project/Conv2D"); |
Georgios Pinitas | f60d671 | 2018-11-29 13:21:54 +0000 | [diff] [blame] | 470 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 471 | if (is_residual == IsResidual::Yes) |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 472 | { |
| 473 | // Add residual node |
| 474 | SubStream right(graph); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 475 | graph |
| 476 | << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(param_path + "/add"); |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 477 | } |
| 478 | else |
| 479 | { |
| 480 | graph.forward_tail(left.tail_node()); |
| 481 | } |
| 482 | } |
| 483 | }; |
| 484 | |
| 485 | /** Main program for MobileNetV2 |
| 486 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 487 | * Model is based on: |
| 488 | * https://arxiv.org/abs/1801.04381 |
| 489 | * "MobileNetV2: Inverted Residuals and Linear Bottlenecks" |
| 490 | * Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen |
| 491 | * |
Georgios Pinitas | 588ebc5 | 2018-12-21 13:39:07 +0000 | [diff] [blame] | 492 | * Provenance: https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz |
| 493 | * |
Georgios Pinitas | 766b70c | 2018-08-13 17:50:34 +0100 | [diff] [blame] | 494 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 495 | * |
| 496 | * @param[in] argc Number of arguments |
| 497 | * @param[in] argv Arguments |
| 498 | */ |
| 499 | int main(int argc, char **argv) |
| 500 | { |
| 501 | return arm_compute::utils::run_example<GraphMobilenetV2Example>(argc, argv); |
| 502 | } |