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