Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +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" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 25 | #include "support/ToolchainSupport.h" |
| 26 | #include "utils/GraphUtils.h" |
| 27 | #include "utils/Utils.h" |
| 28 | |
| 29 | #include <cstdlib> |
| 30 | #include <iostream> |
| 31 | #include <memory> |
| 32 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 33 | using namespace arm_compute::utils; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 34 | using namespace arm_compute::graph::frontend; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 35 | using namespace arm_compute::graph_utils; |
| 36 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 37 | /** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API |
| 38 | * |
| 39 | * @param[in] argc Number of arguments |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 40 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 41 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 42 | class GraphAlexnetExample : public Example |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 43 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 44 | public: |
| 45 | void do_setup(int argc, char **argv) override |
| 46 | { |
| 47 | std::string data_path; /* Path to the trainable data */ |
| 48 | std::string image; /* Image data */ |
| 49 | std::string label; /* Label data */ |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 50 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 51 | // Create a preprocessor object |
| 52 | const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } }; |
| 53 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 54 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 55 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
Anthony Barbier | 7db3a30 | 2018-03-29 11:38:22 +0100 | [diff] [blame] | 56 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 57 | Target target_hint = set_target_hint(target); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 58 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 59 | // TODO (geopin01) : Get GPU target somehow and set gemm also for midgard ? |
| 60 | const bool is_gemm_convolution5x5 = (target_hint == Target::NEON); |
| 61 | const bool is_winograd_convolution3x3 = target_hint == Target::CL; |
| 62 | ConvolutionMethod convolution_5x5_hint = is_gemm_convolution5x5 ? ConvolutionMethod::GEMM : ConvolutionMethod::DIRECT; |
| 63 | ConvolutionMethod convolution_3x3_hint = is_winograd_convolution3x3 ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM; |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 64 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 65 | // Parse arguments |
| 66 | if(argc < 2) |
| 67 | { |
| 68 | // Print help |
| 69 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
| 70 | std::cout << "No data folder provided: using random values\n\n"; |
| 71 | } |
| 72 | else if(argc == 2) |
| 73 | { |
| 74 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 75 | std::cout << "No data folder provided: using random values\n\n"; |
| 76 | } |
| 77 | else if(argc == 3) |
| 78 | { |
| 79 | data_path = argv[2]; |
| 80 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 81 | std::cout << "No image provided: using random values\n\n"; |
| 82 | } |
| 83 | else if(argc == 4) |
| 84 | { |
| 85 | data_path = argv[2]; |
| 86 | image = argv[3]; |
| 87 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
| 88 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | data_path = argv[2]; |
| 93 | image = argv[3]; |
| 94 | label = argv[4]; |
| 95 | } |
| 96 | |
| 97 | graph << target_hint |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 98 | << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32), |
| 99 | get_input_accessor(image, std::move(preprocessor))) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 100 | // Layer 1 |
| 101 | << ConvolutionLayer( |
| 102 | 11U, 11U, 96U, |
| 103 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"), |
| 104 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"), |
| 105 | PadStrideInfo(4, 4, 0, 0)) |
| 106 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 107 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 108 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 109 | // Layer 2 |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 110 | << convolution_5x5_hint |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 111 | << ConvolutionLayer( |
| 112 | 5U, 5U, 256U, |
| 113 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"), |
| 114 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"), |
| 115 | PadStrideInfo(1, 1, 2, 2), 2) |
| 116 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 117 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 118 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
Gian Marco Iodice | ed99f41 | 2018-03-21 17:45:31 +0000 | [diff] [blame] | 119 | << convolution_3x3_hint |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 120 | // Layer 3 |
| 121 | << ConvolutionLayer( |
| 122 | 3U, 3U, 384U, |
| 123 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"), |
| 124 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"), |
| 125 | PadStrideInfo(1, 1, 1, 1)) |
| 126 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 127 | // Layer 4 |
| 128 | << ConvolutionLayer( |
| 129 | 3U, 3U, 384U, |
| 130 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"), |
| 131 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"), |
| 132 | PadStrideInfo(1, 1, 1, 1), 2) |
| 133 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 134 | // Layer 5 |
| 135 | << ConvolutionLayer( |
| 136 | 3U, 3U, 256U, |
| 137 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"), |
| 138 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"), |
| 139 | PadStrideInfo(1, 1, 1, 1), 2) |
| 140 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 141 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 142 | // Layer 6 |
| 143 | << FullyConnectedLayer( |
| 144 | 4096U, |
| 145 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"), |
| 146 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy")) |
| 147 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 148 | // Layer 7 |
| 149 | << FullyConnectedLayer( |
| 150 | 4096U, |
| 151 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"), |
| 152 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy")) |
| 153 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 154 | // Layer 8 |
| 155 | << FullyConnectedLayer( |
| 156 | 1000U, |
| 157 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"), |
| 158 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy")) |
| 159 | // Softmax |
| 160 | << SoftmaxLayer() |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 161 | << OutputLayer(get_output_accessor(label, 5)); |
Gian Marco | c1b6e37 | 2018-02-21 18:03:26 +0000 | [diff] [blame] | 162 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 163 | // Finalize graph |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 164 | GraphConfig config; |
| 165 | config.use_function_memory_manager = true; |
| 166 | config.use_tuner = (target == 2); |
| 167 | graph.finalize(target_hint, config); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 168 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 169 | void do_run() override |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 170 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 171 | // Run graph |
| 172 | graph.run(); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 175 | private: |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 176 | Stream graph{ 0, "AlexNet" }; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 177 | }; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 178 | |
| 179 | /** Main program for AlexNet |
| 180 | * |
| 181 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 182 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] image, [optional] labels ) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 183 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 184 | int main(int argc, char **argv) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 185 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 186 | return arm_compute::utils::run_example<GraphAlexnetExample>(argc, argv); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 187 | } |