2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph.h"
25 #include "support/ToolchainSupport.h"
26 #include "utils/GraphUtils.h"
27 #include "utils/Utils.h"
31 using namespace arm_compute::utils;
32 using namespace arm_compute::graph::frontend;
33 using namespace arm_compute::graph_utils;
35 /** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API
37 * @param[in] argc Number of arguments
38 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] data layout, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
40 class GraphMobilenetExample : public Example
43 void do_setup(int argc, char **argv) override
45 std::string data_path; /* Path to the trainable data */
46 std::string image; /* Image data */
47 std::string label; /* Label data */
49 // Create a preprocessor object
50 std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>();
52 // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON
53 const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0;
54 Target target_hint = set_target_hint(target);
55 ConvolutionMethod convolution_hint = ConvolutionMethod::GEMM;
56 DepthwiseConvolutionMethod depthwise_convolution_hint = DepthwiseConvolutionMethod::OPTIMIZED_3x3;
57 FastMathHint fast_math_hint = FastMathHint::DISABLED;
59 // Set model to execute. 0 (MobileNetV1_1.0_224), 1 (MobileNetV1_0.75_160)
60 int model_id = (argc > 2) ? std::strtol(argv[2], nullptr, 10) : 0;
61 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)");
62 int layout_id = (argc > 3) ? std::strtol(argv[3], nullptr, 10) : 0;
63 ARM_COMPUTE_ERROR_ON_MSG(layout_id > 1, "Invalid layout ID. Layout must be 0 (NCHW) or 1 (NHWC)");
65 float depth_scale = (model_id == 0) ? 1.f : 0.75;
66 unsigned int spatial_size = (model_id == 0) ? 224 : 160;
67 std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/";
68 TensorDescriptor input_descriptor_nchw = TensorDescriptor(TensorShape(spatial_size, spatial_size, 3U, 1U), DataType::F32);
69 TensorDescriptor input_descriptor_nhwc = TensorDescriptor(TensorShape(3U, spatial_size, spatial_size, 1U), DataType::F32).set_layout(DataLayout::NHWC);
70 TensorDescriptor input_descriptor = (layout_id == 0) ? input_descriptor_nchw : input_descriptor_nhwc;
76 std::cout << "Usage: " << argv[0] << " [target] [model] [layout] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
77 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
78 std::cout << "No data layout provided: using NCHW\n\n";
79 std::cout << "No data folder provided: using random values\n\n";
83 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [model] [layout] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
84 std::cout << "No model ID provided: using MobileNetV1_1.0_224\n\n";
85 std::cout << "No data layout provided: using NCHW\n\n";
86 std::cout << "No data folder provided: using random values\n\n";
90 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [layout] [path_to_data] [image] [labels] [fast_math_hint]\n\n";
91 std::cout << "No data layout provided: using NCHW\n\n";
92 std::cout << "No data folder provided: using random values\n\n";
96 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n";
97 std::cout << "No data folder provided: using random values\n\n";
102 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [image] [labels] [fast_math_hint]\n\n";
103 std::cout << "No image provided: using random values\n\n";
104 std::cout << "No text file with labels provided: skipping output accessor\n\n";
110 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n";
111 std::cout << "No text file with labels provided: skipping output accessor\n\n";
118 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n";
119 std::cout << "No fast math info provided: disabling fast math\n\n";
126 fast_math_hint = (std::strtol(argv[7], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED;
129 // Add model path to data path
130 if(!data_path.empty())
132 data_path += model_path;
137 << depthwise_convolution_hint
139 << InputLayer(input_descriptor,
140 get_input_accessor(image, std::move(preprocessor), false))
142 3U, 3U, 32U * depth_scale,
143 get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW),
144 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
145 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
146 .set_name("Conv2d_0")
147 << BatchNormalizationLayer(
148 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"),
149 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"),
150 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"),
151 get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"),
153 .set_name("Conv2d_0/BatchNorm")
154 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6");
155 graph << get_dwsc_node(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
156 graph << get_dwsc_node(data_path, "Conv2d_2", 128 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
157 graph << get_dwsc_node(data_path, "Conv2d_3", 128 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
158 graph << get_dwsc_node(data_path, "Conv2d_4", 256 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
159 graph << get_dwsc_node(data_path, "Conv2d_5", 256 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
160 graph << get_dwsc_node(data_path, "Conv2d_6", 512 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
161 graph << get_dwsc_node(data_path, "Conv2d_7", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
162 graph << get_dwsc_node(data_path, "Conv2d_8", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
163 graph << get_dwsc_node(data_path, "Conv2d_9", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
164 graph << get_dwsc_node(data_path, "Conv2d_10", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
165 graph << get_dwsc_node(data_path, "Conv2d_11", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
166 graph << get_dwsc_node(data_path, "Conv2d_12", 1024 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
167 graph << get_dwsc_node(data_path, "Conv2d_13", 1024 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0));
168 graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool_1a")
171 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW),
172 get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"),
173 PadStrideInfo(1, 1, 0, 0))
174 .set_name("Logits/Conv2d_1c_1x1")
175 << ReshapeLayer(TensorShape(1001U)).set_name("Reshape")
176 << SoftmaxLayer().set_name("Softmax")
177 << OutputLayer(get_output_accessor(label, 5));
181 config.use_tuner = (target == 2);
182 graph.finalize(target_hint, config);
184 void do_run() override
191 Stream graph{ 0, "MobileNetV1" };
193 BranchLayer get_dwsc_node(const std::string &data_path, std::string &¶m_path,
194 unsigned int conv_filt,
195 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
197 std::string total_path = param_path + "_";
199 sg << DepthwiseConvolutionLayer(
201 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW),
202 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
204 .set_name(total_path + "depthwise/depthwise")
205 << BatchNormalizationLayer(
206 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
207 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
208 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
209 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
211 .set_name(total_path + "depthwise/BatchNorm")
212 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6")
215 get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW),
216 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
217 conv_pad_stride_info)
218 .set_name(total_path + "pointwise/Conv2D")
219 << BatchNormalizationLayer(
220 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
221 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
222 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
223 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
225 .set_name(total_path + "pointwise/BatchNorm")
226 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6");
228 return BranchLayer(std::move(sg));
232 /** Main program for MobileNetV1
234 * @param[in] argc Number of arguments
235 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner),
236 * [optional] Model ID (0 = MobileNetV1_1.0_224, 1 = MobileNetV1_0.75_160),
237 * [optional] Path to the weights folder,
240 * [optional] data layout,
241 * [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) )
243 int main(int argc, char **argv)
245 return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv);