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