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