Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 46edf63 | 2018-01-26 14:27:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +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 | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 24 | #include "arm_compute/graph2.h" |
| 25 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
| 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | #include <cstdlib> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 31 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 32 | using namespace arm_compute::utils; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 33 | using namespace arm_compute::graph2::frontend; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 34 | using namespace arm_compute::graph_utils; |
| 35 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 36 | /** Example demonstrating how to implement LeNet's network using the Compute Library's graph API |
| 37 | * |
| 38 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 39 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches ) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 40 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 41 | class GraphLenetExample : public Example |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 42 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 43 | public: |
| 44 | void do_setup(int argc, char **argv) override |
| 45 | { |
| 46 | std::string data_path; /** Path to the trainable data */ |
| 47 | unsigned int batches = 4; /** Number of batches */ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 48 | |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 49 | // Set target. 0 (NEON), 1 (OpenCL), 2 (OpenCL with Tuner). By default it is NEON |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 50 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 51 | Target target_hint = set_target_hint2(target); |
| 52 | bool enable_tuning = (target == 2); |
| 53 | bool enable_memory_management = true; |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 54 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 55 | // Parse arguments |
| 56 | if(argc < 2) |
| 57 | { |
| 58 | // Print help |
| 59 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [batches]\n\n"; |
| 60 | std::cout << "No data folder provided: using random values\n\n"; |
| 61 | } |
| 62 | else if(argc == 2) |
| 63 | { |
| 64 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [batches]\n\n"; |
| 65 | std::cout << "No data folder provided: using random values\n\n"; |
| 66 | } |
| 67 | else if(argc == 3) |
| 68 | { |
| 69 | //Do something with argv[1] |
| 70 | data_path = argv[2]; |
| 71 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches]\n\n"; |
| 72 | std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n"; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | //Do something with argv[1] and argv[2] |
| 77 | data_path = argv[2]; |
| 78 | batches = std::strtol(argv[3], nullptr, 0); |
| 79 | } |
| 80 | |
| 81 | //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx |
| 82 | graph << target_hint |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 83 | << InputLayer(TensorDescriptor(TensorShape(28U, 28U, 1U, batches), DataType::F32), get_input_accessor("")) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 84 | << ConvolutionLayer( |
| 85 | 5U, 5U, 20U, |
| 86 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"), |
| 87 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"), |
| 88 | PadStrideInfo(1, 1, 0, 0)) |
| 89 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 90 | << ConvolutionLayer( |
| 91 | 5U, 5U, 50U, |
| 92 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"), |
| 93 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"), |
| 94 | PadStrideInfo(1, 1, 0, 0)) |
| 95 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))) |
| 96 | << FullyConnectedLayer( |
| 97 | 500U, |
| 98 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"), |
| 99 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy")) |
| 100 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 101 | << FullyConnectedLayer( |
| 102 | 10U, |
| 103 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"), |
| 104 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy")) |
| 105 | << SoftmaxLayer() |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 106 | << OutputLayer(get_output_accessor("")); |
Gian Marco | c1b6e37 | 2018-02-21 18:03:26 +0000 | [diff] [blame] | 107 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 108 | // Finalize graph |
| 109 | graph.finalize(target_hint, enable_tuning, enable_memory_management); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 110 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 111 | void do_run() override |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 112 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 113 | // Run graph |
| 114 | graph.run(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 117 | private: |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 118 | Stream graph{ 0, "LeNet" }; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 119 | }; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 120 | |
| 121 | /** Main program for LeNet |
| 122 | * |
| 123 | * @param[in] argc Number of arguments |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 124 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches ) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 125 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 126 | int main(int argc, char **argv) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 127 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 128 | return arm_compute::utils::run_example<GraphLenetExample>(argc, argv); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 129 | } |