blob: 92a592390e34a04561565f523a65bc4f9872975d [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
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000097 // Initialize graph
98 graph.graph_init(int_target_hint == 2);
99
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000100 graph << target_hint
101 << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32),
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000102 get_input_accessor(image, std::move(preprocessor)))
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000103 << ConvolutionLayer(
104 3U, 3U, 64U,
105 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_w.npy"),
106 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_b.npy"),
107 PadStrideInfo(2, 2, 0, 0))
108 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
109 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
110 << ConvolutionLayer(
111 1U, 1U, 16U,
112 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_w.npy"),
113 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_b.npy"),
114 PadStrideInfo(1, 1, 0, 0))
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
116 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
117 << ConvolutionLayer(
118 1U, 1U, 16U,
119 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_w.npy"),
120 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_b.npy"),
121 PadStrideInfo(1, 1, 0, 0))
122 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
123 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
124 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
125 << ConvolutionLayer(
126 1U, 1U, 32U,
127 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
128 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_b.npy"),
129 PadStrideInfo(1, 1, 0, 0))
130 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
131 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
132 << ConvolutionLayer(
133 1U, 1U, 32U,
134 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
135 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_b.npy"),
136 PadStrideInfo(1, 1, 0, 0))
137 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
138 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
139 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
140 << ConvolutionLayer(
141 1U, 1U, 48U,
142 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
143 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_b.npy"),
144 PadStrideInfo(1, 1, 0, 0))
145 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
146 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
147 << ConvolutionLayer(
148 1U, 1U, 48U,
149 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_w.npy"),
150 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_b.npy"),
151 PadStrideInfo(1, 1, 0, 0))
152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
153 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
154 << ConvolutionLayer(
155 1U, 1U, 64U,
156 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_w.npy"),
157 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_b.npy"),
158 PadStrideInfo(1, 1, 0, 0))
159 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
160 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
161 << ConvolutionLayer(
162 1U, 1U, 64U,
163 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
164 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_b.npy"),
165 PadStrideInfo(1, 1, 0, 0))
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
167 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
168 << ConvolutionLayer(
169 1U, 1U, 1000U,
170 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_w.npy"),
171 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_b.npy"),
172 PadStrideInfo(1, 1, 0, 0))
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
174 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
175 << FlattenLayer()
176 << SoftmaxLayer()
177 << Tensor(get_output_accessor(label, 5));
178 }
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}