blob: 4ebfd3fe8ed7f69b0acef7c338a5d07481e72447 [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 Pinitasd8734b52017-12-22 15:27:52 +000024#include "arm_compute/graph2.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 Pinitasd8734b52017-12-22 15:27:52 +000033using namespace arm_compute::graph2::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 Pinitasd8734b52017-12-22 15:27:52 +000059 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
60 Target target_hint = set_target_hint2(target);
61 bool enable_tuning = (target == 2);
62 bool enable_memory_management = true;
Isabella Gottardibc4484a2018-02-02 11:27:32 +000063
64 // 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)))
Isabella Gottardibc4484a2018-02-02 11:27:32 +000099 << ConvolutionLayer(
100 3U, 3U, 64U,
101 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_w.npy"),
102 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_b.npy"),
103 PadStrideInfo(2, 2, 0, 0))
104 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
105 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
106 << ConvolutionLayer(
107 1U, 1U, 16U,
108 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_w.npy"),
109 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_b.npy"),
110 PadStrideInfo(1, 1, 0, 0))
111 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
112 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
113 << ConvolutionLayer(
114 1U, 1U, 16U,
115 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_w.npy"),
116 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_b.npy"),
117 PadStrideInfo(1, 1, 0, 0))
118 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
119 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
120 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
121 << ConvolutionLayer(
122 1U, 1U, 32U,
123 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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 << ConvolutionLayer(
129 1U, 1U, 32U,
130 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_b.npy"),
132 PadStrideInfo(1, 1, 0, 0))
133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
134 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
135 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
136 << ConvolutionLayer(
137 1U, 1U, 48U,
138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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_1_model/fire7_squeeze1x1_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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_1_model/fire8_squeeze1x1_w.npy"),
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_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 << ConvolutionLayer(
158 1U, 1U, 64U,
159 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_b.npy"),
161 PadStrideInfo(1, 1, 0, 0))
162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
163 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
164 << ConvolutionLayer(
165 1U, 1U, 1000U,
166 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_w.npy"),
167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_b.npy"),
168 PadStrideInfo(1, 1, 0, 0))
169 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
170 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
171 << FlattenLayer()
172 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000173 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000174
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000175 // Finalize graph
176 graph.finalize(target_hint, enable_tuning, enable_memory_management);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000177 }
178 void do_run() override
179 {
180 // Run graph
181 graph.run();
182 }
183
184private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000185 Stream graph{ 0, "SqueezeNetV1.1" };
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000186
187 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
188 {
189 std::string total_path = "/cnn_data/squeezenet_v1_1_model/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000190 SubStream i_a(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000191 i_a << ConvolutionLayer(
192 1U, 1U, expand1_filt,
193 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
194 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
195 PadStrideInfo(1, 1, 0, 0))
196 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
197
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000198 SubStream i_b(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000199 i_b << ConvolutionLayer(
200 3U, 3U, expand3_filt,
201 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
202 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
203 PadStrideInfo(1, 1, 1, 1))
204 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
205
206 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
207 }
208};
209
210/** Main program for Squeezenet v1.1
211 *
212 * @param[in] argc Number of arguments
213 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
214 */
215int main(int argc, char **argv)
216{
217 return arm_compute::utils::run_example<GraphSqueezenet_v1_1Example>(argc, argv);
218}