blob: 2c7f614f64767106281b59ddb7fcc03090d9298e [file] [log] [blame]
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +01001/*
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +01002 * Copyright (c) 2017-2019 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);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010056 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010057
58 // Consume common parameters
59 common_params = consume_common_graph_parameters(common_opts);
60
61 // Return when help menu is requested
62 if(common_params.help)
63 {
64 cmd_parser.print_help(argv[0]);
65 return false;
66 }
67
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010068 // Print parameter values
69 std::cout << common_params << std::endl;
70
71 // Get trainable parameters data path
72 std::string data_path = common_params.data_path;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010073
Georgios Pinitas140fdc72018-02-16 11:42:38 +000074 // Create a preprocessor object
75 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
76 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010077
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010078 // Create input descriptor
79 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
80 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
81
82 // Set weights trained layout
83 const DataLayout weights_layout = DataLayout::NCHW;
84
85 // Create graph
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010086 graph << common_params.target
87 << common_params.fast_math_hint
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010088 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000089 // Layer 1
90 << ConvolutionLayer(
91 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010092 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000093 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
94 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010095 .set_name("conv1_1")
96 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000097 // Layer 2
98 << ConvolutionLayer(
99 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100100 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000101 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
102 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100103 .set_name("conv1_2")
104 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
105 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000106 // Layer 3
107 << ConvolutionLayer(
108 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100109 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000110 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
111 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100112 .set_name("conv2_1")
113 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114 // Layer 4
115 << ConvolutionLayer(
116 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100117 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000118 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
119 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100120 .set_name("conv2_2")
121 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
122 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000123 // Layer 5
124 << ConvolutionLayer(
125 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100126 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000127 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
128 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100129 .set_name("conv3_1")
130 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000131 // Layer 6
132 << ConvolutionLayer(
133 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100134 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000135 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
136 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100137 .set_name("conv3_2")
138 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000139 // Layer 7
140 << ConvolutionLayer(
141 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100142 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000143 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
144 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100145 .set_name("conv3_3")
146 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
147 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000148 // Layer 8
149 << ConvolutionLayer(
150 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100151 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000152 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
153 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100154 .set_name("conv4_1")
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000156 // Layer 9
157 << ConvolutionLayer(
158 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100159 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000160 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
161 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100162 .set_name("conv4_2")
163 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000164 // Layer 10
165 << ConvolutionLayer(
166 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100167 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000168 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
169 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100170 .set_name("conv4_3")
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
172 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000173 // Layer 11
174 << ConvolutionLayer(
175 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100176 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000177 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
178 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100179 .set_name("conv5_1")
180 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000181 // Layer 12
182 << ConvolutionLayer(
183 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100184 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000185 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
186 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100187 .set_name("conv5_2")
188 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000189 // Layer 13
190 << ConvolutionLayer(
191 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000193 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
194 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100195 .set_name("conv5_3")
196 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
197 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000198 // Layer 14
199 << FullyConnectedLayer(
200 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100201 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000202 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100203 .set_name("fc6")
204 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000205 // Layer 15
206 << FullyConnectedLayer(
207 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100208 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000209 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100210 .set_name("fc7")
211 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000212 // Layer 16
213 << FullyConnectedLayer(
214 1000U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100215 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000216 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100217 .set_name("fc8")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000218 // Softmax
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100219 << SoftmaxLayer().set_name("prob")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100220 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000221
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000222 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000223 GraphConfig config;
Georgios Pinitasf4261ad2019-12-02 11:58:19 +0000224 config.num_threads = common_params.threads;
225 config.use_tuner = common_params.enable_tuner;
226 config.tuner_mode = common_params.tuner_mode;
227 config.tuner_file = common_params.tuner_file;
228 config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8);
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100229
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}