blob: dce71327852285e99ddd793e479f634c96e082ca [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
Michalis Spyroue4720822017-10-02 17:44:52 +010028#include "arm_compute/core/Logger.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;
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
66/** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API
67 *
68 * @param[in] argc Number of arguments
69 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
70 */
71void main_graph_alexnet(int argc, const char **argv)
72{
73 std::string data_path; /** Path to the trainable data */
74 unsigned int batches = 4; /** Number of batches */
75
76 // Parse arguments
77 if(argc < 2)
78 {
79 // Print help
80 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
81 std::cout << "No data folder provided: using random values\n\n";
82 }
83 else if(argc == 2)
84 {
85 //Do something with argv[1]
86 data_path = argv[1];
87 std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n";
88 std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n";
89 }
90 else
91 {
92 //Do something with argv[1] and argv[2]
93 data_path = argv[1];
94 batches = std::strtol(argv[2], nullptr, 0);
95 }
96
97 // Check if OpenCL is available and initialize the scheduler
Georgios Pinitasff421f22017-10-04 16:53:58 +010098 TargetHint hint = TargetHint::NEON;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010099 if(arm_compute::opencl_is_available())
100 {
101 arm_compute::CLScheduler::get().default_init();
Georgios Pinitasff421f22017-10-04 16:53:58 +0100102 hint = TargetHint::OPENCL;
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100103 }
104
105 Graph graph;
Michalis Spyroue4720822017-10-02 17:44:52 +0100106 arm_compute::Logger::get().set_logger(std::cout, arm_compute::LoggerVerbosity::INFO);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100107
108 graph << hint
109 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, batches), 1, DataType::F32), DummyAccessor())
110 // Layer 1
111 << ConvolutionLayer(
112 11U, 11U, 96U,
113 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"),
114 get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"),
115 PadStrideInfo(4, 4, 0, 0))
116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
117 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
118 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
119 // Layer 2
Georgios Pinitasff421f22017-10-04 16:53:58 +0100120 << ConvolutionMethodHint::DIRECT
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100121 << ConvolutionLayer(
122 5U, 5U, 256U,
123 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"),
124 get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"),
125 PadStrideInfo(1, 1, 2, 2), 2)
126 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
127 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
128 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
129 // Layer 3
130 << ConvolutionLayer(
131 3U, 3U, 384U,
132 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"),
133 get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"),
134 PadStrideInfo(1, 1, 1, 1))
135 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
136 // Layer 4
137 << ConvolutionLayer(
138 3U, 3U, 384U,
139 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"),
140 get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"),
141 PadStrideInfo(1, 1, 1, 1), 2)
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 // Layer 5
144 << ConvolutionLayer(
145 3U, 3U, 256U,
146 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"),
147 get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"),
148 PadStrideInfo(1, 1, 1, 1), 2)
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)))
151 // Layer 6
152 << FullyConnectedLayer(
153 4096U,
154 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"),
155 get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy"))
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 // Layer 7
158 << FullyConnectedLayer(
159 4096U,
160 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"),
161 get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy"))
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
163 // Layer 8
164 << FullyConnectedLayer(
165 1000U,
166 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"),
167 get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy"))
168 // Softmax
169 << SoftmaxLayer()
170 << Tensor(DummyAccessor());
171
172 // Run graph
173 graph.run();
174}
175
176/** Main program for AlexNet
177 *
178 * @param[in] argc Number of arguments
179 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches )
180 */
181int main(int argc, const char **argv)
182{
183 return arm_compute::utils::run_example(argc, argv, main_graph_alexnet);
184}