Michalis Spyrou | 177a9a5 | 2018-09-06 15:10:22 +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 | |
| 34 | /** Example demonstrating how to implement YOLOv3 network using the Compute Library's graph API |
| 35 | * |
| 36 | * @param[in] argc Number of arguments |
| 37 | * @param[in] argv Arguments |
| 38 | */ |
| 39 | class GraphYOLOv3Example : public Example |
| 40 | { |
| 41 | public: |
| 42 | GraphYOLOv3Example() |
| 43 | : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "YOLOv3") |
| 44 | { |
| 45 | } |
| 46 | bool do_setup(int argc, char **argv) override |
| 47 | { |
| 48 | // Parse arguments |
| 49 | cmd_parser.parse(argc, argv); |
| 50 | |
| 51 | // Consume common parameters |
| 52 | common_params = consume_common_graph_parameters(common_opts); |
| 53 | |
| 54 | // Return when help menu is requested |
| 55 | if(common_params.help) |
| 56 | { |
| 57 | cmd_parser.print_help(argv[0]); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Checks |
| 62 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); |
| 63 | |
| 64 | // Print parameter values |
| 65 | std::cout << common_params << std::endl; |
| 66 | |
| 67 | // Get trainable parameters data path |
| 68 | std::string data_path = common_params.data_path; |
| 69 | |
| 70 | // Create a preprocessor object |
| 71 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>(0.f); |
| 72 | |
| 73 | // Create input descriptor |
| 74 | const TensorShape tensor_shape = permute_shape(TensorShape(608U, 608U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); |
| 75 | TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
| 76 | |
| 77 | // Set weights trained layout |
| 78 | const DataLayout weights_layout = DataLayout::NCHW; |
| 79 | |
| 80 | graph << common_params.target |
| 81 | << common_params.fast_math_hint |
| 82 | << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false)) |
| 83 | // Layer 1 |
| 84 | << ConvolutionLayer( |
| 85 | 3U, 3U, 32U, |
| 86 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_1_w.npy", weights_layout), |
| 87 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 88 | PadStrideInfo(1, 1, 1, 1)) |
| 89 | .set_name("conv2d_1") |
| 90 | << BatchNormalizationLayer( |
| 91 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_1_mean.npy"), |
| 92 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_1_var.npy"), |
| 93 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_1_gamma.npy"), |
| 94 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_1_beta.npy"), |
| 95 | 0.000001f) |
| 96 | .set_name("conv2d_1/BatchNorm") |
| 97 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_1/LeakyRelu") |
| 98 | |
| 99 | // Layer 2 |
| 100 | << ConvolutionLayer( |
| 101 | 3U, 3U, 64U, |
| 102 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_2_w.npy", weights_layout), |
| 103 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 104 | PadStrideInfo(2, 2, 1, 1)) |
| 105 | .set_name("conv2d_2") |
| 106 | << BatchNormalizationLayer( |
| 107 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_2_mean.npy"), |
| 108 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_2_var.npy"), |
| 109 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_2_gamma.npy"), |
| 110 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_2_beta.npy"), |
| 111 | 0.000001f) |
| 112 | .set_name("conv2d_2/BatchNorm") |
| 113 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_2/LeakyRelu"); |
| 114 | darknet53_block(data_path, "3", weights_layout, 32U); |
| 115 | graph << ConvolutionLayer( |
| 116 | 3U, 3U, 128U, |
| 117 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_5_w.npy", weights_layout), |
| 118 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 119 | PadStrideInfo(2, 2, 1, 1)) |
| 120 | .set_name("conv2d_5") |
| 121 | << BatchNormalizationLayer( |
| 122 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_5_mean.npy"), |
| 123 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_5_var.npy"), |
| 124 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_5_gamma.npy"), |
| 125 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_5_beta.npy"), |
| 126 | 0.000001f) |
| 127 | .set_name("conv2d_5/BatchNorm") |
| 128 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_5/LeakyRelu"); |
| 129 | darknet53_block(data_path, "6", weights_layout, 64U); |
| 130 | darknet53_block(data_path, "8", weights_layout, 64U); |
| 131 | graph << ConvolutionLayer( |
| 132 | 3U, 3U, 256U, |
| 133 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_10_w.npy", weights_layout), |
| 134 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 135 | PadStrideInfo(2, 2, 1, 1)) |
| 136 | .set_name("conv2d_10") |
| 137 | << BatchNormalizationLayer( |
| 138 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_10_mean.npy"), |
| 139 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_10_var.npy"), |
| 140 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_10_gamma.npy"), |
| 141 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_10_beta.npy"), |
| 142 | 0.000001f) |
| 143 | .set_name("conv2d_10/BatchNorm") |
| 144 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_10/LeakyRelu"); |
| 145 | darknet53_block(data_path, "11", weights_layout, 128U); |
| 146 | darknet53_block(data_path, "13", weights_layout, 128U); |
| 147 | darknet53_block(data_path, "15", weights_layout, 128U); |
| 148 | darknet53_block(data_path, "17", weights_layout, 128U); |
| 149 | darknet53_block(data_path, "19", weights_layout, 128U); |
| 150 | darknet53_block(data_path, "21", weights_layout, 128U); |
| 151 | darknet53_block(data_path, "23", weights_layout, 128U); |
| 152 | darknet53_block(data_path, "25", weights_layout, 128U); |
| 153 | graph << ConvolutionLayer( |
| 154 | 3U, 3U, 512U, |
| 155 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_27_w.npy", weights_layout), |
| 156 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 157 | PadStrideInfo(2, 2, 1, 1)) |
| 158 | .set_name("conv2d_27") |
| 159 | << BatchNormalizationLayer( |
| 160 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_27_mean.npy"), |
| 161 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_27_var.npy"), |
| 162 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_27_gamma.npy"), |
| 163 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_27_beta.npy"), |
| 164 | 0.000001f) |
| 165 | .set_name("conv2d_27/BatchNorm") |
| 166 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_27/LeakyRelu"); |
| 167 | darknet53_block(data_path, "28", weights_layout, 256U); |
| 168 | darknet53_block(data_path, "30", weights_layout, 256U); |
| 169 | darknet53_block(data_path, "32", weights_layout, 256U); |
| 170 | darknet53_block(data_path, "34", weights_layout, 256U); |
| 171 | darknet53_block(data_path, "36", weights_layout, 256U); |
| 172 | darknet53_block(data_path, "38", weights_layout, 256U); |
| 173 | darknet53_block(data_path, "40", weights_layout, 256U); |
| 174 | darknet53_block(data_path, "42", weights_layout, 256U); |
| 175 | graph << ConvolutionLayer( |
| 176 | 3U, 3U, 1024U, |
| 177 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_44_w.npy", weights_layout), |
| 178 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 179 | PadStrideInfo(2, 2, 1, 1)) |
| 180 | .set_name("conv2d_44") |
| 181 | << BatchNormalizationLayer( |
| 182 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_44_mean.npy"), |
| 183 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_44_var.npy"), |
| 184 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_44_gamma.npy"), |
| 185 | get_weights_accessor(data_path, "/cnn_data/yolov3_model/batch_normalization_44_beta.npy"), |
| 186 | 0.000001f) |
| 187 | .set_name("conv2d_44/BatchNorm") |
| 188 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d_44/LeakyRelu"); |
| 189 | darknet53_block(data_path, "45", weights_layout, 512U); |
| 190 | darknet53_block(data_path, "47", weights_layout, 512U); |
| 191 | darknet53_block(data_path, "49", weights_layout, 512U); |
| 192 | darknet53_block(data_path, "51", weights_layout, 512U); |
| 193 | graph << OutputLayer(get_output_accessor(common_params, 5)); |
| 194 | |
| 195 | // Finalize graph |
| 196 | GraphConfig config; |
| 197 | config.num_threads = common_params.threads; |
| 198 | config.use_tuner = common_params.enable_tuner; |
| 199 | config.tuner_file = common_params.tuner_file; |
| 200 | |
| 201 | graph.finalize(common_params.target, config); |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | void do_run() override |
| 206 | { |
| 207 | // Run graph |
| 208 | graph.run(); |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | CommandLineParser cmd_parser; |
| 213 | CommonGraphOptions common_opts; |
| 214 | CommonGraphParams common_params; |
| 215 | Stream graph; |
| 216 | |
| 217 | void darknet53_block(const std::string &data_path, std::string &¶m_path, DataLayout weights_layout, |
| 218 | unsigned int filter_size) |
| 219 | { |
| 220 | std::string total_path = "/cnn_data/yolov3_model/"; |
| 221 | std::string param_path2 = std::to_string(std::stoi(param_path) + 1); |
| 222 | SubStream i_a(graph); |
| 223 | SubStream i_b(graph); |
| 224 | i_a << ConvolutionLayer( |
| 225 | 1U, 1U, filter_size, |
| 226 | get_weights_accessor(data_path, total_path + "conv2d_" + param_path + "_w.npy", weights_layout), |
| 227 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 228 | PadStrideInfo(1, 1, 0, 0)) |
| 229 | << BatchNormalizationLayer( |
| 230 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path + "_mean.npy"), |
| 231 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path + "_var.npy"), |
| 232 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path + "_gamma.npy"), |
| 233 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path + "_beta.npy"), |
| 234 | 0.000001f) |
| 235 | .set_name("conv2d" + param_path + "/BatchNorm") |
| 236 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d" + param_path + "/LeakyRelu") |
| 237 | << ConvolutionLayer( |
| 238 | 3U, 3U, filter_size * 2, |
| 239 | get_weights_accessor(data_path, total_path + "conv2d_" + param_path2 + "_w.npy", weights_layout), |
| 240 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 241 | PadStrideInfo(1, 1, 1, 1)) |
| 242 | << BatchNormalizationLayer( |
| 243 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path2 + "_mean.npy"), |
| 244 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path2 + "_var.npy"), |
| 245 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path2 + "_gamma.npy"), |
| 246 | get_weights_accessor(data_path, total_path + "batch_normalization_" + param_path2 + "_beta.npy"), |
| 247 | 0.000001f) |
| 248 | .set_name("conv2d" + param_path2 + "/BatchNorm") |
| 249 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LEAKY_RELU, 0.1f)).set_name("conv2d" + param_path2 + "/LeakyRelu"); |
| 250 | |
| 251 | graph << EltwiseLayer(std::move(i_a), std::move(i_b), EltwiseOperation::Add); |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | /** Main program for YOLOv3 |
| 256 | * |
| 257 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 258 | * |
| 259 | * @param[in] argc Number of arguments |
| 260 | * @param[in] argv Arguments |
| 261 | * |
| 262 | * @return Return code |
| 263 | */ |
| 264 | int main(int argc, char **argv) |
| 265 | { |
| 266 | return arm_compute::utils::run_example<GraphYOLOv3Example>(argc, argv); |
| 267 | } |