Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +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 | 108ab0b | 2018-09-14 18:35:11 +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 | 108ab0b | 2018-09-14 18:35:11 +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 | |
| 35 | /** Example demonstrating how to implement ShuffleNet network using the Compute Library's graph API */ |
| 36 | class ShuffleNetExample : public Example |
| 37 | { |
| 38 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | ShuffleNetExample() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ShuffleNet") |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +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 | 108ab0b | 2018-09-14 18:35:11 +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 | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 53 | { |
| 54 | cmd_parser.print_help(argv[0]); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // Set default layout if needed (Single kernel grouped convolution not yet supported int NHWC) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | if (!common_opts.data_layout->is_set()) |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 60 | { |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 61 | common_params.data_layout = DataLayout::NHWC; |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Checks |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), |
| 66 | "QASYMM8 not supported for this graph"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 67 | |
| 68 | // Print parameter values |
| 69 | std::cout << common_params << std::endl; |
| 70 | std::cout << "Model: Shufflenet_1_g4" << std::endl; |
| 71 | |
| 72 | // Create model path |
| 73 | std::string model_path = "/cnn_data/shufflenet_model/"; |
| 74 | |
| 75 | // Get trainable parameters data path |
| 76 | std::string data_path = common_params.data_path; |
| 77 | |
| 78 | // Add model path to data path |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | if (!data_path.empty()) |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 80 | { |
| 81 | data_path += model_path; |
| 82 | } |
| 83 | |
| 84 | // Create input descriptor |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 85 | const auto operation_layout = common_params.data_layout; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | const TensorShape tensor_shape = |
| 87 | permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout); |
| 88 | TensorDescriptor input_descriptor = |
| 89 | TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 90 | |
| 91 | // Set weights trained layout |
| 92 | const DataLayout weights_layout = DataLayout::NCHW; |
| 93 | |
| 94 | // Create preprocessor |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 95 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<TFPreproccessor>(0); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 96 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 97 | graph << common_params.target << common_params.fast_math_hint |
| 98 | << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), |
| 99 | false /* Do not convert to BGR */)) |
| 100 | << ConvolutionLayer(3U, 3U, 24U, get_weights_accessor(data_path, "conv3_0_w_0.npy", weights_layout), |
| 101 | get_weights_accessor(data_path, "conv3_0_b_0.npy", weights_layout), |
| 102 | PadStrideInfo(2, 2, 1, 1)) |
| 103 | .set_name("Conv1/convolution") |
| 104 | << BatchNormalizationLayer(get_weights_accessor(data_path, "conv3_0_bn_rm_0.npy"), |
| 105 | get_weights_accessor(data_path, "conv3_0_bn_riv_0.npy"), |
| 106 | get_weights_accessor(data_path, "conv3_0_bn_s_0.npy"), |
| 107 | get_weights_accessor(data_path, "conv3_0_bn_b_0.npy"), 1e-5f) |
| 108 | .set_name("Conv1/BatchNorm") |
| 109 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 110 | .set_name("Conv1/Relu") |
| 111 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, operation_layout, PadStrideInfo(2, 2, 1, 1))) |
| 112 | .set_name("pool1/MaxPool"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 113 | |
| 114 | // Stage 2 |
| 115 | add_residual_block(data_path, DataLayout::NCHW, 0U /* unit */, 112U /* depth */, 2U /* stride */); |
| 116 | add_residual_block(data_path, DataLayout::NCHW, 1U /* unit */, 136U /* depth */, 1U /* stride */); |
| 117 | add_residual_block(data_path, DataLayout::NCHW, 2U /* unit */, 136U /* depth */, 1U /* stride */); |
| 118 | add_residual_block(data_path, DataLayout::NCHW, 3U /* unit */, 136U /* depth */, 1U /* stride */); |
| 119 | |
| 120 | // Stage 3 |
| 121 | add_residual_block(data_path, DataLayout::NCHW, 4U /* unit */, 136U /* depth */, 2U /* stride */); |
| 122 | add_residual_block(data_path, DataLayout::NCHW, 5U /* unit */, 272U /* depth */, 1U /* stride */); |
| 123 | add_residual_block(data_path, DataLayout::NCHW, 6U /* unit */, 272U /* depth */, 1U /* stride */); |
| 124 | add_residual_block(data_path, DataLayout::NCHW, 7U /* unit */, 272U /* depth */, 1U /* stride */); |
| 125 | add_residual_block(data_path, DataLayout::NCHW, 8U /* unit */, 272U /* depth */, 1U /* stride */); |
| 126 | add_residual_block(data_path, DataLayout::NCHW, 9U /* unit */, 272U /* depth */, 1U /* stride */); |
| 127 | add_residual_block(data_path, DataLayout::NCHW, 10U /* unit */, 272U /* depth */, 1U /* stride */); |
| 128 | add_residual_block(data_path, DataLayout::NCHW, 11U /* unit */, 272U /* depth */, 1U /* stride */); |
| 129 | |
| 130 | // Stage 4 |
| 131 | add_residual_block(data_path, DataLayout::NCHW, 12U /* unit */, 272U /* depth */, 2U /* stride */); |
| 132 | add_residual_block(data_path, DataLayout::NCHW, 13U /* unit */, 544U /* depth */, 1U /* stride */); |
| 133 | add_residual_block(data_path, DataLayout::NCHW, 14U /* unit */, 544U /* depth */, 1U /* stride */); |
| 134 | add_residual_block(data_path, DataLayout::NCHW, 15U /* unit */, 544U /* depth */, 1U /* stride */); |
| 135 | |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 136 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, operation_layout)).set_name("predictions/AvgPool") |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 137 | << FlattenLayer().set_name("predictions/Reshape") |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | << FullyConnectedLayer(1000U, get_weights_accessor(data_path, "pred_w_0.npy", weights_layout), |
| 139 | get_weights_accessor(data_path, "pred_b_0.npy")) |
| 140 | .set_name("predictions/FC") |
| 141 | << SoftmaxLayer().set_name("predictions/Softmax") << OutputLayer(get_output_accessor(common_params, 5)); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 142 | |
| 143 | // Finalize graph |
| 144 | GraphConfig config; |
| 145 | config.num_threads = common_params.threads; |
| 146 | config.use_tuner = common_params.enable_tuner; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 147 | config.tuner_mode = common_params.tuner_mode; |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 148 | config.tuner_file = common_params.tuner_file; |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 149 | config.mlgo_file = common_params.mlgo_file; |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 150 | |
| 151 | graph.finalize(common_params.target, config); |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | void do_run() override |
| 157 | { |
| 158 | // Run graph |
| 159 | graph.run(); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | CommandLineParser cmd_parser; |
| 164 | CommonGraphOptions common_opts; |
| 165 | CommonGraphParams common_params; |
| 166 | Stream graph; |
| 167 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 168 | void add_residual_block(const std::string &data_path, |
| 169 | DataLayout weights_layout, |
| 170 | unsigned int unit, |
| 171 | unsigned int depth, |
| 172 | unsigned int stride) |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 173 | { |
| 174 | PadStrideInfo dwc_info = PadStrideInfo(1, 1, 1, 1); |
| 175 | const unsigned int gconv_id = unit * 2; |
| 176 | const unsigned int num_groups = 4; |
| 177 | const std::string unit_id_name = arm_compute::support::cpp11::to_string(unit); |
| 178 | const std::string gconv_id_name = arm_compute::support::cpp11::to_string(gconv_id); |
| 179 | const std::string gconv_id_1_name = arm_compute::support::cpp11::to_string(gconv_id + 1); |
| 180 | const std::string unit_name = "unit" + unit_id_name; |
| 181 | |
| 182 | SubStream left_ss(graph); |
| 183 | SubStream right_ss(graph); |
| 184 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | if (stride == 2) |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 186 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | right_ss << PoolingLayer( |
| 188 | PoolingLayerInfo(PoolingType::AVG, 3, common_params.data_layout, PadStrideInfo(2, 2, 1, 1))) |
| 189 | .set_name(unit_name + "/pool_1/AveragePool"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 190 | dwc_info = PadStrideInfo(2, 2, 1, 1); |
| 191 | } |
| 192 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | left_ss |
| 194 | << ConvolutionLayer(1U, 1U, depth, |
| 195 | get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_w_0.npy", weights_layout), |
| 196 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 197 | PadStrideInfo(1, 1, 0, 0), num_groups) |
| 198 | .set_name(unit_name + "/gconv1_" + gconv_id_name + "/convolution") |
| 199 | << BatchNormalizationLayer(get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_rm_0.npy"), |
| 200 | get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_riv_0.npy"), |
| 201 | get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_s_0.npy"), |
| 202 | get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_b_0.npy"), |
| 203 | 1e-5f) |
| 204 | .set_name(unit_name + "/gconv1_" + gconv_id_name + "/BatchNorm") |
| 205 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 206 | .set_name(unit_name + "/gconv1_" + gconv_id_name + "/Relu") |
| 207 | << ChannelShuffleLayer(num_groups).set_name(unit_name + "/shuffle_0/ChannelShufle") |
| 208 | << DepthwiseConvolutionLayer( |
| 209 | 3U, 3U, get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_w_0.npy", weights_layout), |
| 210 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), dwc_info) |
| 211 | .set_name(unit_name + "/gconv3_" + unit_id_name + "/depthwise") |
| 212 | << BatchNormalizationLayer(get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_rm_0.npy"), |
| 213 | get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_riv_0.npy"), |
| 214 | get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_s_0.npy"), |
| 215 | get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_b_0.npy"), 1e-5f) |
| 216 | .set_name(unit_name + "/gconv3_" + unit_id_name + "/BatchNorm") |
| 217 | << ConvolutionLayer( |
| 218 | 1U, 1U, depth, |
| 219 | get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_w_0.npy", weights_layout), |
| 220 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), PadStrideInfo(1, 1, 0, 0), num_groups) |
| 221 | .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/convolution") |
| 222 | << BatchNormalizationLayer(get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_rm_0.npy"), |
| 223 | get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_riv_0.npy"), |
| 224 | get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_s_0.npy"), |
| 225 | get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_b_0.npy"), |
| 226 | 1e-5f) |
| 227 | .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/BatchNorm"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 228 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | if (stride == 2) |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 230 | { |
| 231 | graph << ConcatLayer(std::move(left_ss), std::move(right_ss)).set_name(unit_name + "/Concat"); |
| 232 | } |
| 233 | else |
| 234 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 235 | graph << EltwiseLayer(std::move(left_ss), std::move(right_ss), EltwiseOperation::Add) |
| 236 | .set_name(unit_name + "/Add"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 237 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 238 | graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 239 | .set_name(unit_name + "/Relu"); |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | |
| 243 | /** Main program for ShuffleNet |
| 244 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 245 | * Model is based on: |
| 246 | * https://arxiv.org/abs/1707.01083 |
| 247 | * "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices" |
| 248 | * Xiangyu Zhang, Xinyu Zhou, Mengxiao Lin, Jian Sun |
| 249 | * |
Georgios Pinitas | 588ebc5 | 2018-12-21 13:39:07 +0000 | [diff] [blame] | 250 | * Provenance: https://s3.amazonaws.com/download.onnx/models/opset_9/shufflenet.tar.gz |
| 251 | * |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 252 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 253 | * |
| 254 | * @param[in] argc Number of arguments |
| 255 | * @param[in] argv Arguments |
| 256 | */ |
| 257 | int main(int argc, char **argv) |
| 258 | { |
| 259 | return arm_compute::utils::run_example<ShuffleNetExample>(argc, argv); |
| 260 | } |