Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +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 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph.h" |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 25 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 26 | #include "utils/CommonGraphOptions.h" |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 27 | #include "utils/GraphUtils.h" |
| 28 | #include "utils/Utils.h" |
| 29 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 30 | using namespace arm_compute; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 31 | using namespace arm_compute::utils; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | using namespace arm_compute::graph::frontend; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 33 | using namespace arm_compute::graph_utils; |
| 34 | |
Georgios Pinitas | 108ab0b | 2018-09-14 18:35:11 +0100 | [diff] [blame] | 35 | /** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API */ |
Gian Marco Iodice | 11a7e32 | 2018-07-05 15:42:02 +0100 | [diff] [blame] | 36 | class GraphMobilenetExample : public Example |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 37 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 38 | public: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 39 | GraphMobilenetExample() |
| 40 | : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV1") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 41 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 42 | // Add model id option |
| 43 | model_id_opt = cmd_parser.add_option<SimpleOption<int>>("model-id", 0); |
| 44 | model_id_opt->set_help("Mobilenet model id (0: 1.0_224, else: 0.75_160"); |
| 45 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 46 | GraphMobilenetExample(const GraphMobilenetExample &) = delete; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 47 | GraphMobilenetExample &operator=(const GraphMobilenetExample &) = delete; |
Gian Marco Iodice | 11a7e32 | 2018-07-05 15:42:02 +0100 | [diff] [blame] | 48 | GraphMobilenetExample(GraphMobilenetExample &&) = default; // NOLINT |
| 49 | GraphMobilenetExample &operator=(GraphMobilenetExample &&) = default; // NOLINT |
| 50 | ~GraphMobilenetExample() override = default; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 51 | bool do_setup(int argc, char **argv) override |
| 52 | { |
| 53 | // Parse arguments |
| 54 | cmd_parser.parse(argc, argv); |
| 55 | |
| 56 | // Consume common parameters |
| 57 | common_params = consume_common_graph_parameters(common_opts); |
| 58 | |
| 59 | // Return when help menu is requested |
| 60 | if(common_params.help) |
| 61 | { |
| 62 | cmd_parser.print_help(argv[0]); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // Print parameter values |
| 67 | std::cout << common_params << std::endl; |
| 68 | |
| 69 | // Get model parameters |
| 70 | int model_id = model_id_opt->value(); |
| 71 | |
| 72 | // Create input descriptor |
| 73 | unsigned int spatial_size = (model_id == 0 || common_params.data_type == DataType::QASYMM8) ? 224 : 160; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 74 | |
| 75 | // Create input descriptor |
| 76 | const TensorShape tensor_shape = permute_shape(TensorShape(spatial_size, spatial_size, 3U, 1U), DataLayout::NCHW, common_params.data_layout); |
| 77 | TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 78 | |
| 79 | // Set graph hints |
| 80 | graph << common_params.target |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 81 | << DepthwiseConvolutionMethod::Optimized3x3 // TODO(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 82 | << common_params.fast_math_hint; |
| 83 | |
| 84 | // Create core graph |
| 85 | if(arm_compute::is_data_type_float(common_params.data_type)) |
| 86 | { |
| 87 | create_graph_float(input_descriptor, model_id); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | create_graph_qasymm(input_descriptor); |
| 92 | } |
| 93 | |
| 94 | // Create common tail |
| 95 | graph << ReshapeLayer(TensorShape(1001U)).set_name("Reshape") |
| 96 | << SoftmaxLayer().set_name("Softmax") |
| 97 | << OutputLayer(get_output_accessor(common_params, 5)); |
| 98 | |
| 99 | // Finalize graph |
| 100 | GraphConfig config; |
| 101 | config.num_threads = common_params.threads; |
| 102 | config.use_tuner = common_params.enable_tuner; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 103 | config.tuner_mode = common_params.tuner_mode; |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 104 | config.tuner_file = common_params.tuner_file; |
| 105 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 106 | graph.finalize(common_params.target, config); |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | void do_run() override |
| 111 | { |
| 112 | // Run graph |
| 113 | graph.run(); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | CommandLineParser cmd_parser; |
| 118 | CommonGraphOptions common_opts; |
| 119 | SimpleOption<int> *model_id_opt{ nullptr }; |
| 120 | CommonGraphParams common_params; |
| 121 | Stream graph; |
| 122 | |
| 123 | void create_graph_float(TensorDescriptor &input_descriptor, int model_id) |
| 124 | { |
| 125 | float depth_scale = (model_id == 0) ? 1.f : 0.75; |
| 126 | std::string model_path = (model_id == 0) ? "/cnn_data/mobilenet_v1_1_224_model/" : "/cnn_data/mobilenet_v1_075_160_model/"; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 127 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 128 | // Create a preprocessor object |
| 129 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>(); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 130 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 131 | // Get trainable parameters data path |
| 132 | std::string data_path = common_params.data_path; |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 133 | |
| 134 | // Add model path to data path |
| 135 | if(!data_path.empty()) |
| 136 | { |
| 137 | data_path += model_path; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 140 | graph << InputLayer(input_descriptor, |
| 141 | get_input_accessor(common_params, std::move(preprocessor), false)) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 142 | << ConvolutionLayer( |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 143 | 3U, 3U, 32U * depth_scale, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 144 | get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 145 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 146 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 147 | .set_name("Conv2d_0") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 148 | << BatchNormalizationLayer( |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 149 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"), |
| 150 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"), |
| 151 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"), |
| 152 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 153 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 154 | .set_name("Conv2d_0/BatchNorm") |
| 155 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 156 | graph << get_dwsc_node_float(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0)); |
| 157 | graph << get_dwsc_node_float(data_path, "Conv2d_2", 128 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 158 | graph << get_dwsc_node_float(data_path, "Conv2d_3", 128 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 159 | graph << get_dwsc_node_float(data_path, "Conv2d_4", 256 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 160 | graph << get_dwsc_node_float(data_path, "Conv2d_5", 256 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 161 | graph << get_dwsc_node_float(data_path, "Conv2d_6", 512 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 162 | graph << get_dwsc_node_float(data_path, "Conv2d_7", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 163 | graph << get_dwsc_node_float(data_path, "Conv2d_8", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 164 | graph << get_dwsc_node_float(data_path, "Conv2d_9", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 165 | graph << get_dwsc_node_float(data_path, "Conv2d_10", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 166 | graph << get_dwsc_node_float(data_path, "Conv2d_11", 512 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 167 | graph << get_dwsc_node_float(data_path, "Conv2d_12", 1024 * depth_scale, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
| 168 | graph << get_dwsc_node_float(data_path, "Conv2d_13", 1024 * depth_scale, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::CEIL), PadStrideInfo(1, 1, 0, 0)); |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 169 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool_1a") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 170 | << ConvolutionLayer( |
| 171 | 1U, 1U, 1001U, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 172 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy", DataLayout::NCHW), |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 173 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 174 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 175 | .set_name("Logits/Conv2d_1c_1x1"); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 176 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 177 | |
| 178 | void create_graph_qasymm(TensorDescriptor &input_descriptor) |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 179 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 180 | // Get trainable parameters data path |
| 181 | std::string data_path = common_params.data_path; |
| 182 | |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 183 | // Add model path to data path |
| 184 | if(!data_path.empty()) |
| 185 | { |
| 186 | data_path += "/cnn_data/mobilenet_qasymm8_model/"; |
| 187 | } |
| 188 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 189 | // Quantization info taken from the AndroidNN QASYMM8 MobileNet example |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 190 | const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 191 | |
| 192 | const std::vector<QuantizationInfo> conv_weights_quant_info = |
| 193 | { |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 194 | QuantizationInfo(0.02182667888700962f, 151), // conv0 |
| 195 | QuantizationInfo(0.004986600950360298f, 74) // conv14 |
| 196 | }; |
| 197 | const std::vector<QuantizationInfo> conv_out_quant_info = |
| 198 | { |
| 199 | QuantizationInfo(0.023528477177023888f, 0), // conv0 |
| 200 | QuantizationInfo(0.16609922051429749f, 66) // conv14 |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | const std::vector<QuantizationInfo> depth_weights_quant_info = |
| 204 | { |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 205 | QuantizationInfo(0.29219913482666016f, 110), // dwsc1 |
| 206 | QuantizationInfo(0.40277284383773804f, 130), // dwsc2 |
| 207 | QuantizationInfo(0.06053730100393295f, 160), // dwsc3 |
| 208 | QuantizationInfo(0.01675807684659958f, 123), // dwsc4 |
| 209 | QuantizationInfo(0.04105526953935623f, 129), // dwsc5 |
| 210 | QuantizationInfo(0.013460792601108551f, 122), // dwsc6 |
| 211 | QuantizationInfo(0.036934755742549896f, 132), // dwsc7 |
| 212 | QuantizationInfo(0.042609862983226776f, 94), // dwsc8 |
| 213 | QuantizationInfo(0.028358859941363335f, 127), // dwsc9 |
| 214 | QuantizationInfo(0.024329448118805885f, 134), // dwsc10 |
| 215 | QuantizationInfo(0.019366811960935593f, 106), // dwsc11 |
| 216 | QuantizationInfo(0.007835594937205315f, 126), // dwsc12 |
| 217 | QuantizationInfo(0.12616927921772003f, 211) // dwsc13 |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | const std::vector<QuantizationInfo> point_weights_quant_info = |
| 221 | { |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 222 | QuantizationInfo(0.030420949682593346f, 121), // dwsc1 |
| 223 | QuantizationInfo(0.015148180536925793f, 104), // dwsc2 |
| 224 | QuantizationInfo(0.013755458407104015f, 94), // dwsc3 |
| 225 | QuantizationInfo(0.007601846940815449f, 151), // dwsc4 |
| 226 | QuantizationInfo(0.006431614048779011f, 122), // dwsc5 |
| 227 | QuantizationInfo(0.00917122047394514f, 109), // dwsc6 |
| 228 | QuantizationInfo(0.005300046876072884f, 140), // dwsc7 |
| 229 | QuantizationInfo(0.0049632852897048f, 127), // dwsc8 |
| 230 | QuantizationInfo(0.007770895957946777f, 89), // dwsc9 |
| 231 | QuantizationInfo(0.009658650495111942f, 99), // dwsc10 |
| 232 | QuantizationInfo(0.005446993745863438f, 153), // dwsc11 |
| 233 | QuantizationInfo(0.00817922968417406f, 130), // dwsc12 |
| 234 | QuantizationInfo(0.018048152327537537f, 95) // dwsc13 |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info), |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 238 | get_weights_accessor(data_path, common_params.image)) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 239 | << ConvolutionLayer( |
| 240 | 3U, 3U, 32U, |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 241 | get_weights_accessor(data_path, "Conv2d_0_weights.npy"), |
| 242 | get_weights_accessor(data_path, "Conv2d_0_bias.npy"), |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 243 | PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 244 | 1, conv_weights_quant_info.at(0), conv_out_quant_info.at(0)) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 245 | .set_name("Conv2d_0") |
| 246 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("Conv2d_0/Relu6"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 247 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_1", 64U, PadStrideInfo(1U, 1U, 1U, 1U), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(0), point_weights_quant_info.at(0)); |
| 248 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_2", 128U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(1), |
| 249 | point_weights_quant_info.at(1)); |
| 250 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_3", 128U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(2), |
| 251 | point_weights_quant_info.at(2)); |
| 252 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_4", 256U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(3), |
| 253 | point_weights_quant_info.at(3)); |
| 254 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_5", 256U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(4), |
| 255 | point_weights_quant_info.at(4)); |
| 256 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_6", 512U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(5), |
| 257 | point_weights_quant_info.at(5)); |
| 258 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_7", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(6), |
| 259 | point_weights_quant_info.at(6)); |
| 260 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_8", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(7), |
| 261 | point_weights_quant_info.at(7)); |
| 262 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_9", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(8), |
| 263 | point_weights_quant_info.at(8)); |
| 264 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_10", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(9), |
| 265 | point_weights_quant_info.at(9)); |
| 266 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_11", 512U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(10), |
| 267 | point_weights_quant_info.at(10)); |
| 268 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_12", 1024U, PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(11), |
| 269 | point_weights_quant_info.at(11)); |
| 270 | graph << get_dwsc_node_qasymm(data_path, "Conv2d_13", 1024U, PadStrideInfo(1U, 1U, 1U, 1U, 1U, 1U, DimensionRoundingType::FLOOR), PadStrideInfo(1U, 1U, 0U, 0U), depth_weights_quant_info.at(12), |
| 271 | point_weights_quant_info.at(12)) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 272 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool_1a") |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 273 | << ConvolutionLayer( |
| 274 | 1U, 1U, 1001U, |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 275 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_weights.npy"), |
| 276 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_bias.npy"), |
Isabella Gottardi | 8baaa45 | 2019-05-20 18:22:15 +0100 | [diff] [blame] | 277 | PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1), conv_out_quant_info.at(1)) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 278 | .set_name("Logits/Conv2d_1c_1x1"); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 281 | ConcatLayer get_dwsc_node_float(const std::string &data_path, std::string &¶m_path, |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 282 | unsigned int conv_filt, |
| 283 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 284 | { |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 285 | std::string total_path = param_path + "_"; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 286 | SubStream sg(graph); |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 287 | sg << DepthwiseConvolutionLayer( |
| 288 | 3U, 3U, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 289 | get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy", DataLayout::NCHW), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 290 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 291 | dwc_pad_stride_info) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 292 | .set_name(total_path + "depthwise/depthwise") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 293 | << BatchNormalizationLayer( |
| 294 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"), |
| 295 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 296 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"), |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 297 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 298 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 299 | .set_name(total_path + "depthwise/BatchNorm") |
| 300 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 301 | << ConvolutionLayer( |
| 302 | 1U, 1U, conv_filt, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 303 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 304 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 305 | conv_pad_stride_info) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 306 | .set_name(total_path + "pointwise/Conv2D") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 307 | << BatchNormalizationLayer( |
| 308 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"), |
| 309 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 310 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"), |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 311 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 312 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 313 | .set_name(total_path + "pointwise/BatchNorm") |
| 314 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6"); |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 315 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 316 | return ConcatLayer(std::move(sg)); |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 317 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 318 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 319 | ConcatLayer get_dwsc_node_qasymm(const std::string &data_path, std::string &¶m_path, |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 320 | const unsigned int conv_filt, |
| 321 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info, |
| 322 | QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info) |
| 323 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 324 | std::string total_path = param_path + "_"; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 325 | SubStream sg(graph); |
| 326 | |
| 327 | sg << DepthwiseConvolutionLayer( |
| 328 | 3U, 3U, |
| 329 | get_weights_accessor(data_path, total_path + "depthwise_weights.npy"), |
| 330 | get_weights_accessor(data_path, total_path + "depthwise_bias.npy"), |
Michalis Spyrou | 6260e19 | 2019-06-06 13:47:38 +0100 | [diff] [blame] | 331 | dwc_pad_stride_info, 1, std::move(depth_weights_quant_info)) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 332 | .set_name(total_path + "depthwise/depthwise") |
| 333 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6") |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 334 | << ConvolutionLayer( |
| 335 | 1U, 1U, conv_filt, |
| 336 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy"), |
| 337 | get_weights_accessor(data_path, total_path + "pointwise_bias.npy"), |
Michalis Spyrou | 6260e19 | 2019-06-06 13:47:38 +0100 | [diff] [blame] | 338 | conv_pad_stride_info, 1, std::move(point_weights_quant_info)) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 339 | .set_name(total_path + "pointwise/Conv2D") |
| 340 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "pointwise/Relu6"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 341 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 342 | return ConcatLayer(std::move(sg)); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 343 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 344 | }; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 345 | |
| 346 | /** Main program for MobileNetV1 |
| 347 | * |
Georgios Pinitas | bdbbbe8 | 2018-11-07 16:06:47 +0000 | [diff] [blame] | 348 | * Model is based on: |
| 349 | * https://arxiv.org/abs/1704.04861 |
| 350 | * "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" |
| 351 | * Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam |
| 352 | * |
Georgios Pinitas | 588ebc5 | 2018-12-21 13:39:07 +0000 | [diff] [blame] | 353 | * Provenance: download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz |
| 354 | * download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_0.75_160.tgz |
| 355 | * |
Georgios Pinitas | 9f28b39 | 2018-07-18 20:01:53 +0100 | [diff] [blame] | 356 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 357 | * |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 358 | * @param[in] argc Number of arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 359 | * @param[in] argv Arguments |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 360 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 361 | int main(int argc, char **argv) |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 362 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 363 | return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 364 | } |