blob: b2c5a442cdbe2069138819622844d1f4f099f610 [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
Isabella Gottardibc4484a2018-02-02 11:27:32 +000036/** Example demonstrating how to implement Squeezenet's v1.1 network using the Compute Library's graph API
37 *
38 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +010039 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Isabella Gottardibc4484a2018-02-02 11:27:32 +000040 */
41class GraphSqueezenet_v1_1Example : public Example
42{
43public:
44 void do_setup(int argc, char **argv) override
45 {
46 std::string data_path; /* Path to the trainable data */
47 std::string image; /* Image data */
48 std::string label; /* Label data */
49
Georgios Pinitas140fdc72018-02-16 11:42:38 +000050 // Create a preprocessor object
51 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
52 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Isabella Gottardibc4484a2018-02-02 11:27:32 +000053
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000054 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Gian Marco Iodicea8aef292018-05-14 14:21:39 +010055 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
56 Target target_hint = set_target_hint(target);
57 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Georgios Pinitas28705162018-03-21 20:10:53 +000058
Isabella Gottardibc4484a2018-02-02 11:27:32 +000059 // Parse arguments
60 if(argc < 2)
61 {
62 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +010063 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Isabella Gottardibc4484a2018-02-02 11:27:32 +000064 std::cout << "No data folder provided: using random values\n\n";
65 }
66 else if(argc == 2)
67 {
Giorgio Arena59631a12018-05-02 13:59:04 +010068 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Isabella Gottardibc4484a2018-02-02 11:27:32 +000069 std::cout << "No data folder provided: using random values\n\n";
70 }
71 else if(argc == 3)
72 {
73 data_path = argv[2];
Giorgio Arena59631a12018-05-02 13:59:04 +010074 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
Isabella Gottardibc4484a2018-02-02 11:27:32 +000075 std::cout << "No image provided: using random values\n\n";
76 }
77 else if(argc == 4)
78 {
79 data_path = argv[2];
80 image = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +010081 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Isabella Gottardibc4484a2018-02-02 11:27:32 +000082 std::cout << "No text file with labels provided: skipping output accessor\n\n";
83 }
Giorgio Arena59631a12018-05-02 13:59:04 +010084 else if(argc == 5)
Isabella Gottardibc4484a2018-02-02 11:27:32 +000085 {
86 data_path = argv[2];
87 image = argv[3];
88 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +010089 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
90 std::cout << "No fast math info provided: disabling fast math\n\n";
91 }
92 else
93 {
94 data_path = argv[2];
95 image = argv[3];
96 label = argv[4];
97 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Isabella Gottardibc4484a2018-02-02 11:27:32 +000098 }
99
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000100 graph << target_hint
Giorgio Arena59631a12018-05-02 13:59:04 +0100101 << fast_math_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000102 << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32),
103 get_input_accessor(image, std::move(preprocessor)))
Georgios Pinitas28705162018-03-21 20:10:53 +0000104 << ConvolutionMethod::DIRECT
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000105 << ConvolutionLayer(
106 3U, 3U, 64U,
107 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_w.npy"),
108 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_b.npy"),
109 PadStrideInfo(2, 2, 0, 0))
110 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
111 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Gian Marco Iodicec13021e2018-05-09 14:11:34 +0100112 << ConvolutionMethod::DEFAULT
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000113 << ConvolutionLayer(
114 1U, 1U, 16U,
115 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_w.npy"),
116 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_b.npy"),
117 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100118 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
119 graph << get_expand_fire_node(data_path, "fire2", 64U, 64U);
120 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000121 1U, 1U, 16U,
122 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_w.npy"),
123 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_b.npy"),
124 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
126 graph << get_expand_fire_node(data_path, "fire3", 64U, 64U);
127 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000128 << ConvolutionLayer(
129 1U, 1U, 32U,
130 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_b.npy"),
132 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100133 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
134 graph << get_expand_fire_node(data_path, "fire4", 128U, 128U);
135 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000136 1U, 1U, 32U,
137 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_b.npy"),
139 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100140 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
141 graph << get_expand_fire_node(data_path, "fire5", 128U, 128U);
142 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000143 << ConvolutionLayer(
144 1U, 1U, 48U,
145 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_b.npy"),
147 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
149 graph << get_expand_fire_node(data_path, "fire6", 192U, 192U);
150 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000151 1U, 1U, 48U,
152 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_w.npy"),
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_b.npy"),
154 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100155 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
156 graph << get_expand_fire_node(data_path, "fire7", 192U, 192U);
157 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000158 1U, 1U, 64U,
159 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_w.npy"),
160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_b.npy"),
161 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100162 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
163 graph << get_expand_fire_node(data_path, "fire8", 256U, 256U);
164 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000165 1U, 1U, 64U,
166 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_b.npy"),
168 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100169 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
170 graph << get_expand_fire_node(data_path, "fire9", 256U, 256U);
171 graph << ConvolutionLayer(
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000172 1U, 1U, 1000U,
173 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_w.npy"),
174 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_b.npy"),
175 PadStrideInfo(1, 1, 0, 0))
176 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
177 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
178 << FlattenLayer()
179 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000180 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000181
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000182 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000183 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100184 config.use_tuner = (target == 2);
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000185 graph.finalize(target_hint, config);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000186 }
187 void do_run() override
188 {
189 // Run graph
190 graph.run();
191 }
192
193private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000194 Stream graph{ 0, "SqueezeNetV1.1" };
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000195
196 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
197 {
198 std::string total_path = "/cnn_data/squeezenet_v1_1_model/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000199 SubStream i_a(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000200 i_a << ConvolutionLayer(
201 1U, 1U, expand1_filt,
202 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
203 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
204 PadStrideInfo(1, 1, 0, 0))
205 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
206
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000207 SubStream i_b(graph);
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000208 i_b << ConvolutionLayer(
209 3U, 3U, expand3_filt,
210 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
211 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
212 PadStrideInfo(1, 1, 1, 1))
213 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
214
215 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
216 }
217};
218
219/** Main program for Squeezenet v1.1
220 *
221 * @param[in] argc Number of arguments
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100222 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Isabella Gottardibc4484a2018-02-02 11:27:32 +0000223 */
224int main(int argc, char **argv)
225{
226 return arm_compute::utils::run_example<GraphSqueezenet_v1_1Example>(argc, argv);
227}