blob: 990040b5ef1bc1587904206b2343b74f3d465d6e [file] [log] [blame]
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 {
44 // Parse arguments
45 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010046 cmd_parser.validate();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010047
48 // Consume common parameters
49 common_params = consume_common_graph_parameters(common_opts);
50
51 // Return when help menu is requested
52 if(common_params.help)
53 {
54 cmd_parser.print_help(argv[0]);
55 return false;
56 }
57
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058 // Print parameter values
59 std::cout << common_params << std::endl;
60
61 // Get trainable parameters data path
62 std::string data_path = common_params.data_path;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010063
Georgios Pinitas140fdc72018-02-16 11:42:38 +000064 // Create a preprocessor object
65 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
66 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010067
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010068 // Create input descriptor
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000069 const auto operation_layout = common_params.data_layout;
70 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, operation_layout);
71 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010072
73 // Set weights trained layout
74 const DataLayout weights_layout = DataLayout::NCHW;
75
76 // Create graph
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010077 graph << common_params.target
78 << common_params.fast_math_hint
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010079 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000080 // Layer 1
81 << ConvolutionLayer(
82 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010083 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000084 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
85 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010086 .set_name("conv1_1")
87 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000088 // Layer 2
89 << ConvolutionLayer(
90 3U, 3U, 64U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010091 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000092 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
93 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010094 .set_name("conv1_2")
95 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000096 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000097 // Layer 3
98 << ConvolutionLayer(
99 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100100 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000101 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
102 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100103 .set_name("conv2_1")
104 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000105 // Layer 4
106 << ConvolutionLayer(
107 3U, 3U, 128U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100108 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000109 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
110 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100111 .set_name("conv2_2")
112 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000113 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114 // Layer 5
115 << ConvolutionLayer(
116 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100117 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000118 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
119 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100120 .set_name("conv3_1")
121 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000122 // Layer 6
123 << ConvolutionLayer(
124 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100125 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000126 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
127 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100128 .set_name("conv3_2")
129 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000130 // Layer 7
131 << ConvolutionLayer(
132 3U, 3U, 256U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100133 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000134 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
135 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100136 .set_name("conv3_3")
137 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000138 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000139 // Layer 8
140 << ConvolutionLayer(
141 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100142 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000143 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
144 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100145 .set_name("conv4_1")
146 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000147 // Layer 9
148 << ConvolutionLayer(
149 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100150 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000151 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
152 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100153 .set_name("conv4_2")
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000155 // Layer 10
156 << ConvolutionLayer(
157 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100158 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000159 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
160 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100161 .set_name("conv4_3")
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000163 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000164 // Layer 11
165 << ConvolutionLayer(
166 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100167 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000168 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
169 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100170 .set_name("conv5_1")
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000172 // Layer 12
173 << ConvolutionLayer(
174 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100175 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000176 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
177 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100178 .set_name("conv5_2")
179 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000180 // Layer 13
181 << ConvolutionLayer(
182 3U, 3U, 512U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100183 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000184 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
185 PadStrideInfo(1, 1, 1, 1))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100186 .set_name("conv5_3")
187 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000188 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000189 // Layer 14
190 << FullyConnectedLayer(
191 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000193 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100194 .set_name("fc6")
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000196 // Layer 15
197 << FullyConnectedLayer(
198 4096U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100199 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000200 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100201 .set_name("fc7")
202 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000203 // Layer 16
204 << FullyConnectedLayer(
205 1000U,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100206 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000207 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100208 .set_name("fc8")
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000209 // Softmax
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100210 << SoftmaxLayer().set_name("prob")
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100211 << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000212
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000213 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000214 GraphConfig config;
Georgios Pinitasf4261ad2019-12-02 11:58:19 +0000215 config.num_threads = common_params.threads;
216 config.use_tuner = common_params.enable_tuner;
217 config.tuner_mode = common_params.tuner_mode;
218 config.tuner_file = common_params.tuner_file;
219 config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8);
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100220
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100221 graph.finalize(common_params.target, config);
222
223 return true;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100224 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000225 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100226 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000227 // Run graph
228 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100229 }
230
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000231private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100232 CommandLineParser cmd_parser;
233 CommonGraphOptions common_opts;
234 CommonGraphParams common_params;
235 Stream graph;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000236};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100237
238/** Main program for VGG16
239 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000240 * Model is based on:
241 * https://arxiv.org/abs/1409.1556
242 * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
243 * Karen Simonyan, Andrew Zisserman
244 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000245 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
246 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100247 * @note To list all the possible arguments execute the binary appended with the --help option
248 *
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100249 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100250 * @param[in] argv Arguments
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100251 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000252int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100253{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000254 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100255}