2 * Copyright (c) 2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph.h"
25 #include "support/ToolchainSupport.h"
26 #include "utils/GraphUtils.h"
27 #include "utils/Utils.h"
32 using namespace arm_compute::utils;
33 using namespace arm_compute::graph::frontend;
34 using namespace arm_compute::graph_utils;
40 /** Example demonstrating how to implement Squeezenet's v1.1 network using the Compute Library's graph API
42 * @param[in] argc Number of arguments
43 * @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) )
45 class GraphSqueezenet_v1_1Example : public Example
48 void do_setup(int argc, char **argv) override
50 std::string data_path; /* Path to the trainable data */
51 std::string image; /* Image data */
52 std::string label; /* Label data */
54 // 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);
58 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
59 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
60 Target target_hint = set_target_hint(target);
61 FastMathHint fast_math_hint = FastMathHint::DISABLED;
67 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
68 std::cout << "No data folder provided: using random values\n\n";
72 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
73 std::cout << "No data folder provided: using random values\n\n";
78 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
79 std::cout << "No image provided: using random values\n\n";
85 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
86 std::cout << "No text file with labels provided: skipping output accessor\n\n";
93 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
94 std::cout << "No fast math info provided: disabling fast math\n\n";
101 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
106 << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32),
107 get_input_accessor(image, std::move(preprocessor)))
108 << ConvolutionMethod::DIRECT
111 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_w.npy"),
112 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv1_b.npy"),
113 PadStrideInfo(2, 2, 0, 0))
114 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
115 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
116 << ConvolutionMethod::DEFAULT
119 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_w.npy"),
120 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire2_squeeze1x1_b.npy"),
121 PadStrideInfo(1, 1, 0, 0))
122 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
123 graph << get_expand_fire_node(data_path, "fire2", 64U, 64U);
124 graph << ConvolutionLayer(
126 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_w.npy"),
127 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire3_squeeze1x1_b.npy"),
128 PadStrideInfo(1, 1, 0, 0))
129 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
130 graph << get_expand_fire_node(data_path, "fire3", 64U, 64U);
131 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
134 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_w.npy"),
135 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire4_squeeze1x1_b.npy"),
136 PadStrideInfo(1, 1, 0, 0))
137 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
138 graph << get_expand_fire_node(data_path, "fire4", 128U, 128U);
139 graph << ConvolutionLayer(
141 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_w.npy"),
142 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire5_squeeze1x1_b.npy"),
143 PadStrideInfo(1, 1, 0, 0))
144 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
145 graph << get_expand_fire_node(data_path, "fire5", 128U, 128U);
146 graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
149 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_w.npy"),
150 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire6_squeeze1x1_b.npy"),
151 PadStrideInfo(1, 1, 0, 0))
152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
153 graph << get_expand_fire_node(data_path, "fire6", 192U, 192U);
154 graph << ConvolutionLayer(
156 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_w.npy"),
157 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire7_squeeze1x1_b.npy"),
158 PadStrideInfo(1, 1, 0, 0))
159 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
160 graph << get_expand_fire_node(data_path, "fire7", 192U, 192U);
161 graph << ConvolutionLayer(
163 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_w.npy"),
164 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire8_squeeze1x1_b.npy"),
165 PadStrideInfo(1, 1, 0, 0))
166 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
167 graph << get_expand_fire_node(data_path, "fire8", 256U, 256U);
168 graph << ConvolutionLayer(
170 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_w.npy"),
171 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/fire9_squeeze1x1_b.npy"),
172 PadStrideInfo(1, 1, 0, 0))
173 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
174 graph << get_expand_fire_node(data_path, "fire9", 256U, 256U);
175 graph << ConvolutionLayer(
177 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_w.npy"),
178 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1_1_model/conv10_b.npy"),
179 PadStrideInfo(1, 1, 0, 0))
180 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
181 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
184 << OutputLayer(get_output_accessor(label, 5));
188 config.use_tuner = (target == 2);
189 graph.finalize(target_hint, config);
191 void do_run() override
198 Stream graph{ 0, "SqueezeNetV1.1" };
200 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &¶m_path, unsigned int expand1_filt, unsigned int expand3_filt)
202 std::string total_path = "/cnn_data/squeezenet_v1_1_model/" + param_path + "_";
203 SubStream i_a(graph);
204 i_a << ConvolutionLayer(
205 1U, 1U, expand1_filt,
206 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
207 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
208 PadStrideInfo(1, 1, 0, 0))
209 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
211 SubStream i_b(graph);
212 i_b << ConvolutionLayer(
213 3U, 3U, expand3_filt,
214 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
215 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
216 PadStrideInfo(1, 1, 1, 1))
217 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
219 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
223 /** Main program for Squeezenet v1.1
225 * @param[in] argc Number of arguments
226 * @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) )
228 int main(int argc, char **argv)
230 return arm_compute::utils::run_example<GraphSqueezenet_v1_1Example>(argc, argv);