blob: 2dba67f5eb98f34125820ad732d397c76dceb7aa [file] [log] [blame]
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01003 *
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010030#include <tuple>
31
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033using namespace arm_compute::graph::frontend;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010034using namespace arm_compute::graph_utils;
35
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010036/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API
37 *
38 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +010039 * @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) )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010040 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041class GraphGooglenetExample : public Example
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010042{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043public:
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 */
Isabella Gottardia4c61882017-11-03 12:11:55 +000049
Georgios Pinitas140fdc72018-02-16 11:42:38 +000050 // Create a preprocessor object
51 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
52 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010053
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000054 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Gian Marco Iodicea8aef292018-05-14 14:21:39 +010055 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
56 Target target_hint = set_target_hint(target);
57 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Gian Marcobfa3b522017-12-12 10:08:38 +000058
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000059 // Parse arguments
60 if(argc < 2)
61 {
62 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +010063 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000064 std::cout << "No data folder provided: using random values\n\n";
65 }
66 else if(argc == 2)
67 {
Giorgio Arena59631a12018-05-02 13:59:04 +010068 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000069 std::cout << "No data folder provided: using random values\n\n";
70 }
71 else if(argc == 3)
72 {
73 data_path = argv[2];
Giorgio Arena59631a12018-05-02 13:59:04 +010074 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000075 std::cout << "No image provided: using random values\n\n";
76 }
77 else if(argc == 4)
78 {
79 data_path = argv[2];
80 image = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +010081 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000082 std::cout << "No text file with labels provided: skipping output accessor\n\n";
83 }
Giorgio Arena59631a12018-05-02 13:59:04 +010084 else if(argc == 5)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 {
86 data_path = argv[2];
87 image = argv[3];
88 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +010089 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
90 std::cout << "No fast math info provided: disabling fast math\n\n";
91 }
92 else
93 {
94 data_path = argv[2];
95 image = argv[3];
96 label = argv[4];
97 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000098 }
99
100 graph << target_hint
Giorgio Arena59631a12018-05-02 13:59:04 +0100101 << fast_math_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000102 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
103 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000104 << ConvolutionLayer(
105 7U, 7U, 64U,
106 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_w.npy"),
107 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_b.npy"),
108 PadStrideInfo(2, 2, 3, 3))
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
110 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
111 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000112 << ConvolutionLayer(
113 1U, 1U, 64U,
114 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_w.npy"),
115 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_b.npy"),
116 PadStrideInfo(1, 1, 0, 0))
117 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
118 << ConvolutionLayer(
119 3U, 3U, 192U,
120 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_w.npy"),
121 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_b.npy"),
122 PadStrideInfo(1, 1, 1, 1))
123 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
124 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)));
126 graph << get_inception_node(data_path, "inception_3a", 64, std::make_tuple(96U, 128U), std::make_tuple(16U, 32U), 32U);
127 graph << get_inception_node(data_path, "inception_3b", 128, std::make_tuple(128U, 192U), std::make_tuple(32U, 96U), 64U);
128 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)));
129 graph << get_inception_node(data_path, "inception_4a", 192, std::make_tuple(96U, 208U), std::make_tuple(16U, 48U), 64U);
130 graph << get_inception_node(data_path, "inception_4b", 160, std::make_tuple(112U, 224U), std::make_tuple(24U, 64U), 64U);
131 graph << get_inception_node(data_path, "inception_4c", 128, std::make_tuple(128U, 256U), std::make_tuple(24U, 64U), 64U);
132 graph << get_inception_node(data_path, "inception_4d", 112, std::make_tuple(144U, 288U), std::make_tuple(32U, 64U), 64U);
133 graph << get_inception_node(data_path, "inception_4e", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U);
134 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)));
135 graph << get_inception_node(data_path, "inception_5a", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U);
136 graph << get_inception_node(data_path, "inception_5b", 384, std::make_tuple(192U, 384U), std::make_tuple(48U, 128U), 128U);
137 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 7, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000138 << FullyConnectedLayer(
139 1000U,
140 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_w.npy"),
141 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_b.npy"))
142 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000143 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000144
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000145 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000146 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100147 config.use_tuner = (target == 2);
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000148 graph.finalize(target_hint, config);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100149 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000150 void do_run() override
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100151 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000152 // Run graph
153 graph.run();
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100154 }
155
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000156private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000157 Stream graph{ 0, "GoogleNet" };
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100158
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000159 BranchLayer get_inception_node(const std::string &data_path, std::string &&param_path,
160 unsigned int a_filt,
161 std::tuple<unsigned int, unsigned int> b_filters,
162 std::tuple<unsigned int, unsigned int> c_filters,
163 unsigned int d_filt)
164 {
165 std::string total_path = "/cnn_data/googlenet_model/" + param_path + "/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000166 SubStream i_a(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000167 i_a << ConvolutionLayer(
168 1U, 1U, a_filt,
169 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
170 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
171 PadStrideInfo(1, 1, 0, 0))
172 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100173
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000174 SubStream i_b(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000175 i_b << ConvolutionLayer(
176 1U, 1U, std::get<0>(b_filters),
177 get_weights_accessor(data_path, total_path + "3x3_reduce_w.npy"),
178 get_weights_accessor(data_path, total_path + "3x3_reduce_b.npy"),
179 PadStrideInfo(1, 1, 0, 0))
180 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
181 << ConvolutionLayer(
182 3U, 3U, std::get<1>(b_filters),
183 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
184 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
185 PadStrideInfo(1, 1, 1, 1))
186 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
187
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000188 SubStream i_c(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000189 i_c << ConvolutionLayer(
190 1U, 1U, std::get<0>(c_filters),
191 get_weights_accessor(data_path, total_path + "5x5_reduce_w.npy"),
192 get_weights_accessor(data_path, total_path + "5x5_reduce_b.npy"),
193 PadStrideInfo(1, 1, 0, 0))
194 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
195 << ConvolutionLayer(
196 5U, 5U, std::get<1>(c_filters),
197 get_weights_accessor(data_path, total_path + "5x5_w.npy"),
198 get_weights_accessor(data_path, total_path + "5x5_b.npy"),
199 PadStrideInfo(1, 1, 2, 2))
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
201
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000202 SubStream i_d(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000203 i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL)))
204 << ConvolutionLayer(
205 1U, 1U, d_filt,
206 get_weights_accessor(data_path, total_path + "pool_proj_w.npy"),
207 get_weights_accessor(data_path, total_path + "pool_proj_b.npy"),
208 PadStrideInfo(1, 1, 0, 0))
209 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
210
211 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d));
212 }
213};
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100214
215/** Main program for Googlenet
216 *
217 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100218 * @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) )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100219 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000220int main(int argc, char **argv)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100221{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000222 return arm_compute::utils::run_example<GraphGooglenetExample>(argc, argv);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100223}