blob: d58bf6cbf5422a8ee22cf7d4cfeebf9ab121ab93 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
Jenkinsb3a371b2018-05-23 11:36:53 +010024#include "arm_compute/graph.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000025#include "support/ToolchainSupport.h"
Jenkins52ba29e2018-08-29 15:32:11 +000026#include "utils/CommonGraphOptions.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000027#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
Anthony Barbierf45d5a92018-01-24 16:23:15 +000030using namespace arm_compute::utils;
Jenkinsb3a371b2018-05-23 11:36:53 +010031using namespace arm_compute::graph::frontend;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000032using namespace arm_compute::graph_utils;
33
Jenkinsb9abeae2018-11-22 11:58:08 +000034/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000035class GraphVGG16Example : public Example
Anthony Barbier8140e1e2017-12-14 23:48:46 +000036{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000037public:
Jenkins52ba29e2018-08-29 15:32:11 +000038 GraphVGG16Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG16")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000040 {
Jenkins52ba29e2018-08-29 15:32:11 +000041 }
42 bool do_setup(int argc, char **argv) override
43 {
Jenkinsb9abeae2018-11-22 11:58:08 +000044 // 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
Jenkins52ba29e2018-08-29 15:32:11 +000054 // Parse arguments
55 cmd_parser.parse(argc, argv);
Jenkins0e205f72019-11-28 16:53:35 +000056 cmd_parser.validate();
Jenkins52ba29e2018-08-29 15:32:11 +000057
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
69 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
70
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;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000076
Anthony Barbier06ea0482018-02-22 15:45:35 +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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000080
Jenkins52ba29e2018-08-29 15:32:11 +000081 // 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);
Anthony Barbier06ea0482018-02-22 15:45:35 +000084
Jenkins52ba29e2018-08-29 15:32:11 +000085 // Set weights trained layout
86 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000087
Jenkins52ba29e2018-08-29 15:32:11 +000088 // Create graph
89 graph << common_params.target
90 << common_params.fast_math_hint
91 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000092 // Layer 1
93 << ConvolutionLayer(
94 3U, 3U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +000095 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
97 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +010098 .set_name("conv1_1")
99 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000100 // Layer 2
101 << ConvolutionLayer(
102 3U, 3U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +0000103 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000104 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
105 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +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")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000109 // Layer 3
110 << ConvolutionLayer(
111 3U, 3U, 128U,
Jenkins52ba29e2018-08-29 15:32:11 +0000112 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000113 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
114 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100115 .set_name("conv2_1")
116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000117 // Layer 4
118 << ConvolutionLayer(
119 3U, 3U, 128U,
Jenkins52ba29e2018-08-29 15:32:11 +0000120 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000121 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +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")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000126 // Layer 5
127 << ConvolutionLayer(
128 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000129 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000130 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
131 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100132 .set_name("conv3_1")
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000134 // Layer 6
135 << ConvolutionLayer(
136 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000138 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
139 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100140 .set_name("conv3_2")
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000142 // Layer 7
143 << ConvolutionLayer(
144 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000145 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000146 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
147 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +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")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000151 // Layer 8
152 << ConvolutionLayer(
153 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000155 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
156 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100157 .set_name("conv4_1")
158 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000159 // Layer 9
160 << ConvolutionLayer(
161 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000163 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
164 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100165 .set_name("conv4_2")
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000167 // Layer 10
168 << ConvolutionLayer(
169 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000171 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
172 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +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")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000176 // Layer 11
177 << ConvolutionLayer(
178 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000179 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000180 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
181 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100182 .set_name("conv5_1")
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000184 // Layer 12
185 << ConvolutionLayer(
186 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000187 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000188 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
189 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100190 .set_name("conv5_2")
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000192 // Layer 13
193 << ConvolutionLayer(
194 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000195 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000196 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
197 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +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")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000201 // Layer 14
202 << FullyConnectedLayer(
203 4096U,
Jenkins52ba29e2018-08-29 15:32:11 +0000204 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000205 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100206 .set_name("fc6")
207 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000208 // Layer 15
209 << FullyConnectedLayer(
210 4096U,
Jenkins52ba29e2018-08-29 15:32:11 +0000211 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000212 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100213 .set_name("fc7")
214 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000215 // Layer 16
216 << FullyConnectedLayer(
217 1000U,
Jenkins52ba29e2018-08-29 15:32:11 +0000218 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000219 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100220 .set_name("fc8")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000221 // Softmax
Jenkinsb3a371b2018-05-23 11:36:53 +0100222 << SoftmaxLayer().set_name("prob")
Jenkins52ba29e2018-08-29 15:32:11 +0000223 << OutputLayer(get_output_accessor(common_params, 5));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000224
Jenkinsb3a371b2018-05-23 11:36:53 +0100225 // Finalize graph
226 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000227 config.num_threads = common_params.threads;
228 config.use_tuner = common_params.enable_tuner;
Jenkins4ba87db2019-05-23 17:11:51 +0100229 config.tuner_mode = common_params.tuner_mode;
Jenkins52ba29e2018-08-29 15:32:11 +0000230 config.tuner_file = common_params.tuner_file;
231
232 graph.finalize(common_params.target, config);
233
234 return true;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000235 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000236 void do_run() override
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000237 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000238 // Run graph
239 graph.run();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000240 }
241
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000242private:
Jenkins52ba29e2018-08-29 15:32:11 +0000243 CommandLineParser cmd_parser;
244 CommonGraphOptions common_opts;
245 CommonGraphParams common_params;
246 Stream graph;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000247};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000248
249/** Main program for VGG16
250 *
Jenkinsb9abeae2018-11-22 11:58:08 +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 *
Jenkins514be652019-02-28 12:25:18 +0000256 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
257 *
Jenkins52ba29e2018-08-29 15:32:11 +0000258 * @note To list all the possible arguments execute the binary appended with the --help option
259 *
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000260 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000261 * @param[in] argv Arguments
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000262 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000263int main(int argc, char **argv)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000264{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000265 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000266}