blob: d08382ab8ec5779a6f5eed35843f11c5491d8976 [file] [log] [blame]
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01001/*
2 * Copyright (c) 2017 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 */
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
34using namespace arm_compute::graph;
35using namespace arm_compute::graph_utils;
36
Gian Marcobfa3b522017-12-12 10:08:38 +000037namespace
38{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010039BranchLayer get_inception_node(const std::string &data_path, std::string &&param_path,
40 unsigned int a_filt,
41 std::tuple<unsigned int, unsigned int> b_filters,
42 std::tuple<unsigned int, unsigned int> c_filters,
43 unsigned int d_filt)
44{
45 std::string total_path = "/cnn_data/googlenet_model/" + param_path + "/" + param_path + "_";
46 SubGraph i_a;
47 i_a << ConvolutionLayer(
48 1U, 1U, a_filt,
Isabella Gottardia4c61882017-11-03 12:11:55 +000049 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
50 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010051 PadStrideInfo(1, 1, 0, 0))
52 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
53
54 SubGraph i_b;
55 i_b << ConvolutionLayer(
56 1U, 1U, std::get<0>(b_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000057 get_weights_accessor(data_path, total_path + "3x3_reduce_w.npy"),
58 get_weights_accessor(data_path, total_path + "3x3_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010059 PadStrideInfo(1, 1, 0, 0))
60 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
61 << ConvolutionLayer(
62 3U, 3U, std::get<1>(b_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000063 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
64 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010065 PadStrideInfo(1, 1, 1, 1))
66 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
67
68 SubGraph i_c;
69 i_c << ConvolutionLayer(
70 1U, 1U, std::get<0>(c_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000071 get_weights_accessor(data_path, total_path + "5x5_reduce_w.npy"),
72 get_weights_accessor(data_path, total_path + "5x5_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010073 PadStrideInfo(1, 1, 0, 0))
74 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
75 << ConvolutionLayer(
76 5U, 5U, std::get<1>(c_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000077 get_weights_accessor(data_path, total_path + "5x5_w.npy"),
78 get_weights_accessor(data_path, total_path + "5x5_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010079 PadStrideInfo(1, 1, 2, 2))
80 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
81
82 SubGraph i_d;
83 i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL)))
84 << ConvolutionLayer(
85 1U, 1U, d_filt,
Isabella Gottardia4c61882017-11-03 12:11:55 +000086 get_weights_accessor(data_path, total_path + "pool_proj_w.npy"),
87 get_weights_accessor(data_path, total_path + "pool_proj_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010088 PadStrideInfo(1, 1, 0, 0))
89 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
90
91 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d));
92}
Gian Marcobfa3b522017-12-12 10:08:38 +000093} // namespace
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010094
95/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API
96 *
97 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000098 * @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 +010099 */
100void main_graph_googlenet(int argc, const char **argv)
101{
Isabella Gottardia4c61882017-11-03 12:11:55 +0000102 std::string data_path; /* Path to the trainable data */
103 std::string image; /* Image data */
104 std::string label; /* Label data */
105
106 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
107 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
108 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100109
Gian Marcobfa3b522017-12-12 10:08:38 +0000110 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
111 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
112 ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
113
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100114 // Parse arguments
115 if(argc < 2)
116 {
117 // Print help
Gian Marcobfa3b522017-12-12 10:08:38 +0000118 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100119 std::cout << "No data folder provided: using random values\n\n";
120 }
121 else if(argc == 2)
122 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000123 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
124 std::cout << "No data folder provided: using random values\n\n";
Isabella Gottardia4c61882017-11-03 12:11:55 +0000125 }
126 else if(argc == 3)
127 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000128 data_path = argv[2];
129 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
130 std::cout << "No image provided: using random values\n\n";
131 }
132 else if(argc == 4)
133 {
134 data_path = argv[2];
135 image = argv[3];
136 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
137 std::cout << "No text file with labels provided: skipping output accessor\n\n";
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100138 }
139 else
140 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000141 data_path = argv[2];
142 image = argv[3];
143 label = argv[4];
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100144 }
145
146 Graph graph;
147
Gian Marcobfa3b522017-12-12 10:08:38 +0000148 graph << target_hint
Isabella Gottardia4c61882017-11-03 12:11:55 +0000149 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
150 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100151 << ConvolutionLayer(
152 7U, 7U, 64U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000153 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100155 PadStrideInfo(2, 2, 3, 3))
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
158 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
Gian Marcobfa3b522017-12-12 10:08:38 +0000159 << convolution_hint
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100160 << ConvolutionLayer(
161 1U, 1U, 64U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000162 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_w.npy"),
163 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100164 PadStrideInfo(1, 1, 0, 0))
165 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
166 << ConvolutionLayer(
167 3U, 3U, 192U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000168 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_w.npy"),
169 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100170 PadStrideInfo(1, 1, 1, 1))
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
172 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
173 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
174 << get_inception_node(data_path, "inception_3a", 64, std::make_tuple(96U, 128U), std::make_tuple(16U, 32U), 32U)
175 << get_inception_node(data_path, "inception_3b", 128, std::make_tuple(128U, 192U), std::make_tuple(32U, 96U), 64U)
176 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
177 << get_inception_node(data_path, "inception_4a", 192, std::make_tuple(96U, 208U), std::make_tuple(16U, 48U), 64U)
178 << get_inception_node(data_path, "inception_4b", 160, std::make_tuple(112U, 224U), std::make_tuple(24U, 64U), 64U)
179 << get_inception_node(data_path, "inception_4c", 128, std::make_tuple(128U, 256U), std::make_tuple(24U, 64U), 64U)
180 << get_inception_node(data_path, "inception_4d", 112, std::make_tuple(144U, 288U), std::make_tuple(32U, 64U), 64U)
181 << get_inception_node(data_path, "inception_4e", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
182 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
183 << get_inception_node(data_path, "inception_5a", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
184 << get_inception_node(data_path, "inception_5b", 384, std::make_tuple(192U, 384U), std::make_tuple(48U, 128U), 128U)
185 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 7, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL)))
186 << FullyConnectedLayer(
187 1000U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000188 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_w.npy"),
189 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_b.npy"))
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100190 << SoftmaxLayer()
Isabella Gottardia4c61882017-11-03 12:11:55 +0000191 << Tensor(get_output_accessor(label, 5));
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100192
193 graph.run();
194}
195
196/** Main program for Googlenet
197 *
198 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000199 * @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 +0100200 */
201int main(int argc, const char **argv)
202{
203 return arm_compute::utils::run_example(argc, argv, main_graph_googlenet);
204}