blob: 9c736c5df1e9c1912f3ed9124f9407cda28b7c39 [file] [log] [blame]
Georgios Pinitas6f669f02017-09-26 12:32:57 +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/CPP/CPPScheduler.h"
32#include "arm_compute/runtime/Scheduler.h"
33#include "support/ToolchainSupport.h"
34#include "utils/GraphUtils.h"
35#include "utils/Utils.h"
36
37#include <cstdlib>
38#include <iostream>
39#include <memory>
40
41using namespace arm_compute::graph;
42using namespace arm_compute::graph_utils;
43
44/** Generates appropriate accessor according to the specified path
45 *
46 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
47 *
48 * @param[in] path Path to the data files
49 * @param[in] data_file Relative path to the data files from path
50 *
51 * @return An appropriate tensor accessor
52 */
53std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
54{
55 if(path.empty())
56 {
57 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
58 }
59 else
60 {
61 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
62 }
63}
64
65/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
66 *
67 * @param[in] argc Number of arguments
68 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
69 */
70void main_graph_alexnet(int argc, const char **argv)
71{
72 std::string data_path; /** Path to the trainable data */
73 unsigned int batches = 4; /** Number of batches */
74
75 // Parse arguments
76 if(argc < 2)
77 {
78 // Print help
79 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
80 std::cout << "No data folder provided: using random values\n\n";
81 }
82 else if(argc == 2)
83 {
84 //Do something with argv[1]
85 data_path = argv[1];
86 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
87 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
88 }
89 else
90 {
91 //Do something with argv[1] and argv[2]
92 data_path = argv[1];
93 batches = std::strtol(argv[2], nullptr, 0);
94 }
95
96 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +010097 TargetHint hint = TargetHint::NEON;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010098 if(arm_compute::opencl_is_available())
99 {
100 arm_compute::CLScheduler::get().default_init();
Georgios Pinitasff421f22017-10-04 16:53:58 +0100101 hint = TargetHint::OPENCL;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100102 }
103
104 Graph graph;
105 graph.set_info_enablement(true);
106
107 graph << hint
108 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, batches), 1, DataType::F32), DummyAccessor())
109 // Layer 1
110 << ConvolutionLayer(
111 11U, 11U, 96U,
112 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
113 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
114 PadStrideInfo(4, 4, 0, 0))
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
116 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
117 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
118 // Layer 2
Georgios Pinitasff421f22017-10-04 16:53:58 +0100119 << ConvolutionMethodHint::DIRECT
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100120 << ConvolutionLayer(
121 5U, 5U, 256U,
122 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
123 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
124 PadStrideInfo(1, 1, 2, 2), 2)
125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
127 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
128 // Layer 3
129 << ConvolutionLayer(
130 3U, 3U, 384U,
131 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
132 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
133 PadStrideInfo(1, 1, 1, 1))
134 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
135 // Layer 4
136 << ConvolutionLayer(
137 3U, 3U, 384U,
138 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
139 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
140 PadStrideInfo(1, 1, 1, 1), 2)
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
142 // Layer 5
143 << ConvolutionLayer(
144 3U, 3U, 256U,
145 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
146 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
147 PadStrideInfo(1, 1, 1, 1), 2)
148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
149 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
150 // Layer 6
151 << FullyConnectedLayer(
152 4096U,
153 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
154 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
156 // Layer 7
157 << FullyConnectedLayer(
158 4096U,
159 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
160 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
162 // Layer 8
163 << FullyConnectedLayer(
164 1000U,
165 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
166 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
167 // Softmax
168 << SoftmaxLayer()
169 << Tensor(DummyAccessor());
170
171 // Run graph
172 graph.run();
173}
174
175/** Main program for AlexNet
176 *
177 * @param[in] argc Number of arguments
178 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
179 */
180int main(int argc, const char **argv)
181{
182 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
183}