Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +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 | */ |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 24 | #include "arm_compute/graph.h" |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +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 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 33 | using namespace arm_compute::utils; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 34 | using namespace arm_compute::graph::frontend; |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 35 | using namespace arm_compute::graph_utils; |
| 36 | |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +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 |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [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, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 41 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 42 | class GraphAlexnetExample : public Example |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 43 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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 */ |
Anthony Barbier | 8a3da6f | 2017-10-23 18:55:17 +0100 | [diff] [blame] | 50 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +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); |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 54 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 55 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 56 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 57 | Target target_hint = set_target_hint(target); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 58 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 59 | const bool is_neon = (target_hint == Target::NEON); |
| 60 | ConvolutionMethod convolution_5x5_hint = is_neon ? ConvolutionMethod::GEMM : ConvolutionMethod::DIRECT; |
| 61 | ConvolutionMethod convolution_3x3_hint = ConvolutionMethod::DEFAULT; |
| 62 | FastMathHint fast_math_hint = FastMathHint::DISABLED; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 63 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 64 | // Parse arguments |
| 65 | if(argc < 2) |
| 66 | { |
| 67 | // Print help |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 68 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 69 | std::cout << "No data folder provided: using random values\n\n"; |
| 70 | } |
| 71 | else if(argc == 2) |
| 72 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 73 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 74 | std::cout << "No data folder provided: using random values\n\n"; |
| 75 | } |
| 76 | else if(argc == 3) |
| 77 | { |
| 78 | data_path = argv[2]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 79 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 80 | std::cout << "No image provided: using random values\n\n"; |
| 81 | } |
| 82 | else if(argc == 4) |
| 83 | { |
| 84 | data_path = argv[2]; |
| 85 | image = argv[3]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 86 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 87 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 88 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 89 | else if(argc == 5) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 90 | { |
| 91 | data_path = argv[2]; |
| 92 | image = argv[3]; |
| 93 | label = argv[4]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 94 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n"; |
| 95 | std::cout << "No fast math info provided: disabling fast math\n\n"; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | data_path = argv[2]; |
| 100 | image = argv[3]; |
| 101 | label = argv[4]; |
| 102 | fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | graph << target_hint |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 106 | << fast_math_hint |
| 107 | << InputLayer(TensorDescriptor(TensorShape(227U, 227U, 3U, 1U), DataType::F32), |
| 108 | get_input_accessor(image, std::move(preprocessor))) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 109 | // Layer 1 |
| 110 | << ConvolutionLayer( |
| 111 | 11U, 11U, 96U, |
| 112 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"), |
| 113 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"), |
| 114 | PadStrideInfo(4, 4, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 115 | .set_name("conv1") |
| 116 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu1") |
| 117 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)).set_name("norm1") |
| 118 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))).set_name("pool1") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 119 | // Layer 2 |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 120 | << convolution_5x5_hint |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 121 | << ConvolutionLayer( |
| 122 | 5U, 5U, 256U, |
| 123 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"), |
| 124 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"), |
| 125 | PadStrideInfo(1, 1, 2, 2), 2) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 126 | .set_name("conv2") |
| 127 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu2") |
| 128 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)).set_name("norm2") |
| 129 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))).set_name("pool2") |
| 130 | << convolution_3x3_hint |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 131 | // Layer 3 |
| 132 | << ConvolutionLayer( |
| 133 | 3U, 3U, 384U, |
| 134 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"), |
| 135 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"), |
| 136 | PadStrideInfo(1, 1, 1, 1)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 137 | .set_name("conv3") |
| 138 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu3") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 139 | // Layer 4 |
| 140 | << ConvolutionLayer( |
| 141 | 3U, 3U, 384U, |
| 142 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"), |
| 143 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"), |
| 144 | PadStrideInfo(1, 1, 1, 1), 2) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 145 | .set_name("conv4") |
| 146 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu4") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 147 | // Layer 5 |
| 148 | << ConvolutionLayer( |
| 149 | 3U, 3U, 256U, |
| 150 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"), |
| 151 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"), |
| 152 | PadStrideInfo(1, 1, 1, 1), 2) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 153 | .set_name("conv5") |
| 154 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu5") |
| 155 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))).set_name("pool5") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 156 | // Layer 6 |
| 157 | << FullyConnectedLayer( |
| 158 | 4096U, |
| 159 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"), |
| 160 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy")) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 161 | .set_name("fc6") |
| 162 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu6") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 163 | // Layer 7 |
| 164 | << FullyConnectedLayer( |
| 165 | 4096U, |
| 166 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"), |
| 167 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy")) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 168 | .set_name("fc7") |
| 169 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu7") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 170 | // Layer 8 |
| 171 | << FullyConnectedLayer( |
| 172 | 1000U, |
| 173 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"), |
| 174 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy")) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 175 | .set_name("fc8") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 176 | // Softmax |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 177 | << SoftmaxLayer().set_name("prob") |
| 178 | << OutputLayer(get_output_accessor(label, 5)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 179 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 180 | // Finalize graph |
| 181 | GraphConfig config; |
| 182 | config.use_tuner = (target == 2); |
| 183 | graph.finalize(target_hint, config); |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 184 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 185 | void do_run() override |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 186 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 187 | // Run graph |
| 188 | graph.run(); |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 191 | private: |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 192 | Stream graph{ 0, "AlexNet" }; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 193 | }; |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 194 | |
| 195 | /** Main program for AlexNet |
| 196 | * |
| 197 | * @param[in] argc Number of arguments |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 198 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 199 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 200 | int main(int argc, char **argv) |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 201 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 202 | return arm_compute::utils::run_example<GraphAlexnetExample>(argc, argv); |
Kaizen | bf8b01d | 2017-10-12 14:26:51 +0100 | [diff] [blame] | 203 | } |