2 * Copyright (c) 2017 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/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"
34 using namespace arm_compute::graph;
35 using namespace arm_compute::graph_utils;
36 using namespace arm_compute::logging;
40 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &¶m_path, unsigned int expand1_filt, unsigned int expand3_filt)
42 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
44 i_a << ConvolutionLayer(
46 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
47 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
48 PadStrideInfo(1, 1, 0, 0))
49 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
52 i_b << ConvolutionLayer(
54 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
55 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
56 PadStrideInfo(1, 1, 1, 1))
57 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
59 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
63 /** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
65 * @param[in] argc Number of arguments
66 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
68 void main_graph_squeezenet(int argc, const char **argv)
70 std::string data_path; /* Path to the trainable data */
71 std::string image; /* Image data */
72 std::string label; /* Label data */
74 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
75 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
76 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
78 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
79 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
80 ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT;
86 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
87 std::cout << "No data folder provided: using random values\n\n";
91 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
92 std::cout << "No data folder provided: using random values\n\n";
97 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
98 std::cout << "No image provided: using random values\n\n";
104 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
105 std::cout << "No text file with labels provided: skipping output accessor\n\n";
117 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
118 get_input_accessor(image, mean_r, mean_g, mean_b))
121 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
122 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
123 PadStrideInfo(2, 2, 0, 0))
125 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
129 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
130 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
131 PadStrideInfo(1, 1, 0, 0))
132 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
133 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
136 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
137 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
138 PadStrideInfo(1, 1, 0, 0))
139 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
140 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
143 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
144 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
145 PadStrideInfo(1, 1, 0, 0))
146 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
147 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
148 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
151 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
152 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
153 PadStrideInfo(1, 1, 0, 0))
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
155 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
158 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
159 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"),
160 PadStrideInfo(1, 1, 0, 0))
161 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
162 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
165 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
166 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"),
167 PadStrideInfo(1, 1, 0, 0))
168 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
169 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
172 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
173 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
174 PadStrideInfo(1, 1, 0, 0))
175 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
176 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
177 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
180 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
181 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
182 PadStrideInfo(1, 1, 0, 0))
183 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
184 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
187 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
188 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
189 PadStrideInfo(1, 1, 0, 0))
190 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
191 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
194 << Tensor(get_output_accessor(label, 5));
199 /** Main program for Squeezenet v1.0
201 * @param[in] argc Number of arguments
202 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
204 int main(int argc, const char **argv)
206 return arm_compute::utils::run_example(argc, argv, main_graph_squeezenet);