blob: 4d01055c5054daf4ac0bd9195d3bf92fdcf2eb81 [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 Pinitasd8734b52017-12-22 15:27:52 +000024#include "arm_compute/graph2.h"
Georgios Pinitas236bfe72017-11-23 15:59:55 +000025#include "support/ToolchainSupport.h"
26#include "utils/GraphUtils.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000031using namespace arm_compute::utils;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032using namespace arm_compute::graph2::frontend;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000033using namespace arm_compute::graph_utils;
34
Georgios Pinitas236bfe72017-11-23 15:59:55 +000035/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API
36 *
37 * @param[in] argc Number of arguments
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000038 * @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 +000039 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000040class GraphMobilenetExample : public Example
Georgios Pinitas236bfe72017-11-23 15:59:55 +000041{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000042public:
43 void do_setup(int argc, char **argv) override
44 {
45 std::string data_path; /* Path to the trainable data */
46 std::string image; /* Image data */
47 std::string label; /* Label data */
Georgios Pinitas236bfe72017-11-23 15:59:55 +000048
Georgios Pinitas140fdc72018-02-16 11:42:38 +000049 // Create a preprocessor object
50 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
Georgios Pinitas236bfe72017-11-23 15:59:55 +000051
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000052 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
Georgios Pinitasd8734b52017-12-22 15:27:52 +000053 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
54 Target target_hint = set_target_hint2(target);
55 ConvolutionMethod convolution_hint = ConvolutionMethod::GEMM;
56 DepthwiseConvolutionMethod depthwise_convolution_hint = DepthwiseConvolutionMethod::OPTIMIZED_3x3;
57 bool enable_tuning = (target == 2);
58 bool enable_memory_management = true;
Gian Marcobfa3b522017-12-12 10:08:38 +000059
Georgios Pinitas7f530b32018-01-22 11:20:44 +000060 // Set model to execute. 0 (MobileNetV1_1.0_224), 1 (MobileNetV1_0.75_160)
61 int model_id = (argc > 2) ? std::strtol(argv[2], nullptr, 10) : 0;
62 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)");
63 float depth_scale = (model_id == 0) ? 1.f : 0.75;
64 unsigned int spatial_size = (model_id == 0) ? 224 : 160;
65 std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
66
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000067 // Parse arguments
68 if(argc < 2)
69 {
70 // Print help
Georgios Pinitas7f530b32018-01-22 11:20:44 +000071 std::cout << "Usage: " << argv[0] << " [target] [model] [path_to_data] [image] [labels]\n\n";
72 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000073 std::cout << "No data folder provided: using random values\n\n";
74 }
75 else if(argc == 2)
76 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000077 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [model] [path_to_data] [image] [labels]\n\n";
78 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000079 std::cout << "No data folder provided: using random values\n\n";
80 }
81 else if(argc == 3)
82 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000083 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [path_to_data] [image] [labels]\n\n";
84 std::cout << "No data folder provided: using random values\n\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 }
86 else if(argc == 4)
87 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +000088 data_path = argv[3];
89 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [image] [labels]\n\n";
90 std::cout << "No image provided: using random values\n\n";
91 }
92 else if(argc == 5)
93 {
94 data_path = argv[3];
95 image = argv[4];
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000096 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
97 std::cout << "No text file with labels provided: skipping output accessor\n\n";
98 }
99 else
100 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000101 data_path = argv[3];
102 image = argv[4];
103 label = argv[5];
104 }
105
106 // Add model path to data path
107 if(!data_path.empty())
108 {
109 data_path += model_path;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000110 }
111
112 graph << target_hint
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113 << convolution_hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000114 << depthwise_convolution_hint
115 << InputLayer(TensorDescriptor(TensorShape(spatial_size, spatial_size, 3U, 1U), DataType::F32),
116 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"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127 0.001f)
128 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000129 << 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 +0000130 << get_dwsc_node(data_path, "Conv2d_2", 128 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
131 << get_dwsc_node(data_path, "Conv2d_3", 128 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
132 << get_dwsc_node(data_path, "Conv2d_4", 256 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
133 << get_dwsc_node(data_path, "Conv2d_5", 256 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
134 << get_dwsc_node(data_path, "Conv2d_6", 512 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
135 << get_dwsc_node(data_path, "Conv2d_7", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
136 << get_dwsc_node(data_path, "Conv2d_8", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
137 << get_dwsc_node(data_path, "Conv2d_9", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
138 << get_dwsc_node(data_path, "Conv2d_10", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
139 << get_dwsc_node(data_path, "Conv2d_11", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
140 << get_dwsc_node(data_path, "Conv2d_12", 1024 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0))
141 << 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 +0000142 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
143 << ConvolutionLayer(
144 1U, 1U, 1001U,
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000145 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"),
146 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000147 PadStrideInfo(1, 1, 0, 0))
148 << ReshapeLayer(TensorShape(1001U))
149 << SoftmaxLayer()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000150 << OutputLayer(get_output_accessor(label, 5));
Gian Marcoc1b6e372018-02-21 18:03:26 +0000151
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000152 // Finalize graph
153 graph.finalize(target_hint, enable_tuning, enable_memory_management);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000154 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000155 void do_run() override
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000156 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000157 // Run graph
158 graph.run();
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000159 }
160
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000161private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000162 Stream graph{ 0, "MobileNetV1" };
Gian Marcobfa3b522017-12-12 10:08:38 +0000163
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000164 BranchLayer get_dwsc_node(const std::string &data_path, std::string &&param_path,
165 unsigned int conv_filt,
166 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
167 {
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000168 std::string total_path = param_path + "_";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000169 SubStream sg(graph);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000170 sg << DepthwiseConvolutionLayer(
171 3U, 3U,
172 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
173 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000174 dwc_pad_stride_info)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000175 << BatchNormalizationLayer(
176 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
177 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000178 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000179 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000180 0.001f)
181 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000182 << ConvolutionLayer(
183 1U, 1U, conv_filt,
184 get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
185 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
186 conv_pad_stride_info)
187 << BatchNormalizationLayer(
188 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
189 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000190 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000191 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000192 0.001f)
193 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
Gian Marcobfa3b522017-12-12 10:08:38 +0000194
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000195 return BranchLayer(std::move(sg));
196 }
197};
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000198
199/** Main program for MobileNetV1
200 *
201 * @param[in] argc Number of arguments
Georgios Pinitas7f530b32018-01-22 11:20:44 +0000202 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL),
203 * [optional] Model ID (0 = MobileNetV1_1.0_224, 1 = MobileNetV1_0.75_160),
204 * [optional] Path to the weights folder,
205 * [optional] image,
206 * [optional] labels )
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000207 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000208int main(int argc, char **argv)
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000209{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000210 return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000211}