Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | */ |
| 24 | #ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */ |
| 25 | #error "This example needs to be built with -DARM_COMPUTE_CL" |
| 26 | #endif /* ARM_COMPUTE_CL */ |
| 27 | |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame^] | 28 | #include "arm_compute/core/utils/logging/LoggerRegistry.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/Graph.h" |
| 30 | #include "arm_compute/graph/Nodes.h" |
| 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
| 33 | #include "arm_compute/runtime/Scheduler.h" |
| 34 | #include "support/ToolchainSupport.h" |
| 35 | #include "utils/GraphUtils.h" |
| 36 | #include "utils/Utils.h" |
| 37 | |
| 38 | #include <cstdlib> |
| 39 | #include <iostream> |
| 40 | #include <memory> |
| 41 | |
| 42 | using namespace arm_compute::graph; |
| 43 | using namespace arm_compute::graph_utils; |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame^] | 44 | using namespace arm_compute::logging; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 45 | |
| 46 | /** Generates appropriate accessor according to the specified path |
| 47 | * |
| 48 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 49 | * |
| 50 | * @param[in] path Path to the data files |
| 51 | * @param[in] data_file Relative path to the data files from path |
| 52 | * |
| 53 | * @return An appropriate tensor accessor |
| 54 | */ |
| 55 | std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file) |
| 56 | { |
| 57 | if(path.empty()) |
| 58 | { |
| 59 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** Example demonstrating how to implement AlexNet's network using the Compute Library's graph API |
| 68 | * |
| 69 | * @param[in] argc Number of arguments |
| 70 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches ) |
| 71 | */ |
| 72 | void main_graph_alexnet(int argc, const char **argv) |
| 73 | { |
| 74 | std::string data_path; /** Path to the trainable data */ |
| 75 | unsigned int batches = 4; /** Number of batches */ |
| 76 | |
| 77 | // Parse arguments |
| 78 | if(argc < 2) |
| 79 | { |
| 80 | // Print help |
| 81 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n"; |
| 82 | std::cout << "No data folder provided: using random values\n\n"; |
| 83 | } |
| 84 | else if(argc == 2) |
| 85 | { |
| 86 | //Do something with argv[1] |
| 87 | data_path = argv[1]; |
| 88 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n"; |
| 89 | std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n"; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | //Do something with argv[1] and argv[2] |
| 94 | data_path = argv[1]; |
| 95 | batches = std::strtol(argv[2], nullptr, 0); |
| 96 | } |
| 97 | |
| 98 | // Check if OpenCL is available and initialize the scheduler |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 99 | TargetHint hint = TargetHint::NEON; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 100 | if(arm_compute::opencl_is_available()) |
| 101 | { |
| 102 | arm_compute::CLScheduler::get().default_init(); |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 103 | hint = TargetHint::OPENCL; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | Graph graph; |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame^] | 107 | LoggerRegistry::get().create_reserved_loggers(LogLevel::INFO, { std::make_shared<StdPrinter>() }); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 108 | |
| 109 | graph << hint |
| 110 | << Tensor(TensorInfo(TensorShape(227U, 227U, 3U, batches), 1, DataType::F32), DummyAccessor()) |
| 111 | // Layer 1 |
| 112 | << ConvolutionLayer( |
| 113 | 11U, 11U, 96U, |
| 114 | get_accessor(data_path, "/cnn_data/alexnet_model/conv1_w.npy"), |
| 115 | get_accessor(data_path, "/cnn_data/alexnet_model/conv1_b.npy"), |
| 116 | PadStrideInfo(4, 4, 0, 0)) |
| 117 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 118 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 119 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 120 | // Layer 2 |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 121 | << ConvolutionMethodHint::DIRECT |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 122 | << ConvolutionLayer( |
| 123 | 5U, 5U, 256U, |
| 124 | get_accessor(data_path, "/cnn_data/alexnet_model/conv2_w.npy"), |
| 125 | get_accessor(data_path, "/cnn_data/alexnet_model/conv2_b.npy"), |
| 126 | PadStrideInfo(1, 1, 2, 2), 2) |
| 127 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 128 | << NormalizationLayer(NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f)) |
| 129 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 130 | // Layer 3 |
| 131 | << ConvolutionLayer( |
| 132 | 3U, 3U, 384U, |
| 133 | get_accessor(data_path, "/cnn_data/alexnet_model/conv3_w.npy"), |
| 134 | get_accessor(data_path, "/cnn_data/alexnet_model/conv3_b.npy"), |
| 135 | PadStrideInfo(1, 1, 1, 1)) |
| 136 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 137 | // Layer 4 |
| 138 | << ConvolutionLayer( |
| 139 | 3U, 3U, 384U, |
| 140 | get_accessor(data_path, "/cnn_data/alexnet_model/conv4_w.npy"), |
| 141 | get_accessor(data_path, "/cnn_data/alexnet_model/conv4_b.npy"), |
| 142 | PadStrideInfo(1, 1, 1, 1), 2) |
| 143 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 144 | // Layer 5 |
| 145 | << ConvolutionLayer( |
| 146 | 3U, 3U, 256U, |
| 147 | get_accessor(data_path, "/cnn_data/alexnet_model/conv5_w.npy"), |
| 148 | get_accessor(data_path, "/cnn_data/alexnet_model/conv5_b.npy"), |
| 149 | PadStrideInfo(1, 1, 1, 1), 2) |
| 150 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 151 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0))) |
| 152 | // Layer 6 |
| 153 | << FullyConnectedLayer( |
| 154 | 4096U, |
| 155 | get_accessor(data_path, "/cnn_data/alexnet_model/fc6_w.npy"), |
| 156 | get_accessor(data_path, "/cnn_data/alexnet_model/fc6_b.npy")) |
| 157 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 158 | // Layer 7 |
| 159 | << FullyConnectedLayer( |
| 160 | 4096U, |
| 161 | get_accessor(data_path, "/cnn_data/alexnet_model/fc7_w.npy"), |
| 162 | get_accessor(data_path, "/cnn_data/alexnet_model/fc7_b.npy")) |
| 163 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 164 | // Layer 8 |
| 165 | << FullyConnectedLayer( |
| 166 | 1000U, |
| 167 | get_accessor(data_path, "/cnn_data/alexnet_model/fc8_w.npy"), |
| 168 | get_accessor(data_path, "/cnn_data/alexnet_model/fc8_b.npy")) |
| 169 | // Softmax |
| 170 | << SoftmaxLayer() |
| 171 | << Tensor(DummyAccessor()); |
| 172 | |
| 173 | // Run graph |
| 174 | graph.run(); |
| 175 | } |
| 176 | |
| 177 | /** Main program for AlexNet |
| 178 | * |
| 179 | * @param[in] argc Number of arguments |
| 180 | * @param[in] argv Arguments ( [optional] Path to the weights folder, [optional] batches ) |
| 181 | */ |
| 182 | int main(int argc, const char **argv) |
| 183 | { |
| 184 | return arm_compute::utils::run_example(argc, argv, main_graph_alexnet); |
| 185 | } |