blob: e116633a02aa2e0e02ae8d124903c00e0609bef4 [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
25#include "arm_compute/graph/Graph.h"
26#include "arm_compute/graph/Nodes.h"
27#include "arm_compute/graph/SubGraph.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010028#include "support/ToolchainSupport.h"
29#include "utils/GraphUtils.h"
30#include "utils/Utils.h"
31
32#include <cstdlib>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010033#include <tuple>
34
35using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010038BranchLayer get_inception_node(const std::string &data_path, std::string &&param_path,
39 unsigned int a_filt,
40 std::tuple<unsigned int, unsigned int> b_filters,
41 std::tuple<unsigned int, unsigned int> c_filters,
42 unsigned int d_filt)
43{
44 std::string total_path = "/cnn_data/googlenet_model/" + param_path + "/" + param_path + "_";
45 SubGraph i_a;
46 i_a << ConvolutionLayer(
47 1U, 1U, a_filt,
Isabella Gottardia4c61882017-11-03 12:11:55 +000048 get_weights_accessor(data_path, total_path + "1x1_w.npy"),
49 get_weights_accessor(data_path, total_path + "1x1_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010050 PadStrideInfo(1, 1, 0, 0))
51 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
52
53 SubGraph i_b;
54 i_b << ConvolutionLayer(
55 1U, 1U, std::get<0>(b_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000056 get_weights_accessor(data_path, total_path + "3x3_reduce_w.npy"),
57 get_weights_accessor(data_path, total_path + "3x3_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010058 PadStrideInfo(1, 1, 0, 0))
59 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
60 << ConvolutionLayer(
61 3U, 3U, std::get<1>(b_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000062 get_weights_accessor(data_path, total_path + "3x3_w.npy"),
63 get_weights_accessor(data_path, total_path + "3x3_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010064 PadStrideInfo(1, 1, 1, 1))
65 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
66
67 SubGraph i_c;
68 i_c << ConvolutionLayer(
69 1U, 1U, std::get<0>(c_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000070 get_weights_accessor(data_path, total_path + "5x5_reduce_w.npy"),
71 get_weights_accessor(data_path, total_path + "5x5_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010072 PadStrideInfo(1, 1, 0, 0))
73 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
74 << ConvolutionLayer(
75 5U, 5U, std::get<1>(c_filters),
Isabella Gottardia4c61882017-11-03 12:11:55 +000076 get_weights_accessor(data_path, total_path + "5x5_w.npy"),
77 get_weights_accessor(data_path, total_path + "5x5_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010078 PadStrideInfo(1, 1, 2, 2))
79 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
80
81 SubGraph i_d;
82 i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL)))
83 << ConvolutionLayer(
84 1U, 1U, d_filt,
Isabella Gottardia4c61882017-11-03 12:11:55 +000085 get_weights_accessor(data_path, total_path + "pool_proj_w.npy"),
86 get_weights_accessor(data_path, total_path + "pool_proj_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010087 PadStrideInfo(1, 1, 0, 0))
88 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
89
90 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d));
91}
92
93/** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API
94 *
95 * @param[in] argc Number of arguments
Isabella Gottardia4c61882017-11-03 12:11:55 +000096 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010097 */
98void main_graph_googlenet(int argc, const char **argv)
99{
Isabella Gottardia4c61882017-11-03 12:11:55 +0000100 std::string data_path; /* Path to the trainable data */
101 std::string image; /* Image data */
102 std::string label; /* Label data */
103
104 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
105 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
106 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100107
108 // Parse arguments
109 if(argc < 2)
110 {
111 // Print help
Isabella Gottardia4c61882017-11-03 12:11:55 +0000112 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100113 std::cout << "No data folder provided: using random values\n\n";
114 }
115 else if(argc == 2)
116 {
117 //Do something with argv[1]
118 data_path = argv[1];
Isabella Gottardia4c61882017-11-03 12:11:55 +0000119 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
120 std::cout << "No image provided: using random values\n";
121 }
122 else if(argc == 3)
123 {
124 data_path = argv[1];
125 image = argv[2];
126 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
127 std::cout << "No text file with labels provided: skipping output accessor\n";
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100128 }
129 else
130 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100131 data_path = argv[1];
Isabella Gottardia4c61882017-11-03 12:11:55 +0000132 image = argv[2];
133 label = argv[3];
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100134 }
135
136 // Check if OpenCL is available and initialize the scheduler
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000137 TargetHint hint = TargetHint::NEON;
138 if(Graph::opencl_is_available())
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100139 {
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000140 hint = TargetHint::OPENCL;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100141 }
142
143 Graph graph;
144
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000145 graph << hint
Isabella Gottardia4c61882017-11-03 12:11:55 +0000146 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
147 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100148 << ConvolutionLayer(
149 7U, 7U, 64U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000150 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_w.npy"),
151 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100152 PadStrideInfo(2, 2, 3, 3))
153 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
154 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
155 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
156 << ConvolutionLayer(
157 1U, 1U, 64U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000158 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_w.npy"),
159 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100160 PadStrideInfo(1, 1, 0, 0))
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
162 << ConvolutionLayer(
163 3U, 3U, 192U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000164 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_w.npy"),
165 get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_b.npy"),
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100166 PadStrideInfo(1, 1, 1, 1))
167 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
168 << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f))
169 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
170 << get_inception_node(data_path, "inception_3a", 64, std::make_tuple(96U, 128U), std::make_tuple(16U, 32U), 32U)
171 << get_inception_node(data_path, "inception_3b", 128, std::make_tuple(128U, 192U), std::make_tuple(32U, 96U), 64U)
172 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
173 << get_inception_node(data_path, "inception_4a", 192, std::make_tuple(96U, 208U), std::make_tuple(16U, 48U), 64U)
174 << get_inception_node(data_path, "inception_4b", 160, std::make_tuple(112U, 224U), std::make_tuple(24U, 64U), 64U)
175 << get_inception_node(data_path, "inception_4c", 128, std::make_tuple(128U, 256U), std::make_tuple(24U, 64U), 64U)
176 << get_inception_node(data_path, "inception_4d", 112, std::make_tuple(144U, 288U), std::make_tuple(32U, 64U), 64U)
177 << get_inception_node(data_path, "inception_4e", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
178 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
179 << get_inception_node(data_path, "inception_5a", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U)
180 << get_inception_node(data_path, "inception_5b", 384, std::make_tuple(192U, 384U), std::make_tuple(48U, 128U), 128U)
181 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 7, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL)))
182 << FullyConnectedLayer(
183 1000U,
Isabella Gottardia4c61882017-11-03 12:11:55 +0000184 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_w.npy"),
185 get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_b.npy"))
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100186 << SoftmaxLayer()
Isabella Gottardia4c61882017-11-03 12:11:55 +0000187 << Tensor(get_output_accessor(label, 5));
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100188
189 graph.run();
190}
191
192/** Main program for Googlenet
193 *
194 * @param[in] argc Number of arguments
Isabella Gottardia4c61882017-11-03 12:11:55 +0000195 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100196 */
197int main(int argc, const char **argv)
198{
199 return arm_compute::utils::run_example(argc, argv, main_graph_googlenet);
200}