Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 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" |
| 25 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 26 | #include "utils/CommonGraphOptions.h" |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 30 | using namespace arm_compute::utils; |
| 31 | using namespace arm_compute::graph::frontend; |
| 32 | using namespace arm_compute::graph_utils; |
| 33 | |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 34 | /** Example demonstrating how to implement ResNeXt50 network using the Compute Library's graph API */ |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 35 | class GraphResNeXt50Example : public Example |
| 36 | { |
| 37 | public: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 38 | GraphResNeXt50Example() |
| 39 | : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ResNeXt50") |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 40 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 41 | } |
| 42 | bool do_setup(int argc, char **argv) override |
| 43 | { |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 44 | // Parse arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 45 | cmd_parser.parse(argc, argv); |
| 46 | |
| 47 | // Consume common parameters |
| 48 | common_params = consume_common_graph_parameters(common_opts); |
| 49 | |
| 50 | // Return when help menu is requested |
| 51 | if(common_params.help) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 52 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 53 | cmd_parser.print_help(argv[0]); |
| 54 | return false; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 57 | // Checks |
Anthony Barbier | cdd68c0 | 2018-08-23 15:03:41 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 59 | |
| 60 | // Print parameter values |
| 61 | std::cout << common_params << std::endl; |
| 62 | |
| 63 | // Get trainable parameters data path |
| 64 | std::string data_path = common_params.data_path; |
| 65 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 66 | // Create input descriptor |
| 67 | const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); |
| 68 | TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
| 69 | |
| 70 | // Set weights trained layout |
| 71 | const DataLayout weights_layout = DataLayout::NCHW; |
| 72 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 73 | graph << common_params.target |
| 74 | << common_params.fast_math_hint |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 75 | << InputLayer(input_descriptor, get_input_accessor(common_params)) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 76 | << ScaleLayer(get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_mul.npy"), |
| 77 | get_weights_accessor(data_path, "/cnn_data/resnext50_model/bn_data_add.npy")) |
| 78 | .set_name("bn_data/Scale") |
| 79 | << ConvolutionLayer( |
| 80 | 7U, 7U, 64U, |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 81 | get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_weights.npy", weights_layout), |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 82 | get_weights_accessor(data_path, "/cnn_data/resnext50_model/conv0_biases.npy"), |
| 83 | PadStrideInfo(2, 2, 2, 3, 2, 3, DimensionRoundingType::FLOOR)) |
| 84 | .set_name("conv0/Convolution") |
| 85 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv0/Relu") |
| 86 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool0"); |
| 87 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 88 | add_residual_block(data_path, weights_layout, /*ofm*/ 256, /*stage*/ 1, /*num_unit*/ 3, /*stride_conv_unit1*/ 1); |
| 89 | add_residual_block(data_path, weights_layout, 512, 2, 4, 2); |
| 90 | add_residual_block(data_path, weights_layout, 1024, 3, 6, 2); |
| 91 | add_residual_block(data_path, weights_layout, 2048, 4, 3, 2); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 92 | |
| 93 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool1") |
| 94 | << FlattenLayer().set_name("predictions/Reshape") |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 95 | << OutputLayer(get_npy_output_accessor(common_params.labels, TensorShape(2048U), DataType::F32)); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 96 | |
| 97 | // Finalize graph |
| 98 | GraphConfig config; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 99 | config.num_threads = common_params.threads; |
| 100 | config.use_tuner = common_params.enable_tuner; |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 101 | config.tuner_file = common_params.tuner_file; |
| 102 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 103 | graph.finalize(common_params.target, config); |
| 104 | |
| 105 | return true; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void do_run() override |
| 109 | { |
| 110 | // Run graph |
| 111 | graph.run(); |
| 112 | } |
| 113 | |
| 114 | private: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 115 | CommandLineParser cmd_parser; |
| 116 | CommonGraphOptions common_opts; |
| 117 | CommonGraphParams common_params; |
| 118 | Stream graph; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 119 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 120 | void add_residual_block(const std::string &data_path, DataLayout weights_layout, |
| 121 | unsigned int base_depth, unsigned int stage, unsigned int num_units, unsigned int stride_conv_unit1) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 122 | { |
| 123 | for(unsigned int i = 0; i < num_units; ++i) |
| 124 | { |
| 125 | std::stringstream unit_path_ss; |
| 126 | unit_path_ss << "/cnn_data/resnext50_model/stage" << stage << "_unit" << (i + 1) << "_"; |
| 127 | std::string unit_path = unit_path_ss.str(); |
| 128 | |
| 129 | std::stringstream unit_name_ss; |
| 130 | unit_name_ss << "stage" << stage << "/unit" << (i + 1) << "/"; |
| 131 | std::string unit_name = unit_name_ss.str(); |
| 132 | |
| 133 | PadStrideInfo pad_grouped_conv(1, 1, 1, 1); |
| 134 | if(i == 0) |
| 135 | { |
| 136 | pad_grouped_conv = (stage == 1) ? PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 1, 1) : PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 1, 0, 1, DimensionRoundingType::FLOOR); |
| 137 | } |
| 138 | |
| 139 | SubStream right(graph); |
| 140 | right << ConvolutionLayer( |
| 141 | 1U, 1U, base_depth / 2, |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 142 | get_weights_accessor(data_path, unit_path + "conv1_weights.npy", weights_layout), |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 143 | get_weights_accessor(data_path, unit_path + "conv1_biases.npy"), |
| 144 | PadStrideInfo(1, 1, 0, 0)) |
| 145 | .set_name(unit_name + "conv1/convolution") |
| 146 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu") |
| 147 | |
| 148 | << ConvolutionLayer( |
| 149 | 3U, 3U, base_depth / 2, |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 150 | get_weights_accessor(data_path, unit_path + "conv2_weights.npy", weights_layout), |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 151 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 152 | pad_grouped_conv, 32) |
| 153 | .set_name(unit_name + "conv2/convolution") |
| 154 | << ScaleLayer(get_weights_accessor(data_path, unit_path + "bn2_mul.npy"), |
| 155 | get_weights_accessor(data_path, unit_path + "bn2_add.npy")) |
| 156 | .set_name(unit_name + "conv1/Scale") |
| 157 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv2/Relu") |
| 158 | |
| 159 | << ConvolutionLayer( |
| 160 | 1U, 1U, base_depth, |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 161 | get_weights_accessor(data_path, unit_path + "conv3_weights.npy", weights_layout), |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 162 | get_weights_accessor(data_path, unit_path + "conv3_biases.npy"), |
| 163 | PadStrideInfo(1, 1, 0, 0)) |
| 164 | .set_name(unit_name + "conv3/convolution"); |
| 165 | |
| 166 | SubStream left(graph); |
| 167 | if(i == 0) |
| 168 | { |
| 169 | left << ConvolutionLayer( |
| 170 | 1U, 1U, base_depth, |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 171 | get_weights_accessor(data_path, unit_path + "sc_weights.npy", weights_layout), |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 172 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 173 | PadStrideInfo(stride_conv_unit1, stride_conv_unit1, 0, 0)) |
| 174 | .set_name(unit_name + "sc/convolution") |
| 175 | << ScaleLayer(get_weights_accessor(data_path, unit_path + "sc_bn_mul.npy"), |
| 176 | get_weights_accessor(data_path, unit_path + "sc_bn_add.npy")) |
| 177 | .set_name(unit_name + "sc/scale"); |
| 178 | } |
| 179 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 180 | graph << EltwiseLayer(std::move(left), std::move(right), EltwiseOperation::Add).set_name(unit_name + "add"); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 181 | graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu"); |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | /** Main program for ResNeXt50 |
| 187 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 188 | * Model is based on: |
| 189 | * https://arxiv.org/abs/1611.05431 |
| 190 | * "Aggregated Residual Transformations for Deep Neural Networks" |
| 191 | * Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He |
| 192 | * |
Georgios Pinitas | 9f28b39 | 2018-07-18 20:01:53 +0100 | [diff] [blame] | 193 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 194 | * |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 195 | * @param[in] argc Number of arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 196 | * @param[in] argv Arguments |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 197 | */ |
| 198 | int main(int argc, char **argv) |
| 199 | { |
| 200 | return arm_compute::utils::run_example<GraphResNeXt50Example>(argc, argv); |
| 201 | } |