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