blob: 0c916c7ba08d626093df7128776525dc6e213cf3 [file] [log] [blame]
Georgios Pinitas236bfe72017-11-23 15:59:55 +00001/*
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 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
32using namespace arm_compute::graph;
33using namespace arm_compute::graph_utils;
34
Gian Marcobfa3b522017-12-12 10:08:38 +000035namespace
36{
Georgios Pinitas236bfe72017-11-23 15:59:55 +000037BranchLayer get_dwsc_node(const std::string &data_path, std::string &&param_path,
38 unsigned int conv_filt,
39 PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info)
40{
41 std::string total_path = "/cnn_data/mobilenet_v1_model/" + param_path + "_";
42 SubGraph sg;
43 sg << DepthwiseConvolutionLayer(
44 3U, 3U,
45 get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"),
46 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
47 dwc_pad_stride_info,
48 true)
49 << BatchNormalizationLayer(
50 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"),
51 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"),
52 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"),
53 get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"),
54 0.001f)
55 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
56 << ConvolutionLayer(
57 1U, 1U, conv_filt,
58 get_weights_accessor(data_path, total_path + "pointwise_weights.npy"),
59 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
60 conv_pad_stride_info)
61 << BatchNormalizationLayer(
62 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"),
63 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"),
64 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"),
65 get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"),
66 0.001f)
67 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
68
69 return BranchLayer(std::move(sg));
70}
Gian Marcobfa3b522017-12-12 10:08:38 +000071} // namespace
Georgios Pinitas236bfe72017-11-23 15:59:55 +000072
73/** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API
74 *
75 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +000076 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas236bfe72017-11-23 15:59:55 +000077 */
78void main_graph_mobilenet(int argc, const char **argv)
79{
80 std::string data_path; /* Path to the trainable data */
81 std::string image; /* Image data */
82 std::string label; /* Label data */
83
84 constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */
85 constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */
86 constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */
87
Gian Marcobfa3b522017-12-12 10:08:38 +000088 // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON
89 TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0);
90
Georgios Pinitas236bfe72017-11-23 15:59:55 +000091 // Parse arguments
92 if(argc < 2)
93 {
94 // Print help
Gian Marcobfa3b522017-12-12 10:08:38 +000095 std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n";
Georgios Pinitas236bfe72017-11-23 15:59:55 +000096 std::cout << "No data folder provided: using random values\n\n";
97 }
98 else if(argc == 2)
99 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000100 std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n";
101 std::cout << "No data folder provided: using random values\n\n";
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000102 }
103 else if(argc == 3)
104 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000105 data_path = argv[2];
106 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n";
107 std::cout << "No image provided: using random values\n\n";
108 }
109 else if(argc == 4)
110 {
111 data_path = argv[2];
112 image = argv[3];
113 std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n";
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000114 std::cout << "No text file with labels provided: skipping output accessor\n\n";
115 }
116 else
117 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000118 data_path = argv[2];
119 image = argv[3];
120 label = argv[4];
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000121 }
122
123 Graph graph;
Gian Marcobfa3b522017-12-12 10:08:38 +0000124
125 graph << target_hint
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000126 << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32),
127 get_input_accessor(image, mean_r, mean_g, mean_b))
128 << ConvolutionLayer(
129 3U, 3U, 32U,
130 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_weights.npy"),
131 std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr),
132 PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR))
133 << BatchNormalizationLayer(
134 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_moving_mean.npy"),
135 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_moving_variance.npy"),
136 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_beta.npy"),
137 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_gamma.npy"),
138 0.001f)
Gian Marcobfa3b522017-12-12 10:08:38 +0000139
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000140 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))
141 << get_dwsc_node(data_path, "Conv2d_1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0))
142 << get_dwsc_node(data_path, "Conv2d_2", 128, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
143 << get_dwsc_node(data_path, "Conv2d_3", 128, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
144 << get_dwsc_node(data_path, "Conv2d_4", 256, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
145 << get_dwsc_node(data_path, "Conv2d_5", 256, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
146 << get_dwsc_node(data_path, "Conv2d_6", 512, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
147 << get_dwsc_node(data_path, "Conv2d_7", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
148 << get_dwsc_node(data_path, "Conv2d_8", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
149 << get_dwsc_node(data_path, "Conv2d_9", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
150 << get_dwsc_node(data_path, "Conv2d_10", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
151 << get_dwsc_node(data_path, "Conv2d_11", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
152 << get_dwsc_node(data_path, "Conv2d_12", 1024, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
153 << get_dwsc_node(data_path, "Conv2d_13", 1024, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0))
154 << PoolingLayer(PoolingLayerInfo(PoolingType::AVG))
155 << ConvolutionLayer(
156 1U, 1U, 1001U,
157 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Logits_Conv2d_1c_1x1_weights.npy"),
158 get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Logits_Conv2d_1c_1x1_biases.npy"),
159 PadStrideInfo(1, 1, 0, 0))
160 << ReshapeLayer(TensorShape(1001U))
161 << SoftmaxLayer()
162 << Tensor(get_output_accessor(label, 5));
163
164 graph.run();
165}
166
167/** Main program for MobileNetV1
168 *
169 * @param[in] argc Number of arguments
Gian Marcobfa3b522017-12-12 10:08:38 +0000170 * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels )
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000171 */
172int main(int argc, const char **argv)
173{
174 return arm_compute::utils::run_example(argc, argv, main_graph_mobilenet);
175}