blob: 72ac9694b1735450ac9e9e90a889558610f88796 [file] [log] [blame]
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +01001/*
SiCong Li4841c972021-02-03 12:17:35 +00002 * Copyright (c) 2017-2021 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010026#include "support/ToolchainSupport.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010027#include "utils/CommonGraphOptions.h"
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010028#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000031using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032using namespace arm_compute::graph::frontend;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010033using namespace arm_compute::graph_utils;
34
Georgios Pinitas108ab0b2018-09-14 18:35:11 +010035/** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000036class GraphVGG16Example : public Example
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +010037{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000038public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 GraphVGG16Example() : 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (common_params.help)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010053 {
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 const std::array<float, 3> mean_rgb{{123.68f, 116.779f, 103.939f}};
Georgios Pinitas40f51a62020-11-21 03:04:18 +000066 std::unique_ptr<IPreprocessor> preprocessor = std::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;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 const TensorShape tensor_shape =
71 permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
72 TensorDescriptor input_descriptor =
73 TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010074
75 // Set weights trained layout
76 const DataLayout weights_layout = DataLayout::NCHW;
77
78 // Create graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 graph
80 << common_params.target << common_params.fast_math_hint
81 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
82 // Layer 1
83 << ConvolutionLayer(
84 3U, 3U, 64U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
85 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"), PadStrideInfo(1, 1, 1, 1))
86 .set_name("conv1_1")
87 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
88 .set_name("conv1_1/Relu")
89 // Layer 2
90 << ConvolutionLayer(
91 3U, 3U, 64U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout),
92 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"), PadStrideInfo(1, 1, 1, 1))
93 .set_name("conv1_2")
94 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
95 .set_name("conv1_2/Relu")
96 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0)))
97 .set_name("pool1")
98 // Layer 3
99 << ConvolutionLayer(
100 3U, 3U, 128U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout),
101 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"), PadStrideInfo(1, 1, 1, 1))
102 .set_name("conv2_1")
103 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
104 .set_name("conv2_1/Relu")
105 // Layer 4
106 << ConvolutionLayer(
107 3U, 3U, 128U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
108 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"), PadStrideInfo(1, 1, 1, 1))
109 .set_name("conv2_2")
110 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
111 .set_name("conv2_2/Relu")
112 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0)))
113 .set_name("pool2")
114 // Layer 5
115 << ConvolutionLayer(
116 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
117 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"), PadStrideInfo(1, 1, 1, 1))
118 .set_name("conv3_1")
119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
120 .set_name("conv3_1/Relu")
121 // Layer 6
122 << ConvolutionLayer(
123 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
124 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"), PadStrideInfo(1, 1, 1, 1))
125 .set_name("conv3_2")
126 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
127 .set_name("conv3_2/Relu")
128 // Layer 7
129 << ConvolutionLayer(
130 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
131 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"), PadStrideInfo(1, 1, 1, 1))
132 .set_name("conv3_3")
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
134 .set_name("conv3_3/Relu")
135 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0)))
136 .set_name("pool3")
137 // Layer 8
138 << ConvolutionLayer(
139 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
140 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"), PadStrideInfo(1, 1, 1, 1))
141 .set_name("conv4_1")
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 .set_name("conv4_1/Relu")
144 // Layer 9
145 << ConvolutionLayer(
146 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
147 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"), PadStrideInfo(1, 1, 1, 1))
148 .set_name("conv4_2")
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 .set_name("conv4_2/Relu")
151 // Layer 10
152 << ConvolutionLayer(
153 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"), PadStrideInfo(1, 1, 1, 1))
155 .set_name("conv4_3")
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 .set_name("conv4_3/Relu")
158 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0)))
159 .set_name("pool4")
160 // Layer 11
161 << ConvolutionLayer(
162 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
163 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"), PadStrideInfo(1, 1, 1, 1))
164 .set_name("conv5_1")
165 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
166 .set_name("conv5_1/Relu")
167 // Layer 12
168 << ConvolutionLayer(
169 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
170 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"), PadStrideInfo(1, 1, 1, 1))
171 .set_name("conv5_2")
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
173 .set_name("conv5_2/Relu")
174 // Layer 13
175 << ConvolutionLayer(
176 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
177 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"), PadStrideInfo(1, 1, 1, 1))
178 .set_name("conv5_3")
179 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
180 .set_name("conv5_3/Relu")
181 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0)))
182 .set_name("pool5")
183 // Layer 14
184 << FullyConnectedLayer(4096U,
185 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
186 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
187 .set_name("fc6")
188 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
189 // Layer 15
190 << FullyConnectedLayer(4096U,
191 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
193 .set_name("fc7")
194 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
195 // Layer 16
196 << FullyConnectedLayer(1000U,
197 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
198 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
199 .set_name("fc8")
200 // Softmax
201 << SoftmaxLayer().set_name("prob") << OutputLayer(get_output_accessor(common_params, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000202
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000203 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000204 GraphConfig config;
SiCongLif466d752021-03-01 15:26:18 +0000205 config.num_threads = common_params.threads;
206 config.use_tuner = common_params.enable_tuner;
207 config.tuner_mode = common_params.tuner_mode;
208 config.tuner_file = common_params.tuner_file;
209 config.mlgo_file = common_params.mlgo_file;
210 config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type);
211 config.synthetic_type = common_params.data_type;
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100212
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100213 graph.finalize(common_params.target, config);
214
215 return true;
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100216 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000217 void do_run() override
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100218 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000219 // Run graph
220 graph.run();
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100221 }
222
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000223private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100224 CommandLineParser cmd_parser;
225 CommonGraphOptions common_opts;
226 CommonGraphParams common_params;
227 Stream graph;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000228};
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100229
230/** Main program for VGG16
231 *
Georgios Pinitasbdbbbe82018-11-07 16:06:47 +0000232 * Model is based on:
233 * https://arxiv.org/abs/1409.1556
234 * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
235 * Karen Simonyan, Andrew Zisserman
236 *
Georgios Pinitas588ebc52018-12-21 13:39:07 +0000237 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
238 *
Georgios Pinitas9f28b392018-07-18 20:01:53 +0100239 * @note To list all the possible arguments execute the binary appended with the --help option
240 *
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100241 * @param[in] argc Number of arguments
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100242 * @param[in] argv Arguments
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100243 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000244int main(int argc, char **argv)
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100245{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000246 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
Gian Marco Iodicee10bddb2017-10-11 15:03:26 +0100247}