blob: 1b2f0d6f3bca288f06c0d923a5657bc189f91da8 [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 Pinitase2c82fe2017-10-02 18:51:47 +010024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
26#include "arm_compute/graph/SubGraph.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010027#include "support/ToolchainSupport.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31#include <cstdlib>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010032#include <tuple>
33
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000034using namespace arm_compute::utils;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010035using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010038/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API
39 *
40 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000041 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010042 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043class GraphGooglenetExample : public Example
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010044{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000045public:
46 void do_setup(int argc, char **argv) override
47 {
48 std::string data_path; /* Path to the trainable data */
49 std::string image; /* Image data */
50 std::string label; /* Label data */
Isabella Gottardia4c61882017-11-03 12:11:55 +000051
Georgios Pinitas140fdc72018-02-16 11:42:38 +000052 // Create a preprocessor object
53 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
54 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010055
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000056 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
57 const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
58 TargetHint target_hint = set_target_hint(int_target_hint);
Gian Marco36a0a462018-01-12 10:21:40 +000059 ConvolutionMethodHint convolution_hint = ConvolutionMethodHint::GEMM;
Gian Marcobfa3b522017-12-12 10:08:38 +000060
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000061 // Parse arguments
62 if(argc < 2)
63 {
64 // Print help
65 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
66 std::cout << "No data folder provided: using random values\n\n";
67 }
68 else if(argc == 2)
69 {
70 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
71 std::cout << "No data folder provided: using random values\n\n";
72 }
73 else if(argc == 3)
74 {
75 data_path = argv[2];
76 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
77 std::cout << "No image provided: using random values\n\n";
78 }
79 else if(argc == 4)
80 {
81 data_path = argv[2];
82 image = argv[3];
83 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
84 std::cout << "No text file with labels provided: skipping output accessor\n\n";
85 }
86 else
87 {
88 data_path = argv[2];
89 image = argv[3];
90 label = argv[4];
91 }
92
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000093 // Initialize graph
94 graph.graph_init(int_target_hint == 2);
95
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000096 graph << target_hint
97 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
Georgios Pinitas140fdc72018-02-16 11:42:38 +000098 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000099 << ConvolutionLayer(
100 7U, 7U, 64U,
101 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_w.npy"),
102 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_b.npy"),
103 PadStrideInfo(2, 2, 3, 3))
104 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
105 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
106 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
107 << convolution_hint
108 << ConvolutionLayer(
109 1U, 1U, 64U,
110 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_w.npy"),
111 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_b.npy"),
112 PadStrideInfo(1, 1, 0, 0))
113 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
114 << ConvolutionLayer(
115 3U, 3U, 192U,
116 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_w.npy"),
117 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_b.npy"),
118 PadStrideInfo(1, 1, 1, 1))
119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
120 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
121 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
122 << get_inception_node(data_path, "inception_3a", 64, std::make_tuple(96U, 128U), std::make_tuple(16U, 32U), 32U)
123 << get_inception_node(data_path, "inception_3b", 128, std::make_tuple(128U, 192U), std::make_tuple(32U, 96U), 64U)
124 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
125 << get_inception_node(data_path, "inception_4a", 192, std::make_tuple(96U, 208U), std::make_tuple(16U, 48U), 64U)
126 << get_inception_node(data_path, "inception_4b", 160, std::make_tuple(112U, 224U), std::make_tuple(24U, 64U), 64U)
127 << get_inception_node(data_path, "inception_4c", 128, std::make_tuple(128U, 256U), std::make_tuple(24U, 64U), 64U)
128 << get_inception_node(data_path, "inception_4d", 112, std::make_tuple(144U, 288U), std::make_tuple(32U, 64U), 64U)
129 << get_inception_node(data_path, "inception_4e", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
130 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
131 << get_inception_node(data_path, "inception_5a", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
132 << get_inception_node(data_path, "inception_5b", 384, std::make_tuple(192U, 384U), std::make_tuple(48U, 128U), 128U)
133 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 7, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL)))
134 << FullyConnectedLayer(
135 1000U,
136 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_w.npy"),
137 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_b.npy"))
138 << SoftmaxLayer()
139 << Tensor(get_output_accessor(label, 5));
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100140 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000141 void do_run() override
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100142 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000143 // Run graph
144 graph.run();
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100145 }
146
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000147private:
148 Graph graph{};
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100149
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000150 BranchLayer get_inception_node(const std::string &data_path, std::string &&param_path,
151 unsigned int a_filt,
152 std::tuple<unsigned int, unsigned int> b_filters,
153 std::tuple<unsigned int, unsigned int> c_filters,
154 unsigned int d_filt)
155 {
156 std::string total_path = "/cnn_data/googlenet_model/" + param_path + "/" + param_path + "_";
157 SubGraph i_a;
158 i_a << ConvolutionLayer(
159 1U, 1U, a_filt,
160 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
161 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
162 PadStrideInfo(1, 1, 0, 0))
163 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100164
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000165 SubGraph i_b;
166 i_b << ConvolutionLayer(
167 1U, 1U, std::get<0>(b_filters),
168 get_weights_accessor(data_path, total_path + "3x3_reduce_w.npy"),
169 get_weights_accessor(data_path, total_path + "3x3_reduce_b.npy"),
170 PadStrideInfo(1, 1, 0, 0))
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
172 << ConvolutionLayer(
173 3U, 3U, std::get<1>(b_filters),
174 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
175 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
176 PadStrideInfo(1, 1, 1, 1))
177 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
178
179 SubGraph i_c;
180 i_c << ConvolutionLayer(
181 1U, 1U, std::get<0>(c_filters),
182 get_weights_accessor(data_path, total_path + "5x5_reduce_w.npy"),
183 get_weights_accessor(data_path, total_path + "5x5_reduce_b.npy"),
184 PadStrideInfo(1, 1, 0, 0))
185 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
186 << ConvolutionLayer(
187 5U, 5U, std::get<1>(c_filters),
188 get_weights_accessor(data_path, total_path + "5x5_w.npy"),
189 get_weights_accessor(data_path, total_path + "5x5_b.npy"),
190 PadStrideInfo(1, 1, 2, 2))
191 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
192
193 SubGraph i_d;
194 i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL)))
195 << ConvolutionLayer(
196 1U, 1U, d_filt,
197 get_weights_accessor(data_path, total_path + "pool_proj_w.npy"),
198 get_weights_accessor(data_path, total_path + "pool_proj_b.npy"),
199 PadStrideInfo(1, 1, 0, 0))
200 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
201
202 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d));
203 }
204};
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100205
206/** Main program for Googlenet
207 *
208 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000209 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100210 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000211int main(int argc, char **argv)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100212{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000213 return arm_compute::utils::run_example<GraphGooglenetExample>(argc, argv);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100214}