blob: 32be14305f2d3fc6d8a0744c026bd1a4e40c8783 [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
Gian Marcobfa3b522017-12-12 10:08:38 +000040 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
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
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000056 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010057 Target target_hint = set_target_hint(target);
Gian Marcobfa3b522017-12-12 10:08:38 +000058
Georgios Pinitas28705162018-03-21 20:10:53 +000059 ConvolutionMethod convolution_hint = (target_hint == Target::CL) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
60
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000061 // Parse arguments
62 if(argc < 2)
63 {
64 // Print help
65 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
66 std::cout << "No data folder provided: using random values\n\n";
67 }
68 else if(argc == 2)
69 {
70 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
71 std::cout << "No data folder provided: using random values\n\n";
72 }
73 else if(argc == 3)
74 {
75 data_path = argv[2];
76 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
77 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];
83 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
84 std::cout << "No text file with labels provided: skipping output accessor\n\n";
85 }
86 else
87 {
88 data_path = argv[2];
89 image = argv[3];
90 label = argv[4];
91 }
92
93 graph << target_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +000094 << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32),
95 get_input_accessor(image, std::move(preprocessor)))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000096 << ConvolutionLayer(
97 7U, 7U, 96U,
98 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
99 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
100 PadStrideInfo(2, 2, 0, 0))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000101 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
102 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
Georgios Pinitas28705162018-03-21 20:10:53 +0000103 << convolution_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000104 << ConvolutionLayer(
105 1U, 1U, 16U,
106 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
107 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
108 PadStrideInfo(1, 1, 0, 0))
109 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
110 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
111 << ConvolutionLayer(
112 1U, 1U, 16U,
113 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
114 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
115 PadStrideInfo(1, 1, 0, 0))
116 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
117 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
118 << ConvolutionLayer(
119 1U, 1U, 32U,
120 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
121 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
122 PadStrideInfo(1, 1, 0, 0))
123 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
124 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
125 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
126 << ConvolutionLayer(
127 1U, 1U, 32U,
128 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
129 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
130 PadStrideInfo(1, 1, 0, 0))
131 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
132 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
133 << ConvolutionLayer(
134 1U, 1U, 48U,
135 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
136 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"),
137 PadStrideInfo(1, 1, 0, 0))
138 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
139 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
140 << ConvolutionLayer(
141 1U, 1U, 48U,
142 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
143 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"),
144 PadStrideInfo(1, 1, 0, 0))
145 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
146 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
147 << ConvolutionLayer(
148 1U, 1U, 64U,
149 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
150 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
151 PadStrideInfo(1, 1, 0, 0))
152 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
153 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
154 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
155 << ConvolutionLayer(
156 1U, 1U, 64U,
157 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
158 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
159 PadStrideInfo(1, 1, 0, 0))
160 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
161 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
162 << ConvolutionLayer(
163 1U, 1U, 1000U,
164 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
165 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
166 PadStrideInfo(1, 1, 0, 0))
167 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
168 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
169 << FlattenLayer()
170 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000171 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000172
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000173 // Finalize graph
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000174 GraphConfig config;
175 config.use_function_memory_manager = true;
176 config.use_tuner = (target == 2);
177 graph.finalize(target_hint, config);
Georgios Pinitas37561862017-10-19 10:51:03 +0100178 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000179 void do_run() override
Georgios Pinitas37561862017-10-19 10:51:03 +0100180 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000181 // Run graph
182 graph.run();
Georgios Pinitas37561862017-10-19 10:51:03 +0100183 }
184
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000185private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000186 Stream graph{ 0, "SqueezeNetV1" };
Georgios Pinitas37561862017-10-19 10:51:03 +0100187
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000188 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
189 {
190 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000191 SubStream i_a(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000192 i_a << ConvolutionLayer(
193 1U, 1U, expand1_filt,
194 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
195 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
196 PadStrideInfo(1, 1, 0, 0))
197 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Georgios Pinitas37561862017-10-19 10:51:03 +0100198
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000199 SubStream i_b(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000200 i_b << ConvolutionLayer(
201 3U, 3U, expand3_filt,
202 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
203 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
204 PadStrideInfo(1, 1, 1, 1))
205 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
206
207 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
208 }
209};
Georgios Pinitas37561862017-10-19 10:51:03 +0100210
211/** Main program for Squeezenet v1.0
212 *
213 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000214 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas37561862017-10-19 10:51:03 +0100215 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000216int main(int argc, char **argv)
Georgios Pinitas37561862017-10-19 10:51:03 +0100217{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000218 return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000219}