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