blob: 82be97c316000503db418060d3bd0aff24882171 [file] [log] [blame]
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +01003 *
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000031using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032using namespace arm_compute::graph::frontend;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010033using namespace arm_compute::graph_utils;
34
Gian Marco5ca74092018-02-08 16:21:54 +000035namespace
36{
37/** This function checks if we can use GEMM-based convolution trying to allocate a memory of size "size_in_bytes"
38 *
39 * @param[in] size_in_bytes Memory size in bytes needed for VGG-16
40 *
41 * @return The convolution layer hint
42 */
Georgios Pinitasd8734b52017-12-22 15:27:52 +000043ConvolutionMethod convolution_hint_vgg16(size_t size_in_bytes)
Gian Marco5ca74092018-02-08 16:21:54 +000044{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000045 return ((get_mem_free_from_meminfo() * 1024) >= size_in_bytes) ? ConvolutionMethod::GEMM : ConvolutionMethod::DIRECT;
Gian Marco5ca74092018-02-08 16:21:54 +000046}
47} // namespace
48
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010049/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API
50 *
51 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000052 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010053 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000054class GraphVGG16Example : public Example
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010055{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000056public:
57 void do_setup(int argc, char **argv) override
58 {
59 std::string data_path; /* Path to the trainable data */
60 std::string image; /* Image data */
61 std::string label; /* Label data */
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010062
Georgios Pinitas140fdc72018-02-16 11:42:38 +000063 // Create a preprocessor object
64 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
65 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010066
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000067 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000068 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010069 Target target_hint = set_target_hint(target);
Gian Marco5ca74092018-02-08 16:21:54 +000070
71 // Check if we can use GEMM-based convolutions evaluating if the platform has at least 1.8 GB of available memory
Gian Marco Iodiceed99f412018-03-21 17:45:31 +000072 const size_t memory_required = 1932735283L;
73 const bool is_opencl = target_hint == Target::CL;
74 ConvolutionMethod first_convolution3x3_hint = is_opencl ? ConvolutionMethod::DIRECT : ConvolutionMethod::GEMM;
75 ConvolutionMethod convolution3x3_hint = is_opencl ? ConvolutionMethod::WINOGRAD : convolution_hint_vgg16(memory_required);
Gian Marcobfa3b522017-12-12 10:08:38 +000076
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000077 // Parse arguments
78 if(argc < 2)
79 {
80 // Print help
81 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
82 std::cout << "No data folder provided: using random values\n\n";
83 }
84 else if(argc == 2)
85 {
86 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
87 std::cout << "No data folder provided: using random values\n\n";
88 }
89 else if(argc == 3)
90 {
91 data_path = argv[2];
92 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
93 std::cout << "No image provided: using random values\n\n";
94 }
95 else if(argc == 4)
96 {
97 data_path = argv[2];
98 image = argv[3];
99 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
100 std::cout << "No text file with labels provided: skipping output accessor\n\n";
101 }
102 else
103 {
104 data_path = argv[2];
105 image = argv[3];
106 label = argv[4];
107 }
108
109 graph << target_hint
Gian Marco Iodiceed99f412018-03-21 17:45:31 +0000110 << first_convolution3x3_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000111 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
112 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113 // Layer 1
114 << ConvolutionLayer(
115 3U, 3U, 64U,
116 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"),
117 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
118 PadStrideInfo(1, 1, 1, 1))
119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
Gian Marco Iodiceed99f412018-03-21 17:45:31 +0000120 << convolution3x3_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000121 // Layer 2
122 << ConvolutionLayer(
123 3U, 3U, 64U,
124 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
126 PadStrideInfo(1, 1, 1, 1))
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
129 // Layer 3
130 << ConvolutionLayer(
131 3U, 3U, 128U,
132 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"),
133 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
134 PadStrideInfo(1, 1, 1, 1))
135 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
136 // Layer 4
137 << ConvolutionLayer(
138 3U, 3U, 128U,
139 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"),
140 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
141 PadStrideInfo(1, 1, 1, 1))
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
144 // Layer 5
145 << ConvolutionLayer(
146 3U, 3U, 256U,
147 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"),
148 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
149 PadStrideInfo(1, 1, 1, 1))
150 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
151 // Layer 6
152 << ConvolutionLayer(
153 3U, 3U, 256U,
154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"),
155 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
156 PadStrideInfo(1, 1, 1, 1))
157 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
158 // Layer 7
159 << ConvolutionLayer(
160 3U, 3U, 256U,
161 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"),
162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
163 PadStrideInfo(1, 1, 1, 1))
164 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
165 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
166 // Layer 8
167 << ConvolutionLayer(
168 3U, 3U, 512U,
169 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"),
170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
171 PadStrideInfo(1, 1, 1, 1))
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
173 // Layer 9
174 << ConvolutionLayer(
175 3U, 3U, 512U,
176 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"),
177 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
178 PadStrideInfo(1, 1, 1, 1))
179 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
180 // Layer 10
181 << ConvolutionLayer(
182 3U, 3U, 512U,
183 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"),
184 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
185 PadStrideInfo(1, 1, 1, 1))
186 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
187 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
188 // Layer 11
189 << ConvolutionLayer(
190 3U, 3U, 512U,
191 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"),
192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
193 PadStrideInfo(1, 1, 1, 1))
194 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
195 // Layer 12
196 << ConvolutionLayer(
197 3U, 3U, 512U,
198 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"),
199 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
200 PadStrideInfo(1, 1, 1, 1))
201 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
202 // Layer 13
203 << ConvolutionLayer(
204 3U, 3U, 512U,
205 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"),
206 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
207 PadStrideInfo(1, 1, 1, 1))
208 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
209 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
210 // Layer 14
211 << FullyConnectedLayer(
212 4096U,
213 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"),
214 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
215 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
216 // Layer 15
217 << FullyConnectedLayer(
218 4096U,
219 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"),
220 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
221 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
222 // Layer 16
223 << FullyConnectedLayer(
224 1000U,
225 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"),
226 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
227 // Softmax
228 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000229 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000230
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000231 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000232 GraphConfig config;
233 config.use_function_memory_manager = true;
234 config.use_tuner = (target == 2);
235 graph.finalize(target_hint, config);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100236 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000237 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100238 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000239 // Run graph
240 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100241 }
242
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000243private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000244 Stream graph{ 0, "VGG16" };
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000245};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100246
247/** Main program for VGG16
248 *
249 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000250 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100251 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000252int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100253{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000254 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100255}