2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph.h"
25 #include "support/ToolchainSupport.h"
26 #include "utils/GraphUtils.h"
27 #include "utils/Utils.h"
31 using namespace arm_compute::utils;
32 using namespace arm_compute::graph::frontend;
33 using namespace arm_compute::graph_utils;
35 /** Example demonstrating how to implement VGG16's network using the Compute Library's graph API
37 * @param[in] argc Number of arguments
38 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
40 class GraphVGG16Example : public Example
43 void do_setup(int argc, char **argv) override
45 std::string data_path; /* Path to the trainable data */
46 std::string image; /* Image data */
47 std::string label; /* Label data */
49 // Create a preprocessor object
50 const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } };
51 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
53 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
54 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
55 Target target_hint = set_target_hint(target);
56 const bool is_opencl = target_hint == Target::CL;
58 ConvolutionMethod first_convolution3x3_hint = is_opencl ? ConvolutionMethod::DIRECT : ConvolutionMethod::GEMM;
59 ConvolutionMethod convolution3x3_hint = ConvolutionMethod::DEFAULT;
60 FastMathHint fast_math_hint = FastMathHint::DISABLED;
66 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
67 std::cout << "No data folder provided: using random values\n\n";
71 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
72 std::cout << "No data folder provided: using random values\n\n";
77 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
78 std::cout << "No image provided: using random values\n\n";
84 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
85 std::cout << "No text file with labels provided: skipping output accessor\n\n";
92 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
93 std::cout << "No fast math info provided: disabling fast math\n\n";
100 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
105 << first_convolution3x3_hint
106 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
107 get_input_accessor(image, std::move(preprocessor)))
111 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"),
112 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"),
113 PadStrideInfo(1, 1, 1, 1))
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_1/Relu")
116 << convolution3x3_hint
120 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"),
121 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
124 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv1_2/Relu")
125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1")
129 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"),
130 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"),
131 PadStrideInfo(1, 1, 1, 1))
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_1/Relu")
137 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"),
138 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"),
139 PadStrideInfo(1, 1, 1, 1))
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv2_2/Relu")
142 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2")
146 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"),
148 PadStrideInfo(1, 1, 1, 1))
150 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_1/Relu")
154 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"),
155 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"),
156 PadStrideInfo(1, 1, 1, 1))
158 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_2/Relu")
162 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"),
163 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"),
164 PadStrideInfo(1, 1, 1, 1))
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv3_3/Relu")
167 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool3")
171 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"),
172 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"),
173 PadStrideInfo(1, 1, 1, 1))
175 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_1/Relu")
179 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"),
180 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"),
181 PadStrideInfo(1, 1, 1, 1))
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_2/Relu")
187 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"),
188 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"),
189 PadStrideInfo(1, 1, 1, 1))
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv4_3/Relu")
192 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool4")
196 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"),
197 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"),
198 PadStrideInfo(1, 1, 1, 1))
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_1/Relu")
204 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"),
205 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"),
206 PadStrideInfo(1, 1, 1, 1))
208 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_2/Relu")
212 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"),
213 get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"),
214 PadStrideInfo(1, 1, 1, 1))
216 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("conv5_3/Relu")
217 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool5")
219 << FullyConnectedLayer(
221 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"),
222 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy"))
224 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu")
226 << FullyConnectedLayer(
228 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"),
229 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy"))
231 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1")
233 << FullyConnectedLayer(
235 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"),
236 get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy"))
239 << SoftmaxLayer().set_name("prob")
240 << OutputLayer(get_output_accessor(label, 5));
244 config.use_tuner = (target == 2);
245 graph.finalize(target_hint, config);
247 void do_run() override
254 Stream graph{ 0, "VGG16" };
257 /** Main program for VGG16
259 * @param[in] argc Number of arguments
260 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
262 int main(int argc, char **argv)
264 return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv);