blob: 8aca0fa45b21c8cd28d034441faa3bdd5fa77630 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Anthony Barbier46edf632018-01-26 14:27:15 +00002 * Copyright (c) 2017-2018 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"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Anthony Barbier2a07e182017-08-04 18:20:27 +010026#include "support/ToolchainSupport.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30#include <cstdlib>
Anthony Barbier2a07e182017-08-04 18:20:27 +010031
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033using namespace arm_compute::graph::frontend;
Anthony Barbier2a07e182017-08-04 18:20:27 +010034using namespace arm_compute::graph_utils;
35
Anthony Barbier2a07e182017-08-04 18:20:27 +010036/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API
37 *
38 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000039 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches )
Anthony Barbier2a07e182017-08-04 18:20:27 +010040 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041class GraphLenetExample : public Example
Anthony Barbier2a07e182017-08-04 18:20:27 +010042{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043public:
44 void do_setup(int argc, char **argv) override
45 {
46 std::string data_path; /** Path to the trainable data */
47 unsigned int batches = 4; /** Number of batches */
Anthony Barbier2a07e182017-08-04 18:20:27 +010048
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000049 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000050 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010051 Target target_hint = set_target_hint(target);
Gian Marcobfa3b522017-12-12 10:08:38 +000052
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000053 // Parse arguments
54 if(argc < 2)
55 {
56 // Print help
57 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [batches]\n\n";
58 std::cout << "No data folder provided: using random values\n\n";
59 }
60 else if(argc == 2)
61 {
62 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [batches]\n\n";
63 std::cout << "No data folder provided: using random values\n\n";
64 }
65 else if(argc == 3)
66 {
67 //Do something with argv[1]
68 data_path = argv[2];
69 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
70 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
71 }
72 else
73 {
74 //Do something with argv[1] and argv[2]
75 data_path = argv[2];
76 batches = std::strtol(argv[3], nullptr, 0);
77 }
78
79 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
80 graph << target_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +000081 << InputLayer(TensorDescriptor(TensorShape(28U, 28U, 1U, batches), DataType::F32), get_input_accessor(""))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000082 << ConvolutionLayer(
83 5U, 5U, 20U,
84 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"),
85 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
86 PadStrideInfo(1, 1, 0, 0))
87 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
88 << ConvolutionLayer(
89 5U, 5U, 50U,
90 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"),
91 get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
92 PadStrideInfo(1, 1, 0, 0))
93 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
94 << FullyConnectedLayer(
95 500U,
96 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"),
97 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
98 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
99 << FullyConnectedLayer(
100 10U,
101 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"),
102 get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
103 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104 << OutputLayer(get_output_accessor(""));
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;
108 config.use_function_memory_manager = true;
109 config.use_tuner = (target == 2);
110 graph.finalize(target_hint, config);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100111 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000112 void do_run() override
Anthony Barbier2a07e182017-08-04 18:20:27 +0100113 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114 // Run graph
115 graph.run();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116 }
117
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000118private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119 Stream graph{ 0, "LeNet" };
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000120};
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121
122/** Main program for LeNet
123 *
124 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000125 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches )
Anthony Barbier2a07e182017-08-04 18:20:27 +0100126 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000127int main(int argc, char **argv)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100128{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000129 return arm_compute::utils::run_example<GraphLenetExample>(argc, argv);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100130}