1 /*
2 * Copyright (c) 2017-2021 Arm Limited.
3 *
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 */
24 #include "arm_compute/graph.h"
25 #include "support/ToolchainSupport.h"
26 #include "utils/CommonGraphOptions.h"
27 #include "utils/GraphUtils.h"
28 #include "utils/Utils.h"
29
30 using namespace arm_compute::utils;
31 using namespace arm_compute::graph::frontend;
32 using namespace arm_compute::graph_utils;
33
34 /** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */
35 class GraphVGG16Example : public Example
36 {
37 public:
GraphVGG16Example()38 GraphVGG16Example()
39 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG16")
40 {
41 }
do_setup(int argc,char ** argv)42 bool do_setup(int argc, char **argv) override
43 {
44 // Parse arguments
45 cmd_parser.parse(argc, argv);
46 cmd_parser.validate();
47
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
58 // 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;
63
64 // Create a preprocessor object
65 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
66 std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<CaffePreproccessor>(mean_rgb);
67
68 // Create input descriptor
69 const auto operation_layout = common_params.data_layout;
70 const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout);
71 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout);
72
73 // Set weights trained layout
74 const DataLayout weights_layout = DataLayout::NCHW;
75
76 // Create graph
77 graph << common_params.target
78 << common_params.fast_math_hint
79 << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor)))
80 // Layer 1
81 << ConvolutionLayer(
82 3U, 3U, 64U,
83 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout),
84 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
85 PadStrideInfo(1, 1, 1, 1))
86 .set_name("conv1_1")
87 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
88 // Layer 2
89 << ConvolutionLayer(
90 3U, 3U, 64U,
91 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"),
93 PadStrideInfo(1, 1, 1, 1))
94 .set_name("conv1_2")
95 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
96 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
97 // Layer 3
98 << ConvolutionLayer(
99 3U, 3U, 128U,
100 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"),
102 PadStrideInfo(1, 1, 1, 1))
103 .set_name("conv2_1")
104 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
105 // Layer 4
106 << ConvolutionLayer(
107 3U, 3U, 128U,
108 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout),
109 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
110 PadStrideInfo(1, 1, 1, 1))
111 .set_name("conv2_2")
112 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
113 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
114 // Layer 5
115 << ConvolutionLayer(
116 3U, 3U, 256U,
117 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout),
118 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
119 PadStrideInfo(1, 1, 1, 1))
120 .set_name("conv3_1")
121 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
122 // Layer 6
123 << ConvolutionLayer(
124 3U, 3U, 256U,
125 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout),
126 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
127 PadStrideInfo(1, 1, 1, 1))
128 .set_name("conv3_2")
129 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
130 // Layer 7
131 << ConvolutionLayer(
132 3U, 3U, 256U,
133 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout),
134 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
135 PadStrideInfo(1, 1, 1, 1))
136 .set_name("conv3_3")
137 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
138 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
139 // Layer 8
140 << ConvolutionLayer(
141 3U, 3U, 512U,
142 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout),
143 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
144 PadStrideInfo(1, 1, 1, 1))
145 .set_name("conv4_1")
146 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
147 // Layer 9
148 << ConvolutionLayer(
149 3U, 3U, 512U,
150 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout),
151 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
152 PadStrideInfo(1, 1, 1, 1))
153 .set_name("conv4_2")
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
155 // Layer 10
156 << ConvolutionLayer(
157 3U, 3U, 512U,
158 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout),
159 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
160 PadStrideInfo(1, 1, 1, 1))
161 .set_name("conv4_3")
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
163 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
164 // Layer 11
165 << ConvolutionLayer(
166 3U, 3U, 512U,
167 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout),
168 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
169 PadStrideInfo(1, 1, 1, 1))
170 .set_name("conv5_1")
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
172 // Layer 12
173 << ConvolutionLayer(
174 3U, 3U, 512U,
175 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout),
176 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
177 PadStrideInfo(1, 1, 1, 1))
178 .set_name("conv5_2")
179 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
180 // Layer 13
181 << ConvolutionLayer(
182 3U, 3U, 512U,
183 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout),
184 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
185 PadStrideInfo(1, 1, 1, 1))
186 .set_name("conv5_3")
187 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
188 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
189 // Layer 14
190 << FullyConnectedLayer(
191 4096U,
192 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout),
193 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
194 .set_name("fc6")
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
196 // Layer 15
197 << FullyConnectedLayer(
198 4096U,
199 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout),
200 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
201 .set_name("fc7")
202 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
203 // Layer 16
204 << FullyConnectedLayer(
205 1000U,
206 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout),
207 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
208 .set_name("fc8")
209 // Softmax
210 << SoftmaxLayer().set_name("prob")
211 << OutputLayer(get_output_accessor(common_params, 5));
212
213 // Finalize graph
214 GraphConfig config;
215 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.mlgo_file = common_params.mlgo_file;
220 config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type);
221 config.synthetic_type = common_params.data_type;
222
223 graph.finalize(common_params.target, config);
224
225 return true;
226 }
do_run()227 void do_run() override
228 {
229 // Run graph
230 graph.run();
231 }
232
233 private:
234 CommandLineParser cmd_parser;
235 CommonGraphOptions common_opts;
236 CommonGraphParams common_params;
237 Stream graph;
238 };
239
240 /** Main program for VGG16
241 *
242 * Model is based on:
243 * https://arxiv.org/abs/1409.1556
244 * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
245 * Karen Simonyan, Andrew Zisserman
246 *
247 * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
248 *
249 * @note To list all the possible arguments execute the binary appended with the --help option
250 *
251 * @param[in] argc Number of arguments
252 * @param[in] argv Arguments
253 */
main(int argc,char ** argv)254 int main(int argc, char **argv)
255 {
256 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
257 }
258