blob: 6db4e386dee92d46ca1317eeeda04508161b3b82 [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
35/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API
36 *
37 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +010038 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010039 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000040class GraphVGG16Example : public Example
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010041{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000042public:
43 void do_setup(int argc, char **argv) override
44 {
45 std::string data_path; /* Path to the trainable data */
46 std::string image; /* Image data */
47 std::string label; /* Label data */
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010048
Georgios Pinitas140fdc72018-02-16 11:42:38 +000049 // Create a preprocessor object
50 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
51 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010052
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000053 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Gian Marco Iodice1ed442a2018-04-11 10:58:31 +010054 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
55 Target target_hint = set_target_hint(target);
56 const bool is_opencl = target_hint == Target::CL;
Gian Marco5ca74092018-02-08 16:21:54 +000057
Gian Marco Iodiceed99f412018-03-21 17:45:31 +000058 ConvolutionMethod first_convolution3x3_hint = is_opencl ? ConvolutionMethod::DIRECT : ConvolutionMethod::GEMM;
Gian Marco Iodice1ed442a2018-04-11 10:58:31 +010059 ConvolutionMethod convolution3x3_hint = ConvolutionMethod::DEFAULT;
Giorgio Arena59631a12018-05-02 13:59:04 +010060 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Gian Marcobfa3b522017-12-12 10:08:38 +000061
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000062 // Parse arguments
63 if(argc < 2)
64 {
65 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +010066 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000067 std::cout << "No data folder provided: using random values\n\n";
68 }
69 else if(argc == 2)
70 {
Giorgio Arena59631a12018-05-02 13:59:04 +010071 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000072 std::cout << "No data folder provided: using random values\n\n";
73 }
74 else if(argc == 3)
75 {
76 data_path = argv[2];
Giorgio Arena59631a12018-05-02 13:59:04 +010077 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000078 std::cout << "No image provided: using random values\n\n";
79 }
80 else if(argc == 4)
81 {
82 data_path = argv[2];
83 image = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +010084 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 std::cout << "No text file with labels provided: skipping output accessor\n\n";
86 }
Giorgio Arena59631a12018-05-02 13:59:04 +010087 else if(argc == 5)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000088 {
89 data_path = argv[2];
90 image = argv[3];
91 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +010092 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
93 std::cout << "No fast math info provided: disabling fast math\n\n";
94 }
95 else
96 {
97 data_path = argv[2];
98 image = argv[3];
99 label = argv[4];
100 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000101 }
102
103 graph << target_hint
Giorgio Arena59631a12018-05-02 13:59:04 +0100104 << fast_math_hint
Gian Marco Iodiceed99f412018-03-21 17:45:31 +0000105 << first_convolution3x3_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000106 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
107 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000108 // Layer 1
109 << ConvolutionLayer(
110 3U, 3U, 64U,
111 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"),
112 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
113 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100114 .set_name("conv1_1")
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Gian Marco Iodiceed99f412018-03-21 17:45:31 +0000116 << convolution3x3_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000117 // Layer 2
118 << ConvolutionLayer(
119 3U, 3U, 64U,
120 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"),
121 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100123 .set_name("conv1_2")
124 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000126 // 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))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100132 .set_name("conv2_1")
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000134 // Layer 4
135 << ConvolutionLayer(
136 3U, 3U, 128U,
137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"),
138 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
139 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100140 .set_name("conv2_2")
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
142 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000143 // Layer 5
144 << ConvolutionLayer(
145 3U, 3U, 256U,
146 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
148 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100149 .set_name("conv3_1")
150 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000151 // 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))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100157 .set_name("conv3_2")
158 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000159 // 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))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100165 .set_name("conv3_3")
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
167 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000168 // Layer 8
169 << ConvolutionLayer(
170 3U, 3U, 512U,
171 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"),
172 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
173 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100174 .set_name("conv4_1")
175 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000176 // Layer 9
177 << ConvolutionLayer(
178 3U, 3U, 512U,
179 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"),
180 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
181 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100182 .set_name("conv4_2")
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000184 // Layer 10
185 << ConvolutionLayer(
186 3U, 3U, 512U,
187 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"),
188 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
189 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100190 .set_name("conv4_3")
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
192 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000193 // Layer 11
194 << ConvolutionLayer(
195 3U, 3U, 512U,
196 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"),
197 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
198 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100199 .set_name("conv5_1")
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000201 // Layer 12
202 << ConvolutionLayer(
203 3U, 3U, 512U,
204 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"),
205 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
206 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100207 .set_name("conv5_2")
208 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000209 // Layer 13
210 << ConvolutionLayer(
211 3U, 3U, 512U,
212 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"),
213 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
214 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100215 .set_name("conv5_3")
216 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
217 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000218 // Layer 14
219 << FullyConnectedLayer(
220 4096U,
221 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"),
222 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100223 .set_name("fc6")
224 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000225 // Layer 15
226 << FullyConnectedLayer(
227 4096U,
228 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"),
229 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100230 .set_name("fc7")
231 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000232 // Layer 16
233 << FullyConnectedLayer(
234 1000U,
235 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"),
236 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100237 .set_name("fc8")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000238 // Softmax
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100239 << SoftmaxLayer().set_name("prob")
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000240 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000241
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000242 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000243 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100244 config.use_tuner = (target == 2);
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000245 graph.finalize(target_hint, config);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100246 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000247 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100248 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000249 // Run graph
250 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100251 }
252
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000253private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000254 Stream graph{ 0, "VGG16" };
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000255};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100256
257/** Main program for VGG16
258 *
259 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +0100260 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100261 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000262int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100263{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000264 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100265}