Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | #include "arm_compute/graph/Graph.h" |
| 26 | #include "arm_compute/graph/Nodes.h" |
| 27 | #include "support/ToolchainSupport.h" |
| 28 | #include "utils/GraphUtils.h" |
| 29 | #include "utils/Utils.h" |
| 30 | |
| 31 | #include <cstdlib> |
| 32 | |
| 33 | using namespace arm_compute::graph; |
| 34 | using namespace arm_compute::graph_utils; |
| 35 | |
| 36 | BranchLayer get_dwsc_node(const std::string &data_path, std::string &¶m_path, |
| 37 | unsigned int conv_filt, |
| 38 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info) |
| 39 | { |
| 40 | std::string total_path = "/cnn_data/mobilenet_v1_model/" + param_path + "_"; |
| 41 | SubGraph sg; |
| 42 | sg << DepthwiseConvolutionLayer( |
| 43 | 3U, 3U, |
| 44 | get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"), |
| 45 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 46 | dwc_pad_stride_info, |
| 47 | true) |
| 48 | << BatchNormalizationLayer( |
| 49 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"), |
| 50 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"), |
| 51 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), |
| 52 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"), |
| 53 | 0.001f) |
| 54 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 55 | << ConvolutionLayer( |
| 56 | 1U, 1U, conv_filt, |
| 57 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy"), |
| 58 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 59 | conv_pad_stride_info) |
| 60 | << BatchNormalizationLayer( |
| 61 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"), |
| 62 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"), |
| 63 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"), |
| 64 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"), |
| 65 | 0.001f) |
| 66 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)); |
| 67 | |
| 68 | return BranchLayer(std::move(sg)); |
| 69 | } |
| 70 | |
| 71 | /** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API |
| 72 | * |
| 73 | * @param[in] argc Number of arguments |
| 74 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 75 | */ |
| 76 | void main_graph_mobilenet(int argc, const char **argv) |
| 77 | { |
| 78 | std::string data_path; /* Path to the trainable data */ |
| 79 | std::string image; /* Image data */ |
| 80 | std::string label; /* Label data */ |
| 81 | |
| 82 | constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */ |
| 83 | constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */ |
| 84 | constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */ |
| 85 | |
| 86 | // Parse arguments |
| 87 | if(argc < 2) |
| 88 | { |
| 89 | // Print help |
| 90 | std::cout << "Usage: " << argv[0] << " [path_to_data] [image] [labels]\n\n"; |
| 91 | std::cout << "No data folder provided: using random values\n\n"; |
| 92 | } |
| 93 | else if(argc == 2) |
| 94 | { |
| 95 | data_path = argv[1]; |
| 96 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [image] [labels]\n\n"; |
| 97 | std::cout << "No image provided: using random values\n\n"; |
| 98 | } |
| 99 | else if(argc == 3) |
| 100 | { |
| 101 | data_path = argv[1]; |
| 102 | image = argv[2]; |
| 103 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [labels]\n\n"; |
| 104 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | data_path = argv[1]; |
| 109 | image = argv[2]; |
| 110 | label = argv[3]; |
| 111 | } |
| 112 | |
| 113 | // Check if OpenCL is available and initialize the scheduler |
| 114 | TargetHint hint = TargetHint::NEON; |
| 115 | if(Graph::opencl_is_available()) |
| 116 | { |
| 117 | hint = TargetHint::OPENCL; |
| 118 | } |
| 119 | |
| 120 | Graph graph; |
| 121 | graph << hint |
| 122 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32), |
| 123 | get_input_accessor(image, mean_r, mean_g, mean_b)) |
| 124 | << ConvolutionLayer( |
| 125 | 3U, 3U, 32U, |
| 126 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_weights.npy"), |
| 127 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 128 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)) |
| 129 | << BatchNormalizationLayer( |
| 130 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_moving_mean.npy"), |
| 131 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_moving_variance.npy"), |
| 132 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_beta.npy"), |
| 133 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Conv2d_0_BatchNorm_gamma.npy"), |
| 134 | 0.001f) |
| 135 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)) |
| 136 | << get_dwsc_node(data_path, "Conv2d_1", 64, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0)) |
| 137 | << get_dwsc_node(data_path, "Conv2d_2", 128, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 138 | << get_dwsc_node(data_path, "Conv2d_3", 128, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 139 | << get_dwsc_node(data_path, "Conv2d_4", 256, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 140 | << get_dwsc_node(data_path, "Conv2d_5", 256, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 141 | << get_dwsc_node(data_path, "Conv2d_6", 512, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 142 | << get_dwsc_node(data_path, "Conv2d_7", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 143 | << get_dwsc_node(data_path, "Conv2d_8", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 144 | << get_dwsc_node(data_path, "Conv2d_9", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 145 | << get_dwsc_node(data_path, "Conv2d_10", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 146 | << get_dwsc_node(data_path, "Conv2d_11", 512, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 147 | << get_dwsc_node(data_path, "Conv2d_12", 1024, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 148 | << get_dwsc_node(data_path, "Conv2d_13", 1024, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0)) |
| 149 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) |
| 150 | << ConvolutionLayer( |
| 151 | 1U, 1U, 1001U, |
| 152 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Logits_Conv2d_1c_1x1_weights.npy"), |
| 153 | get_weights_accessor(data_path, "/cnn_data/mobilenet_v1_model/Logits_Conv2d_1c_1x1_biases.npy"), |
| 154 | PadStrideInfo(1, 1, 0, 0)) |
| 155 | << ReshapeLayer(TensorShape(1001U)) |
| 156 | << SoftmaxLayer() |
| 157 | << Tensor(get_output_accessor(label, 5)); |
| 158 | |
| 159 | graph.run(); |
| 160 | } |
| 161 | |
| 162 | /** Main program for MobileNetV1 |
| 163 | * |
| 164 | * @param[in] argc Number of arguments |
| 165 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 166 | */ |
| 167 | int main(int argc, const char **argv) |
| 168 | { |
| 169 | return arm_compute::utils::run_example(argc, argv, main_graph_mobilenet); |
| 170 | } |