blob: 1f04ff8a50f99701b6912e2c13e9b502aded20cc [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 */
24#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
28#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/Nodes.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/Scheduler.h"
32#include "support/ToolchainSupport.h"
33#include "utils/GraphUtils.h"
34#include "utils/Utils.h"
35
36#include <cstdlib>
37#include <iostream>
38#include <memory>
39
40using namespace arm_compute::graph;
41using namespace arm_compute::graph_utils;
42
43/** Generates appropriate accessor according to the specified path
44 *
45 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
46 *
47 * @param path Path to the data files
48 * @param data_file Relative path to the data files from path
49 *
50 * @return An appropriate tensor accessor
51 */
52std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
53{
54 if(path.empty())
55 {
56 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
57 }
58 else
59 {
60 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
61 }
62}
63
64/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API
65 *
66 * @param[in] argc Number of arguments
67 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
68 */
69void main_graph_lenet(int argc, const char **argv)
70{
71 std::string data_path; /** Path to the trainable data */
72 unsigned int batches = 4; /** Number of batches */
73
74 // Parse arguments
75 if(argc < 2)
76 {
77 // Print help
78 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
79 std::cout << "No data folder provided: using random values\n\n";
80 }
81 else if(argc == 2)
82 {
83 //Do something with argv[1]
84 data_path = argv[1];
85 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
86 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
87 }
88 else
89 {
90 //Do something with argv[1] and argv[2]
91 data_path = argv[1];
92 batches = std::strtol(argv[2], nullptr, 0);
93 }
94
95 // Check if OpenCL is available and initialize the scheduler
Gian Marco Iodice1914db72017-09-29 12:05:49 +010096 Hint hint = Hint::NEON;
Anthony Barbier2a07e182017-08-04 18:20:27 +010097 if(arm_compute::opencl_is_available())
98 {
99 arm_compute::CLScheduler::get().default_init();
Gian Marco Iodice1914db72017-09-29 12:05:49 +0100100 hint = Hint::OPENCL;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100101 }
102
103 Graph graph;
104 graph.set_info_enablement(true);
105
106 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Gian Marco Iodice1914db72017-09-29 12:05:49 +0100107 graph << hint
Anthony Barbier2a07e182017-08-04 18:20:27 +0100108 << Tensor(TensorInfo(TensorShape(28U, 28U, 1U, batches), 1, DataType::F32), DummyAccessor())
109 << ConvolutionLayer(
110 5U, 5U, 20U,
111 get_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"),
112 get_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
113 PadStrideInfo(1, 1, 0, 0))
114 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
115 << ConvolutionLayer(
116 5U, 5U, 50U,
117 get_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"),
118 get_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
119 PadStrideInfo(1, 1, 0, 0))
120 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
121 << FullyConnectedLayer(
122 500U,
123 get_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"),
124 get_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126 << FullyConnectedLayer(
127 10U,
128 get_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"),
129 get_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
130 << SoftmaxLayer()
131 << Tensor(DummyAccessor());
132
133 graph.run();
134}
135
136/** Main program for LeNet
137 *
138 * @param[in] argc Number of arguments
139 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
140 */
141int main(int argc, const char **argv)
142{
143 return arm_compute::utils::run_example(argc, argv, main_graph_lenet);
144}