Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +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 | */ |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | #include <cstdlib> |
| 31 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 32 | using namespace arm_compute::utils; |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 33 | using namespace arm_compute::graph; |
| 34 | using namespace arm_compute::graph_utils; |
| 35 | |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 36 | namespace |
| 37 | { |
| 38 | /** This function checks if we can use GEMM-based convolution trying to allocate a memory of size "size_in_bytes" |
| 39 | * |
| 40 | * @param[in] size_in_bytes Memory size in bytes needed for VGG-16 |
| 41 | * |
| 42 | * @return The convolution layer hint |
| 43 | */ |
| 44 | ConvolutionMethodHint convolution_hint_vgg16(size_t size_in_bytes) |
| 45 | { |
| 46 | return ((get_mem_free_from_meminfo() * 1024) >= size_in_bytes) ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT; |
| 47 | } |
| 48 | } // namespace |
| 49 | |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 50 | /** Example demonstrating how to implement VGG16's network using the Compute Library's graph API |
| 51 | * |
| 52 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 53 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 54 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 55 | class GraphVGG16Example : public Example |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 56 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 57 | public: |
| 58 | void do_setup(int argc, char **argv) override |
| 59 | { |
| 60 | std::string data_path; /* Path to the trainable data */ |
| 61 | std::string image; /* Image data */ |
| 62 | std::string label; /* Label data */ |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 63 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 64 | // Create a preprocessor object |
| 65 | const std::array<float, 3> mean_rgb{ { 123.68f, 116.779f, 103.939f } }; |
| 66 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 67 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 68 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
| 69 | const int int_target_hint = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 70 | TargetHint target_hint = set_target_hint(int_target_hint); |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 71 | |
| 72 | // Check if we can use GEMM-based convolutions evaluating if the platform has at least 1.8 GB of available memory |
| 73 | const size_t memory_required = 1932735283L; |
| 74 | ConvolutionMethodHint convolution_hint = convolution_hint_vgg16(memory_required); |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 75 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 76 | // Parse arguments |
| 77 | if(argc < 2) |
| 78 | { |
| 79 | // Print help |
| 80 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 81 | std::cout << "No data folder provided: using random values\n\n"; |
| 82 | } |
| 83 | else if(argc == 2) |
| 84 | { |
| 85 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 86 | std::cout << "No data folder provided: using random values\n\n"; |
| 87 | } |
| 88 | else if(argc == 3) |
| 89 | { |
| 90 | data_path = argv[2]; |
| 91 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 92 | std::cout << "No image provided: using random values\n\n"; |
| 93 | } |
| 94 | else if(argc == 4) |
| 95 | { |
| 96 | data_path = argv[2]; |
| 97 | image = argv[3]; |
| 98 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 99 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | data_path = argv[2]; |
| 104 | image = argv[3]; |
| 105 | label = argv[4]; |
| 106 | } |
| 107 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 108 | // Initialize graph |
| 109 | graph.graph_init(int_target_hint == 2); |
| 110 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 111 | graph << target_hint |
| 112 | << convolution_hint |
| 113 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32), |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 114 | get_input_accessor(image, std::move(preprocessor))) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 115 | // Layer 1 |
| 116 | << ConvolutionLayer( |
| 117 | 3U, 3U, 64U, |
| 118 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"), |
| 119 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"), |
| 120 | PadStrideInfo(1, 1, 1, 1)) |
| 121 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 122 | // Layer 2 |
| 123 | << ConvolutionLayer( |
| 124 | 3U, 3U, 64U, |
| 125 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"), |
| 126 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"), |
| 127 | PadStrideInfo(1, 1, 1, 1)) |
| 128 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 129 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 130 | // Layer 3 |
| 131 | << ConvolutionLayer( |
| 132 | 3U, 3U, 128U, |
| 133 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"), |
| 134 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"), |
| 135 | PadStrideInfo(1, 1, 1, 1)) |
| 136 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 137 | // Layer 4 |
| 138 | << ConvolutionLayer( |
| 139 | 3U, 3U, 128U, |
| 140 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"), |
| 141 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"), |
| 142 | PadStrideInfo(1, 1, 1, 1)) |
| 143 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 144 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 145 | // Layer 5 |
| 146 | << ConvolutionLayer( |
| 147 | 3U, 3U, 256U, |
| 148 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"), |
| 149 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"), |
| 150 | PadStrideInfo(1, 1, 1, 1)) |
| 151 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 152 | // Layer 6 |
| 153 | << ConvolutionLayer( |
| 154 | 3U, 3U, 256U, |
| 155 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"), |
| 156 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"), |
| 157 | PadStrideInfo(1, 1, 1, 1)) |
| 158 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 159 | // Layer 7 |
| 160 | << ConvolutionLayer( |
| 161 | 3U, 3U, 256U, |
| 162 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"), |
| 163 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"), |
| 164 | PadStrideInfo(1, 1, 1, 1)) |
| 165 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 166 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 167 | // Layer 8 |
| 168 | << ConvolutionLayer( |
| 169 | 3U, 3U, 512U, |
| 170 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"), |
| 171 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"), |
| 172 | PadStrideInfo(1, 1, 1, 1)) |
| 173 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 174 | // Layer 9 |
| 175 | << ConvolutionLayer( |
| 176 | 3U, 3U, 512U, |
| 177 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"), |
| 178 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"), |
| 179 | PadStrideInfo(1, 1, 1, 1)) |
| 180 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 181 | // Layer 10 |
| 182 | << ConvolutionLayer( |
| 183 | 3U, 3U, 512U, |
| 184 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"), |
| 185 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"), |
| 186 | PadStrideInfo(1, 1, 1, 1)) |
| 187 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 188 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 189 | // Layer 11 |
| 190 | << ConvolutionLayer( |
| 191 | 3U, 3U, 512U, |
| 192 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"), |
| 193 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"), |
| 194 | PadStrideInfo(1, 1, 1, 1)) |
| 195 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 196 | // Layer 12 |
| 197 | << ConvolutionLayer( |
| 198 | 3U, 3U, 512U, |
| 199 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"), |
| 200 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"), |
| 201 | PadStrideInfo(1, 1, 1, 1)) |
| 202 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 203 | // Layer 13 |
| 204 | << ConvolutionLayer( |
| 205 | 3U, 3U, 512U, |
| 206 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"), |
| 207 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"), |
| 208 | PadStrideInfo(1, 1, 1, 1)) |
| 209 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 210 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 211 | // Layer 14 |
| 212 | << FullyConnectedLayer( |
| 213 | 4096U, |
| 214 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"), |
| 215 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy")) |
| 216 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 217 | // Layer 15 |
| 218 | << FullyConnectedLayer( |
| 219 | 4096U, |
| 220 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"), |
| 221 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy")) |
| 222 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 223 | // Layer 16 |
| 224 | << FullyConnectedLayer( |
| 225 | 1000U, |
| 226 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"), |
| 227 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy")) |
| 228 | // Softmax |
| 229 | << SoftmaxLayer() |
| 230 | << Tensor(get_output_accessor(label, 5)); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 231 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 232 | void do_run() override |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 233 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 234 | // Run graph |
| 235 | graph.run(); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 238 | private: |
| 239 | Graph graph{}; |
| 240 | }; |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 241 | |
| 242 | /** Main program for VGG16 |
| 243 | * |
| 244 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 245 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 246 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 247 | int main(int argc, char **argv) |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 248 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 249 | return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 250 | } |