blob: d58bf6cbf5422a8ee22cf7d4cfeebf9ab121ab93 [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
68 // Checks
Anthony Barbiercdd68c02018-08-23 15:03:41 +010069 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 +010070
71 // Print parameter values
72 std::cout << common_params << std::endl;
73
74 // Get trainable parameters data path
75 std::string data_path = common_params.data_path;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010076
Georgios Pinitas140fdc72018-02-16 11:42:38 +000077 // Create a preprocessor object
78 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
79 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010080
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010081 // Create input descriptor
82 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
83 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
84
85 // Set weights trained layout
86 const DataLayout weights_layout = DataLayout::NCHW;
87
88 // Create graph
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010089 graph << common_params.target
90 << common_params.fast_math_hint
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010091 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000092 // Layer 1
93 << ConvolutionLayer(
94 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010095 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000096 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
97 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010098 .set_name("conv1_1")
99 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000100 // Layer 2
101 << ConvolutionLayer(
102 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100103 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000104 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
105 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100106 .set_name("conv1_2")
107 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
108 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000109 // Layer 3
110 << ConvolutionLayer(
111 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100112 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
114 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100115 .set_name("conv2_1")
116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000117 // Layer 4
118 << ConvolutionLayer(
119 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100120 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000121 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100123 .set_name("conv2_2")
124 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000126 // Layer 5
127 << ConvolutionLayer(
128 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100129 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000130 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
131 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100132 .set_name("conv3_1")
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000134 // Layer 6
135 << ConvolutionLayer(
136 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000138 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
139 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100140 .set_name("conv3_2")
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000142 // Layer 7
143 << ConvolutionLayer(
144 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100145 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000146 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
147 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100148 .set_name("conv3_3")
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
150 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000151 // Layer 8
152 << ConvolutionLayer(
153 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000155 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
156 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100157 .set_name("conv4_1")
158 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000159 // Layer 9
160 << ConvolutionLayer(
161 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000163 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
164 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100165 .set_name("conv4_2")
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000167 // Layer 10
168 << ConvolutionLayer(
169 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000171 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
172 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100173 .set_name("conv4_3")
174 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
175 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000176 // Layer 11
177 << ConvolutionLayer(
178 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100179 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000180 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
181 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100182 .set_name("conv5_1")
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000184 // Layer 12
185 << ConvolutionLayer(
186 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100187 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000188 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
189 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100190 .set_name("conv5_2")
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000192 // Layer 13
193 << ConvolutionLayer(
194 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100195 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000196 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
197 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100198 .set_name("conv5_3")
199 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
200 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000201 // Layer 14
202 << FullyConnectedLayer(
203 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100204 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000205 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100206 .set_name("fc6")
207 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000208 // Layer 15
209 << FullyConnectedLayer(
210 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100211 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000212 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100213 .set_name("fc7")
214 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000215 // Layer 16
216 << FullyConnectedLayer(
217 1000U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100218 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000219 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100220 .set_name("fc8")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000221 // Softmax
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100222 << SoftmaxLayer().set_name("prob")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100223 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000224
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000225 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000226 GraphConfig config;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100227 config.num_threads = common_params.threads;
228 config.use_tuner = common_params.enable_tuner;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100229 config.tuner_mode = common_params.tuner_mode;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100230 config.tuner_file = common_params.tuner_file;
231
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100232 graph.finalize(common_params.target, config);
233
234 return true;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100235 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000236 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100237 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000238 // Run graph
239 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100240 }
241
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000242private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100243 CommandLineParser cmd_parser;
244 CommonGraphOptions common_opts;
245 CommonGraphParams common_params;
246 Stream graph;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000247};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100248
249/** Main program for VGG16
250 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000251 * Model is based on:
252 * https://arxiv.org/abs/1409.1556
253 * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
254 * Karen Simonyan, Andrew Zisserman
255 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000256 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
257 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100258 * @note To list all the possible arguments execute the binary appended with the --help option
259 *
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100260 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100261 * @param[in] argv Arguments
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100262 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000263int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100264{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000265 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100266}