blob: eb66138df4f7d48f4a51066c018bbc18fcc85ffa [file] [log] [blame]
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +00001/*
2 * Copyright (c) 2019 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
25#include "arm_compute/graph.h"
26#include "support/ToolchainSupport.h"
27#include "utils/CommonGraphOptions.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31using namespace arm_compute;
32using namespace arm_compute::utils;
33using namespace arm_compute::graph::frontend;
34using namespace arm_compute::graph_utils;
35
36/** Example demonstrating how to implement Mnist's network using the Compute Library's graph API */
37class GraphMnistExample : public Example
38{
39public:
40 GraphMnistExample()
41 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "LeNet")
42 {
43 }
44 bool do_setup(int argc, char **argv) override
45 {
46 // Parse arguments
47 cmd_parser.parse(argc, argv);
48 cmd_parser.validate();
49
50 // Consume common parameters
51 common_params = consume_common_graph_parameters(common_opts);
52
53 // Return when help menu is requested
54 if(common_params.help)
55 {
56 cmd_parser.print_help(argv[0]);
57 return false;
58 }
59
60 // Print parameter values
61 std::cout << common_params << std::endl;
62
63 // Get trainable parameters data path
64 std::string data_path = common_params.data_path;
65
66 // Add model path to data path
67 if(!data_path.empty() && arm_compute::is_data_type_quantized_asymmetric(common_params.data_type))
68 {
69 data_path += "/cnn_data/mnist_qasymm8_model/";
70 }
71
72 // Create input descriptor
73 const TensorShape tensor_shape = permute_shape(TensorShape(28U, 28U, 1U), DataLayout::NCHW, common_params.data_layout);
74 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
75
76 const QuantizationInfo in_quant_info = QuantizationInfo(0.003921568859368563f, 0);
77
78 const std::vector<std::pair<QuantizationInfo, QuantizationInfo>> conv_quant_info =
79 {
80 { QuantizationInfo(0.004083447158336639f, 138), QuantizationInfo(0.0046257381327450275f, 0) }, // conv0
81 { QuantizationInfo(0.0048590428195893764f, 149), QuantizationInfo(0.03558270260691643f, 0) }, // conv1
82 { QuantizationInfo(0.004008443560451269f, 146), QuantizationInfo(0.09117382764816284f, 0) }, // conv2
83 { QuantizationInfo(0.004344311077147722f, 160), QuantizationInfo(0.5494495034217834f, 167) }, // fc
84 };
85
86 // Set weights trained layout
87 const DataLayout weights_layout = DataLayout::NHWC;
88 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo();
89 fc_info.set_weights_trained_layout(weights_layout);
90
91 graph << common_params.target
92 << common_params.fast_math_hint
93 << InputLayer(input_descriptor.set_quantization_info(in_quant_info),
94 get_input_accessor(common_params))
95 << ConvolutionLayer(
96 3U, 3U, 32U,
97 get_weights_accessor(data_path, "conv2d_weights_quant_FakeQuantWithMinMaxVars.npy", weights_layout),
98 get_weights_accessor(data_path, "conv2d_Conv2D_bias.npy"),
99 PadStrideInfo(1U, 1U, 1U, 1U), 1, conv_quant_info.at(0).first, conv_quant_info.at(0).second)
100 .set_name("Conv0")
101
102 << ConvolutionLayer(
103 3U, 3U, 32U,
104 get_weights_accessor(data_path, "conv2d_1_weights_quant_FakeQuantWithMinMaxVars.npy", weights_layout),
105 get_weights_accessor(data_path, "conv2d_1_Conv2D_bias.npy"),
106 PadStrideInfo(1U, 1U, 1U, 1U), 1, conv_quant_info.at(1).first, conv_quant_info.at(1).second)
107 .set_name("conv1")
108
109 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("maxpool1")
110
111 << ConvolutionLayer(
112 3U, 3U, 32U,
113 get_weights_accessor(data_path, "conv2d_2_weights_quant_FakeQuantWithMinMaxVars.npy", weights_layout),
114 get_weights_accessor(data_path, "conv2d_2_Conv2D_bias.npy"),
115 PadStrideInfo(1U, 1U, 1U, 1U), 1, conv_quant_info.at(2).first, conv_quant_info.at(2).second)
116 .set_name("conv2")
117
118 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("maxpool2")
119
120 << FullyConnectedLayer(
121 10U,
122 get_weights_accessor(data_path, "dense_weights_quant_FakeQuantWithMinMaxVars_transpose.npy", weights_layout),
123 get_weights_accessor(data_path, "dense_MatMul_bias.npy"),
124 fc_info, conv_quant_info.at(3).first, conv_quant_info.at(3).second)
125 .set_name("fc")
126
127 << SoftmaxLayer().set_name("prob");
128
129 if(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type))
130 {
131 graph << DequantizationLayer().set_name("dequantize");
132 }
133
134 graph << OutputLayer(get_output_accessor(common_params, 5));
135
136 // Finalize graph
137 GraphConfig config;
138 config.num_threads = common_params.threads;
139 config.use_tuner = common_params.enable_tuner;
140 config.tuner_mode = common_params.tuner_mode;
141 config.tuner_file = common_params.tuner_file;
142
143 graph.finalize(common_params.target, config);
144
145 return true;
146 }
147 void do_run() override
148 {
149 // Run graph
150 graph.run();
151 }
152
153private:
154 CommandLineParser cmd_parser;
155 CommonGraphOptions common_opts;
156 CommonGraphParams common_params;
157 Stream graph;
158};
159
160/** Main program for Mnist Example
161 *
162 * @note To list all the possible arguments execute the binary appended with the --help option
163 *
164 * @param[in] argc Number of arguments
165 * @param[in] argv Arguments
166 */
167int main(int argc, char **argv)
168{
169 return arm_compute::utils::run_example<GraphMnistExample>(argc, argv);
170}