Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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 | */ |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Graph.h" |
| 25 | #include "arm_compute/graph/Nodes.h" |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | #include <cstdlib> |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 31 | |
| 32 | using namespace arm_compute::graph; |
| 33 | using namespace arm_compute::graph_utils; |
| 34 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 35 | namespace |
| 36 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 37 | /** Generates appropriate accessor according to the specified path |
| 38 | * |
| 39 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 40 | * |
| 41 | * @param path Path to the data files |
| 42 | * @param data_file Relative path to the data files from path |
| 43 | * |
| 44 | * @return An appropriate tensor accessor |
| 45 | */ |
| 46 | std::unique_ptr<ITensorAccessor> get_accessor(const std::string &path, const std::string &data_file) |
| 47 | { |
| 48 | if(path.empty()) |
| 49 | { |
| 50 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file); |
| 55 | } |
| 56 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 57 | } // namespace |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 58 | |
| 59 | /** Example demonstrating how to implement LeNet's network using the Compute Library's graph API |
| 60 | * |
| 61 | * @param[in] argc Number of arguments |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 62 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches ) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 63 | */ |
| 64 | void main_graph_lenet(int argc, const char **argv) |
| 65 | { |
| 66 | std::string data_path; /** Path to the trainable data */ |
| 67 | unsigned int batches = 4; /** Number of batches */ |
| 68 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 69 | // Set target. 0 (NEON), 1 (OpenCL). By default it is NEON |
| 70 | TargetHint target_hint = set_target_hint(argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0); |
| 71 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 72 | // Parse arguments |
| 73 | if(argc < 2) |
| 74 | { |
| 75 | // Print help |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 76 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [batches]\n\n"; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 77 | std::cout << "No data folder provided: using random values\n\n"; |
| 78 | } |
| 79 | else if(argc == 2) |
| 80 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 81 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [batches]\n\n"; |
| 82 | std::cout << "No data folder provided: using random values\n\n"; |
| 83 | } |
| 84 | else if(argc == 3) |
| 85 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 86 | //Do something with argv[1] |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 87 | data_path = argv[2]; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 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] |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 94 | data_path = argv[2]; |
| 95 | batches = std::strtol(argv[3], nullptr, 0); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Graph graph; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 99 | |
| 100 | //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 101 | graph << target_hint |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 102 | << Tensor(TensorInfo(TensorShape(28U, 28U, 1U, batches), 1, DataType::F32), DummyAccessor()) |
| 103 | << ConvolutionLayer( |
| 104 | 5U, 5U, 20U, |
| 105 | get_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"), |
| 106 | get_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"), |
| 107 | PadStrideInfo(1, 1, 0, 0)) |
| 108 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 109 | << ConvolutionLayer( |
| 110 | 5U, 5U, 50U, |
| 111 | get_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"), |
| 112 | get_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"), |
| 113 | PadStrideInfo(1, 1, 0, 0)) |
| 114 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 115 | << FullyConnectedLayer( |
| 116 | 500U, |
| 117 | get_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"), |
| 118 | get_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy")) |
| 119 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 120 | << FullyConnectedLayer( |
| 121 | 10U, |
| 122 | get_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"), |
| 123 | get_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy")) |
| 124 | << SoftmaxLayer() |
| 125 | << Tensor(DummyAccessor()); |
| 126 | |
| 127 | graph.run(); |
| 128 | } |
| 129 | |
| 130 | /** Main program for LeNet |
| 131 | * |
| 132 | * @param[in] argc Number of arguments |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 133 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches ) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 134 | */ |
| 135 | int main(int argc, const char **argv) |
| 136 | { |
| 137 | return arm_compute::utils::run_example(argc, argv, main_graph_lenet); |
| 138 | } |