arm_compute v18.01
[platform/upstream/armcl.git] / examples / graph_vgg19.cpp
1 /*
2  * Copyright (c) 2017, 2018 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/Graph.h"
25 #include "arm_compute/graph/Nodes.h"
26 #include "support/ToolchainSupport.h"
27 #include "utils/GraphUtils.h"
28 #include "utils/Utils.h"
29
30 #include <cstdlib>
31
32 using namespace arm_compute::utils;
33 using namespace arm_compute::graph;
34 using namespace arm_compute::graph_utils;
35
36 /** Example demonstrating how to implement VGG19's network using the Compute Library's graph API
37  *
38  * @param[in] argc Number of arguments
39  * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
40  */
41 class GraphVGG19Example : public Example
42 {
43 public:
44     void do_setup(int argc, char **argv) override
45     {
46         std::string data_path; /* Path to the trainable data */
47         std::string image;     /* Image data */
48         std::string label;     /* Label data */
49
50         constexpr float mean_r = 123.68f;  /* Mean value to subtract from red channel */
51         constexpr float mean_g = 116.779f; /* Mean value to subtract from green channel */
52         constexpr float mean_b = 103.939f; /* Mean value to subtract from blue channel */
53
54         // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
55         TargetHint            target_hint      = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
56         ConvolutionMethodHint convolution_hint = ConvolutionMethodHint::DIRECT;
57
58         // Parse arguments
59         if(argc < 2)
60         {
61             // Print help
62             std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
63             std::cout << "No data folder provided: using random values\n\n";
64         }
65         else if(argc == 2)
66         {
67             std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
68             std::cout << "No data folder provided: using random values\n\n";
69         }
70         else if(argc == 3)
71         {
72             data_path = argv[2];
73             std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
74             std::cout << "No image provided: using random values\n\n";
75         }
76         else if(argc == 4)
77         {
78             data_path = argv[2];
79             image     = argv[3];
80             std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
81             std::cout << "No text file with labels provided: skipping output accessor\n\n";
82         }
83         else
84         {
85             data_path = argv[2];
86             image     = argv[3];
87             label     = argv[4];
88         }
89
90         graph << target_hint
91               << convolution_hint
92               << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
93                         get_input_accessor(image, mean_r, mean_g, mean_b))
94               // Layer 1
95               << ConvolutionLayer(
96                   3U, 3U, 64U,
97                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_w.npy"),
98                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_1_b.npy"),
99                   PadStrideInfo(1, 1, 1, 1))
100               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
101               << ConvolutionLayer(
102                   3U, 3U, 64U,
103                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_w.npy"),
104                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv1_2_b.npy"),
105                   PadStrideInfo(1, 1, 1, 1))
106               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
107               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
108               // Layer 2
109               << ConvolutionLayer(
110                   3U, 3U, 128U,
111                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_w.npy"),
112                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_1_b.npy"),
113                   PadStrideInfo(1, 1, 1, 1))
114               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
115               << ConvolutionLayer(
116                   3U, 3U, 128U,
117                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_w.npy"),
118                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv2_2_b.npy"),
119                   PadStrideInfo(1, 1, 1, 1))
120               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
121               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
122               // Layer 3
123               << ConvolutionLayer(
124                   3U, 3U, 256U,
125                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_w.npy"),
126                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_1_b.npy"),
127                   PadStrideInfo(1, 1, 1, 1))
128               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
129               << ConvolutionLayer(
130                   3U, 3U, 256U,
131                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_w.npy"),
132                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_2_b.npy"),
133                   PadStrideInfo(1, 1, 1, 1))
134               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
135               << ConvolutionLayer(
136                   3U, 3U, 256U,
137                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_w.npy"),
138                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_3_b.npy"),
139                   PadStrideInfo(1, 1, 1, 1))
140               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
141               << ConvolutionLayer(
142                   3U, 3U, 256U,
143                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_w.npy"),
144                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv3_4_b.npy"),
145                   PadStrideInfo(1, 1, 1, 1))
146               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
147               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
148               // Layer 4
149               << ConvolutionLayer(
150                   3U, 3U, 512U,
151                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_w.npy"),
152                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_1_b.npy"),
153                   PadStrideInfo(1, 1, 1, 1))
154               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
155               << ConvolutionLayer(
156                   3U, 3U, 512U,
157                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_w.npy"),
158                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_2_b.npy"),
159                   PadStrideInfo(1, 1, 1, 1))
160               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
161               << ConvolutionLayer(
162                   3U, 3U, 512U,
163                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_w.npy"),
164                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_3_b.npy"),
165                   PadStrideInfo(1, 1, 1, 1))
166               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
167               << ConvolutionLayer(
168                   3U, 3U, 512U,
169                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_w.npy"),
170                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv4_4_b.npy"),
171                   PadStrideInfo(1, 1, 1, 1))
172               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
173               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
174               // Layer 5
175               << ConvolutionLayer(
176                   3U, 3U, 512U,
177                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_w.npy"),
178                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_1_b.npy"),
179                   PadStrideInfo(1, 1, 1, 1))
180               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
181               << ConvolutionLayer(
182                   3U, 3U, 512U,
183                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_w.npy"),
184                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_2_b.npy"),
185                   PadStrideInfo(1, 1, 1, 1))
186               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
187               << ConvolutionLayer(
188                   3U, 3U, 512U,
189                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_w.npy"),
190                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_3_b.npy"),
191                   PadStrideInfo(1, 1, 1, 1))
192               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
193               << ConvolutionLayer(
194                   3U, 3U, 512U,
195                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_w.npy"),
196                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/conv5_4_b.npy"),
197                   PadStrideInfo(1, 1, 1, 1))
198               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
199               << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)))
200               // Layer 6
201               << FullyConnectedLayer(
202                   4096U,
203                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_w.npy"),
204                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc6_b.npy"))
205               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
206               // Layer 7
207               << FullyConnectedLayer(
208                   4096U,
209                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_w.npy"),
210                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc7_b.npy"))
211               << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
212               // Layer 8
213               << FullyConnectedLayer(
214                   1000U,
215                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_w.npy"),
216                   get_weights_accessor(data_path, "/cnn_data/vgg19_model/fc8_b.npy"))
217               // Softmax
218               << SoftmaxLayer()
219               << Tensor(get_output_accessor(label, 5));
220     }
221     void do_run() override
222     {
223         // Run graph
224         graph.run();
225     }
226
227 private:
228     Graph graph{};
229 };
230
231 /** Main program for VGG19
232  *
233  * @param[in] argc Number of arguments
234  * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
235  */
236 int main(int argc, char **argv)
237 {
238     return arm_compute::utils::run_example<GraphVGG19Example>(argc, argv);
239 }