blob: c75a2f85262ae2803b0900b8e4522da1e88a8e99 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010025#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010026#include "utils/CommonGraphOptions.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000030using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031using namespace arm_compute::graph::frontend;
Anthony Barbier2a07e182017-08-04 18:20:27 +010032using namespace arm_compute::graph_utils;
33
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010034/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000035class GraphLenetExample : public Example
Anthony Barbier2a07e182017-08-04 18:20:27 +010036{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000037public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010038 GraphLenetExample()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "LeNet")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000040 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010041 }
42 bool do_setup(int argc, char **argv) override
43 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044 // Parse arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010045 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)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000052 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010053 cmd_parser.print_help(argv[0]);
54 return false;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000055 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010056
57 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010058 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010059
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 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000066
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010067 // 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;
73
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000074 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010075 graph << common_params.target
76 << common_params.fast_math_hint
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010077 << InputLayer(input_descriptor, get_input_accessor(common_params))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000078 << ConvolutionLayer(
79 5U, 5U, 20U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010080 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000081 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
82 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010083 .set_name("conv1")
84 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 << ConvolutionLayer(
86 5U, 5U, 50U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010087 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000088 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
89 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010090 .set_name("conv2")
91 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000092 << FullyConnectedLayer(
93 500U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010094 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000095 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010096 .set_name("ip1")
97 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000098 << FullyConnectedLayer(
99 10U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100100 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000101 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100102 .set_name("ip2")
103 << SoftmaxLayer().set_name("prob")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100104 << OutputLayer(get_output_accessor(common_params));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000105
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000106 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000107 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100108 config.num_threads = common_params.threads;
109 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100110 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100111 config.tuner_file = common_params.tuner_file;
112
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100113 graph.finalize(common_params.target, config);
114
115 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000117 void do_run() override
Anthony Barbier2a07e182017-08-04 18:20:27 +0100118 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000119 // Run graph
120 graph.run();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121 }
122
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000123private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100124 CommandLineParser cmd_parser;
125 CommonGraphOptions common_opts;
126 CommonGraphParams common_params;
127 Stream graph;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000128};
Anthony Barbier2a07e182017-08-04 18:20:27 +0100129
130/** Main program for LeNet
131 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100132 * @note To list all the possible arguments execute the binary appended with the --help option
133 *
Anthony Barbier2a07e182017-08-04 18:20:27 +0100134 * @param[in] argc Number of arguments
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100135 * @param[in] argv Arguments
Anthony Barbier2a07e182017-08-04 18:20:27 +0100136 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000137int main(int argc, char **argv)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100138{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000139 return arm_compute::utils::run_example<GraphLenetExample>(argc, argv);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100140}