blob: 8d64c1cdbb99615fcc26048f7342aada9e46502a [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 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;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000033/** Example demonstrating how to implement VGG19's network using the Compute Library's graph API
34 *
35 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +000036 * @param[in] argv Arguments
Anthony Barbier8140e1e2017-12-14 23:48:46 +000037 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000038class GraphVGG19Example : public Example
Anthony Barbier8140e1e2017-12-14 23:48:46 +000039{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000040public:
Jenkins52ba29e2018-08-29 15:32:11 +000041 GraphVGG19Example()
42 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG19")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000043 {
Jenkins52ba29e2018-08-29 15:32:11 +000044 }
45 bool do_setup(int argc, char **argv) override
46 {
47 // Parse arguments
48 cmd_parser.parse(argc, argv);
49
50 // Consume common parameters
51 common_params = consume_common_graph_parameters(common_opts);
52
53 // Return when help menu is requested
54 if(common_params.help)
55 {
56 cmd_parser.print_help(argv[0]);
57 return false;
58 }
59
60 // Checks
61 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
62
63 // Print parameter values
64 std::cout << common_params << std::endl;
65
66 // Get trainable parameters data path
67 std::string data_path = common_params.data_path;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000068
Anthony Barbier06ea0482018-02-22 15:45:35 +000069 // Create a preprocessor object
70 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
71 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000072
Jenkins52ba29e2018-08-29 15:32:11 +000073 // Create input descriptor
74 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
75 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
Jenkinsb3a371b2018-05-23 11:36:53 +010076
Jenkins52ba29e2018-08-29 15:32:11 +000077 // Set weights trained layout
78 const DataLayout weights_layout = DataLayout::NCHW;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000079
Jenkins52ba29e2018-08-29 15:32:11 +000080 graph << common_params.target
81 << common_params.fast_math_hint
82 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
Anthony Barbierf45d5a92018-01-24 16:23:15 +000083 // Layer 1
84 << ConvolutionLayer(
85 3U, 3U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +000086 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000087 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_b.npy"),
88 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +010089 .set_name("conv1_1")
90 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000091 << ConvolutionLayer(
92 3U, 3U, 64U,
Jenkins52ba29e2018-08-29 15:32:11 +000093 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000094 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_b.npy"),
95 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +010096 .set_name("conv1_2")
97 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
98 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +000099 // Layer 2
100 << ConvolutionLayer(
101 3U, 3U, 128U,
Jenkins52ba29e2018-08-29 15:32:11 +0000102 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000103 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_b.npy"),
104 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100105 .set_name("conv2_1")
106 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000107 << ConvolutionLayer(
108 3U, 3U, 128U,
Jenkins52ba29e2018-08-29 15:32:11 +0000109 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000110 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_b.npy"),
111 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100112 .set_name("conv2_2")
113 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
114 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000115 // Layer 3
116 << ConvolutionLayer(
117 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000118 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000119 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_b.npy"),
120 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100121 .set_name("conv3_1")
122 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000123 << ConvolutionLayer(
124 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000125 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000126 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_b.npy"),
127 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100128 .set_name("conv3_2")
129 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000130 << ConvolutionLayer(
131 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000132 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000133 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_b.npy"),
134 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100135 .set_name("conv3_3")
136 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000137 << ConvolutionLayer(
138 3U, 3U, 256U,
Jenkins52ba29e2018-08-29 15:32:11 +0000139 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000140 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_b.npy"),
141 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100142 .set_name("conv3_4")
143 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_4/Relu")
144 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000145 // Layer 4
146 << ConvolutionLayer(
147 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000148 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000149 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_b.npy"),
150 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100151 .set_name("conv4_1")
152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000153 << ConvolutionLayer(
154 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000155 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000156 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_b.npy"),
157 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100158 .set_name("conv4_2")
159 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000160 << ConvolutionLayer(
161 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000162 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000163 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_b.npy"),
164 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100165 .set_name("conv4_3")
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000167 << ConvolutionLayer(
168 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000169 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000170 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_b.npy"),
171 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100172 .set_name("conv4_4")
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_4/Relu")
174 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000175 // Layer 5
176 << ConvolutionLayer(
177 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000178 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000179 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_b.npy"),
180 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100181 .set_name("conv5_1")
182 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000183 << ConvolutionLayer(
184 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000185 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000186 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_b.npy"),
187 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100188 .set_name("conv5_2")
189 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000190 << ConvolutionLayer(
191 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000192 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000193 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_b.npy"),
194 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100195 .set_name("conv5_3")
196 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000197 << ConvolutionLayer(
198 3U, 3U, 512U,
Jenkins52ba29e2018-08-29 15:32:11 +0000199 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000200 get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_b.npy"),
201 PadStrideInfo(1, 1, 1, 1))
Jenkinsb3a371b2018-05-23 11:36:53 +0100202 .set_name("conv5_4")
203 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_4/Relu")
204 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000205 // Layer 6
206 << FullyConnectedLayer(
207 4096U,
Jenkins52ba29e2018-08-29 15:32:11 +0000208 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000209 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100210 .set_name("fc6")
211 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000212 // Layer 7
213 << FullyConnectedLayer(
214 4096U,
Jenkins52ba29e2018-08-29 15:32:11 +0000215 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000216 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100217 .set_name("fc7")
218 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000219 // Layer 8
220 << FullyConnectedLayer(
221 1000U,
Jenkins52ba29e2018-08-29 15:32:11 +0000222 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_w.npy", weights_layout),
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000223 get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_b.npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100224 .set_name("fc8")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000225 // Softmax
Jenkinsb3a371b2018-05-23 11:36:53 +0100226 << SoftmaxLayer().set_name("prob")
Jenkins52ba29e2018-08-29 15:32:11 +0000227 << OutputLayer(get_output_accessor(common_params, 5));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000228
Jenkinsb3a371b2018-05-23 11:36:53 +0100229 // Finalize graph
230 GraphConfig config;
Jenkins52ba29e2018-08-29 15:32:11 +0000231 config.num_threads = common_params.threads;
232 config.use_tuner = common_params.enable_tuner;
233 config.tuner_file = common_params.tuner_file;
234
235 graph.finalize(common_params.target, config);
236
237 return true;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000238 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000239 void do_run() override
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000240 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000241 // Run graph
242 graph.run();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000243 }
244
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000245private:
Jenkins52ba29e2018-08-29 15:32:11 +0000246 CommandLineParser cmd_parser;
247 CommonGraphOptions common_opts;
248 CommonGraphParams common_params;
249 Stream graph;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000250};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000251
252/** Main program for VGG19
253 *
Jenkins52ba29e2018-08-29 15:32:11 +0000254 * @note To list all the possible arguments execute the binary appended with the --help option
255 *
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000256 * @param[in] argc Number of arguments
Jenkins52ba29e2018-08-29 15:32:11 +0000257 * @param[in] argv Arguments
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000258 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000259int main(int argc, char **argv)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000260{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000261 return arm_compute::utils::run_example<GraphVGG19Example>(argc, argv);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000262}