Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 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 Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
| 26 | #include "arm_compute/graph/SubGraph.h" |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 27 | #include "support/ToolchainSupport.h" |
| 28 | #include "utils/GraphUtils.h" |
| 29 | #include "utils/Utils.h" |
| 30 | |
| 31 | #include <cstdlib> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 32 | #include <tuple> |
| 33 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 34 | using namespace arm_compute::utils; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 35 | using namespace arm_compute::graph; |
| 36 | using namespace arm_compute::graph_utils; |
| 37 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 38 | /** Example demonstrating how to implement Googlenet's network using the Compute Library's graph API |
| 39 | * |
| 40 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 41 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 42 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 43 | class GraphGooglenetExample : public Example |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 44 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 45 | public: |
| 46 | void do_setup(int argc, char **argv) override |
| 47 | { |
| 48 | std::string data_path; /* Path to the trainable data */ |
| 49 | std::string image; /* Image data */ |
| 50 | std::string label; /* Label data */ |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 51 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 52 | constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */ |
| 53 | constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */ |
| 54 | constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */ |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 55 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 56 | // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON |
| 57 | TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 58 | ConvolutionMethodHint convolution_hint = ConvolutionMethodHint::GEMM; |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 59 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 60 | // Parse arguments |
| 61 | if(argc < 2) |
| 62 | { |
| 63 | // Print help |
| 64 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 65 | std::cout << "No data folder provided: using random values\n\n"; |
| 66 | } |
| 67 | else if(argc == 2) |
| 68 | { |
| 69 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 70 | std::cout << "No data folder provided: using random values\n\n"; |
| 71 | } |
| 72 | else if(argc == 3) |
| 73 | { |
| 74 | data_path = argv[2]; |
| 75 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 76 | std::cout << "No image provided: using random values\n\n"; |
| 77 | } |
| 78 | else if(argc == 4) |
| 79 | { |
| 80 | data_path = argv[2]; |
| 81 | image = argv[3]; |
| 82 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 83 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | data_path = argv[2]; |
| 88 | image = argv[3]; |
| 89 | label = argv[4]; |
| 90 | } |
| 91 | |
| 92 | graph << target_hint |
| 93 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32), |
| 94 | get_input_accessor(image, mean_r, mean_g, mean_b)) |
| 95 | << ConvolutionLayer( |
| 96 | 7U, 7U, 64U, |
| 97 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_w.npy"), |
| 98 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv1/conv1_7x7_s2_b.npy"), |
| 99 | PadStrideInfo(2, 2, 3, 3)) |
| 100 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 101 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 102 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 103 | << convolution_hint |
| 104 | << ConvolutionLayer( |
| 105 | 1U, 1U, 64U, |
| 106 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_w.npy"), |
| 107 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_reduce_b.npy"), |
| 108 | PadStrideInfo(1, 1, 0, 0)) |
| 109 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 110 | << ConvolutionLayer( |
| 111 | 3U, 3U, 192U, |
| 112 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_w.npy"), |
| 113 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/conv2/conv2_3x3_b.npy"), |
| 114 | PadStrideInfo(1, 1, 1, 1)) |
| 115 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 116 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 117 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 118 | << get_inception_node(data_path, "inception_3a", 64, std::make_tuple(96U, 128U), std::make_tuple(16U, 32U), 32U) |
| 119 | << get_inception_node(data_path, "inception_3b", 128, std::make_tuple(128U, 192U), std::make_tuple(32U, 96U), 64U) |
| 120 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 121 | << get_inception_node(data_path, "inception_4a", 192, std::make_tuple(96U, 208U), std::make_tuple(16U, 48U), 64U) |
| 122 | << get_inception_node(data_path, "inception_4b", 160, std::make_tuple(112U, 224U), std::make_tuple(24U, 64U), 64U) |
| 123 | << get_inception_node(data_path, "inception_4c", 128, std::make_tuple(128U, 256U), std::make_tuple(24U, 64U), 64U) |
| 124 | << get_inception_node(data_path, "inception_4d", 112, std::make_tuple(144U, 288U), std::make_tuple(32U, 64U), 64U) |
| 125 | << get_inception_node(data_path, "inception_4e", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U) |
| 126 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 127 | << get_inception_node(data_path, "inception_5a", 256, std::make_tuple(160U, 320U), std::make_tuple(32U, 128U), 128U) |
| 128 | << get_inception_node(data_path, "inception_5b", 384, std::make_tuple(192U, 384U), std::make_tuple(48U, 128U), 128U) |
| 129 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 7, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL))) |
| 130 | << FullyConnectedLayer( |
| 131 | 1000U, |
| 132 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_w.npy"), |
| 133 | get_weights_accessor(data_path, "/cnn_data/googlenet_model/loss3/loss3_classifier_b.npy")) |
| 134 | << SoftmaxLayer() |
| 135 | << Tensor(get_output_accessor(label, 5)); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 136 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 137 | void do_run() override |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 138 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 139 | // Run graph |
| 140 | graph.run(); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 143 | private: |
| 144 | Graph graph{}; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 145 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 146 | BranchLayer get_inception_node(const std::string &data_path, std::string &¶m_path, |
| 147 | unsigned int a_filt, |
| 148 | std::tuple<unsigned int, unsigned int> b_filters, |
| 149 | std::tuple<unsigned int, unsigned int> c_filters, |
| 150 | unsigned int d_filt) |
| 151 | { |
| 152 | std::string total_path = "/cnn_data/googlenet_model/" + param_path + "/" + param_path + "_"; |
| 153 | SubGraph i_a; |
| 154 | i_a << ConvolutionLayer( |
| 155 | 1U, 1U, a_filt, |
| 156 | get_weights_accessor(data_path, total_path + "1x1_w.npy"), |
| 157 | get_weights_accessor(data_path, total_path + "1x1_b.npy"), |
| 158 | PadStrideInfo(1, 1, 0, 0)) |
| 159 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 160 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 161 | SubGraph i_b; |
| 162 | i_b << ConvolutionLayer( |
| 163 | 1U, 1U, std::get<0>(b_filters), |
| 164 | get_weights_accessor(data_path, total_path + "3x3_reduce_w.npy"), |
| 165 | get_weights_accessor(data_path, total_path + "3x3_reduce_b.npy"), |
| 166 | PadStrideInfo(1, 1, 0, 0)) |
| 167 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 168 | << ConvolutionLayer( |
| 169 | 3U, 3U, std::get<1>(b_filters), |
| 170 | get_weights_accessor(data_path, total_path + "3x3_w.npy"), |
| 171 | get_weights_accessor(data_path, total_path + "3x3_b.npy"), |
| 172 | PadStrideInfo(1, 1, 1, 1)) |
| 173 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 174 | |
| 175 | SubGraph i_c; |
| 176 | i_c << ConvolutionLayer( |
| 177 | 1U, 1U, std::get<0>(c_filters), |
| 178 | get_weights_accessor(data_path, total_path + "5x5_reduce_w.npy"), |
| 179 | get_weights_accessor(data_path, total_path + "5x5_reduce_b.npy"), |
| 180 | PadStrideInfo(1, 1, 0, 0)) |
| 181 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 182 | << ConvolutionLayer( |
| 183 | 5U, 5U, std::get<1>(c_filters), |
| 184 | get_weights_accessor(data_path, total_path + "5x5_w.npy"), |
| 185 | get_weights_accessor(data_path, total_path + "5x5_b.npy"), |
| 186 | PadStrideInfo(1, 1, 2, 2)) |
| 187 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 188 | |
| 189 | SubGraph i_d; |
| 190 | i_d << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL))) |
| 191 | << ConvolutionLayer( |
| 192 | 1U, 1U, d_filt, |
| 193 | get_weights_accessor(data_path, total_path + "pool_proj_w.npy"), |
| 194 | get_weights_accessor(data_path, total_path + "pool_proj_b.npy"), |
| 195 | PadStrideInfo(1, 1, 0, 0)) |
| 196 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 197 | |
| 198 | return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b), std::move(i_c), std::move(i_d)); |
| 199 | } |
| 200 | }; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 201 | |
| 202 | /** Main program for Googlenet |
| 203 | * |
| 204 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 205 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 206 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 207 | int main(int argc, char **argv) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 208 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 209 | return arm_compute::utils::run_example<GraphGooglenetExample>(argc, argv); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 210 | } |