Compute Library
 21.02
graph_vgg16.cpp
Go to the documentation of this file.
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"
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:
38  GraphVGG16Example()
39  : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG16")
40  {
41  }
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, 1U), 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
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
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
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
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")
196  // Layer 15
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")
203  // Layer 16
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.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8);
221 
222  graph.finalize(common_params.target, config);
223 
224  return true;
225  }
226  void do_run() override
227  {
228  // Run graph
229  graph.run();
230  }
231 
232 private:
233  CommandLineParser cmd_parser;
234  CommonGraphOptions common_opts;
235  CommonGraphParams common_params;
236  Stream graph;
237 };
238 
239 /** Main program for VGG16
240  *
241  * Model is based on:
242  * https://arxiv.org/abs/1409.1556
243  * "Very Deep Convolutional Networks for Large-Scale Image Recognition"
244  * Karen Simonyan, Andrew Zisserman
245  *
246  * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
247  *
248  * @note To list all the possible arguments execute the binary appended with the --help option
249  *
250  * @param[in] argc Number of arguments
251  * @param[in] argv Arguments
252  */
253 int main(int argc, char **argv)
254 {
255  return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);
256 }
Graph configuration structure Device target types.
Definition: Types.h:80
CLTunerMode tuner_mode
Tuner mode to be used by the CL tuner.
Definition: Types.h:87
std::unique_ptr< graph::ITensorAccessor > get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, std::unique_ptr< IPreprocessor > preprocessor=nullptr, bool bgr=true)
Generates appropriate input accessor according to the specified graph parameters. ...
Definition: GraphUtils.h:497
bool convert_to_uint8
Convert graph to a synthetic uint8 graph.
Definition: Types.h:86
void consume_common_graph_parameters(CommonGraphValidateOptions &options, CommonParams &common_params)
Consumes the consume_common_graph_parameters graph options and creates a structure containing any inf...
Includes all the Graph headers at once.
Common command line options used to configure the graph examples.
Class to parse command line arguments.
std::string mlgo_file
Filename to load MLGO heuristics from.
Definition: Types.h:90
std::string tuner_file
File to load/store tuning values from.
Definition: Types.h:89
quantized, asymmetric fixed-point 8-bit number unsigned
int main(int argc, char **argv)
Main program for VGG16.
Abstract Example class.
Definition: Utils.h:78
Num samples, channels, height, width.
TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
Permutes a given tensor shape given the input and output data layout.
Definition: GraphUtils.h:664
TensorDescriptor & set_layout(DataLayout data_layout)
Sets tensor descriptor data layout.
Structure holding all the common graph parameters.
std::unique_ptr< graph::ITensorAccessor > get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, size_t top_n=5, bool is_validation=false, std::ostream &output_stream=std::cout)
Generates appropriate output accessor according to the specified graph parameters.
Definition: GraphUtils.h:543
bool use_tuner
Use a tuner in tunable backends.
Definition: Types.h:85
std::unique_ptr< graph::ITensorAccessor > get_weights_accessor(const std::string &path, const std::string &data_file, DataLayout file_layout=DataLayout::NCHW)
Generates appropriate weights accessor according to the specified path.
Definition: GraphUtils.h:475
int num_threads
Number of threads to use (thread capable backends), if 0 the backend will auto-initialize, if -1 the backend will stay as it is.
Definition: Types.h:88
Stream frontend class to construct simple graphs in a stream fashion.
Definition: Stream.h:45
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
ILayer & set_name(std::string name)
Sets the name of the layer.
Definition: ILayer.h:55