blob: bb5905ec28aeb0d344d41cd1b415cc45c4bce53e [file] [log] [blame]
Kaizenbf8b01d2017-10-12 14:26:51 +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/core/Logger.h"
29#include "arm_compute/graph/Graph.h"
30#include "arm_compute/graph/Nodes.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "arm_compute/runtime/CPP/CPPScheduler.h"
33#include "arm_compute/runtime/Scheduler.h"
34#include "support/ToolchainSupport.h"
35#include "utils/GraphUtils.h"
36#include "utils/Utils.h"
37
38#include <cstdlib>
39#include <iostream>
40#include <memory>
41
42using namespace arm_compute::graph;
43using namespace arm_compute::graph_utils;
44
45/** Generates appropriate accessor according to the specified path
46 *
47 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
48 *
49 * @param[in] path Path to the data files
50 * @param[in] data_file Relative path to the data files from path
51 *
52 * @return An appropriate tensor accessor
53 */
54std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
55{
56 if(path.empty())
57 {
58 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
59 }
60 else
61 {
62 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
63 }
64}
65
Anthony Barbier8a3da6f2017-10-23 18:55:17 +010066/** Generates appropriate input accessor according to the specified ppm_path
67 *
68 * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor
69 *
70 * @param[in] ppm_path Path to PPM file
71 * @param[in] mean_r Red mean value to be subtracted from red channel
72 * @param[in] mean_g Green mean value to be subtracted from green channel
73 * @param[in] mean_b Blue mean value to be subtracted from blue channel
74 *
75 * @return An appropriate tensor accessor
76 */
77std::unique_ptr<ITensorAccessor> get_input_accessor(const std::string &ppm_path, float mean_r, float mean_g, float mean_b)
78{
79 if(ppm_path.empty())
80 {
81 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
82 }
83 else
84 {
85 return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, true, mean_r, mean_g, mean_b);
86 }
87}
88
89/** Generates appropriate output accessor according to the specified labels_path
90 *
91 * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
92 *
93 * @param[in] labels_path Path to labels text file
94 * @param[in] top_n (Optional) Number of output classes to print
95 * @param[out] output_stream (Optional) Output stream
96 *
97 * @return An appropriate tensor accessor
98 */
99std::unique_ptr<ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout)
100{
101 if(labels_path.empty())
102 {
103 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
104 }
105 else
106 {
107 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
108 }
109}
110
Kaizenbf8b01d2017-10-12 14:26:51 +0100111/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
112 *
113 * @param[in] argc Number of arguments
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100114 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Kaizenbf8b01d2017-10-12 14:26:51 +0100115 */
116void main_graph_alexnet(int argc, const char **argv)
117{
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100118 std::string data_path; /* Path to the trainable data */
119 std::string image; /* Image data */
120 std::string label; /* Label data */
121
122 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
123 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
124 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Kaizenbf8b01d2017-10-12 14:26:51 +0100125
126 // Parse arguments
127 if(argc < 2)
128 {
129 // Print help
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100130 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Kaizenbf8b01d2017-10-12 14:26:51 +0100131 std::cout << "No data folder provided: using random values\n\n";
132 }
133 else if(argc == 2)
134 {
Kaizenbf8b01d2017-10-12 14:26:51 +0100135 data_path = argv[1];
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100136 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
137 std::cout << "No image provided: using random values\n\n";
138 }
139 else if(argc == 3)
140 {
141 data_path = argv[1];
142 image = argv[2];
143 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
144 std::cout << "No text file with labels provided: skipping output accessor\n\n";
Kaizenbf8b01d2017-10-12 14:26:51 +0100145 }
146 else
147 {
Kaizenbf8b01d2017-10-12 14:26:51 +0100148 data_path = argv[1];
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100149 image = argv[2];
150 label = argv[3];
Kaizenbf8b01d2017-10-12 14:26:51 +0100151 }
152
153 // Check if OpenCL is available and initialize the scheduler
154 TargetHint hint = TargetHint::NEON;
155 if(arm_compute::opencl_is_available())
156 {
157 arm_compute::CLScheduler::get().default_init();
158 hint = TargetHint::OPENCL;
159 }
160
161 Graph graph;
162 arm_compute::Logger::get().set_logger(std::cout, arm_compute::LoggerVerbosity::INFO);
163
164 graph << hint
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100165 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
166 get_input_accessor(image, mean_r, mean_g, mean_b))
Kaizenbf8b01d2017-10-12 14:26:51 +0100167 // Layer 1
168 << ConvolutionLayer(
169 11U, 11U, 96U,
170 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
171 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
172 PadStrideInfo(4, 4, 0, 0))
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
174 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
175 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
176 // Layer 2
177 << ConvolutionMethodHint::DIRECT
178 << ConvolutionLayer(
179 5U, 5U, 256U,
180 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
181 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
182 PadStrideInfo(1, 1, 2, 2), 2)
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
184 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
185 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
186 // Layer 3
187 << ConvolutionLayer(
188 3U, 3U, 384U,
189 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
190 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
191 PadStrideInfo(1, 1, 1, 1))
192 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
193 // Layer 4
194 << ConvolutionLayer(
195 3U, 3U, 384U,
196 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
197 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
198 PadStrideInfo(1, 1, 1, 1), 2)
199 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
200 // Layer 5
201 << ConvolutionLayer(
202 3U, 3U, 256U,
203 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
204 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
205 PadStrideInfo(1, 1, 1, 1), 2)
206 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
207 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
208 // Layer 6
209 << FullyConnectedLayer(
210 4096U,
211 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
212 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
213 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
214 // Layer 7
215 << FullyConnectedLayer(
216 4096U,
217 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
218 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
219 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
220 // Layer 8
221 << FullyConnectedLayer(
222 1000U,
223 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
224 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
225 // Softmax
226 << SoftmaxLayer()
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100227 << Tensor(get_output_accessor(label, 5));
Kaizenbf8b01d2017-10-12 14:26:51 +0100228
229 // Run graph
230 graph.run();
231}
232
233/** Main program for AlexNet
234 *
235 * @param[in] argc Number of arguments
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100236 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Kaizenbf8b01d2017-10-12 14:26:51 +0100237 */
238int main(int argc, const char **argv)
239{
240 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
241}