Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [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" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 25 | #include "support/ToolchainSupport.h" |
| 26 | #include "utils/GraphUtils.h" |
| 27 | #include "utils/Utils.h" |
| 28 | |
| 29 | #include <cstdlib> |
| 30 | #include <tuple> |
| 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; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 34 | using namespace arm_compute::graph_utils; |
| 35 | using namespace arm_compute::logging; |
| 36 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 37 | /** Example demonstrating how to implement Squeezenet's network using the Compute Library's graph API |
| 38 | * |
| 39 | * @param[in] argc Number of arguments |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 40 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 41 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 42 | class GraphSqueezenetExample : public Example |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 43 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 44 | public: |
| 45 | void do_setup(int argc, char **argv) override |
| 46 | { |
| 47 | std::string data_path; /* Path to the trainable data */ |
| 48 | std::string image; /* Image data */ |
| 49 | std::string label; /* Label data */ |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 50 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 51 | // Create a preprocessor object |
| 52 | const std::array<float, 3> mean_rgb{ { 122.68f, 116.67f, 104.01f } }; |
| 53 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<CaffePreproccessor>(mean_rgb); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 54 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 55 | // 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^] | 56 | const int target = argc > 1 ? std::strtol(argv[1], nullptr, 10) : 0; |
| 57 | Target target_hint = set_target_hint(target); |
| 58 | FastMathHint fast_math_hint = FastMathHint::DISABLED; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 59 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 60 | // Parse arguments |
| 61 | if(argc < 2) |
| 62 | { |
| 63 | // Print help |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 64 | std::cout << "Usage: " << argv[0] << " [target] [path_to_data] [image] [labels] [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 == 2) |
| 68 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 69 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " [path_to_data] [image] [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 70 | std::cout << "No data folder provided: using random values\n\n"; |
| 71 | } |
| 72 | else if(argc == 3) |
| 73 | { |
| 74 | data_path = argv[2]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 75 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " [image] [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 76 | std::cout << "No image provided: using random values\n\n"; |
| 77 | } |
| 78 | else if(argc == 4) |
| 79 | { |
| 80 | data_path = argv[2]; |
| 81 | image = argv[3]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 82 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " [labels] [fast_math_hint]\n\n"; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 83 | std::cout << "No text file with labels provided: skipping output accessor\n\n"; |
| 84 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 85 | else if(argc == 5) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 86 | { |
| 87 | data_path = argv[2]; |
| 88 | image = argv[3]; |
| 89 | label = argv[4]; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 90 | std::cout << "Usage: " << argv[0] << " " << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << " [fast_math_hint]\n\n"; |
| 91 | std::cout << "No fast math info provided: disabling fast math\n\n"; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | data_path = argv[2]; |
| 96 | image = argv[3]; |
| 97 | label = argv[4]; |
| 98 | fast_math_hint = (std::strtol(argv[5], nullptr, 1) == 0) ? FastMathHint::DISABLED : FastMathHint::ENABLED; |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | graph << target_hint |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 102 | << fast_math_hint |
| 103 | << InputLayer(TensorDescriptor(TensorShape(224U, 224U, 3U, 1U), DataType::F32), |
| 104 | get_input_accessor(image, std::move(preprocessor))) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 105 | << ConvolutionLayer( |
| 106 | 7U, 7U, 96U, |
| 107 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_w.npy"), |
| 108 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv1_b.npy"), |
| 109 | PadStrideInfo(2, 2, 0, 0)) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 110 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 111 | << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
| 112 | << ConvolutionLayer( |
| 113 | 1U, 1U, 16U, |
| 114 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_w.npy"), |
| 115 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire2_squeeze1x1_b.npy"), |
| 116 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 117 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 118 | graph << get_expand_fire_node(data_path, "fire2", 64U, 64U); |
| 119 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 120 | 1U, 1U, 16U, |
| 121 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_w.npy"), |
| 122 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire3_squeeze1x1_b.npy"), |
| 123 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 124 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 125 | graph << get_expand_fire_node(data_path, "fire3", 64U, 64U); |
| 126 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 127 | 1U, 1U, 32U, |
| 128 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_w.npy"), |
| 129 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire4_squeeze1x1_b.npy"), |
| 130 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 131 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 132 | graph << get_expand_fire_node(data_path, "fire4", 128U, 128U); |
| 133 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 134 | << ConvolutionLayer( |
| 135 | 1U, 1U, 32U, |
| 136 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_w.npy"), |
| 137 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire5_squeeze1x1_b.npy"), |
| 138 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 139 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 140 | graph << get_expand_fire_node(data_path, "fire5", 128U, 128U); |
| 141 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 142 | 1U, 1U, 48U, |
| 143 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_w.npy"), |
| 144 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire6_squeeze1x1_b.npy"), |
| 145 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 146 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 147 | graph << get_expand_fire_node(data_path, "fire6", 192U, 192U); |
| 148 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 149 | 1U, 1U, 48U, |
| 150 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_w.npy"), |
| 151 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire7_squeeze1x1_b.npy"), |
| 152 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 153 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 154 | graph << get_expand_fire_node(data_path, "fire7", 192U, 192U); |
| 155 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 156 | 1U, 1U, 64U, |
| 157 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_w.npy"), |
| 158 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire8_squeeze1x1_b.npy"), |
| 159 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 160 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 161 | graph << get_expand_fire_node(data_path, "fire8", 256U, 256U); |
| 162 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0, DimensionRoundingType::CEIL))) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 163 | << ConvolutionLayer( |
| 164 | 1U, 1U, 64U, |
| 165 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_w.npy"), |
| 166 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/fire9_squeeze1x1_b.npy"), |
| 167 | PadStrideInfo(1, 1, 0, 0)) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 168 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 169 | graph << get_expand_fire_node(data_path, "fire9", 256U, 256U); |
| 170 | graph << ConvolutionLayer( |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 171 | 1U, 1U, 1000U, |
| 172 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_w.npy"), |
| 173 | get_weights_accessor(data_path, "/cnn_data/squeezenet_v1.0_model/conv10_b.npy"), |
| 174 | PadStrideInfo(1, 1, 0, 0)) |
| 175 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)) |
| 176 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) |
| 177 | << FlattenLayer() |
| 178 | << SoftmaxLayer() |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 179 | << OutputLayer(get_output_accessor(label, 5)); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 180 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 181 | // Finalize graph |
| 182 | GraphConfig config; |
| 183 | config.use_tuner = (target == 2); |
| 184 | graph.finalize(target_hint, config); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 185 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 186 | void do_run() override |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 187 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 188 | // Run graph |
| 189 | graph.run(); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 192 | private: |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 193 | Stream graph{ 0, "SqueezeNetV1" }; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 194 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 195 | BranchLayer get_expand_fire_node(const std::string &data_path, std::string &¶m_path, unsigned int expand1_filt, unsigned int expand3_filt) |
| 196 | { |
| 197 | std::string total_path = "/cnn_data/squeezenet_v1.0_model/" + param_path + "_"; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 198 | SubStream i_a(graph); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 199 | i_a << ConvolutionLayer( |
| 200 | 1U, 1U, expand1_filt, |
| 201 | get_weights_accessor(data_path, total_path + "expand1x1_w.npy"), |
| 202 | get_weights_accessor(data_path, total_path + "expand1x1_b.npy"), |
| 203 | PadStrideInfo(1, 1, 0, 0)) |
| 204 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 205 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 206 | SubStream i_b(graph); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 207 | i_b << ConvolutionLayer( |
| 208 | 3U, 3U, expand3_filt, |
| 209 | get_weights_accessor(data_path, total_path + "expand3x3_w.npy"), |
| 210 | get_weights_accessor(data_path, total_path + "expand3x3_b.npy"), |
| 211 | PadStrideInfo(1, 1, 1, 1)) |
| 212 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)); |
| 213 | |
| 214 | return BranchLayer(BranchMergeMethod::DEPTH_CONCATENATE, std::move(i_a), std::move(i_b)); |
| 215 | } |
| 216 | }; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 217 | |
| 218 | /** Main program for Squeezenet v1.0 |
| 219 | * |
| 220 | * @param[in] argc Number of arguments |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 221 | * @param[in] argv Arguments ( [optional] Target (0 = NEON, 1 = OpenCL, 2 = OpenCL with Tuner), [optional] Path to the weights folder, [optional] image, [optional] labels, [optional] Fast math for convolution layer (0 = DISABLED, 1 = ENABLED) ) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 222 | */ |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 223 | int main(int argc, char **argv) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 224 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 225 | return arm_compute::utils::run_example<GraphSqueezenetExample>(argc, argv); |
| 226 | } |