blob: 2801209985fadc0e7746e40e88ad8250ab966779 [file] [log] [blame]
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00001/*
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010029#include <cstdlib>
30
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010031using namespace arm_compute;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032using namespace arm_compute::utils;
33using namespace arm_compute::graph::frontend;
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000034using namespace arm_compute::graph_utils;
35
36/** Example demonstrating how to implement QASYMM8 MobileNet's network using the Compute Library's graph API
37 *
38 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +010039 * @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, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000040 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010041class GraphMobileNetQASYMM8Example : public Example
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000042{
43public:
44 void do_setup(int argc, char **argv) override
45 {
46 std::string data_path; /* Path to the trainable data */
47 std::string input; /* Image data */
48 std::string label; /* Label data */
49
50 // Quantization info taken from the AndroidNN QASYMM8 MobileNet example
51 const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128);
52 const QuantizationInfo mid_quant_info = QuantizationInfo(0.0784313753247f, 128);
53
54 const std::vector<QuantizationInfo> conv_weights_quant_info =
55 {
56 QuantizationInfo(0.031778190285f, 156), // conv0
57 QuantizationInfo(0.00604454148561f, 66) // conv14
58 };
59
60 const std::vector<QuantizationInfo> depth_weights_quant_info =
61 {
62 QuantizationInfo(0.254282623529f, 129), // dwsc1
63 QuantizationInfo(0.12828284502f, 172), // dwsc2
64 QuantizationInfo(0.265911251307f, 83), // dwsc3
65 QuantizationInfo(0.0985597148538f, 30), // dwsc4
66 QuantizationInfo(0.0631204470992f, 54), // dwsc5
67 QuantizationInfo(0.0137207424268f, 141), // dwsc6
68 QuantizationInfo(0.0817828401923f, 125), // dwsc7
69 QuantizationInfo(0.0393880493939f, 164), // dwsc8
70 QuantizationInfo(0.211694166064f, 129), // dwsc9
71 QuantizationInfo(0.158015936613f, 103), // dwsc10
72 QuantizationInfo(0.0182712618262f, 137), // dwsc11
73 QuantizationInfo(0.0127998134121f, 134), // dwsc12
74 QuantizationInfo(0.299285322428f, 161) // dwsc13
75 };
76
77 const std::vector<QuantizationInfo> point_weights_quant_info =
78 {
79 QuantizationInfo(0.0425766184926f, 129), // dwsc1
80 QuantizationInfo(0.0250773020089f, 94), // dwsc2
81 QuantizationInfo(0.015851572156f, 93), // dwsc3
82 QuantizationInfo(0.0167811904103f, 98), // dwsc4
83 QuantizationInfo(0.00951790809631f, 135), // dwsc5
84 QuantizationInfo(0.00999817531556f, 128), // dwsc6
85 QuantizationInfo(0.00590536883101f, 126), // dwsc7
86 QuantizationInfo(0.00576109671965f, 133), // dwsc8
87 QuantizationInfo(0.00830461271107f, 142), // dwsc9
88 QuantizationInfo(0.0152327232063f, 72), // dwsc10
89 QuantizationInfo(0.00741417845711f, 125), // dwsc11
90 QuantizationInfo(0.0135628981516f, 142), // dwsc12
91 QuantizationInfo(0.0338749065995f, 140) // dwsc13
92 };
93
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000094 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Giorgio Arena59631a12018-05-02 13:59:04 +010095 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
96 Target target_hint = set_target_hint(target);
97 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000098
Giorgio Arenaa66eaa22017-12-21 19:50:06 +000099 // Parse arguments
100 if(argc < 2)
101 {
102 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +0100103 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [npy_input] [labels] [fast_math_hint]\n\n";
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000104 std::cout << "No data folder provided: using random values\n\n";
105 }
106 else if(argc == 2)
107 {
Giorgio Arena59631a12018-05-02 13:59:04 +0100108 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [npy_input] [labels] [fast_math_hint]\n\n";
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000109 std::cout << "No input provided: using random values\n\n";
110 }
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000111 else if(argc == 4)
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000112 {
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000113 data_path = argv[2];
114 input = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +0100115 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000116 std::cout << "No text file with labels provided: skipping output accessor\n\n";
117 }
Giorgio Arena59631a12018-05-02 13:59:04 +0100118 else if(argc == 5)
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000119 {
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000120 data_path = argv[2];
121 input = argv[3];
122 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +0100123 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
124 std::cout << "No fast math info provided: disabling fast math\n\n";
125 }
126 else
127 {
128 data_path = argv[2];
129 input = argv[3];
130 label = argv[4];
131 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000132 }
133
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000134 graph << target_hint
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100135 << DepthwiseConvolutionMethod::OPTIMIZED_3x3 // FIXME(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method
Giorgio Arena59631a12018-05-02 13:59:04 +0100136 << fast_math_hint
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100137 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::QASYMM8, in_quant_info),
138 get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/" + input))
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000139 << ConvolutionLayer(
140 3U, 3U, 32U,
141 get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_weights.npy"),
142 get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_bias.npy"),
143 PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR),
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100144 1, conv_weights_quant_info.at(0), mid_quant_info)
145 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f));
146 graph << 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));
147 graph << 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),
148 point_weights_quant_info.at(1));
149 graph << 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),
150 point_weights_quant_info.at(2));
151 graph << 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),
152 point_weights_quant_info.at(3));
153 graph << 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),
154 point_weights_quant_info.at(4));
155 graph << 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),
156 point_weights_quant_info.at(5));
157 graph << 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),
158 point_weights_quant_info.at(6));
159 graph << 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),
160 point_weights_quant_info.at(7));
161 graph << 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),
162 point_weights_quant_info.at(8));
163 graph << 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),
164 point_weights_quant_info.at(9));
165 graph << 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),
166 point_weights_quant_info.at(10));
167 graph << 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),
168 point_weights_quant_info.at(11));
169 graph << 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),
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000170 point_weights_quant_info.at(12))
171 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
172 << ConvolutionLayer(
173 1U, 1U, 1001U,
174 get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_weights.npy"),
175 get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_bias.npy"),
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100176 PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1))
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000177 << ReshapeLayer(TensorShape(1001U))
178 << SoftmaxLayer()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100179 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000180
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100181 // Finalize graph
182 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100183 config.use_tuner = (target == 2);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100184 graph.finalize(target_hint, config);
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000185 }
186 void do_run() override
187 {
188 // Run graph
189 graph.run();
190 }
191
192private:
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100193 Stream graph{ 0, "MobileNetV1_QASYMM8" };
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000194
195 /** This function produces a depthwise separable convolution node (i.e. depthwise + pointwise layers) with ReLU6 activation after each layer.
196 *
197 * @param[in] data_path Path to trainable data folder
198 * @param[in] param_path Prefix of specific set of weights/biases data
199 * @param[in] conv_filt Filters depths for pointwise convolution
200 * @param[in] dwc_pad_stride_info PadStrideInfo for depthwise convolution
201 * @param[in] conv_pad_stride_info PadStrideInfo for pointwise convolution
202 * @param[in] depth_weights_quant_info QuantizationInfo for depthwise convolution's weights
203 * @param[in] point_weights_quant_info QuantizationInfo for pointwise convolution's weights
204 *
205 * @return The complete dwsc node
206 */
207 BranchLayer get_dwsc_node(const std::string &data_path, std::string &&param_path,
208 const unsigned int conv_filt,
209 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info,
210 QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info)
211 {
212 std::string total_path = "/cnn_data/mobilenet_qasymm8_model/" + param_path + "_";
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100213 SubStream sg(graph);
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000214
215 sg << DepthwiseConvolutionLayer(
216 3U, 3U,
217 get_weights_accessor(data_path, total_path + "depthwise_weights.npy"),
218 get_weights_accessor(data_path, total_path + "depthwise_bias.npy"),
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100219 dwc_pad_stride_info, depth_weights_quant_info)
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000220 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f))
221 << ConvolutionLayer(
222 1U, 1U, conv_filt,
223 get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
224 get_weights_accessor(data_path, total_path + "pointwise_bias.npy"),
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100225 conv_pad_stride_info, 1, point_weights_quant_info)
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000226 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f));
227
228 return BranchLayer(std::move(sg));
229 }
230};
231/** Main program for MobileNetQASYMM8
232 *
233 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +0100234 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] npy_input, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000235 */
236int main(int argc, char **argv)
237{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100238 return arm_compute::utils::run_example<GraphMobileNetQASYMM8Example>(argc, argv);
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000239}