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