blob: 195fe0addb40c80ddc0f08c0493315b62f11cbd1 [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 */
24#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
Georgios Pinitas37561862017-10-19 10:51:03 +010028#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/Nodes.h"
30#include "arm_compute/graph/SubGraph.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "arm_compute/runtime/Scheduler.h"
33#include "support/ToolchainSupport.h"
34#include "utils/GraphUtils.h"
35#include "utils/Utils.h"
36
37#include <cstdlib>
38#include <iostream>
39#include <memory>
40#include <tuple>
41
42using namespace arm_compute::graph;
43using namespace arm_compute::graph_utils;
Isabella Gottardi4398bec2017-10-19 16:10:59 +010044using namespace arm_compute::logging;
Georgios Pinitas37561862017-10-19 10:51:03 +010045
Georgios Pinitas37561862017-10-19 10:51:03 +010046BranchLayer get_expand_fire_node(const std::string &data_path, std::string &&param_path, unsigned int expand1_filt, unsigned int expand3_filt)
47{
48 std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_";
49 SubGraph i_a;
50 i_a << ConvolutionLayer(
51 1U, 1U, expand1_filt,
Isabella Gottardi97988a42017-11-03 14:39:44 +000052 get_weights_accessor(data_path, total_path + "expand1x1_w.npy"),
53 get_weights_accessor(data_path, total_path + "expand1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +010054 PadStrideInfo(1, 1, 0, 0))
55 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
56
57 SubGraph i_b;
58 i_b << ConvolutionLayer(
59 3U, 3U, expand3_filt,
Isabella Gottardi97988a42017-11-03 14:39:44 +000060 get_weights_accessor(data_path, total_path + "expand3x3_w.npy"),
61 get_weights_accessor(data_path, total_path + "expand3x3_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +010062 PadStrideInfo(1, 1, 1, 1))
63 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
64
65 return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b));
66}
67
68/** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API
69 *
70 * @param[in] argc Number of arguments
Isabella Gottardi97988a42017-11-03 14:39:44 +000071 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas37561862017-10-19 10:51:03 +010072 */
73void main_graph_squeezenet(int argc, const char **argv)
74{
Isabella Gottardi97988a42017-11-03 14:39:44 +000075 std::string data_path; /* Path to the trainable data */
76 std::string image; /* Image data */
77 std::string label; /* Label data */
78
79 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
80 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
81 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
Georgios Pinitas37561862017-10-19 10:51:03 +010082
83 // Parse arguments
84 if(argc < 2)
85 {
86 // Print help
Isabella Gottardi97988a42017-11-03 14:39:44 +000087 std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n";
Georgios Pinitas37561862017-10-19 10:51:03 +010088 std::cout << "No data folder provided: using random values\n\n";
89 }
90 else if(argc == 2)
91 {
92 //Do something with argv[1]
93 data_path = argv[1];
Isabella Gottardi97988a42017-11-03 14:39:44 +000094 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n";
95 std::cout << "No image provided: using random values\n";
96 }
97 else if(argc == 3)
98 {
99 data_path = argv[1];
100 image = argv[2];
101 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n";
102 std::cout << "No text file with labels provided: skipping output accessor\n";
Georgios Pinitas37561862017-10-19 10:51:03 +0100103 }
104 else
105 {
Georgios Pinitas37561862017-10-19 10:51:03 +0100106 data_path = argv[1];
Isabella Gottardi97988a42017-11-03 14:39:44 +0000107 image = argv[2];
108 label = argv[3];
Georgios Pinitas37561862017-10-19 10:51:03 +0100109 }
110
111 // Check if OpenCL is available and initialize the scheduler
112 if(arm_compute::opencl_is_available())
113 {
114 arm_compute::CLScheduler::get().default_init();
115 }
116
117 Graph graph;
Georgios Pinitas37561862017-10-19 10:51:03 +0100118
119 graph << TargetHint::OPENCL
Isabella Gottardi97988a42017-11-03 14:39:44 +0000120 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
121 get_input_accessor(image, mean_r, mean_g, mean_b))
Georgios Pinitas37561862017-10-19 10:51:03 +0100122 << ConvolutionLayer(
123 7U, 7U, 96U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000124 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"),
125 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100126 PadStrideInfo(2, 2, 0, 0))
127 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
128 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
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/fire2_squeeze1x1_w.npy"),
132 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_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, "fire2", 64U, 64U)
136 << ConvolutionLayer(
137 1U, 1U, 16U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000138 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"),
139 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_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, "fire3", 64U, 64U)
143 << ConvolutionLayer(
144 1U, 1U, 32U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000145 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"),
146 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100147 PadStrideInfo(1, 1, 0, 0))
148 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
149 << get_expand_fire_node(data_path, "fire4", 128U, 128U)
150 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
151 << ConvolutionLayer(
152 1U, 1U, 32U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000153 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"),
154 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_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, "fire5", 128U, 128U)
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/fire6_squeeze1x1_w.npy"),
161 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_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, "fire6", 192U, 192U)
165 << ConvolutionLayer(
166 1U, 1U, 48U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000167 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"),
168 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_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, "fire7", 192U, 192U)
172 << ConvolutionLayer(
173 1U, 1U, 64U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000174 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"),
175 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100176 PadStrideInfo(1, 1, 0, 0))
177 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
178 << get_expand_fire_node(data_path, "fire8", 256U, 256U)
179 << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL)))
180 << ConvolutionLayer(
181 1U, 1U, 64U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000182 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"),
183 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100184 PadStrideInfo(1, 1, 0, 0))
185 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
186 << get_expand_fire_node(data_path, "fire9", 256U, 256U)
187 << ConvolutionLayer(
188 1U, 1U, 1000U,
Isabella Gottardi97988a42017-11-03 14:39:44 +0000189 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"),
190 get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"),
Georgios Pinitas37561862017-10-19 10:51:03 +0100191 PadStrideInfo(1, 1, 0, 0))
192 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))
193 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 13, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL)))
Isabella Gottardi97988a42017-11-03 14:39:44 +0000194 << FlattenLayer()
Georgios Pinitas37561862017-10-19 10:51:03 +0100195 << SoftmaxLayer()
Isabella Gottardi97988a42017-11-03 14:39:44 +0000196 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas37561862017-10-19 10:51:03 +0100197
198 graph.run();
199}
200
201/** Main program for Squeezenet v1.0
202 *
203 * @param[in] argc Number of arguments
Isabella Gottardi97988a42017-11-03 14:39:44 +0000204 * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas37561862017-10-19 10:51:03 +0100205 */
206int main(int argc, const char **argv)
207{
208 return arm_compute::utils::run_example(argc, argv, main_graph_squeezenet);
Isabella Gottardi97988a42017-11-03 14:39:44 +0000209}