blob: be852b94ff0dc2134f3544c9e8d56cd26d1f7047 [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
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010028#include "arm_compute/core/utils/logging/LoggerRegistry.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010029#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;
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010044using namespace arm_compute::logging;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010045
46/** Generates appropriate accessor according to the specified path
47 *
48 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
49 *
50 * @param[in] path Path to the data files
51 * @param[in] data_file Relative path to the data files from path
52 *
53 * @return An appropriate tensor accessor
54 */
55std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file)
56{
57 if(path.empty())
58 {
59 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
60 }
61 else
62 {
63 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
64 }
65}
66
Gian Marco44ec2e72017-10-19 14:13:38 +010067/** Generates appropriate input accessor according to the specified ppm_path
68 *
69 * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor
70 *
71 * @param[in] ppm_path Path to PPM file
72 * @param[in] mean_r Red mean value to be subtracted from red channel
73 * @param[in] mean_g Green mean value to be subtracted from green channel
74 * @param[in] mean_b Blue mean value to be subtracted from blue channel
75 *
76 * @return An appropriate tensor accessor
77 */
78std::unique_ptr<ITensorAccessor> get_input_accessor(const std::string &ppm_path, float mean_r, float mean_g, float mean_b)
79{
80 if(ppm_path.empty())
81 {
82 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
83 }
84 else
85 {
86 return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, true, mean_r, mean_g, mean_b);
87 }
88}
89
90/** Generates appropriate output accessor according to the specified labels_path
91 *
92 * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
93 *
94 * @param[in] labels_path Path to labels text file
95 * @param[in] top_n (Optional) Number of output classes to print
96 * @param[out] output_stream (Optional) Output stream
97 *
98 * @return An appropriate tensor accessor
99 */
100std::unique_ptr<ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout)
101{
102 if(labels_path.empty())
103 {
104 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
105 }
106 else
107 {
108 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
109 }
110}
111
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100112/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
113 *
114 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +0100115 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100116 */
117void main_graph_alexnet(int argc, const char **argv)
118{
Gian Marco44ec2e72017-10-19 14:13:38 +0100119 std::string data_path; /* Path to the trainable data */
120 std::string image; /* Image data */
121 std::string label; /* Label data */
122
123 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
124 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
125 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100126
127 // Parse arguments
128 if(argc < 2)
129 {
130 // Print help
Gian Marco44ec2e72017-10-19 14:13:38 +0100131 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100132 std::cout << "No data folder provided: using random values\n\n";
133 }
134 else if(argc == 2)
135 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100136 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +0100137 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
138 std::cout << "No image provided: using random values\n\n";
139 }
140 else if(argc == 3)
141 {
142 data_path = argv[1];
143 image = argv[2];
144 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
145 std::cout << "No text file with labels provided: skipping output accessor\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100146 }
147 else
148 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100149 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +0100150 image = argv[2];
151 label = argv[3];
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100152 }
153
154 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +0100155 TargetHint hint = TargetHint::NEON;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100156 if(arm_compute::opencl_is_available())
157 {
158 arm_compute::CLScheduler::get().default_init();
Georgios Pinitasff421f22017-10-04 16:53:58 +0100159 hint = TargetHint::OPENCL;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100160 }
161
162 Graph graph;
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100163 LoggerRegistry::get().create_reserved_loggers(LogLevel::INFO, { std::make_shared<StdPrinter>() });
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100164
165 graph << hint
Gian Marco44ec2e72017-10-19 14:13:38 +0100166 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
167 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100168 // Layer 1
169 << ConvolutionLayer(
170 11U, 11U, 96U,
171 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
172 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
173 PadStrideInfo(4, 4, 0, 0))
174 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
175 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
176 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
177 // Layer 2
Georgios Pinitasff421f22017-10-04 16:53:58 +0100178 << ConvolutionMethodHint::DIRECT
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100179 << ConvolutionLayer(
180 5U, 5U, 256U,
181 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
182 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
183 PadStrideInfo(1, 1, 2, 2), 2)
184 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
185 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
186 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
187 // Layer 3
188 << ConvolutionLayer(
189 3U, 3U, 384U,
190 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
191 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
192 PadStrideInfo(1, 1, 1, 1))
193 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
194 // Layer 4
195 << ConvolutionLayer(
196 3U, 3U, 384U,
197 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
198 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
199 PadStrideInfo(1, 1, 1, 1), 2)
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
201 // Layer 5
202 << ConvolutionLayer(
203 3U, 3U, 256U,
204 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
205 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
206 PadStrideInfo(1, 1, 1, 1), 2)
207 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
208 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
209 // Layer 6
210 << FullyConnectedLayer(
211 4096U,
212 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
213 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
214 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
215 // Layer 7
216 << FullyConnectedLayer(
217 4096U,
218 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
219 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
220 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
221 // Layer 8
222 << FullyConnectedLayer(
223 1000U,
224 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
225 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
226 // Softmax
227 << SoftmaxLayer()
Gian Marco44ec2e72017-10-19 14:13:38 +0100228 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100229
230 // Run graph
231 graph.run();
232}
233
234/** Main program for AlexNet
235 *
236 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +0100237 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100238 */
239int main(int argc, const char **argv)
240{
241 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
242}