Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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" |
| 25 | |
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 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 32 | using namespace arm_compute::utils; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 33 | using namespace arm_compute::graph::frontend; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 34 | using namespace arm_compute::graph_utils; |
| 35 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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 |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 39 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL), [optional] Path to the weights folder, [optional] batches, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 40 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 41 | class GraphLenetExample : public Example |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 42 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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 */ |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 48 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 49 | // 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] | 50 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 51 | Target target_hint = set_target_hint(target); |
| 52 | |
| 53 | FastMathHint fast_math_hint = FastMathHint::DISABLED; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 54 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 55 | // Parse arguments |
| 56 | if(argc < 2) |
| 57 | { |
| 58 | // Print help |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 59 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [batches] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 60 | std::cout << "No data folder provided: using random values\n\n"; |
| 61 | } |
| 62 | else if(argc == 2) |
| 63 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 64 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [batches] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 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]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 71 | std::cout << "Usage: " << argv[0] << " [path_to_data] [batches] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 72 | std::cout << "No number of batches where specified, thus will use the default : " << batches << "\n\n"; |
| 73 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 74 | else if(argc == 4) |
| 75 | { |
| 76 | data_path = argv[2]; |
| 77 | batches = std::strtol(argv[3], nullptr, 0); |
| 78 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [fast_math_hint]\n\n"; |
| 79 | std::cout << "No fast math info provided: disabling fast math\n\n"; |
| 80 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 81 | else |
| 82 | { |
| 83 | //Do something with argv[1] and argv[2] |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 84 | data_path = argv[2]; |
| 85 | batches = std::strtol(argv[3], nullptr, 0); |
| 86 | fast_math_hint = (std::strtol(argv[4], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | //conv1 << pool1 << conv2 << pool2 << fc1 << act1 << fc2 << smx |
| 90 | graph << target_hint |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 91 | << fast_math_hint |
| 92 | << InputLayer(TensorDescriptor(TensorShape(28U, 28U, 1U, batches), DataType::F32), get_input_accessor("")) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 93 | << ConvolutionLayer( |
| 94 | 5U, 5U, 20U, |
| 95 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_w.npy"), |
| 96 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv1_b.npy"), |
| 97 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 98 | .set_name("conv1") |
| 99 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool1") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 100 | << ConvolutionLayer( |
| 101 | 5U, 5U, 50U, |
| 102 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_w.npy"), |
| 103 | get_weights_accessor(data_path, "/cnn_data/lenet_model/conv2_b.npy"), |
| 104 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 105 | .set_name("conv2") |
| 106 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0))).set_name("pool2") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 107 | << FullyConnectedLayer( |
| 108 | 500U, |
| 109 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_w.npy"), |
| 110 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip1_b.npy")) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 111 | .set_name("ip1") |
| 112 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("relu") |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 113 | << FullyConnectedLayer( |
| 114 | 10U, |
| 115 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_w.npy"), |
| 116 | get_weights_accessor(data_path, "/cnn_data/lenet_model/ip2_b.npy")) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 117 | .set_name("ip2") |
| 118 | << SoftmaxLayer().set_name("prob") |
| 119 | << OutputLayer(get_output_accessor("")); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 120 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 121 | // Finalize graph |
| 122 | GraphConfig config; |
| 123 | config.use_tuner = (target == 2); |
| 124 | graph.finalize(target_hint, config); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 125 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 126 | void do_run() override |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 127 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 128 | // Run graph |
| 129 | graph.run(); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 132 | private: |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 133 | Stream graph{ 0, "LeNet" }; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 134 | }; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 135 | |
| 136 | /** Main program for LeNet |
| 137 | * |
| 138 | * @param[in] argc Number of arguments |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 139 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] batches, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 140 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 141 | int main(int argc, char **argv) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 142 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 143 | return arm_compute::utils::run_example<GraphLenetExample>(argc, argv); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 144 | } |