blob: c3eb922f0ec7d9fb0afdbf3e7b922c216482c578 [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
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000064 constexpr float mean_r = 123.68f; /* Mean value to subtract from red channel */
65 constexpr float mean_g = 116.779f; /* Mean value to subtract from green channel */
66 constexpr float mean_b = 103.939f; /* Mean value to subtract from blue channel */
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010067
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000068 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
Gian Marco5ca74092018-02-08 16:21:54 +000069 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
70
71 // Check if we can use GEMM-based convolutions evaluating if the platform has at least 1.8 GB of available memory
72 const size_t memory_required = 1932735283L;
73 ConvolutionMethodHint convolution_hint = convolution_hint_vgg16(memory_required);
Gian Marcobfa3b522017-12-12 10:08:38 +000074
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000075 // Parse arguments
76 if(argc < 2)
77 {
78 // Print help
79 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
80 std::cout << "No data folder provided: using random values\n\n";
81 }
82 else if(argc == 2)
83 {
84 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
85 std::cout << "No data folder provided: using random values\n\n";
86 }
87 else if(argc == 3)
88 {
89 data_path = argv[2];
90 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
91 std::cout << "No image provided: using random values\n\n";
92 }
93 else if(argc == 4)
94 {
95 data_path = argv[2];
96 image = argv[3];
97 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
98 std::cout << "No text file with labels provided: skipping output accessor\n\n";
99 }
100 else
101 {
102 data_path = argv[2];
103 image = argv[3];
104 label = argv[4];
105 }
106
107 graph << target_hint
108 << convolution_hint
109 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
110 get_input_accessor(image, mean_r, mean_g, mean_b))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000111 // Layer 1
112 << ConvolutionLayer(
113 3U, 3U, 64U,
114 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"),
115 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
116 PadStrideInfo(1, 1, 1, 1))
117 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
118 // Layer 2
119 << ConvolutionLayer(
120 3U, 3U, 64U,
121 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"),
122 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
123 PadStrideInfo(1, 1, 1, 1))
124 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
126 // Layer 3
127 << ConvolutionLayer(
128 3U, 3U, 128U,
129 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"),
130 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
131 PadStrideInfo(1, 1, 1, 1))
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
133 // Layer 4
134 << ConvolutionLayer(
135 3U, 3U, 128U,
136 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"),
137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
138 PadStrideInfo(1, 1, 1, 1))
139 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
140 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
141 // Layer 5
142 << ConvolutionLayer(
143 3U, 3U, 256U,
144 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"),
145 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
146 PadStrideInfo(1, 1, 1, 1))
147 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
148 // Layer 6
149 << ConvolutionLayer(
150 3U, 3U, 256U,
151 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"),
152 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
153 PadStrideInfo(1, 1, 1, 1))
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
155 // Layer 7
156 << ConvolutionLayer(
157 3U, 3U, 256U,
158 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"),
159 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
160 PadStrideInfo(1, 1, 1, 1))
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
162 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
163 // Layer 8
164 << ConvolutionLayer(
165 3U, 3U, 512U,
166 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"),
167 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
168 PadStrideInfo(1, 1, 1, 1))
169 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
170 // Layer 9
171 << ConvolutionLayer(
172 3U, 3U, 512U,
173 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"),
174 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
175 PadStrideInfo(1, 1, 1, 1))
176 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
177 // Layer 10
178 << ConvolutionLayer(
179 3U, 3U, 512U,
180 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"),
181 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
182 PadStrideInfo(1, 1, 1, 1))
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
184 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
185 // Layer 11
186 << ConvolutionLayer(
187 3U, 3U, 512U,
188 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"),
189 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
190 PadStrideInfo(1, 1, 1, 1))
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
192 // Layer 12
193 << ConvolutionLayer(
194 3U, 3U, 512U,
195 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"),
196 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
197 PadStrideInfo(1, 1, 1, 1))
198 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
199 // Layer 13
200 << ConvolutionLayer(
201 3U, 3U, 512U,
202 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"),
203 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
204 PadStrideInfo(1, 1, 1, 1))
205 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
206 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
207 // Layer 14
208 << FullyConnectedLayer(
209 4096U,
210 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"),
211 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
212 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
213 // Layer 15
214 << FullyConnectedLayer(
215 4096U,
216 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"),
217 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
218 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
219 // Layer 16
220 << FullyConnectedLayer(
221 1000U,
222 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"),
223 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
224 // Softmax
225 << SoftmaxLayer()
226 << Tensor(get_output_accessor(label, 5));
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100227 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000228 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100229 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000230 // Run graph
231 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100232 }
233
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000234private:
235 Graph graph{};
236};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100237
238/** Main program for VGG16
239 *
240 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000241 * @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 +0100242 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000243int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100244{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000245 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100246}