blob: 8c992162eb83d26e17ddbd8f17c3ec2204f7e13f [file] [log] [blame]
Georgios Pinitas236bfe72017-11-23 15:59:55 +00001/*
Georgios Pinitas7f530b32018-01-22 11:20:44 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas236bfe72017-11-23 15:59:55 +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 */
Georgios Pinitas236bfe72017-11-23 15:59:55 +000024#include "arm_compute/graph/Graph.h"
25#include "arm_compute/graph/Nodes.h"
26#include "support/ToolchainSupport.h"
27#include "utils/GraphUtils.h"
28#include "utils/Utils.h"
29
30#include <cstdlib>
31
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000032using namespace arm_compute::utils;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000033using namespace arm_compute::graph;
34using namespace arm_compute::graph_utils;
35
Georgios Pinitas236bfe72017-11-23 15:59:55 +000036/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API
37 *
38 * @param[in] argc Number of arguments
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000039 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas236bfe72017-11-23 15:59:55 +000040 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041class GraphMobilenetExample : public Example
Georgios Pinitas236bfe72017-11-23 15:59:55 +000042{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043public:
44 void do_setup(int argc, char **argv) override
45 {
46 std::string data_path; /* Path to the trainable data */
47 std::string image; /* Image data */
48 std::string label; /* Label data */
Georgios Pinitas236bfe72017-11-23 15:59:55 +000049
Georgios Pinitas140fdc72018-02-16 11:42:38 +000050 // Create a preprocessor object
51 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
Georgios Pinitas236bfe72017-11-23 15:59:55 +000052
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000053 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
54 const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
55 TargetHint target_hint = set_target_hint(int_target_hint);
Gian Marco36a0a462018-01-12 10:21:40 +000056 ConvolutionMethodHint convolution_hint = ConvolutionMethodHint::GEMM;
Gian Marcobfa3b522017-12-12 10:08:38 +000057
Georgios Pinitas7f530b32018-01-22 11:20:44 +000058 // Set model to execute. 0 (MobileNetV1_1.0_224), 1 (MobileNetV1_0.75_160)
59 int model_id = (argc > 2) ? std::strtol(argv[2], nullptr, 10) : 0;
60 ARM_COMPUTE_ERROR_ON_MSG(model_id > 1, "Invalid model ID. Model must be 0 (MobileNetV1_1.0_224) or 1 (MobileNetV1_0.75_160)");
61 float depth_scale = (model_id == 0) ? 1.f : 0.75;
62 unsigned int spatial_size = (model_id == 0) ? 224 : 160;
63 std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
64
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000065 // Parse arguments
66 if(argc < 2)
67 {
68 // Print help
Georgios Pinitas7f530b32018-01-22 11:20:44 +000069 std::cout << "Usage: " << argv[0] << " [target] [model] [path_to_data] [image] [labels]\n\n";
70 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000071 std::cout << "No data folder provided: using random values\n\n";
72 }
73 else if(argc == 2)
74 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000075 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [model] [path_to_data] [image] [labels]\n\n";
76 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000077 std::cout << "No data folder provided: using random values\n\n";
78 }
79 else if(argc == 3)
80 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000081 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [path_to_data] [image] [labels]\n\n";
82 std::cout << "No data folder provided: using random values\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000083 }
84 else if(argc == 4)
85 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000086 data_path = argv[3];
87 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [image] [labels]\n\n";
88 std::cout << "No image provided: using random values\n\n";
89 }
90 else if(argc == 5)
91 {
92 data_path = argv[3];
93 image = argv[4];
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000094 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
95 std::cout << "No text file with labels provided: skipping output accessor\n\n";
96 }
97 else
98 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000099 data_path = argv[3];
100 image = argv[4];
101 label = argv[5];
102 }
103
104 // Add model path to data path
105 if(!data_path.empty())
106 {
107 data_path += model_path;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000108 }
109
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +0000110 // Initialize graph
111 graph.graph_init(int_target_hint == 2);
112
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113 graph << target_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114 << convolution_hint
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000115 << Tensor(TensorInfo(TensorShape(spatial_size, spatial_size, 3U, 1U), 1, DataType::F32),
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000116 get_input_accessor(image, std::move(preprocessor), false))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000117 << ConvolutionLayer(
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000118 3U, 3U, 32U * depth_scale,
119 get_weights_accessor(data_path, "Conv2d_0_weights.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000120 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
121 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
122 << BatchNormalizationLayer(
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000123 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"),
124 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"),
125 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"),
126 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"),
Giorgio Arena11674872018-02-07 15:38:12 +0000127 0.001f, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000128 << get_dwsc_node(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0))
Georgios Pinitas4074c992018-01-30 18:13:46 +0000129 << get_dwsc_node(data_path, "Conv2d_2", 128 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
130 << get_dwsc_node(data_path, "Conv2d_3", 128 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
131 << get_dwsc_node(data_path, "Conv2d_4", 256 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
132 << get_dwsc_node(data_path, "Conv2d_5", 256 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
133 << get_dwsc_node(data_path, "Conv2d_6", 512 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
134 << get_dwsc_node(data_path, "Conv2d_7", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
135 << get_dwsc_node(data_path, "Conv2d_8", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
136 << get_dwsc_node(data_path, "Conv2d_9", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
137 << get_dwsc_node(data_path, "Conv2d_10", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
138 << get_dwsc_node(data_path, "Conv2d_11", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
139 << get_dwsc_node(data_path, "Conv2d_12", 1024 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
140 << get_dwsc_node(data_path, "Conv2d_13", 1024 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000141 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
142 << ConvolutionLayer(
143 1U, 1U, 1001U,
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000144 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
145 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000146 PadStrideInfo(1, 1, 0, 0))
147 << ReshapeLayer(TensorShape(1001U))
148 << SoftmaxLayer()
149 << Tensor(get_output_accessor(label, 5));
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000150 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000151 void do_run() override
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000152 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000153 // Run graph
154 graph.run();
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000155 }
156
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000157private:
158 Graph graph{};
Gian Marcobfa3b522017-12-12 10:08:38 +0000159
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000160 BranchLayer get_dwsc_node(const std::string &data_path, std::string &&param_path,
161 unsigned int conv_filt,
162 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
163 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000164 std::string total_path = param_path + "_";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000165 SubGraph sg;
166 sg << DepthwiseConvolutionLayer(
167 3U, 3U,
168 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
169 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
170 dwc_pad_stride_info,
171 true)
172 << BatchNormalizationLayer(
173 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
174 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000175 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000176 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
Giorgio Arena11674872018-02-07 15:38:12 +0000177 0.001f, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000178 << ConvolutionLayer(
179 1U, 1U, conv_filt,
180 get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
181 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
182 conv_pad_stride_info)
183 << BatchNormalizationLayer(
184 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
185 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000186 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000187 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
Giorgio Arena11674872018-02-07 15:38:12 +0000188 0.001f, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
Gian Marcobfa3b522017-12-12 10:08:38 +0000189
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000190 return BranchLayer(std::move(sg));
191 }
192};
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000193
194/** Main program for MobileNetV1
195 *
196 * @param[in] argc Number of arguments
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000197 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL),
198 * [optional] Model ID (0 = MobileNetV1_1.0_224, 1 = MobileNetV1_0.75_160),
199 * [optional] Path to the weights folder,
200 * [optional] image,
201 * [optional] labels )
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000202 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000203int main(int argc, char **argv)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000204{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000205 return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000206}