blob: 9936ea52ae99685f53f74c13d10e972d04941d6b [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2017-2019 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +01003 *
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 */
Jenkinsb3a371b2018-05-23 11:36:53 +010024#include "arm_compute/graph.h"
Kaizen8938bd32017-09-28 14:38:23 +010025#include "support/ToolchainSupport.h"
Jenkins52ba29e2018-08-29 15:32:11 +000026#include "utils/CommonGraphOptions.h"
Kaizen8938bd32017-09-28 14:38:23 +010027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Anthony Barbierf45d5a92018-01-24 16:23:15 +000030using namespace arm_compute::utils;
Jenkinsb3a371b2018-05-23 11:36:53 +010031using namespace arm_compute::graph::frontend;
Kaizen8938bd32017-09-28 14:38:23 +010032using namespace arm_compute::graph_utils;
33
Jenkinsb9abeae2018-11-22 11:58:08 +000034/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000035class GraphLenetExample : public Example
Kaizen8938bd32017-09-28 14:38:23 +010036{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000037public:
Jenkins52ba29e2018-08-29 15:32:11 +000038 GraphLenetExample()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "LeNet")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000040 {
Jenkins52ba29e2018-08-29 15:32:11 +000041 }
42 bool do_setup(int argc, char **argv) override
43 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000044 // Parse arguments
Jenkins52ba29e2018-08-29 15:32:11 +000045 cmd_parser.parse(argc, argv);
Jenkins0e205f72019-11-28 16:53:35 +000046 cmd_parser.validate();
Jenkins52ba29e2018-08-29 15:32:11 +000047
48 // Consume common parameters
49 common_params = consume_common_graph_parameters(common_opts);
50
51 // Return when help menu is requested
52 if(common_params.help)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000053 {
Jenkins52ba29e2018-08-29 15:32:11 +000054 cmd_parser.print_help(argv[0]);
55 return false;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000056 }
Jenkins52ba29e2018-08-29 15:32:11 +000057
58 // Checks
59 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
60
61 // Print parameter values
62 std::cout << common_params << std::endl;
63
64 // Get trainable parameters data path
65 std::string data_path = common_params.data_path;
66 unsigned int batches = 4; /** Number of batches */
67
68 // Create input descriptor
69 const TensorShape tensor_shape = permute_shape(TensorShape(28U, 28U, 1U, batches), DataLayout::NCHW, common_params.data_layout);
70 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
71
72 // Set weights trained layout
73 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000074
75 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Jenkins52ba29e2018-08-29 15:32:11 +000076 graph << common_params.target
77 << common_params.fast_math_hint
78 << InputLayer(input_descriptor, get_input_accessor(common_params))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000079 << ConvolutionLayer(
80 5U, 5U, 20U,
Jenkins52ba29e2018-08-29 15:32:11 +000081 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000082 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
83 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010084 .set_name("conv1")
85 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000086 << ConvolutionLayer(
87 5U, 5U, 50U,
Jenkins52ba29e2018-08-29 15:32:11 +000088 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
90 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010091 .set_name("conv2")
92 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000093 << FullyConnectedLayer(
94 500U,
Jenkins52ba29e2018-08-29 15:32:11 +000095 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +010097 .set_name("ip1")
98 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000099 << FullyConnectedLayer(
100 10U,
Jenkins52ba29e2018-08-29 15:32:11 +0000101 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000102 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100103 .set_name("ip2")
104 << SoftmaxLayer().set_name("prob")
Jenkins52ba29e2018-08-29 15:32:11 +0000105 << OutputLayer(get_output_accessor(common_params));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000106
Jenkinsb3a371b2018-05-23 11:36:53 +0100107 // Finalize graph
108 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000109 config.num_threads = common_params.threads;
110 config.use_tuner = common_params.enable_tuner;
Jenkins4ba87db2019-05-23 17:11:51 +0100111 config.tuner_mode = common_params.tuner_mode;
Jenkins52ba29e2018-08-29 15:32:11 +0000112 config.tuner_file = common_params.tuner_file;
113
114 graph.finalize(common_params.target, config);
115
116 return true;
Kaizen8938bd32017-09-28 14:38:23 +0100117 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000118 void do_run() override
Kaizen8938bd32017-09-28 14:38:23 +0100119 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000120 // Run graph
121 graph.run();
Kaizen8938bd32017-09-28 14:38:23 +0100122 }
123
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000124private:
Jenkins52ba29e2018-08-29 15:32:11 +0000125 CommandLineParser cmd_parser;
126 CommonGraphOptions common_opts;
127 CommonGraphParams common_params;
128 Stream graph;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000129};
Kaizen8938bd32017-09-28 14:38:23 +0100130
131/** Main program for LeNet
132 *
Jenkins52ba29e2018-08-29 15:32:11 +0000133 * @note To list all the possible arguments execute the binary appended with the --help option
134 *
Kaizen8938bd32017-09-28 14:38:23 +0100135 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000136 * @param[in] argv Arguments
Kaizen8938bd32017-09-28 14:38:23 +0100137 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000138int main(int argc, char **argv)
Kaizen8938bd32017-09-28 14:38:23 +0100139{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000140 return arm_compute::utils::run_example<GraphLenetExample>(argc, argv);
Kaizen8938bd32017-09-28 14:38:23 +0100141}