blob: eb8bd42e1ae5319b2ef13ce7eaccdc88ed5cde1f [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 */
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010026#include "support/ToolchainSupport.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30#include <cstdlib>
31
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010033using namespace arm_compute::graph;
34using namespace arm_compute::graph_utils;
35
Gian Marco5ca74092018-02-08 16:21:54 +000036namespace
37{
38/** This function checks if we can use GEMM-based convolution trying to allocate a memory of size "size_in_bytes"
39 *
40 * @param[in] size_in_bytes Memory size in bytes needed for VGG-16
41 *
42 * @return The convolution layer hint
43 */
44ConvolutionMethodHint convolution_hint_vgg16(size_t size_in_bytes)
45{
46 return ((get_mem_free_from_meminfo() * 1024) >= size_in_bytes) ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
47}
48} // namespace
49
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010050/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API
51 *
52 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000053 * @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 +010054 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000055class GraphVGG16Example : public Example
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010056{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000057public:
58 void do_setup(int argc, char **argv) override
59 {
60 std::string data_path; /* Path to the trainable data */
61 std::string image; /* Image data */
62 std::string label; /* Label data */
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010063
Georgios Pinitas140fdc72018-02-16 11:42:38 +000064 // Create a preprocessor object
65 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
66 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010067
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000068 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
69 const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
70 TargetHint target_hint = set_target_hint(int_target_hint);
Gian Marco5ca74092018-02-08 16:21:54 +000071
72 // Check if we can use GEMM-based convolutions evaluating if the platform has at least 1.8 GB of available memory
73 const size_t memory_required = 1932735283L;
74 ConvolutionMethodHint convolution_hint = convolution_hint_vgg16(memory_required);
Gian Marcobfa3b522017-12-12 10:08:38 +000075
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000076 // Parse arguments
77 if(argc < 2)
78 {
79 // Print help
80 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
81 std::cout << "No data folder provided: using random values\n\n";
82 }
83 else if(argc == 2)
84 {
85 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
86 std::cout << "No data folder provided: using random values\n\n";
87 }
88 else if(argc == 3)
89 {
90 data_path = argv[2];
91 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
92 std::cout << "No image provided: using random values\n\n";
93 }
94 else if(argc == 4)
95 {
96 data_path = argv[2];
97 image = argv[3];
98 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
99 std::cout << "No text file with labels provided: skipping output accessor\n\n";
100 }
101 else
102 {
103 data_path = argv[2];
104 image = argv[3];
105 label = argv[4];
106 }
107
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000108 // Initialize graph
109 graph.graph_init(int_target_hint == 2);
110
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000111 graph << target_hint
112 << convolution_hint
113 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000114 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000115 // Layer 1
116 << ConvolutionLayer(
117 3U, 3U, 64U,
118 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"),
119 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
120 PadStrideInfo(1, 1, 1, 1))
121 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
122 // Layer 2
123 << ConvolutionLayer(
124 3U, 3U, 64U,
125 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"),
126 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
127 PadStrideInfo(1, 1, 1, 1))
128 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
129 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
130 // Layer 3
131 << ConvolutionLayer(
132 3U, 3U, 128U,
133 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"),
134 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
135 PadStrideInfo(1, 1, 1, 1))
136 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
137 // Layer 4
138 << ConvolutionLayer(
139 3U, 3U, 128U,
140 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"),
141 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
142 PadStrideInfo(1, 1, 1, 1))
143 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
144 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
145 // Layer 5
146 << ConvolutionLayer(
147 3U, 3U, 256U,
148 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"),
149 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
150 PadStrideInfo(1, 1, 1, 1))
151 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
152 // Layer 6
153 << ConvolutionLayer(
154 3U, 3U, 256U,
155 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"),
156 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
157 PadStrideInfo(1, 1, 1, 1))
158 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
159 // Layer 7
160 << ConvolutionLayer(
161 3U, 3U, 256U,
162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"),
163 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
164 PadStrideInfo(1, 1, 1, 1))
165 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
166 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
167 // Layer 8
168 << ConvolutionLayer(
169 3U, 3U, 512U,
170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"),
171 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
172 PadStrideInfo(1, 1, 1, 1))
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
174 // Layer 9
175 << ConvolutionLayer(
176 3U, 3U, 512U,
177 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"),
178 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
179 PadStrideInfo(1, 1, 1, 1))
180 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
181 // Layer 10
182 << ConvolutionLayer(
183 3U, 3U, 512U,
184 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"),
185 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
186 PadStrideInfo(1, 1, 1, 1))
187 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
188 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
189 // Layer 11
190 << ConvolutionLayer(
191 3U, 3U, 512U,
192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"),
193 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
194 PadStrideInfo(1, 1, 1, 1))
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
196 // Layer 12
197 << ConvolutionLayer(
198 3U, 3U, 512U,
199 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"),
200 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
201 PadStrideInfo(1, 1, 1, 1))
202 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
203 // Layer 13
204 << ConvolutionLayer(
205 3U, 3U, 512U,
206 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"),
207 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
208 PadStrideInfo(1, 1, 1, 1))
209 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
210 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
211 // Layer 14
212 << FullyConnectedLayer(
213 4096U,
214 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"),
215 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
216 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
217 // Layer 15
218 << FullyConnectedLayer(
219 4096U,
220 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"),
221 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
222 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
223 // Layer 16
224 << FullyConnectedLayer(
225 1000U,
226 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"),
227 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
228 // Softmax
229 << SoftmaxLayer()
230 << Tensor(get_output_accessor(label, 5));
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100231 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000232 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100233 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000234 // Run graph
235 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100236 }
237
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000238private:
239 Graph graph{};
240};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100241
242/** Main program for VGG16
243 *
244 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000245 * @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 +0100246 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000247int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100248{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000249 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100250}