Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +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 | */ |
| 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
| 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | using namespace arm_compute::graph; |
| 32 | using namespace arm_compute::graph_utils; |
| 33 | |
| 34 | /** Example demonstrating how to implement QASYMM8 MobileNet's network using the Compute Library's graph API |
| 35 | * |
| 36 | * @param[in] argc Number of arguments |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 37 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] npy_input, [optional] labels ) |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 38 | */ |
| 39 | class GraphMobileNetQASYMM8Example : public utils::Example |
| 40 | { |
| 41 | public: |
| 42 | void do_setup(int argc, char **argv) override |
| 43 | { |
| 44 | std::string data_path; /* Path to the trainable data */ |
| 45 | std::string input; /* Image data */ |
| 46 | std::string label; /* Label data */ |
| 47 | |
| 48 | // Quantization info taken from the AndroidNN QASYMM8 MobileNet example |
| 49 | const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128); |
| 50 | const QuantizationInfo mid_quant_info = QuantizationInfo(0.0784313753247f, 128); |
| 51 | |
| 52 | const std::vector<QuantizationInfo> conv_weights_quant_info = |
| 53 | { |
| 54 | QuantizationInfo(0.031778190285f, 156), // conv0 |
| 55 | QuantizationInfo(0.00604454148561f, 66) // conv14 |
| 56 | }; |
| 57 | |
| 58 | const std::vector<QuantizationInfo> depth_weights_quant_info = |
| 59 | { |
| 60 | QuantizationInfo(0.254282623529f, 129), // dwsc1 |
| 61 | QuantizationInfo(0.12828284502f, 172), // dwsc2 |
| 62 | QuantizationInfo(0.265911251307f, 83), // dwsc3 |
| 63 | QuantizationInfo(0.0985597148538f, 30), // dwsc4 |
| 64 | QuantizationInfo(0.0631204470992f, 54), // dwsc5 |
| 65 | QuantizationInfo(0.0137207424268f, 141), // dwsc6 |
| 66 | QuantizationInfo(0.0817828401923f, 125), // dwsc7 |
| 67 | QuantizationInfo(0.0393880493939f, 164), // dwsc8 |
| 68 | QuantizationInfo(0.211694166064f, 129), // dwsc9 |
| 69 | QuantizationInfo(0.158015936613f, 103), // dwsc10 |
| 70 | QuantizationInfo(0.0182712618262f, 137), // dwsc11 |
| 71 | QuantizationInfo(0.0127998134121f, 134), // dwsc12 |
| 72 | QuantizationInfo(0.299285322428f, 161) // dwsc13 |
| 73 | }; |
| 74 | |
| 75 | const std::vector<QuantizationInfo> point_weights_quant_info = |
| 76 | { |
| 77 | QuantizationInfo(0.0425766184926f, 129), // dwsc1 |
| 78 | QuantizationInfo(0.0250773020089f, 94), // dwsc2 |
| 79 | QuantizationInfo(0.015851572156f, 93), // dwsc3 |
| 80 | QuantizationInfo(0.0167811904103f, 98), // dwsc4 |
| 81 | QuantizationInfo(0.00951790809631f, 135), // dwsc5 |
| 82 | QuantizationInfo(0.00999817531556f, 128), // dwsc6 |
| 83 | QuantizationInfo(0.00590536883101f, 126), // dwsc7 |
| 84 | QuantizationInfo(0.00576109671965f, 133), // dwsc8 |
| 85 | QuantizationInfo(0.00830461271107f, 142), // dwsc9 |
| 86 | QuantizationInfo(0.0152327232063f, 72), // dwsc10 |
| 87 | QuantizationInfo(0.00741417845711f, 125), // dwsc11 |
| 88 | QuantizationInfo(0.0135628981516f, 142), // dwsc12 |
| 89 | QuantizationInfo(0.0338749065995f, 140) // dwsc13 |
| 90 | }; |
| 91 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 92 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
| 93 | const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 94 | TargetHint target_hint = set_target_hint(int_target_hint); |
| 95 | |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 96 | // Parse arguments |
| 97 | if(argc < 2) |
| 98 | { |
| 99 | // Print help |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 100 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [npy_input] [labels]\n\n"; |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 101 | std::cout << "No data folder provided: using random values\n\n"; |
| 102 | } |
| 103 | else if(argc == 2) |
| 104 | { |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 105 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [npy_input] [labels]\n\n"; |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 106 | std::cout << "No input provided: using random values\n\n"; |
| 107 | } |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 108 | else if(argc == 4) |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 109 | { |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 110 | data_path = argv[2]; |
| 111 | input = argv[3]; |
| 112 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 113 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 114 | } |
| 115 | else |
| 116 | { |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 117 | data_path = argv[2]; |
| 118 | input = argv[3]; |
| 119 | label = argv[4]; |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 122 | graph << target_hint |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 123 | << arm_compute::graph::Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::QASYMM8, in_quant_info), |
| 124 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/" + input)) |
| 125 | << ConvolutionLayer( |
| 126 | 3U, 3U, 32U, |
| 127 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_weights.npy"), |
| 128 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_bias.npy"), |
| 129 | PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), |
| 130 | 1, WeightsInfo(), |
| 131 | conv_weights_quant_info.at(0), |
| 132 | mid_quant_info) |
| 133 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 134 | << get_dwsc_node(data_path, "Conv2d_1", 64U, PadStrideInfo(1U, 1U, 1U, 1U), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(0), point_weights_quant_info.at(0)) |
| 135 | << get_dwsc_node(data_path, "Conv2d_2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(1), |
| 136 | point_weights_quant_info.at(1)) |
| 137 | << get_dwsc_node(data_path, "Conv2d_3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(2), |
| 138 | point_weights_quant_info.at(2)) |
| 139 | << get_dwsc_node(data_path, "Conv2d_4", 256U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(3), |
| 140 | point_weights_quant_info.at(3)) |
| 141 | << get_dwsc_node(data_path, "Conv2d_5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(4), |
| 142 | point_weights_quant_info.at(4)) |
| 143 | << get_dwsc_node(data_path, "Conv2d_6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(5), |
| 144 | point_weights_quant_info.at(5)) |
| 145 | << get_dwsc_node(data_path, "Conv2d_7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(6), |
| 146 | point_weights_quant_info.at(6)) |
| 147 | << get_dwsc_node(data_path, "Conv2d_8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(7), |
| 148 | point_weights_quant_info.at(7)) |
| 149 | << get_dwsc_node(data_path, "Conv2d_9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(8), |
| 150 | point_weights_quant_info.at(8)) |
| 151 | << get_dwsc_node(data_path, "Conv2d_10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(9), |
| 152 | point_weights_quant_info.at(9)) |
| 153 | << get_dwsc_node(data_path, "Conv2d_11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(10), |
| 154 | point_weights_quant_info.at(10)) |
| 155 | << get_dwsc_node(data_path, "Conv2d_12", 1024U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(11), |
| 156 | point_weights_quant_info.at(11)) |
| 157 | << get_dwsc_node(data_path, "Conv2d_13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(12), |
| 158 | point_weights_quant_info.at(12)) |
| 159 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) |
| 160 | << ConvolutionLayer( |
| 161 | 1U, 1U, 1001U, |
| 162 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_weights.npy"), |
| 163 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_bias.npy"), |
| 164 | PadStrideInfo(1U, 1U, 0U, 0U), 1, WeightsInfo(), conv_weights_quant_info.at(1)) |
| 165 | << ReshapeLayer(TensorShape(1001U)) |
| 166 | << SoftmaxLayer() |
| 167 | << arm_compute::graph::Tensor(get_output_accessor(label, 5)); |
Gian Marco | c1b6e37 | 2018-02-21 18:03:26 +0000 | [diff] [blame] | 168 | |
| 169 | // In order to enable the OpenCL tuner, graph_init() has to be called only when all nodes have been instantiated |
| 170 | graph.graph_init(int_target_hint == 2); |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 171 | } |
| 172 | void do_run() override |
| 173 | { |
| 174 | // Run graph |
| 175 | graph.run(); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | Graph graph{}; |
| 180 | |
| 181 | /** This function produces a depthwise separable convolution node (i.e. depthwise + pointwise layers) with ReLU6 activation after each layer. |
| 182 | * |
| 183 | * @param[in] data_path Path to trainable data folder |
| 184 | * @param[in] param_path Prefix of specific set of weights/biases data |
| 185 | * @param[in] conv_filt Filters depths for pointwise convolution |
| 186 | * @param[in] dwc_pad_stride_info PadStrideInfo for depthwise convolution |
| 187 | * @param[in] conv_pad_stride_info PadStrideInfo for pointwise convolution |
| 188 | * @param[in] depth_weights_quant_info QuantizationInfo for depthwise convolution's weights |
| 189 | * @param[in] point_weights_quant_info QuantizationInfo for pointwise convolution's weights |
| 190 | * |
| 191 | * @return The complete dwsc node |
| 192 | */ |
| 193 | BranchLayer get_dwsc_node(const std::string &data_path, std::string &¶m_path, |
| 194 | const unsigned int conv_filt, |
| 195 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info, |
| 196 | QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info) |
| 197 | { |
| 198 | std::string total_path = "/cnn_data/mobilenet_qasymm8_model/" + param_path + "_"; |
| 199 | SubGraph sg; |
| 200 | |
| 201 | sg << DepthwiseConvolutionLayer( |
| 202 | 3U, 3U, |
| 203 | get_weights_accessor(data_path, total_path + "depthwise_weights.npy"), |
| 204 | get_weights_accessor(data_path, total_path + "depthwise_bias.npy"), |
| 205 | dwc_pad_stride_info, |
| 206 | true, |
| 207 | depth_weights_quant_info) |
| 208 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 209 | << ConvolutionLayer( |
| 210 | 1U, 1U, conv_filt, |
| 211 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy"), |
| 212 | get_weights_accessor(data_path, total_path + "pointwise_bias.npy"), |
| 213 | conv_pad_stride_info, |
| 214 | 1, WeightsInfo(), |
| 215 | point_weights_quant_info) |
| 216 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)); |
| 217 | |
| 218 | return BranchLayer(std::move(sg)); |
| 219 | } |
| 220 | }; |
| 221 | /** Main program for MobileNetQASYMM8 |
| 222 | * |
| 223 | * @param[in] argc Number of arguments |
| 224 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] npy_input, [optional] labels ) |
| 225 | */ |
| 226 | int main(int argc, char **argv) |
| 227 | { |
| 228 | return utils::run_example<GraphMobileNetQASYMM8Example>(argc, argv); |
| 229 | } |