blob: 79cf12233e55991c6276692584bb0d92e01899b3 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 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);
46
47 // Consume common parameters
48 common_params = consume_common_graph_parameters(common_opts);
49
50 // Return when help menu is requested
51 if(common_params.help)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000052 {
Jenkins52ba29e2018-08-29 15:32:11 +000053 cmd_parser.print_help(argv[0]);
54 return false;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000055 }
Jenkins52ba29e2018-08-29 15:32:11 +000056
57 // Checks
58 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
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 unsigned int batches = 4; /** Number of batches */
66
67 // Create input descriptor
68 const TensorShape tensor_shape = permute_shape(TensorShape(28U, 28U, 1U, batches), DataLayout::NCHW, common_params.data_layout);
69 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
70
71 // Set weights trained layout
72 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000073
74 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Jenkins52ba29e2018-08-29 15:32:11 +000075 graph << common_params.target
76 << common_params.fast_math_hint
77 << InputLayer(input_descriptor, get_input_accessor(common_params))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000078 << ConvolutionLayer(
79 5U, 5U, 20U,
Jenkins52ba29e2018-08-29 15:32:11 +000080 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000081 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
82 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010083 .set_name("conv1")
84 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000085 << ConvolutionLayer(
86 5U, 5U, 50U,
Jenkins52ba29e2018-08-29 15:32:11 +000087 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000088 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
89 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010090 .set_name("conv2")
91 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000092 << FullyConnectedLayer(
93 500U,
Jenkins52ba29e2018-08-29 15:32:11 +000094 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000095 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +010096 .set_name("ip1")
97 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000098 << FullyConnectedLayer(
99 10U,
Jenkins52ba29e2018-08-29 15:32:11 +0000100 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000101 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100102 .set_name("ip2")
103 << SoftmaxLayer().set_name("prob")
Jenkins52ba29e2018-08-29 15:32:11 +0000104 << OutputLayer(get_output_accessor(common_params));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000105
Jenkinsb3a371b2018-05-23 11:36:53 +0100106 // Finalize graph
107 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000108 config.num_threads = common_params.threads;
109 config.use_tuner = common_params.enable_tuner;
110 config.tuner_file = common_params.tuner_file;
111
112 graph.finalize(common_params.target, config);
113
114 return true;
Kaizen8938bd32017-09-28 14:38:23 +0100115 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000116 void do_run() override
Kaizen8938bd32017-09-28 14:38:23 +0100117 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000118 // Run graph
119 graph.run();
Kaizen8938bd32017-09-28 14:38:23 +0100120 }
121
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000122private:
Jenkins52ba29e2018-08-29 15:32:11 +0000123 CommandLineParser cmd_parser;
124 CommonGraphOptions common_opts;
125 CommonGraphParams common_params;
126 Stream graph;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000127};
Kaizen8938bd32017-09-28 14:38:23 +0100128
129/** Main program for LeNet
130 *
Jenkins52ba29e2018-08-29 15:32:11 +0000131 * @note To list all the possible arguments execute the binary appended with the --help option
132 *
Kaizen8938bd32017-09-28 14:38:23 +0100133 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000134 * @param[in] argv Arguments
Kaizen8938bd32017-09-28 14:38:23 +0100135 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000136int main(int argc, char **argv)
Kaizen8938bd32017-09-28 14:38:23 +0100137{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000138 return arm_compute::utils::run_example<GraphLenetExample>(argc, argv);
Kaizen8938bd32017-09-28 14:38:23 +0100139}