blob: 17435092562240284784319131b0000203d52a1b [file] [log] [blame]
Georgios Pinitas37561862017-10-19 10:51:03 +01001/*
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 */
Georgios Pinitas37561862017-10-19 10:51:03 +010024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
26#include "arm_compute/graph/SubGraph.h"
Georgios Pinitas37561862017-10-19 10:51:03 +010027#include "support/ToolchainSupport.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31#include <cstdlib>
Georgios Pinitas37561862017-10-19 10:51:03 +010032#include <tuple>
33
34using namespace arm_compute::graph;
35using namespace arm_compute::graph_utils;
Isabella Gottardi4398bec2017-10-19 16:10:59 +010036using namespace arm_compute::logging;
Georgios Pinitas37561862017-10-19 10:51:03 +010037
Georgios Pinitas37561862017-10-19 10:51:03 +010038BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
39{
40 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
41 SubGraph i_a;
42 i_a << ConvolutionLayer(
43 1U, 1U, expand1_filt,
Isabella Gottardi97988a42017-11-03 14:39:44 +000044 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
45 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +010046 PadStrideInfo(1, 1, 0, 0))
47 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
48
49 SubGraph i_b;
50 i_b << ConvolutionLayer(
51 3U, 3U, expand3_filt,
Isabella Gottardi97988a42017-11-03 14:39:44 +000052 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
53 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +010054 PadStrideInfo(1, 1, 1, 1))
55 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
56
57 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
58}
59
60/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
61 *
62 * @param[in] argc Number of arguments
Isabella Gottardi97988a42017-11-03 14:39:44 +000063 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas37561862017-10-19 10:51:03 +010064 */
65void main_graph_squeezenet(int argc, const char **argv)
66{
Isabella Gottardi97988a42017-11-03 14:39:44 +000067 std::string data_path; /* Path to the trainable data */
68 std::string image; /* Image data */
69 std::string label; /* Label data */
70
71 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
72 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
73 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitas37561862017-10-19 10:51:03 +010074
75 // Parse arguments
76 if(argc < 2)
77 {
78 // Print help
Isabella Gottardi97988a42017-11-03 14:39:44 +000079 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitas37561862017-10-19 10:51:03 +010080 std::cout << "No data folder provided: using random values\n\n";
81 }
82 else if(argc == 2)
83 {
84 //Do something with argv[1]
85 data_path = argv[1];
Isabella Gottardi97988a42017-11-03 14:39:44 +000086 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
87 std::cout << "No image provided: using random values\n";
88 }
89 else if(argc == 3)
90 {
91 data_path = argv[1];
92 image = argv[2];
93 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
94 std::cout << "No text file with labels provided: skipping output accessor\n";
Georgios Pinitas37561862017-10-19 10:51:03 +010095 }
96 else
97 {
Georgios Pinitas37561862017-10-19 10:51:03 +010098 data_path = argv[1];
Isabella Gottardi97988a42017-11-03 14:39:44 +000099 image = argv[2];
100 label = argv[3];
Georgios Pinitas37561862017-10-19 10:51:03 +0100101 }
102
103 // Check if OpenCL is available and initialize the scheduler
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000104 TargetHint hint = TargetHint::NEON;
105 if(Graph::opencl_is_available())
Georgios Pinitas37561862017-10-19 10:51:03 +0100106 {
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000107 hint = TargetHint::OPENCL;
Georgios Pinitas37561862017-10-19 10:51:03 +0100108 }
109
110 Graph graph;
Georgios Pinitas37561862017-10-19 10:51:03 +0100111
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000112 graph << hint
Isabella Gottardi97988a42017-11-03 14:39:44 +0000113 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
114 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas37561862017-10-19 10:51:03 +0100115 << ConvolutionLayer(
116 7U, 7U, 96U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000117 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
118 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100119 PadStrideInfo(2, 2, 0, 0))
120 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
121 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
122 << ConvolutionLayer(
123 1U, 1U, 16U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100126 PadStrideInfo(1, 1, 0, 0))
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 << get_expand_fire_node(data_path, "fire2", 64U, 64U)
129 << ConvolutionLayer(
130 1U, 1U, 16U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000131 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100133 PadStrideInfo(1, 1, 0, 0))
134 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
135 << get_expand_fire_node(data_path, "fire3", 64U, 64U)
136 << ConvolutionLayer(
137 1U, 1U, 32U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100140 PadStrideInfo(1, 1, 0, 0))
141 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
142 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
143 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
144 << ConvolutionLayer(
145 1U, 1U, 32U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
147 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100148 PadStrideInfo(1, 1, 0, 0))
149 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
150 << get_expand_fire_node(data_path, "fire5", 128U, 128U)
151 << ConvolutionLayer(
152 1U, 1U, 48U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100155 PadStrideInfo(1, 1, 0, 0))
156 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
157 << get_expand_fire_node(data_path, "fire6", 192U, 192U)
158 << ConvolutionLayer(
159 1U, 1U, 48U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000160 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100162 PadStrideInfo(1, 1, 0, 0))
163 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
164 << get_expand_fire_node(data_path, "fire7", 192U, 192U)
165 << ConvolutionLayer(
166 1U, 1U, 64U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100169 PadStrideInfo(1, 1, 0, 0))
170 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
171 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
172 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
173 << ConvolutionLayer(
174 1U, 1U, 64U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000175 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
176 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100177 PadStrideInfo(1, 1, 0, 0))
178 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
179 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
180 << ConvolutionLayer(
181 1U, 1U, 1000U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000182 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
183 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100184 PadStrideInfo(1, 1, 0, 0))
185 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000186 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
Isabella Gottardi97988a42017-11-03 14:39:44 +0000187 << FlattenLayer()
Georgios Pinitas37561862017-10-19 10:51:03 +0100188 << SoftmaxLayer()
Isabella Gottardi97988a42017-11-03 14:39:44 +0000189 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas37561862017-10-19 10:51:03 +0100190
191 graph.run();
192}
193
194/** Main program for Squeezenet v1.0
195 *
196 * @param[in] argc Number of arguments
Isabella Gottardi97988a42017-11-03 14:39:44 +0000197 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas37561862017-10-19 10:51:03 +0100198 */
199int main(int argc, const char **argv)
200{
201 return arm_compute::utils::run_example(argc, argv, main_graph_squeezenet);
Isabella Gottardi97988a42017-11-03 14:39:44 +0000202}