blob: f6af14c4cfefe68c8c87466c3308755430e23ee3 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 */
Anthony Barbier2a07e182017-08-04 18:20:27 +010024
25#include "arm_compute/graph/Graph.h"
26#include "arm_compute/graph/Nodes.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010027#include "support/ToolchainSupport.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31#include <cstdlib>
Anthony Barbier2a07e182017-08-04 18:20:27 +010032
33using namespace arm_compute::graph;
34using namespace arm_compute::graph_utils;
35
36/** Generates appropriate accessor according to the specified path
37 *
38 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
39 *
40 * @param path Path to the data files
41 * @param data_file Relative path to the data files from path
42 *
43 * @return An appropriate tensor accessor
44 */
45std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
46{
47 if(path.empty())
48 {
49 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
50 }
51 else
52 {
53 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
54 }
55}
56
57/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API
58 *
59 * @param[in] argc Number of arguments
60 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
61 */
62void main_graph_lenet(int argc, const char **argv)
63{
64 std::string data_path; /** Path to the trainable data */
65 unsigned int batches = 4; /** Number of batches */
66
67 // Parse arguments
68 if(argc < 2)
69 {
70 // Print help
71 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
72 std::cout << "No data folder provided: using random values\n\n";
73 }
74 else if(argc == 2)
75 {
76 //Do something with argv[1]
77 data_path = argv[1];
78 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
79 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
80 }
81 else
82 {
83 //Do something with argv[1] and argv[2]
84 data_path = argv[1];
85 batches = std::strtol(argv[2], nullptr, 0);
86 }
87
88 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +010089 TargetHint hint = TargetHint::NEON;
Isabella Gottardib28f29d2017-11-09 17:05:07 +000090 if(Graph::opencl_is_available())
Anthony Barbier2a07e182017-08-04 18:20:27 +010091 {
Georgios Pinitasff421f22017-10-04 16:53:58 +010092 hint = TargetHint::OPENCL;
Anthony Barbier2a07e182017-08-04 18:20:27 +010093 }
94
95 Graph graph;
Anthony Barbier2a07e182017-08-04 18:20:27 +010096
97 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Gian Marco Iodice1914db72017-09-29 12:05:49 +010098 graph << hint
Anthony Barbier2a07e182017-08-04 18:20:27 +010099 << Tensor(TensorInfo(TensorShape(28U, 28U, 1U, batches), 1, DataType::F32), DummyAccessor())
100 << ConvolutionLayer(
101 5U, 5U, 20U,
102 get_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"),
103 get_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
104 PadStrideInfo(1, 1, 0, 0))
105 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
106 << ConvolutionLayer(
107 5U, 5U, 50U,
108 get_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"),
109 get_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
110 PadStrideInfo(1, 1, 0, 0))
111 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
112 << FullyConnectedLayer(
113 500U,
114 get_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"),
115 get_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
117 << FullyConnectedLayer(
118 10U,
119 get_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"),
120 get_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
121 << SoftmaxLayer()
122 << Tensor(DummyAccessor());
123
124 graph.run();
125}
126
127/** Main program for LeNet
128 *
129 * @param[in] argc Number of arguments
130 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
131 */
132int main(int argc, const char **argv)
133{
134 return arm_compute::utils::run_example(argc, argv, main_graph_lenet);
135}