blob: 6b9f302312f4021e3ed5c82b70ad899638ce34d1 [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
Kaizen8938bd32017-09-28 14:38:23 +010034/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API
35 *
36 * @param[in] argc Number of arguments
Jenkinsb3a371b2018-05-23 11:36:53 +010037 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Kaizen8938bd32017-09-28 14:38:23 +010038 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000039class GraphLenetExample : public Example
Kaizen8938bd32017-09-28 14:38:23 +010040{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000041public:
Jenkins52ba29e2018-08-29 15:32:11 +000042 GraphLenetExample()
43 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "LeNet")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000044 {
Jenkins52ba29e2018-08-29 15:32:11 +000045 }
46 bool do_setup(int argc, char **argv) override
47 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000048 // Parse arguments
Jenkins52ba29e2018-08-29 15:32:11 +000049 cmd_parser.parse(argc, argv);
50
51 // Consume common parameters
52 common_params = consume_common_graph_parameters(common_opts);
53
54 // Return when help menu is requested
55 if(common_params.help)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000056 {
Jenkins52ba29e2018-08-29 15:32:11 +000057 cmd_parser.print_help(argv[0]);
58 return false;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000059 }
Jenkins52ba29e2018-08-29 15:32:11 +000060
61 // Checks
62 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
63
64 // Print parameter values
65 std::cout << common_params << std::endl;
66
67 // Get trainable parameters data path
68 std::string data_path = common_params.data_path;
69 unsigned int batches = 4; /** Number of batches */
70
71 // Create input descriptor
72 const TensorShape tensor_shape = permute_shape(TensorShape(28U, 28U, 1U, batches), DataLayout::NCHW, common_params.data_layout);
73 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
74
75 // Set weights trained layout
76 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000077
78 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Jenkins52ba29e2018-08-29 15:32:11 +000079 graph << common_params.target
80 << common_params.fast_math_hint
81 << InputLayer(input_descriptor, get_input_accessor(common_params))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000082 << ConvolutionLayer(
83 5U, 5U, 20U,
Jenkins52ba29e2018-08-29 15:32:11 +000084 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000085 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
86 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010087 .set_name("conv1")
88 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089 << ConvolutionLayer(
90 5U, 5U, 50U,
Jenkins52ba29e2018-08-29 15:32:11 +000091 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000092 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
93 PadStrideInfo(1, 1, 0, 0))
Jenkinsb3a371b2018-05-23 11:36:53 +010094 .set_name("conv2")
95 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096 << FullyConnectedLayer(
97 500U,
Jenkins52ba29e2018-08-29 15:32:11 +000098 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000099 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100100 .set_name("ip1")
101 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000102 << FullyConnectedLayer(
103 10U,
Jenkins52ba29e2018-08-29 15:32:11 +0000104 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000105 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100106 .set_name("ip2")
107 << SoftmaxLayer().set_name("prob")
Jenkins52ba29e2018-08-29 15:32:11 +0000108 << OutputLayer(get_output_accessor(common_params));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109
Jenkinsb3a371b2018-05-23 11:36:53 +0100110 // Finalize graph
111 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000112 config.num_threads = common_params.threads;
113 config.use_tuner = common_params.enable_tuner;
114 config.tuner_file = common_params.tuner_file;
115
116 graph.finalize(common_params.target, config);
117
118 return true;
Kaizen8938bd32017-09-28 14:38:23 +0100119 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000120 void do_run() override
Kaizen8938bd32017-09-28 14:38:23 +0100121 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000122 // Run graph
123 graph.run();
Kaizen8938bd32017-09-28 14:38:23 +0100124 }
125
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000126private:
Jenkins52ba29e2018-08-29 15:32:11 +0000127 CommandLineParser cmd_parser;
128 CommonGraphOptions common_opts;
129 CommonGraphParams common_params;
130 Stream graph;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000131};
Kaizen8938bd32017-09-28 14:38:23 +0100132
133/** Main program for LeNet
134 *
Jenkins52ba29e2018-08-29 15:32:11 +0000135 * @note To list all the possible arguments execute the binary appended with the --help option
136 *
Kaizen8938bd32017-09-28 14:38:23 +0100137 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000138 * @param[in] argv Arguments
Kaizen8938bd32017-09-28 14:38:23 +0100139 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000140int main(int argc, char **argv)
Kaizen8938bd32017-09-28 14:38:23 +0100141{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000142 return arm_compute::utils::run_example<GraphLenetExample>(argc, argv);
Kaizen8938bd32017-09-28 14:38:23 +0100143}