blob: 2627c96774c640c5bff5a64fe2b5a8eb8be76040 [file] [log] [blame]
Georgios Pinitas37561862017-10-19 10:51:03 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas37561862017-10-19 10:51:03 +01003 *
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"
Georgios Pinitas37561862017-10-19 10:51:03 +010025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
Georgios Pinitas37561862017-10-19 10:51:03 +010030#include <tuple>
31
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033using namespace arm_compute::graph::frontend;
Georgios Pinitas37561862017-10-19 10:51:03 +010034using namespace arm_compute::graph_utils;
Isabella Gottardi4398bec2017-10-19 16:10:59 +010035using namespace arm_compute::logging;
Georgios Pinitas37561862017-10-19 10:51:03 +010036
Georgios Pinitas37561862017-10-19 10:51:03 +010037/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
38 *
39 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +010040 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Georgios Pinitas37561862017-10-19 10:51:03 +010041 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000042class GraphSqueezenetExample : public Example
Georgios Pinitas37561862017-10-19 10:51:03 +010043{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044public:
45 void do_setup(int argc, char **argv) override
46 {
47 std::string data_path; /* Path to the trainable data */
48 std::string image; /* Image data */
49 std::string label; /* Label data */
Isabella Gottardi97988a42017-11-03 14:39:44 +000050
Georgios Pinitas140fdc72018-02-16 11:42:38 +000051 // Create a preprocessor object
52 const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } };
53 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb);
Georgios Pinitas37561862017-10-19 10:51:03 +010054
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000055 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Gian Marco Iodicec13021e2018-05-09 14:11:34 +010056 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
57 Target target_hint = set_target_hint(target);
58 ConvolutionMethod convolution_hint = target_hint == Target::NEON ? ConvolutionMethod::GEMM : ConvolutionMethod::DEFAULT;
Giorgio Arena59631a12018-05-02 13:59:04 +010059 FastMathHint fast_math_hint = FastMathHint::DISABLED;
Georgios Pinitas28705162018-03-21 20:10:53 +000060
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000061 // Parse arguments
62 if(argc < 2)
63 {
64 // Print help
Giorgio Arena59631a12018-05-02 13:59:04 +010065 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000066 std::cout << "No data folder provided: using random values\n\n";
67 }
68 else if(argc == 2)
69 {
Giorgio Arena59631a12018-05-02 13:59:04 +010070 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000071 std::cout << "No data folder provided: using random values\n\n";
72 }
73 else if(argc == 3)
74 {
75 data_path = argv[2];
Giorgio Arena59631a12018-05-02 13:59:04 +010076 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000077 std::cout << "No image provided: using random values\n\n";
78 }
79 else if(argc == 4)
80 {
81 data_path = argv[2];
82 image = argv[3];
Giorgio Arena59631a12018-05-02 13:59:04 +010083 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000084 std::cout << "No text file with labels provided: skipping output accessor\n\n";
85 }
Giorgio Arena59631a12018-05-02 13:59:04 +010086 else if(argc == 5)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000087 {
88 data_path = argv[2];
89 image = argv[3];
90 label = argv[4];
Giorgio Arena59631a12018-05-02 13:59:04 +010091 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
92 std::cout << "No fast math info provided: disabling fast math\n\n";
93 }
94 else
95 {
96 data_path = argv[2];
97 image = argv[3];
98 label = argv[4];
99 fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000100 }
101
102 graph << target_hint
Giorgio Arena59631a12018-05-02 13:59:04 +0100103 << fast_math_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
105 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000106 << ConvolutionLayer(
107 7U, 7U, 96U,
108 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
109 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
110 PadStrideInfo(2, 2, 0, 0))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000111 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
112 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Georgios Pinitas28705162018-03-21 20:10:53 +0000113 << convolution_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114 << ConvolutionLayer(
115 1U, 1U, 16U,
116 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
118 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100119 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
120 graph << get_expand_fire_node(data_path, "fire2", 64U, 64U);
121 graph << ConvolutionLayer(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000122 1U, 1U, 16U,
123 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
125 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100126 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
127 graph << get_expand_fire_node(data_path, "fire3", 64U, 64U);
128 graph << ConvolutionLayer(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000129 1U, 1U, 32U,
130 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_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 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000136 << ConvolutionLayer(
137 1U, 1U, 32U,
138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
140 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
142 graph << get_expand_fire_node(data_path, "fire5", 128U, 128U);
143 graph << ConvolutionLayer(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000144 1U, 1U, 48U,
145 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_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(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000151 1U, 1U, 48U,
152 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_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(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000158 1U, 1U, 64U,
159 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_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 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000165 << ConvolutionLayer(
166 1U, 1U, 64U,
167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
169 PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas41c482d2018-04-17 13:23:26 +0100170 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
171 graph << get_expand_fire_node(data_path, "fire9", 256U, 256U);
172 graph << ConvolutionLayer(
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000173 1U, 1U, 1000U,
174 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
175 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
176 PadStrideInfo(1, 1, 0, 0))
177 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
178 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
179 << FlattenLayer()
180 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000181 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000182
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000183 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000184 GraphConfig config;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100185 config.use_tuner = (target == 2);
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000186 graph.finalize(target_hint, config);
Georgios Pinitas37561862017-10-19 10:51:03 +0100187 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000188 void do_run() override
Georgios Pinitas37561862017-10-19 10:51:03 +0100189 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000190 // Run graph
191 graph.run();
Georgios Pinitas37561862017-10-19 10:51:03 +0100192 }
193
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000194private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000195 Stream graph{ 0, "SqueezeNetV1" };
Georgios Pinitas37561862017-10-19 10:51:03 +0100196
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000197 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
198 {
199 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000200 SubStream i_a(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000201 i_a << ConvolutionLayer(
202 1U, 1U, expand1_filt,
203 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
204 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
205 PadStrideInfo(1, 1, 0, 0))
206 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Georgios Pinitas37561862017-10-19 10:51:03 +0100207
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000208 SubStream i_b(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000209 i_b << ConvolutionLayer(
210 3U, 3U, expand3_filt,
211 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
212 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
213 PadStrideInfo(1, 1, 1, 1))
214 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
215
216 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
217 }
218};
Georgios Pinitas37561862017-10-19 10:51:03 +0100219
220/** Main program for Squeezenet v1.0
221 *
222 * @param[in] argc Number of arguments
Giorgio Arena59631a12018-05-02 13:59:04 +0100223 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
Georgios Pinitas37561862017-10-19 10:51:03 +0100224 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000225int main(int argc, char **argv)
Georgios Pinitas37561862017-10-19 10:51:03 +0100226{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000227 return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000228}