blob: 189cc027fda93e4e9b3edf99b5a2744e696d8dbe [file] [log] [blame]
Isabella Gottardibc4484a2018-02-02 11:27:32 +00001/*
2 * Copyright (c) 2018 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 */
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
34using namespace arm_compute::utils;
35using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37using namespace arm_compute::logging;
38
39namespace
40{
41} // namespace
42
43/** Example demonstrating how to implement Squeezenet's v1.1 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 */
48class GraphSqueezenet_v1_1Example : public Example
49{
50public:
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 */
56
Georgios Pinitas140fdc72018-02-16 11:42:38 +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);
Isabella Gottardibc4484a2018-02-02 11:27:32 +000060
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +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);
Isabella Gottardibc4484a2018-02-02 11:27:32 +000064
65 // 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
Isabella Gottardibc4484a2018-02-02 11:27:32 +000097 graph << target_hint
98 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
Georgios Pinitas140fdc72018-02-16 11:42:38 +000099 get_input_accessor(image, std::move(preprocessor)))
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000100 << ConvolutionLayer(
101 3U, 3U, 64U,
102 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_w.npy"),
103 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_b.npy"),
104 PadStrideInfo(2, 2, 0, 0))
105 << 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_1_model/fire2_squeeze1x1_w.npy"),
110 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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_1_model/fire3_squeeze1x1_w.npy"),
117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
122 << ConvolutionLayer(
123 1U, 1U, 32U,
124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_b.npy"),
126 PadStrideInfo(1, 1, 0, 0))
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
129 << ConvolutionLayer(
130 1U, 1U, 32U,
131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
137 << ConvolutionLayer(
138 1U, 1U, 48U,
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
140 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_b.npy"),
141 PadStrideInfo(1, 1, 0, 0))
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
144 << ConvolutionLayer(
145 1U, 1U, 48U,
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_b.npy"),
148 PadStrideInfo(1, 1, 0, 0))
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
151 << ConvolutionLayer(
152 1U, 1U, 64U,
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_b.npy"),
155 PadStrideInfo(1, 1, 0, 0))
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
158 << ConvolutionLayer(
159 1U, 1U, 64U,
160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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_1_model/conv10_w.npy"),
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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));
Gian Marcoc1b6e372018-02-21 18:03:26 +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);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000178 }
179 void do_run() override
180 {
181 // Run graph
182 graph.run();
183 }
184
185private:
186 Graph graph{};
187
188 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_1_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));
198
199 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};
210
211/** Main program for Squeezenet v1.1
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 */
216int main(int argc, char **argv)
217{
218 return arm_compute::utils::run_example<GraphSqueezenet_v1_1Example>(argc, argv);
219}