blob: 2bdb00d7c2c5d0a140756fa001a951363aa9bad4 [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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph.h"
Isabella Gottardibc4484a2018-02-02 11:27:32 +000025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30#include <tuple>
31
32using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033using namespace arm_compute::graph::frontend;
Isabella Gottardibc4484a2018-02-02 11:27:32 +000034using namespace arm_compute::graph_utils;
Isabella Gottardibc4484a2018-02-02 11:27:32 +000035
36namespace
37{
38} // namespace
39
40/** Example demonstrating how to implement Squeezenet's v1.1 network using the Compute Library's graph API
41 *
42 * @param[in] argc Number of arguments
43 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
44 */
45class GraphSqueezenet_v1_1Example : public Example
46{
47public:
48 void do_setup(int argc, char **argv) override
49 {
50 std::string data_path; /* Path to the trainable data */
51 std::string image; /* Image data */
52 std::string label; /* Label data */
53
Georgios Pinitas140fdc72018-02-16 11:42:38 +000054 // Create a preprocessor object
55 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
56 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Isabella Gottardibc4484a2018-02-02 11:27:32 +000057
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000058 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000059 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010060 Target target_hint = set_target_hint(target);
Isabella Gottardibc4484a2018-02-02 11:27:32 +000061
Georgios Pinitas28705162018-03-21 20:10:53 +000062 ConvolutionMethod convolution_hint = (target_hint == Target::CL) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
63
Isabella Gottardibc4484a2018-02-02 11:27:32 +000064 // Parse arguments
65 if(argc < 2)
66 {
67 // Print help
68 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
69 std::cout << "No data folder provided: using random values\n\n";
70 }
71 else if(argc == 2)
72 {
73 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
74 std::cout << "No data folder provided: using random values\n\n";
75 }
76 else if(argc == 3)
77 {
78 data_path = argv[2];
79 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
80 std::cout << "No image provided: using random values\n\n";
81 }
82 else if(argc == 4)
83 {
84 data_path = argv[2];
85 image = argv[3];
86 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
87 std::cout << "No text file with labels provided: skipping output accessor\n\n";
88 }
89 else
90 {
91 data_path = argv[2];
92 image = argv[3];
93 label = argv[4];
94 }
95
Isabella Gottardibc4484a2018-02-02 11:27:32 +000096 graph << target_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +000097 << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32),
98 get_input_accessor(image, std::move(preprocessor)))
Georgios Pinitas28705162018-03-21 20:10:53 +000099 << ConvolutionMethod::DIRECT
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)))
Georgios Pinitas28705162018-03-21 20:10:53 +0000107 << convolution_hint
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000108 << ConvolutionLayer(
109 1U, 1U, 16U,
110 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_w.npy"),
111 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_b.npy"),
112 PadStrideInfo(1, 1, 0, 0))
113 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
114 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
115 << ConvolutionLayer(
116 1U, 1U, 16U,
117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_w.npy"),
118 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_b.npy"),
119 PadStrideInfo(1, 1, 0, 0))
120 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
121 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
122 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
123 << ConvolutionLayer(
124 1U, 1U, 32U,
125 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
126 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_b.npy"),
127 PadStrideInfo(1, 1, 0, 0))
128 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
129 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
130 << ConvolutionLayer(
131 1U, 1U, 32U,
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
133 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_b.npy"),
134 PadStrideInfo(1, 1, 0, 0))
135 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
136 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
137 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
138 << ConvolutionLayer(
139 1U, 1U, 48U,
140 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
141 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_b.npy"),
142 PadStrideInfo(1, 1, 0, 0))
143 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
144 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
145 << ConvolutionLayer(
146 1U, 1U, 48U,
147 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_w.npy"),
148 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_b.npy"),
149 PadStrideInfo(1, 1, 0, 0))
150 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
151 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
152 << ConvolutionLayer(
153 1U, 1U, 64U,
154 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_w.npy"),
155 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_b.npy"),
156 PadStrideInfo(1, 1, 0, 0))
157 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
158 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
159 << ConvolutionLayer(
160 1U, 1U, 64U,
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
162 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_b.npy"),
163 PadStrideInfo(1, 1, 0, 0))
164 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
165 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
166 << ConvolutionLayer(
167 1U, 1U, 1000U,
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_w.npy"),
169 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_b.npy"),
170 PadStrideInfo(1, 1, 0, 0))
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
172 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
173 << FlattenLayer()
174 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000175 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000176
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000177 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000178 GraphConfig config;
179 config.use_function_memory_manager = true;
180 config.use_tuner = (target == 2);
181 graph.finalize(target_hint, config);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000182 }
183 void do_run() override
184 {
185 // Run graph
186 graph.run();
187 }
188
189private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000190 Stream graph{ 0, "SqueezeNetV1.1" };
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000191
192 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
193 {
194 std::string total_path = "/cnn_data/squeezenet_v1_1_model/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000195 SubStream i_a(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000196 i_a << ConvolutionLayer(
197 1U, 1U, expand1_filt,
198 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
199 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
200 PadStrideInfo(1, 1, 0, 0))
201 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
202
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000203 SubStream i_b(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000204 i_b << ConvolutionLayer(
205 3U, 3U, expand3_filt,
206 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
207 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
208 PadStrideInfo(1, 1, 1, 1))
209 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
210
211 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
212 }
213};
214
215/** Main program for Squeezenet v1.1
216 *
217 * @param[in] argc Number of arguments
218 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
219 */
220int main(int argc, char **argv)
221{
222 return arm_compute::utils::run_example<GraphSqueezenet_v1_1Example>(argc, argv);
223}