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