blob: 1d041997e3ed0a618eefef672981f7f9bc1c3759 [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;
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010043using namespace arm_compute::logging;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010044
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
Gian Marco44ec2e72017-10-19 14:13:38 +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
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100111/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
112 *
113 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +0100114 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100115 */
116void main_graph_alexnet(int argc, const char **argv)
117{
Gian Marco44ec2e72017-10-19 14:13:38 +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 */
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100125
126 // Parse arguments
127 if(argc < 2)
128 {
129 // Print help
Gian Marco44ec2e72017-10-19 14:13:38 +0100130 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100131 std::cout << "No data folder provided: using random values\n\n";
132 }
133 else if(argc == 2)
134 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100135 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +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";
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100145 }
146 else
147 {
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100148 data_path = argv[1];
Gian Marco44ec2e72017-10-19 14:13:38 +0100149 image = argv[2];
150 label = argv[3];
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100151 }
152
153 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +0100154 TargetHint hint = TargetHint::NEON;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100155 if(arm_compute::opencl_is_available())
156 {
157 arm_compute::CLScheduler::get().default_init();
Georgios Pinitasff421f22017-10-04 16:53:58 +0100158 hint = TargetHint::OPENCL;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100159 }
160
161 Graph graph;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100162
163 graph << hint
Gian Marco44ec2e72017-10-19 14:13:38 +0100164 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
165 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100166 // Layer 1
167 << ConvolutionLayer(
168 11U, 11U, 96U,
169 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
170 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
171 PadStrideInfo(4, 4, 0, 0))
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
173 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
174 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
175 // Layer 2
Georgios Pinitasff421f22017-10-04 16:53:58 +0100176 << ConvolutionMethodHint::DIRECT
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100177 << ConvolutionLayer(
178 5U, 5U, 256U,
179 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
180 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
181 PadStrideInfo(1, 1, 2, 2), 2)
182 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
183 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
184 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
185 // Layer 3
186 << ConvolutionLayer(
187 3U, 3U, 384U,
188 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
189 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
190 PadStrideInfo(1, 1, 1, 1))
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
192 // Layer 4
193 << ConvolutionLayer(
194 3U, 3U, 384U,
195 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
196 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
197 PadStrideInfo(1, 1, 1, 1), 2)
198 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
199 // Layer 5
200 << ConvolutionLayer(
201 3U, 3U, 256U,
202 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
203 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
204 PadStrideInfo(1, 1, 1, 1), 2)
205 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
206 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
207 // Layer 6
208 << FullyConnectedLayer(
209 4096U,
210 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
211 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
212 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
213 // Layer 7
214 << FullyConnectedLayer(
215 4096U,
216 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
217 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
218 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
219 // Layer 8
220 << FullyConnectedLayer(
221 1000U,
222 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
223 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
224 // Softmax
225 << SoftmaxLayer()
Gian Marco44ec2e72017-10-19 14:13:38 +0100226 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100227
228 // Run graph
229 graph.run();
230}
231
232/** Main program for AlexNet
233 *
234 * @param[in] argc Number of arguments
Gian Marco44ec2e72017-10-19 14:13:38 +0100235 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100236 */
237int main(int argc, const char **argv)
238{
239 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
240}