Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +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 | |
| 34 | using namespace arm_compute::graph; |
| 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 |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 40 | * @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] | 41 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 42 | void main_graph_alexnet(int argc, char **argv) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 43 | { |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 44 | std::string data_path; /* Path to the trainable data */ |
| 45 | std::string image; /* Image data */ |
| 46 | std::string label; /* Label data */ |
| 47 | |
| 48 | constexpr float mean_r = 122.68f; /* Mean value to subtract from red channel */ |
| 49 | constexpr float mean_g = 116.67f; /* Mean value to subtract from green channel */ |
| 50 | constexpr float mean_b = 104.01f; /* Mean value to subtract from blue channel */ |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 51 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 52 | // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON |
| 53 | TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0); |
| 54 | ConvolutionMethodHint convolution_hint = target_hint == TargetHint::NEON ? ConvolutionMethodHint::GEMM : ConvolutionMethodHint::DIRECT; |
| 55 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 56 | // Parse arguments |
| 57 | if(argc < 2) |
| 58 | { |
| 59 | // Print help |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 60 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels]\n\n"; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 61 | std::cout << "No data folder provided: using random values\n\n"; |
| 62 | } |
| 63 | else if(argc == 2) |
| 64 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 65 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels]\n\n"; |
| 66 | std::cout << "No data folder provided: using random values\n\n"; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 67 | } |
| 68 | else if(argc == 3) |
| 69 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 70 | data_path = argv[2]; |
| 71 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels]\n\n"; |
| 72 | std::cout << "No image provided: using random values\n\n"; |
| 73 | } |
| 74 | else if(argc == 4) |
| 75 | { |
| 76 | data_path = argv[2]; |
| 77 | image = argv[3]; |
| 78 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels]\n\n"; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 79 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 80 | } |
| 81 | else |
| 82 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 83 | data_path = argv[2]; |
| 84 | image = argv[3]; |
| 85 | label = argv[4]; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Graph graph; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 89 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 90 | graph << target_hint |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 91 | << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, 1U), 1, DataType::F32), |
| 92 | get_input_accessor(image, mean_r, mean_g, mean_b)) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 93 | // Layer 1 |
| 94 | << ConvolutionLayer( |
| 95 | 11U, 11U, 96U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 96 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"), |
| 97 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 98 | PadStrideInfo(4, 4, 0, 0)) |
| 99 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 100 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 101 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 102 | // Layer 2 |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 103 | << convolution_hint |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 104 | << ConvolutionLayer( |
| 105 | 5U, 5U, 256U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 106 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"), |
| 107 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 108 | PadStrideInfo(1, 1, 2, 2), 2) |
| 109 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 110 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 111 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 112 | // Layer 3 |
| 113 | << ConvolutionLayer( |
| 114 | 3U, 3U, 384U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 115 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"), |
| 116 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 117 | PadStrideInfo(1, 1, 1, 1)) |
| 118 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 119 | // Layer 4 |
| 120 | << ConvolutionLayer( |
| 121 | 3U, 3U, 384U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 122 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"), |
| 123 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 124 | PadStrideInfo(1, 1, 1, 1), 2) |
| 125 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 126 | // Layer 5 |
| 127 | << ConvolutionLayer( |
| 128 | 3U, 3U, 256U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 129 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"), |
| 130 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 131 | PadStrideInfo(1, 1, 1, 1), 2) |
| 132 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 133 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 134 | // Layer 6 |
| 135 | << FullyConnectedLayer( |
| 136 | 4096U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 137 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"), |
| 138 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy")) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 139 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 140 | // Layer 7 |
| 141 | << FullyConnectedLayer( |
| 142 | 4096U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 143 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"), |
| 144 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy")) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 145 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 146 | // Layer 8 |
| 147 | << FullyConnectedLayer( |
| 148 | 1000U, |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 149 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"), |
| 150 | get_weights_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy")) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 151 | // Softmax |
| 152 | << SoftmaxLayer() |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 153 | << Tensor(get_output_accessor(label, 5)); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 154 | |
| 155 | // Run graph |
| 156 | graph.run(); |
| 157 | } |
| 158 | |
| 159 | /** Main program for AlexNet |
| 160 | * |
| 161 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 162 | * @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] | 163 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 164 | int main(int argc, char **argv) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 165 | { |
| 166 | return arm_compute::utils::run_example(argc, argv, main_graph_alexnet); |
| 167 | } |