blob: d4a44382b42196d72395d2dfa57f9a3bd5ea4d88 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +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 */
Kaizen8938bd32017-09-28 14:38:23 +010024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
Kaizen8938bd32017-09-28 14:38:23 +010026#include "support/ToolchainSupport.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30#include <cstdlib>
Kaizen8938bd32017-09-28 14:38:23 +010031
32using namespace arm_compute::graph;
33using namespace arm_compute::graph_utils;
34
Anthony Barbier8140e1e2017-12-14 23:48:46 +000035namespace
36{
Kaizen8938bd32017-09-28 14:38:23 +010037/** Generates appropriate accessor according to the specified path
38 *
39 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
40 *
41 * @param path Path to the data files
42 * @param data_file Relative path to the data files from path
43 *
44 * @return An appropriate tensor accessor
45 */
46std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
47{
48 if(path.empty())
49 {
50 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
51 }
52 else
53 {
54 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
55 }
56}
Anthony Barbier8140e1e2017-12-14 23:48:46 +000057} // namespace
Kaizen8938bd32017-09-28 14:38:23 +010058
59/** Example demonstrating how to implement LeNet's network using the Compute Library's graph API
60 *
61 * @param[in] argc Number of arguments
Anthony Barbier8140e1e2017-12-14 23:48:46 +000062 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches )
Kaizen8938bd32017-09-28 14:38:23 +010063 */
64void main_graph_lenet(int argc, const char **argv)
65{
66 std::string data_path; /** Path to the trainable data */
67 unsigned int batches = 4; /** Number of batches */
68
Anthony Barbier8140e1e2017-12-14 23:48:46 +000069 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
70 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
71
Kaizen8938bd32017-09-28 14:38:23 +010072 // Parse arguments
73 if(argc < 2)
74 {
75 // Print help
Anthony Barbier8140e1e2017-12-14 23:48:46 +000076 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [batches]\n\n";
Kaizen8938bd32017-09-28 14:38:23 +010077 std::cout << "No data folder provided: using random values\n\n";
78 }
79 else if(argc == 2)
80 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +000081 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [batches]\n\n";
82 std::cout << "No data folder provided: using random values\n\n";
83 }
84 else if(argc == 3)
85 {
Kaizen8938bd32017-09-28 14:38:23 +010086 //Do something with argv[1]
Anthony Barbier8140e1e2017-12-14 23:48:46 +000087 data_path = argv[2];
Kaizen8938bd32017-09-28 14:38:23 +010088 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
89 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
90 }
91 else
92 {
93 //Do something with argv[1] and argv[2]
Anthony Barbier8140e1e2017-12-14 23:48:46 +000094 data_path = argv[2];
95 batches = std::strtol(argv[3], nullptr, 0);
Kaizen8938bd32017-09-28 14:38:23 +010096 }
97
98 Graph graph;
Kaizen8938bd32017-09-28 14:38:23 +010099
100 //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000101 graph << target_hint
Kaizen8938bd32017-09-28 14:38:23 +0100102 << Tensor(TensorInfo(TensorShape(28U, 28U, 1U, batches), 1, DataType::F32), DummyAccessor())
103 << ConvolutionLayer(
104 5U, 5U, 20U,
105 get_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"),
106 get_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"),
107 PadStrideInfo(1, 1, 0, 0))
108 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
109 << ConvolutionLayer(
110 5U, 5U, 50U,
111 get_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"),
112 get_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"),
113 PadStrideInfo(1, 1, 0, 0))
114 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
115 << FullyConnectedLayer(
116 500U,
117 get_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"),
118 get_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy"))
119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
120 << FullyConnectedLayer(
121 10U,
122 get_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"),
123 get_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy"))
124 << SoftmaxLayer()
125 << Tensor(DummyAccessor());
126
127 graph.run();
128}
129
130/** Main program for LeNet
131 *
132 * @param[in] argc Number of arguments
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000133 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches )
Kaizen8938bd32017-09-28 14:38:23 +0100134 */
135int main(int argc, const char **argv)
136{
137 return arm_compute::utils::run_example(argc, argv, main_graph_lenet);
138}