blob: 74cb65ab69fa2e283a4cbbe3fe1239ac4f07f754 [file] [log] [blame]
Isabella Gottardi9f20bda2017-11-03 17:16:20 +00001/*
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/Scheduler.h"
32#include "support/ToolchainSupport.h"
33#include "utils/GraphUtils.h"
34#include "utils/Utils.h"
35
36#include <cstdlib>
37
38using namespace arm_compute::graph;
39using namespace arm_compute::graph_utils;
40using namespace arm_compute::logging;
41
42/** Example demonstrating how to implement VGG19's network using the Compute Library's graph API
43 *
44 * @param[in] argc Number of arguments
45 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
46 */
47void main_graph_vgg19(int argc, const char **argv)
48{
49 std::string data_path; /* Path to the trainable data */
50 std::string image; /* Image data */
51 std::string label; /* Label data */
52
53 constexpr float mean_r = 123.68f; /* Mean value to subtract from red channel */
54 constexpr float mean_g = 116.779f; /* Mean value to subtract from green channel */
55 constexpr float mean_b = 103.939f; /* Mean value to subtract from blue channel */
56
57 // Parse arguments
58 if(argc < 2)
59 {
60 // Print help
61 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
62 std::cout << "No data folder provided: using random values\n\n";
63 }
64 else if(argc == 2)
65 {
66 data_path = argv[1];
67 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
68 std::cout << "No image provided: using random values\n\n";
69 }
70 else if(argc == 3)
71 {
72 data_path = argv[1];
73 image = argv[2];
74 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
75 std::cout << "No text file with labels provided: skipping output accessor\n\n";
76 }
77 else
78 {
79 data_path = argv[1];
80 image = argv[2];
81 label = argv[3];
82 }
83
84 // Check if OpenCL is available and initialize the scheduler
85 TargetHint hint = TargetHint::NEON;
86 if(arm_compute::opencl_is_available())
87 {
88 arm_compute::CLScheduler::get().default_init();
89 hint = TargetHint::OPENCL;
90 }
91
92 Graph graph;
93
94 graph << hint
95 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
96 get_input_accessor(image, mean_r, mean_g, mean_b))
97 // Layer 1
98 << ConvolutionLayer(
99 3U, 3U, 64U,
100 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_w.npy"),
101 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_b.npy"),
102 PadStrideInfo(1, 1, 1, 1))
103 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
104 << ConvolutionLayer(
105 3U, 3U, 64U,
106 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_w.npy"),
107 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_b.npy"),
108 PadStrideInfo(1, 1, 1, 1))
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
110 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
111 // Layer 2
112 << ConvolutionLayer(
113 3U, 3U, 128U,
114 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_w.npy"),
115 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_b.npy"),
116 PadStrideInfo(1, 1, 1, 1))
117 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
118 << ConvolutionLayer(
119 3U, 3U, 128U,
120 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_w.npy"),
121 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
123 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
124 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
125 // Layer 3
126 << ConvolutionLayer(
127 3U, 3U, 256U,
128 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_w.npy"),
129 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_b.npy"),
130 PadStrideInfo(1, 1, 1, 1))
131 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
132 << ConvolutionLayer(
133 3U, 3U, 256U,
134 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_w.npy"),
135 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_b.npy"),
136 PadStrideInfo(1, 1, 1, 1))
137 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
138 << ConvolutionLayer(
139 3U, 3U, 256U,
140 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_w.npy"),
141 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_b.npy"),
142 PadStrideInfo(1, 1, 1, 1))
143 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
144 << ConvolutionLayer(
145 3U, 3U, 256U,
146 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_b.npy"),
148 PadStrideInfo(1, 1, 1, 1))
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
151 // Layer 4
152 << ConvolutionLayer(
153 3U, 3U, 512U,
154 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_w.npy"),
155 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_b.npy"),
156 PadStrideInfo(1, 1, 1, 1))
157 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
158 << ConvolutionLayer(
159 3U, 3U, 512U,
160 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_w.npy"),
161 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_b.npy"),
162 PadStrideInfo(1, 1, 1, 1))
163 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
164 << ConvolutionLayer(
165 3U, 3U, 512U,
166 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_w.npy"),
167 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_b.npy"),
168 PadStrideInfo(1, 1, 1, 1))
169 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
170 << ConvolutionLayer(
171 3U, 3U, 512U,
172 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_w.npy"),
173 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_b.npy"),
174 PadStrideInfo(1, 1, 1, 1))
175 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
176 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
177 // Layer 5
178 << ConvolutionLayer(
179 3U, 3U, 512U,
180 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_w.npy"),
181 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_b.npy"),
182 PadStrideInfo(1, 1, 1, 1))
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
184 << ConvolutionLayer(
185 3U, 3U, 512U,
186 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_w.npy"),
187 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_b.npy"),
188 PadStrideInfo(1, 1, 1, 1))
189 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
190 << ConvolutionLayer(
191 3U, 3U, 512U,
192 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_w.npy"),
193 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_b.npy"),
194 PadStrideInfo(1, 1, 1, 1))
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
196 << ConvolutionLayer(
197 3U, 3U, 512U,
198 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_w.npy"),
199 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_b.npy"),
200 PadStrideInfo(1, 1, 1, 1))
201 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
202 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
203 // Layer 6
204 << FullyConnectedLayer(
205 4096U,
206 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_w.npy"),
207 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_b.npy"))
208 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
209 // Layer 7
210 << FullyConnectedLayer(
211 4096U,
212 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_w.npy"),
213 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_b.npy"))
214 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
215 // Layer 8
216 << FullyConnectedLayer(
217 1000U,
218 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_w.npy"),
219 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_b.npy"))
220 // Softmax
221 << SoftmaxLayer()
222 << Tensor(get_output_accessor(label, 5));
223
224 // Run graph
225 graph.run();
226}
227
228/** Main program for VGG19
229 *
230 * @param[in] argc Number of arguments
231 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
232 */
233int main(int argc, const char **argv)
234{
235 return arm_compute::utils::run_example(argc, argv, main_graph_vgg19);
236}