arm_compute v17.12
[platform/upstream/armcl.git] / examples / graph_squeezenet.cpp
1 /*
2  * Copyright (c) 2017 ARM Limited.
3  *
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
34 using namespace arm_compute::graph;
35 using namespace arm_compute::graph_utils;
36 using namespace arm_compute::logging;
37
38 namespace
39 {
40 BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
41 {
42     std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
43     SubGraph    i_a;
44     i_a << ConvolutionLayer(
45             1U, 1U, expand1_filt,
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));
50
51     SubGraph i_b;
52     i_b << ConvolutionLayer(
53             3U, 3U, expand3_filt,
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));
58
59     return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
60 }
61 } // namespace
62
63 /** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
64  *
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 )
67  */
68 void main_graph_squeezenet(int argc, const char **argv)
69 {
70     std::string data_path; /* Path to the trainable data */
71     std::string image;     /* Image data */
72     std::string label;     /* Label data */
73
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 */
77
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;
81
82     // Parse arguments
83     if(argc < 2)
84     {
85         // Print help
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";
88     }
89     else if(argc == 2)
90     {
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";
93     }
94     else if(argc == 3)
95     {
96         data_path = argv[2];
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";
99     }
100     else if(argc == 4)
101     {
102         data_path = argv[2];
103         image     = argv[3];
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";
106     }
107     else
108     {
109         data_path = argv[2];
110         image     = argv[3];
111         label     = argv[4];
112     }
113
114     Graph graph;
115
116     graph << target_hint
117           << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
118                     get_input_accessor(image, mean_r, mean_g, mean_b))
119           << ConvolutionLayer(
120               7U, 7U, 96U,
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))
124           << convolution_hint
125           << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
126           << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
127           << ConvolutionLayer(
128               1U, 1U, 16U,
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)
134           << ConvolutionLayer(
135               1U, 1U, 16U,
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)
141           << ConvolutionLayer(
142               1U, 1U, 32U,
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)))
149           << ConvolutionLayer(
150               1U, 1U, 32U,
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)
156           << ConvolutionLayer(
157               1U, 1U, 48U,
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)
163           << ConvolutionLayer(
164               1U, 1U, 48U,
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)
170           << ConvolutionLayer(
171               1U, 1U, 64U,
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)))
178           << ConvolutionLayer(
179               1U, 1U, 64U,
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)
185           << ConvolutionLayer(
186               1U, 1U, 1000U,
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))
192           << FlattenLayer()
193           << SoftmaxLayer()
194           << Tensor(get_output_accessor(label, 5));
195
196     graph.run();
197 }
198
199 /** Main program for Squeezenet v1.0
200  *
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 )
203  */
204 int main(int argc, const char **argv)
205 {
206     return arm_compute::utils::run_example(argc, argv, main_graph_squeezenet);
207 }