Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-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 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph.h" |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 25 | #include "support/ToolchainSupport.h" |
| 26 | #include "utils/GraphUtils.h" |
| 27 | #include "utils/Utils.h" |
| 28 | |
| 29 | #include <cstdlib> |
| 30 | |
| 31 | using namespace arm_compute::utils; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | using namespace arm_compute::graph::frontend; |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 33 | using namespace arm_compute::graph_utils; |
| 34 | |
| 35 | /** Example demonstrating how to implement Microsoft's ResNet50 network using the Compute Library's graph API |
| 36 | * |
| 37 | * @param[in] argc Number of arguments |
| 38 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 39 | */ |
| 40 | class GraphResNet50Example : public Example |
| 41 | { |
| 42 | public: |
| 43 | void do_setup(int argc, char **argv) override |
| 44 | { |
| 45 | std::string data_path; /* Path to the trainable data */ |
| 46 | std::string image; /* Image data */ |
| 47 | std::string label; /* Label data */ |
| 48 | |
| 49 | // Create a preprocessor object |
| 50 | const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } }; |
| 51 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb, |
| 52 | false /* Do not convert to BGR */); |
| 53 | |
| 54 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 55 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 56 | Target target_hint = set_target_hint(target); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 57 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 58 | ConvolutionMethod convolution_hint = (target_hint == Target::CL) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM; |
| 59 | |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 60 | // Parse arguments |
| 61 | if(argc < 2) |
| 62 | { |
| 63 | // Print help |
| 64 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 65 | std::cout << "No data folder provided: using random values\n\n"; |
| 66 | } |
| 67 | else if(argc == 2) |
| 68 | { |
| 69 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 70 | std::cout << "No data folder provided: using random values\n\n"; |
| 71 | } |
| 72 | else if(argc == 3) |
| 73 | { |
| 74 | data_path = argv[2]; |
| 75 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 76 | std::cout << "No image provided: using random values\n\n"; |
| 77 | } |
| 78 | else if(argc == 4) |
| 79 | { |
| 80 | data_path = argv[2]; |
| 81 | image = argv[3]; |
| 82 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 83 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | data_path = argv[2]; |
| 88 | image = argv[3]; |
| 89 | label = argv[4]; |
| 90 | } |
| 91 | |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 92 | graph << target_hint |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 93 | << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32), |
| 94 | get_input_accessor(image, std::move(preprocessor), false /* Do not convert to BGR */)) |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 95 | << ConvolutionLayer( |
| 96 | 7U, 7U, 64U, |
| 97 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_weights.npy"), |
| 98 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 99 | PadStrideInfo(2, 2, 3, 3)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 100 | .set_name("conv1/convolution") |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 101 | << convolution_hint |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 102 | << BatchNormalizationLayer( |
| 103 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_mean.npy"), |
| 104 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_moving_variance.npy"), |
| 105 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_gamma.npy"), |
| 106 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/conv1_BatchNorm_beta.npy"), |
| 107 | 0.0000100099996416f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 108 | .set_name("conv1/BatchNorm") |
| 109 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1/Relu") |
| 110 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))).set_name("pool1/MaxPool"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 111 | |
| 112 | add_residual_block(data_path, "block1", 64, 3, 2); |
| 113 | add_residual_block(data_path, "block2", 128, 4, 2); |
| 114 | add_residual_block(data_path, "block3", 256, 6, 2); |
| 115 | add_residual_block(data_path, "block4", 512, 3, 1); |
| 116 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 117 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("pool5") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 118 | << ConvolutionLayer( |
| 119 | 1U, 1U, 1000U, |
| 120 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_weights.npy"), |
| 121 | get_weights_accessor(data_path, "/cnn_data/resnet50_model/logits_biases.npy"), |
| 122 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 123 | .set_name("logits/convolution") |
| 124 | << FlattenLayer().set_name("predictions/Reshape") |
| 125 | << SoftmaxLayer().set_name("predictions/Softmax") |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 126 | << OutputLayer(get_output_accessor(label, 5)); |
Gian Marco | c1b6e37 | 2018-02-21 18:03:26 +0000 | [diff] [blame] | 127 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 128 | // Finalize graph |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 129 | GraphConfig config; |
| 130 | config.use_function_memory_manager = true; |
| 131 | config.use_tuner = (target == 2); |
| 132 | graph.finalize(target_hint, config); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 133 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 134 | |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 135 | void do_run() override |
| 136 | { |
| 137 | // Run graph |
| 138 | graph.run(); |
| 139 | } |
| 140 | |
| 141 | private: |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 142 | Stream graph{ 0, "ResNet50" }; |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 143 | |
| 144 | void add_residual_block(const std::string &data_path, const std::string &name, unsigned int base_depth, unsigned int num_units, unsigned int stride) |
| 145 | { |
| 146 | for(unsigned int i = 0; i < num_units; ++i) |
| 147 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 148 | std::stringstream unit_path_ss; |
| 149 | unit_path_ss << "/cnn_data/resnet50_model/" << name << "_unit_" << (i + 1) << "_bottleneck_v1_"; |
| 150 | std::stringstream unit_name_ss; |
| 151 | unit_name_ss << name << "/unit" << (i + 1) << "/bottleneck_v1/"; |
| 152 | |
| 153 | std::string unit_path = unit_path_ss.str(); |
| 154 | std::string unit_name = unit_name_ss.str(); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 155 | |
| 156 | unsigned int middle_stride = 1; |
| 157 | |
| 158 | if(i == (num_units - 1)) |
| 159 | { |
| 160 | middle_stride = stride; |
| 161 | } |
| 162 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 163 | SubStream right(graph); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 164 | right << ConvolutionLayer( |
| 165 | 1U, 1U, base_depth, |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 166 | get_weights_accessor(data_path, unit_path + "conv1_weights.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 167 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 168 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 169 | .set_name(unit_name + "conv1/convolution") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 170 | << BatchNormalizationLayer( |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 171 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_mean.npy"), |
| 172 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_moving_variance.npy"), |
| 173 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_gamma.npy"), |
| 174 | get_weights_accessor(data_path, unit_path + "conv1_BatchNorm_beta.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 175 | 0.0000100099996416f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 176 | .set_name(unit_name + "conv1/BatchNorm") |
| 177 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 178 | |
| 179 | << ConvolutionLayer( |
| 180 | 3U, 3U, base_depth, |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 181 | get_weights_accessor(data_path, unit_path + "conv2_weights.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 182 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 183 | PadStrideInfo(middle_stride, middle_stride, 1, 1)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 184 | .set_name(unit_name + "conv2/convolution") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 185 | << BatchNormalizationLayer( |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 186 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_mean.npy"), |
| 187 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_moving_variance.npy"), |
| 188 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_gamma.npy"), |
| 189 | get_weights_accessor(data_path, unit_path + "conv2_BatchNorm_beta.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 190 | 0.0000100099996416f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 191 | .set_name(unit_name + "conv2/BatchNorm") |
| 192 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "conv1/Relu") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 193 | |
| 194 | << ConvolutionLayer( |
| 195 | 1U, 1U, base_depth * 4, |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 196 | get_weights_accessor(data_path, unit_path + "conv3_weights.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 197 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 198 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 199 | .set_name(unit_name + "conv3/convolution") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 200 | << BatchNormalizationLayer( |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 201 | get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_mean.npy"), |
| 202 | get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_moving_variance.npy"), |
| 203 | get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_gamma.npy"), |
| 204 | get_weights_accessor(data_path, unit_path + "conv3_BatchNorm_beta.npy"), |
| 205 | 0.0000100099996416f) |
| 206 | .set_name(unit_name + "conv2/BatchNorm"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 207 | |
| 208 | if(i == 0) |
| 209 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 210 | SubStream left(graph); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 211 | left << ConvolutionLayer( |
| 212 | 1U, 1U, base_depth * 4, |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 213 | get_weights_accessor(data_path, unit_path + "shortcut_weights.npy"), |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 214 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 215 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 216 | .set_name(unit_name + "shortcut/convolution") |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 217 | << BatchNormalizationLayer( |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 218 | get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_mean.npy"), |
| 219 | get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_moving_variance.npy"), |
| 220 | get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_gamma.npy"), |
| 221 | get_weights_accessor(data_path, unit_path + "shortcut_BatchNorm_beta.npy"), |
| 222 | 0.0000100099996416f) |
| 223 | .set_name(unit_name + "shortcut/BatchNorm"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 224 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 225 | graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 226 | } |
| 227 | else if(middle_stride > 1) |
| 228 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 229 | SubStream left(graph); |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 230 | left << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 1, PadStrideInfo(middle_stride, middle_stride, 0, 0), true)).set_name(unit_name + "shortcut/MaxPool"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 231 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 232 | graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 236 | SubStream left(graph); |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 237 | graph << BranchLayer(BranchMergeMethod::ADD, std::move(left), std::move(right)).set_name(unit_name + "add"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame^] | 240 | graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "Relu"); |
Alex Gilday | 8913d8d | 2018-02-15 11:07:18 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | /** Main program for ResNet50 |
| 246 | * |
| 247 | * @param[in] argc Number of arguments |
| 248 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 249 | */ |
| 250 | int main(int argc, char **argv) |
| 251 | { |
| 252 | return arm_compute::utils::run_example<GraphResNet50Example>(argc, argv); |
| 253 | } |