Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [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 | */ |
| 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
| 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | #include <cstdlib> |
| 31 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 32 | using namespace arm_compute::utils; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 33 | using namespace arm_compute::graph; |
| 34 | using namespace arm_compute::graph_utils; |
| 35 | |
| 36 | /** Example demonstrating how to implement VGG16's network using the Compute Library's graph API |
| 37 | * |
| 38 | * @param[in] argc Number of arguments |
| 39 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 40 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 41 | class GraphVGG16Example : public Example |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 42 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 43 | public: |
| 44 | void do_setup(int argc, char **argv) override |
| 45 | { |
| 46 | std::string data_path; /* Path to the trainable data */ |
| 47 | std::string image; /* Image data */ |
| 48 | std::string label; /* Label data */ |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 49 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 50 | constexpr float mean_r = 123.68f; /* Mean value to subtract from red channel */ |
| 51 | constexpr float mean_g = 116.779f; /* Mean value to subtract from green channel */ |
| 52 | constexpr float mean_b = 103.939f; /* Mean value to subtract from blue channel */ |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 53 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 54 | // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON |
| 55 | TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0); |
| 56 | ConvolutionMethodHint convolution_hint = ConvolutionMethodHint::DIRECT; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 57 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 58 | // Parse arguments |
| 59 | if(argc < 2) |
| 60 | { |
| 61 | // Print help |
| 62 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 63 | std::cout << "No data folder provided: using random values\n\n"; |
| 64 | } |
| 65 | else if(argc == 2) |
| 66 | { |
| 67 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 68 | std::cout << "No data folder provided: using random values\n\n"; |
| 69 | } |
| 70 | else if(argc == 3) |
| 71 | { |
| 72 | data_path = argv[2]; |
| 73 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 74 | std::cout << "No image provided: using random values\n\n"; |
| 75 | } |
| 76 | else if(argc == 4) |
| 77 | { |
| 78 | data_path = argv[2]; |
| 79 | image = argv[3]; |
| 80 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 81 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | data_path = argv[2]; |
| 86 | image = argv[3]; |
| 87 | label = argv[4]; |
| 88 | } |
| 89 | |
| 90 | graph << target_hint |
| 91 | << convolution_hint |
| 92 | << Tensor(TensorInfo(TensorShape(224U, 224U, 3U, 1U), 1, DataType::F32), |
| 93 | get_input_accessor(image, mean_r, mean_g, mean_b)) |
| 94 | << ConvolutionMethodHint::DIRECT |
| 95 | // Layer 1 |
| 96 | << ConvolutionLayer( |
| 97 | 3U, 3U, 64U, |
| 98 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy"), |
| 99 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"), |
| 100 | PadStrideInfo(1, 1, 1, 1)) |
| 101 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 102 | // Layer 2 |
| 103 | << ConvolutionLayer( |
| 104 | 3U, 3U, 64U, |
| 105 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy"), |
| 106 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"), |
| 107 | PadStrideInfo(1, 1, 1, 1)) |
| 108 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 109 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 110 | // Layer 3 |
| 111 | << ConvolutionLayer( |
| 112 | 3U, 3U, 128U, |
| 113 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy"), |
| 114 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"), |
| 115 | PadStrideInfo(1, 1, 1, 1)) |
| 116 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 117 | // Layer 4 |
| 118 | << ConvolutionLayer( |
| 119 | 3U, 3U, 128U, |
| 120 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy"), |
| 121 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"), |
| 122 | PadStrideInfo(1, 1, 1, 1)) |
| 123 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 124 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 125 | // Layer 5 |
| 126 | << ConvolutionLayer( |
| 127 | 3U, 3U, 256U, |
| 128 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy"), |
| 129 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"), |
| 130 | PadStrideInfo(1, 1, 1, 1)) |
| 131 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 132 | // Layer 6 |
| 133 | << ConvolutionLayer( |
| 134 | 3U, 3U, 256U, |
| 135 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy"), |
| 136 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"), |
| 137 | PadStrideInfo(1, 1, 1, 1)) |
| 138 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 139 | // Layer 7 |
| 140 | << ConvolutionLayer( |
| 141 | 3U, 3U, 256U, |
| 142 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy"), |
| 143 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"), |
| 144 | PadStrideInfo(1, 1, 1, 1)) |
| 145 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 146 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 147 | // Layer 8 |
| 148 | << ConvolutionLayer( |
| 149 | 3U, 3U, 512U, |
| 150 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy"), |
| 151 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"), |
| 152 | PadStrideInfo(1, 1, 1, 1)) |
| 153 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 154 | // Layer 9 |
| 155 | << ConvolutionLayer( |
| 156 | 3U, 3U, 512U, |
| 157 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy"), |
| 158 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"), |
| 159 | PadStrideInfo(1, 1, 1, 1)) |
| 160 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 161 | // Layer 10 |
| 162 | << ConvolutionLayer( |
| 163 | 3U, 3U, 512U, |
| 164 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy"), |
| 165 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"), |
| 166 | PadStrideInfo(1, 1, 1, 1)) |
| 167 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 168 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 169 | // Layer 11 |
| 170 | << ConvolutionLayer( |
| 171 | 3U, 3U, 512U, |
| 172 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy"), |
| 173 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"), |
| 174 | PadStrideInfo(1, 1, 1, 1)) |
| 175 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 176 | // Layer 12 |
| 177 | << ConvolutionLayer( |
| 178 | 3U, 3U, 512U, |
| 179 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy"), |
| 180 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"), |
| 181 | PadStrideInfo(1, 1, 1, 1)) |
| 182 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 183 | // Layer 13 |
| 184 | << ConvolutionLayer( |
| 185 | 3U, 3U, 512U, |
| 186 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy"), |
| 187 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"), |
| 188 | PadStrideInfo(1, 1, 1, 1)) |
| 189 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 190 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 191 | // Layer 14 |
| 192 | << FullyConnectedLayer( |
| 193 | 4096U, |
| 194 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy"), |
| 195 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy")) |
| 196 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 197 | // Layer 15 |
| 198 | << FullyConnectedLayer( |
| 199 | 4096U, |
| 200 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy"), |
| 201 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy")) |
| 202 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 203 | // Layer 16 |
| 204 | << FullyConnectedLayer( |
| 205 | 1000U, |
| 206 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy"), |
| 207 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy")) |
| 208 | // Softmax |
| 209 | << SoftmaxLayer() |
| 210 | << Tensor(get_output_accessor(label, 5)); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 211 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 212 | void do_run() override |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 213 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 214 | // Run graph |
| 215 | graph.run(); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 218 | private: |
| 219 | Graph graph{}; |
| 220 | }; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 221 | |
| 222 | /** Main program for VGG16 |
| 223 | * |
| 224 | * @param[in] argc Number of arguments |
| 225 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
| 226 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 227 | int main(int argc, char **argv) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 228 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame^] | 229 | return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 230 | } |