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