blob: b21f2fe5c4df842c0a163a757a8f6caadff993f6 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017, 2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
26#include "arm_compute/graph/SubGraph.h"
27#include "support/ToolchainSupport.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31#include <cstdlib>
32#include <tuple>
33
Anthony Barbierf45d5a92018-01-24 16:23:15 +000034using namespace arm_compute::utils;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000035using namespace arm_compute::graph;
36using namespace arm_compute::graph_utils;
37using namespace arm_compute::logging;
38
39namespace
40{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000041} // namespace
42
43/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
44 *
45 * @param[in] argc Number of arguments
46 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
47 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +000048class GraphSqueezenetExample : public Example
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000050public:
51 void do_setup(int argc, char **argv) override
52 {
53 std::string data_path; /* Path to the trainable data */
54 std::string image; /* Image data */
55 std::string label; /* Label data */
Anthony Barbier8140e1e2017-12-14 23:48:46 +000056
Anthony Barbierf45d5a92018-01-24 16:23:15 +000057 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
58 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
59 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Anthony Barbier8140e1e2017-12-14 23:48:46 +000060
Anthony Barbierf45d5a92018-01-24 16:23:15 +000061 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
62 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
63 ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000064
Anthony Barbierf45d5a92018-01-24 16:23:15 +000065 // Parse arguments
66 if(argc < 2)
67 {
68 // Print help
69 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
70 std::cout << "No data folder provided: using random values\n\n";
71 }
72 else if(argc == 2)
73 {
74 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
75 std::cout << "No data folder provided: using random values\n\n";
76 }
77 else if(argc == 3)
78 {
79 data_path = argv[2];
80 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
81 std::cout << "No image provided: using random values\n\n";
82 }
83 else if(argc == 4)
84 {
85 data_path = argv[2];
86 image = argv[3];
87 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
88 std::cout << "No text file with labels provided: skipping output accessor\n\n";
89 }
90 else
91 {
92 data_path = argv[2];
93 image = argv[3];
94 label = argv[4];
95 }
96
97 graph << target_hint
98 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
99 get_input_accessor(image, mean_r, mean_g, mean_b))
100 << ConvolutionLayer(
101 7U, 7U, 96U,
102 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
103 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
104 PadStrideInfo(2, 2, 0, 0))
105 << convolution_hint
106 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
107 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
108 << ConvolutionLayer(
109 1U, 1U, 16U,
110 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
111 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
112 PadStrideInfo(1, 1, 0, 0))
113 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
114 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
115 << ConvolutionLayer(
116 1U, 1U, 16U,
117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
118 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
119 PadStrideInfo(1, 1, 0, 0))
120 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
121 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
122 << ConvolutionLayer(
123 1U, 1U, 32U,
124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
126 PadStrideInfo(1, 1, 0, 0))
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
129 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
130 << ConvolutionLayer(
131 1U, 1U, 32U,
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
133 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
134 PadStrideInfo(1, 1, 0, 0))
135 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
136 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
137 << ConvolutionLayer(
138 1U, 1U, 48U,
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
140 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"),
141 PadStrideInfo(1, 1, 0, 0))
142 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
143 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
144 << ConvolutionLayer(
145 1U, 1U, 48U,
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"),
148 PadStrideInfo(1, 1, 0, 0))
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
151 << ConvolutionLayer(
152 1U, 1U, 64U,
153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
155 PadStrideInfo(1, 1, 0, 0))
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
158 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
159 << ConvolutionLayer(
160 1U, 1U, 64U,
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
162 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
163 PadStrideInfo(1, 1, 0, 0))
164 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
165 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
166 << ConvolutionLayer(
167 1U, 1U, 1000U,
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
169 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
170 PadStrideInfo(1, 1, 0, 0))
171 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
172 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
173 << FlattenLayer()
174 << SoftmaxLayer()
175 << Tensor(get_output_accessor(label, 5));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000176 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000177 void do_run() override
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000178 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000179 // Run graph
180 graph.run();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000181 }
182
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000183private:
184 Graph graph{};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000185
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000186 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
187 {
188 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
189 SubGraph i_a;
190 i_a << ConvolutionLayer(
191 1U, 1U, expand1_filt,
192 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
193 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
194 PadStrideInfo(1, 1, 0, 0))
195 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000196
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000197 SubGraph i_b;
198 i_b << ConvolutionLayer(
199 3U, 3U, expand3_filt,
200 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
201 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
202 PadStrideInfo(1, 1, 1, 1))
203 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
204
205 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
206 }
207};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000208
209/** Main program for Squeezenet v1.0
210 *
211 * @param[in] argc Number of arguments
212 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
213 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000214int main(int argc, char **argv)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000215{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000216 return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv);
217}