blob: 290d1e7e98b5890511cc1c040c09b7a1b0275cda [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"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010026#include "utils/CommonGraphOptions.h"
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000030using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031using namespace arm_compute::graph::frontend;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010032using namespace arm_compute::graph_utils;
33
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010034/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000035class GraphVGG16Example : public Example
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010036{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000037public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010038 GraphVGG16Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG16")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000040 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010041 }
42 bool do_setup(int argc, char **argv) override
43 {
Pablo Tello0cf77982018-10-24 15:32:39 +010044 // Check if the system has enough RAM to run the example, systems with less than 2GB have
45 // to hint the API to minimize memory consumption otherwise it'll run out of memory and
46 // fail throwing the bad_alloc exception
47 arm_compute::MEMInfo meminfo;
48 const size_t mem_total = meminfo.get_total_in_kb();
49 if(mem_total <= arm_compute::MEMInfo::TWO_GB_IN_KB)
50 {
51 arm_compute::MEMInfo::set_policy(arm_compute::MemoryPolicy::MINIMIZE);
52 }
53
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010054 // Parse arguments
55 cmd_parser.parse(argc, argv);
56
57 // Consume common parameters
58 common_params = consume_common_graph_parameters(common_opts);
59
60 // Return when help menu is requested
61 if(common_params.help)
62 {
63 cmd_parser.print_help(argv[0]);
64 return false;
65 }
66
67 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010068 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010069
70 // Print parameter values
71 std::cout << common_params << std::endl;
72
73 // Get trainable parameters data path
74 std::string data_path = common_params.data_path;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010075
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076 // Create a preprocessor object
77 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
78 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010079
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010080 // Create input descriptor
81 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
82 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
83
84 // Set weights trained layout
85 const DataLayout weights_layout = DataLayout::NCHW;
86
87 // Create graph
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010088 graph << common_params.target
89 << common_params.fast_math_hint
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010090 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000091 // Layer 1
92 << ConvolutionLayer(
93 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010094 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000095 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
96 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010097 .set_name("conv1_1")
98 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000099 // Layer 2
100 << ConvolutionLayer(
101 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100102 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000103 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
104 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100105 .set_name("conv1_2")
106 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
107 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000108 // Layer 3
109 << ConvolutionLayer(
110 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100111 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000112 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
113 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100114 .set_name("conv2_1")
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000116 // Layer 4
117 << ConvolutionLayer(
118 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100119 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000120 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
121 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100122 .set_name("conv2_2")
123 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
124 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000125 // Layer 5
126 << ConvolutionLayer(
127 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100128 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000129 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
130 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100131 .set_name("conv3_1")
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000133 // Layer 6
134 << ConvolutionLayer(
135 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100136 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
138 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100139 .set_name("conv3_2")
140 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000141 // Layer 7
142 << ConvolutionLayer(
143 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100144 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000145 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
146 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100147 .set_name("conv3_3")
148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
149 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000150 // Layer 8
151 << ConvolutionLayer(
152 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100153 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
155 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100156 .set_name("conv4_1")
157 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000158 // Layer 9
159 << ConvolutionLayer(
160 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100161 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
163 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100164 .set_name("conv4_2")
165 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000166 // Layer 10
167 << ConvolutionLayer(
168 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100169 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
171 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100172 .set_name("conv4_3")
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
174 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000175 // Layer 11
176 << ConvolutionLayer(
177 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100178 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000179 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
180 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100181 .set_name("conv5_1")
182 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000183 // Layer 12
184 << ConvolutionLayer(
185 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100186 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000187 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
188 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100189 .set_name("conv5_2")
190 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000191 // Layer 13
192 << ConvolutionLayer(
193 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100194 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000195 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
196 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100197 .set_name("conv5_3")
198 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
199 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000200 // Layer 14
201 << FullyConnectedLayer(
202 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100203 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000204 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100205 .set_name("fc6")
206 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000207 // Layer 15
208 << FullyConnectedLayer(
209 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100210 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000211 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100212 .set_name("fc7")
213 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000214 // Layer 16
215 << FullyConnectedLayer(
216 1000U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100217 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000218 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100219 .set_name("fc8")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000220 // Softmax
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100221 << SoftmaxLayer().set_name("prob")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100222 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000223
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000224 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000225 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100226 config.num_threads = common_params.threads;
227 config.use_tuner = common_params.enable_tuner;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100228 config.tuner_file = common_params.tuner_file;
229
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100230 graph.finalize(common_params.target, config);
231
232 return true;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100233 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000234 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100235 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000236 // Run graph
237 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100238 }
239
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000240private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100241 CommandLineParser cmd_parser;
242 CommonGraphOptions common_opts;
243 CommonGraphParams common_params;
244 Stream graph;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000245};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100246
247/** Main program for VGG16
248 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000249 * Model is based on:
250 * https://arxiv.org/abs/1409.1556
251 * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
252 * Karen Simonyan, Andrew Zisserman
253 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000254 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
255 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100256 * @note To list all the possible arguments execute the binary appended with the --help option
257 *
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100258 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100259 * @param[in] argv Arguments
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100260 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000261int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100262{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000263 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100264}