Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 27 | #include "utils/CommonGraphOptions.h" |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 28 | #include "utils/GraphUtils.h" |
| 29 | #include "utils/Utils.h" |
| 30 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 31 | using namespace arm_compute::utils; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | using namespace arm_compute::graph::frontend; |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 33 | using namespace arm_compute::graph_utils; |
| 34 | |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 35 | /** Example demonstrating how to implement VGG16's network using the Compute Library's graph API */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 36 | class GraphVGG16Example : public Example |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 37 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 38 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | GraphVGG16Example() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "VGG16") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 40 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 41 | } |
| 42 | bool do_setup(int argc, char **argv) override |
| 43 | { |
| 44 | // Parse arguments |
| 45 | cmd_parser.parse(argc, argv); |
Georgios Pinitas | cd60a5f | 2019-08-21 17:06:54 +0100 | [diff] [blame] | 46 | cmd_parser.validate(); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 47 | |
| 48 | // Consume common parameters |
| 49 | common_params = consume_common_graph_parameters(common_opts); |
| 50 | |
| 51 | // Return when help menu is requested |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | if (common_params.help) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 53 | { |
| 54 | cmd_parser.print_help(argv[0]); |
| 55 | return false; |
| 56 | } |
| 57 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 58 | // Print parameter values |
| 59 | std::cout << common_params << std::endl; |
| 60 | |
| 61 | // Get trainable parameters data path |
| 62 | std::string data_path = common_params.data_path; |
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 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | const std::array<float, 3> mean_rgb{{123.68f, 116.779f, 103.939f}}; |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 66 | std::unique_ptr<IPreprocessor> preprocessor = std::make_unique<CaffePreproccessor>(mean_rgb); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 67 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 68 | // Create input descriptor |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 69 | const auto operation_layout = common_params.data_layout; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | const TensorShape tensor_shape = |
| 71 | permute_shape(TensorShape(224U, 224U, 3U, common_params.batches), DataLayout::NCHW, operation_layout); |
| 72 | TensorDescriptor input_descriptor = |
| 73 | TensorDescriptor(tensor_shape, common_params.data_type).set_layout(operation_layout); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 74 | |
| 75 | // Set weights trained layout |
| 76 | const DataLayout weights_layout = DataLayout::NCHW; |
| 77 | |
| 78 | // Create graph |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | graph |
| 80 | << common_params.target << common_params.fast_math_hint |
| 81 | << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor))) |
| 82 | // Layer 1 |
| 83 | << ConvolutionLayer( |
| 84 | 3U, 3U, 64U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_w.npy", weights_layout), |
| 85 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_1_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 86 | .set_name("conv1_1") |
| 87 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 88 | .set_name("conv1_1/Relu") |
| 89 | // Layer 2 |
| 90 | << ConvolutionLayer( |
| 91 | 3U, 3U, 64U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_w.npy", weights_layout), |
| 92 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv1_2_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 93 | .set_name("conv1_2") |
| 94 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 95 | .set_name("conv1_2/Relu") |
| 96 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))) |
| 97 | .set_name("pool1") |
| 98 | // Layer 3 |
| 99 | << ConvolutionLayer( |
| 100 | 3U, 3U, 128U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_w.npy", weights_layout), |
| 101 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_1_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 102 | .set_name("conv2_1") |
| 103 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 104 | .set_name("conv2_1/Relu") |
| 105 | // Layer 4 |
| 106 | << ConvolutionLayer( |
| 107 | 3U, 3U, 128U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_w.npy", weights_layout), |
| 108 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv2_2_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 109 | .set_name("conv2_2") |
| 110 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 111 | .set_name("conv2_2/Relu") |
| 112 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))) |
| 113 | .set_name("pool2") |
| 114 | // Layer 5 |
| 115 | << ConvolutionLayer( |
| 116 | 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_w.npy", weights_layout), |
| 117 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_1_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 118 | .set_name("conv3_1") |
| 119 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 120 | .set_name("conv3_1/Relu") |
| 121 | // Layer 6 |
| 122 | << ConvolutionLayer( |
| 123 | 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_w.npy", weights_layout), |
| 124 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_2_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 125 | .set_name("conv3_2") |
| 126 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 127 | .set_name("conv3_2/Relu") |
| 128 | // Layer 7 |
| 129 | << ConvolutionLayer( |
| 130 | 3U, 3U, 256U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_w.npy", weights_layout), |
| 131 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv3_3_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 132 | .set_name("conv3_3") |
| 133 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 134 | .set_name("conv3_3/Relu") |
| 135 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))) |
| 136 | .set_name("pool3") |
| 137 | // Layer 8 |
| 138 | << ConvolutionLayer( |
| 139 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_w.npy", weights_layout), |
| 140 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_1_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 141 | .set_name("conv4_1") |
| 142 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 143 | .set_name("conv4_1/Relu") |
| 144 | // Layer 9 |
| 145 | << ConvolutionLayer( |
| 146 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_w.npy", weights_layout), |
| 147 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_2_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 148 | .set_name("conv4_2") |
| 149 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 150 | .set_name("conv4_2/Relu") |
| 151 | // Layer 10 |
| 152 | << ConvolutionLayer( |
| 153 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_w.npy", weights_layout), |
| 154 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv4_3_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 155 | .set_name("conv4_3") |
| 156 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 157 | .set_name("conv4_3/Relu") |
| 158 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))) |
| 159 | .set_name("pool4") |
| 160 | // Layer 11 |
| 161 | << ConvolutionLayer( |
| 162 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_w.npy", weights_layout), |
| 163 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_1_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 164 | .set_name("conv5_1") |
| 165 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 166 | .set_name("conv5_1/Relu") |
| 167 | // Layer 12 |
| 168 | << ConvolutionLayer( |
| 169 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_w.npy", weights_layout), |
| 170 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_2_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 171 | .set_name("conv5_2") |
| 172 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 173 | .set_name("conv5_2/Relu") |
| 174 | // Layer 13 |
| 175 | << ConvolutionLayer( |
| 176 | 3U, 3U, 512U, get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_w.npy", weights_layout), |
| 177 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/conv5_3_b.npy"), PadStrideInfo(1, 1, 1, 1)) |
| 178 | .set_name("conv5_3") |
| 179 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 180 | .set_name("conv5_3/Relu") |
| 181 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, operation_layout, PadStrideInfo(2, 2, 0, 0))) |
| 182 | .set_name("pool5") |
| 183 | // Layer 14 |
| 184 | << FullyConnectedLayer(4096U, |
| 185 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_w.npy", weights_layout), |
| 186 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc6_b.npy")) |
| 187 | .set_name("fc6") |
| 188 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu") |
| 189 | // Layer 15 |
| 190 | << FullyConnectedLayer(4096U, |
| 191 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_w.npy", weights_layout), |
| 192 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc7_b.npy")) |
| 193 | .set_name("fc7") |
| 194 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Relu_1") |
| 195 | // Layer 16 |
| 196 | << FullyConnectedLayer(1000U, |
| 197 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_w.npy", weights_layout), |
| 198 | get_weights_accessor(data_path, "/cnn_data/vgg16_model/fc8_b.npy")) |
| 199 | .set_name("fc8") |
| 200 | // Softmax |
| 201 | << SoftmaxLayer().set_name("prob") << OutputLayer(get_output_accessor(common_params, 5)); |
Gian Marco | c1b6e37 | 2018-02-21 18:03:26 +0000 | [diff] [blame] | 202 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 203 | // Finalize graph |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 204 | GraphConfig config; |
SiCongLi | f466d75 | 2021-03-01 15:26:18 +0000 | [diff] [blame] | 205 | config.num_threads = common_params.threads; |
| 206 | config.use_tuner = common_params.enable_tuner; |
| 207 | config.tuner_mode = common_params.tuner_mode; |
| 208 | config.tuner_file = common_params.tuner_file; |
| 209 | config.mlgo_file = common_params.mlgo_file; |
| 210 | config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type); |
| 211 | config.synthetic_type = common_params.data_type; |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 212 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 213 | graph.finalize(common_params.target, config); |
| 214 | |
| 215 | return true; |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 216 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 217 | void do_run() override |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 218 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 219 | // Run graph |
| 220 | graph.run(); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 223 | private: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 224 | CommandLineParser cmd_parser; |
| 225 | CommonGraphOptions common_opts; |
| 226 | CommonGraphParams common_params; |
| 227 | Stream graph; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 228 | }; |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 229 | |
| 230 | /** Main program for VGG16 |
| 231 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 232 | * Model is based on: |
| 233 | * https://arxiv.org/abs/1409.1556 |
| 234 | * "Very Deep Convolutional Networks for Large-Scale Image Recognition" |
| 235 | * Karen Simonyan, Andrew Zisserman |
| 236 | * |
Georgios Pinitas | 588ebc5 | 2018-12-21 13:39:07 +0000 | [diff] [blame] | 237 | * Provenance: www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel |
| 238 | * |
Georgios Pinitas | 9f28b39 | 2018-07-18 20:01:53 +0100 | [diff] [blame] | 239 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 240 | * |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 241 | * @param[in] argc Number of arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 242 | * @param[in] argv Arguments |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 243 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 244 | int main(int argc, char **argv) |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 245 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 246 | return arm_compute::utils::run_example<GraphVGG16Example>(argc, argv); |
Gian Marco Iodice | e10bddb | 2017-10-11 15:03:26 +0100 | [diff] [blame] | 247 | } |