blob: d0c823a11c9d5c7ccebca66b5fe288345dcac1a9 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
26#include "arm_compute/graph/SubGraph.h"
27#include "support/ToolchainSupport.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31#include <cstdlib>
32#include <tuple>
33
Anthony Barbierf45d5a92018-01-24 16:23:15 +000034using namespace arm_compute::utils;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000035using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37using namespace arm_compute::logging;
38
39namespace
40{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000041} // namespace
42
43/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
44 *
45 * @param[in] argc Number of arguments
46 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
47 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000048class GraphSqueezenetExample : public Example
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000050public:
51 void do_setup(int argc, char **argv) override
52 {
53 std::string data_path; /* Path to the trainable data */
54 std::string image; /* Image data */
55 std::string label; /* Label data */
Anthony Barbier8140e1e2017-12-14 23:48:46 +000056
Anthony Barbier06ea0482018-02-22 15:45:35 +000057 // Create a preprocessor object
58 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
59 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000060
Anthony Barbier06ea0482018-02-22 15:45:35 +000061 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
62 const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
63 TargetHint target_hint = set_target_hint(int_target_hint);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000064
Anthony Barbierf45d5a92018-01-24 16:23:15 +000065 // Parse arguments
66 if(argc < 2)
67 {
68 // Print help
69 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
70 std::cout << "No data folder provided: using random values\n\n";
71 }
72 else if(argc == 2)
73 {
74 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
75 std::cout << "No data folder provided: using random values\n\n";
76 }
77 else if(argc == 3)
78 {
79 data_path = argv[2];
80 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
81 std::cout << "No image provided: using random values\n\n";
82 }
83 else if(argc == 4)
84 {
85 data_path = argv[2];
86 image = argv[3];
87 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
88 std::cout << "No text file with labels provided: skipping output accessor\n\n";
89 }
90 else
91 {
92 data_path = argv[2];
93 image = argv[3];
94 label = argv[4];
95 }
96
97 graph << target_hint
98 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
Anthony Barbier06ea0482018-02-22 15:45:35 +000099 get_input_accessor(image, std::move(preprocessor)))
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000100 << ConvolutionLayer(
101 7U, 7U, 96U,
102 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
103 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
104 PadStrideInfo(2, 2, 0, 0))
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000105 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
106 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
107 << ConvolutionLayer(
108 1U, 1U, 16U,
109 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
110 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
111 PadStrideInfo(1, 1, 0, 0))
112 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
113 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
114 << ConvolutionLayer(
115 1U, 1U, 16U,
116 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
118 PadStrideInfo(1, 1, 0, 0))
119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
120 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
121 << ConvolutionLayer(
122 1U, 1U, 32U,
123 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
125 PadStrideInfo(1, 1, 0, 0))
126 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
127 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
128 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
129 << ConvolutionLayer(
130 1U, 1U, 32U,
131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
133 PadStrideInfo(1, 1, 0, 0))
134 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
135 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
136 << ConvolutionLayer(
137 1U, 1U, 48U,
138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"),
140 PadStrideInfo(1, 1, 0, 0))
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
142 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
143 << ConvolutionLayer(
144 1U, 1U, 48U,
145 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"),
147 PadStrideInfo(1, 1, 0, 0))
148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
149 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
150 << ConvolutionLayer(
151 1U, 1U, 64U,
152 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
154 PadStrideInfo(1, 1, 0, 0))
155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
156 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
157 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
158 << ConvolutionLayer(
159 1U, 1U, 64U,
160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
162 PadStrideInfo(1, 1, 0, 0))
163 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
164 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
165 << ConvolutionLayer(
166 1U, 1U, 1000U,
167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
169 PadStrideInfo(1, 1, 0, 0))
170 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
171 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
172 << FlattenLayer()
173 << SoftmaxLayer()
174 << Tensor(get_output_accessor(label, 5));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000175
176 // In order to enable the OpenCL tuner, graph_init() has to be called only when all nodes have been instantiated
177 graph.graph_init(int_target_hint == 2);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000178 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000179 void do_run() override
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000180 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000181 // Run graph
182 graph.run();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000183 }
184
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000185private:
186 Graph graph{};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000187
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000188 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
189 {
190 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
191 SubGraph i_a;
192 i_a << ConvolutionLayer(
193 1U, 1U, expand1_filt,
194 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
195 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
196 PadStrideInfo(1, 1, 0, 0))
197 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000198
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000199 SubGraph i_b;
200 i_b << ConvolutionLayer(
201 3U, 3U, expand3_filt,
202 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
203 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
204 PadStrideInfo(1, 1, 1, 1))
205 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
206
207 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
208 }
209};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000210
211/** Main program for Squeezenet v1.0
212 *
213 * @param[in] argc Number of arguments
214 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
215 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000216int main(int argc, char **argv)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000217{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000218 return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv);
219}