Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 35 | /** Example demonstrating how to implement MobileNet's network using the Compute Library's graph API |
| 36 | * |
| 37 | * @param[in] argc Number of arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 38 | * @param[in] argv Arguments |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 39 | */ |
Gian Marco Iodice | 11a7e32 | 2018-07-05 15:42:02 +0100 | [diff] [blame] | 40 | class GraphMobilenetExample : public Example |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 41 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 42 | public: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 43 | GraphMobilenetExample() |
| 44 | : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "MobileNetV1") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 45 | { |
Gian Marco Iodice | 11a7e32 | 2018-07-05 15:42:02 +0100 | [diff] [blame] | 46 | // Sets default layout to NHWC |
| 47 | common_opts.data_layout->parse("NHWC"); |
| 48 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 49 | // Add model id option |
| 50 | model_id_opt = cmd_parser.add_option<SimpleOption<int>>("model-id", 0); |
| 51 | model_id_opt->set_help("Mobilenet model id (0: 1.0_224, else: 0.75_160"); |
| 52 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 53 | GraphMobilenetExample(const GraphMobilenetExample &) = delete; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 54 | GraphMobilenetExample &operator=(const GraphMobilenetExample &) = delete; |
Gian Marco Iodice | 11a7e32 | 2018-07-05 15:42:02 +0100 | [diff] [blame] | 55 | GraphMobilenetExample(GraphMobilenetExample &&) = default; // NOLINT |
| 56 | GraphMobilenetExample &operator=(GraphMobilenetExample &&) = default; // NOLINT |
| 57 | ~GraphMobilenetExample() override = default; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 58 | bool do_setup(int argc, char **argv) override |
| 59 | { |
| 60 | // Parse arguments |
| 61 | cmd_parser.parse(argc, argv); |
| 62 | |
| 63 | // Consume common parameters |
| 64 | common_params = consume_common_graph_parameters(common_opts); |
| 65 | |
| 66 | // Return when help menu is requested |
| 67 | if(common_params.help) |
| 68 | { |
| 69 | cmd_parser.print_help(argv[0]); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Print parameter values |
| 74 | std::cout << common_params << std::endl; |
| 75 | |
| 76 | // Get model parameters |
| 77 | int model_id = model_id_opt->value(); |
| 78 | |
| 79 | // Create input descriptor |
| 80 | 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] | 81 | |
| 82 | // Create input descriptor |
| 83 | const TensorShape tensor_shape = permute_shape(TensorShape(spatial_size, spatial_size, 3U, 1U), DataLayout::NCHW, common_params.data_layout); |
| 84 | 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] | 85 | |
| 86 | // Set graph hints |
| 87 | graph << common_params.target |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame^] | 88 | << DepthwiseConvolutionMethod::Optimized3x3 // FIXME(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 89 | << common_params.fast_math_hint; |
| 90 | |
| 91 | // Create core graph |
| 92 | if(arm_compute::is_data_type_float(common_params.data_type)) |
| 93 | { |
| 94 | create_graph_float(input_descriptor, model_id); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | create_graph_qasymm(input_descriptor); |
| 99 | } |
| 100 | |
| 101 | // Create common tail |
| 102 | graph << ReshapeLayer(TensorShape(1001U)).set_name("Reshape") |
| 103 | << SoftmaxLayer().set_name("Softmax") |
| 104 | << OutputLayer(get_output_accessor(common_params, 5)); |
| 105 | |
| 106 | // Finalize graph |
| 107 | GraphConfig config; |
| 108 | config.num_threads = common_params.threads; |
| 109 | config.use_tuner = common_params.enable_tuner; |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 110 | config.tuner_file = common_params.tuner_file; |
| 111 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 112 | graph.finalize(common_params.target, config); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | void do_run() override |
| 117 | { |
| 118 | // Run graph |
| 119 | graph.run(); |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | CommandLineParser cmd_parser; |
| 124 | CommonGraphOptions common_opts; |
| 125 | SimpleOption<int> *model_id_opt{ nullptr }; |
| 126 | CommonGraphParams common_params; |
| 127 | Stream graph; |
| 128 | |
| 129 | void create_graph_float(TensorDescriptor &input_descriptor, int model_id) |
| 130 | { |
| 131 | float depth_scale = (model_id == 0) ? 1.f : 0.75; |
| 132 | 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] | 133 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 134 | // Create a preprocessor object |
| 135 | std::unique_ptr<IPreprocessor> preprocessor = arm_compute::support::cpp14::make_unique<TFPreproccessor>(); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 136 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 137 | // Get trainable parameters data path |
| 138 | std::string data_path = common_params.data_path; |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 139 | |
| 140 | // Add model path to data path |
| 141 | if(!data_path.empty()) |
| 142 | { |
| 143 | data_path += model_path; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 146 | graph << InputLayer(input_descriptor, |
| 147 | get_input_accessor(common_params, std::move(preprocessor), false)) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 148 | << ConvolutionLayer( |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 149 | 3U, 3U, 32U * depth_scale, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 150 | get_weights_accessor(data_path, "Conv2d_0_weights.npy", DataLayout::NCHW), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 151 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 152 | PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR)) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 153 | .set_name("Conv2d_0") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 154 | << BatchNormalizationLayer( |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 155 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_mean.npy"), |
| 156 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_moving_variance.npy"), |
| 157 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_gamma.npy"), |
| 158 | get_weights_accessor(data_path, "Conv2d_0_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 159 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 160 | .set_name("Conv2d_0/BatchNorm") |
| 161 | << 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] | 162 | graph << get_dwsc_node_float(data_path, "Conv2d_1", 64 * depth_scale, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0)); |
| 163 | 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)); |
| 164 | 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)); |
| 165 | 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)); |
| 166 | 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)); |
| 167 | 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)); |
| 168 | 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)); |
| 169 | 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)); |
| 170 | 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)); |
| 171 | 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)); |
| 172 | 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)); |
| 173 | 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)); |
| 174 | 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] | 175 | graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("Logits/AvgPool_1a") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 176 | << ConvolutionLayer( |
| 177 | 1U, 1U, 1001U, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 178 | 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] | 179 | get_weights_accessor(data_path, "Logits_Conv2d_1c_1x1_biases.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 180 | PadStrideInfo(1, 1, 0, 0)) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 181 | .set_name("Logits/Conv2d_1c_1x1"); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 182 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 183 | |
| 184 | void create_graph_qasymm(TensorDescriptor &input_descriptor) |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 185 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 186 | // Get trainable parameters data path |
| 187 | std::string data_path = common_params.data_path; |
| 188 | |
| 189 | // Quantization info taken from the AndroidNN QASYMM8 MobileNet example |
| 190 | const QuantizationInfo in_quant_info = QuantizationInfo(0.0078125f, 128); |
| 191 | const QuantizationInfo mid_quant_info = QuantizationInfo(0.0784313753247f, 128); |
| 192 | |
| 193 | const std::vector<QuantizationInfo> conv_weights_quant_info = |
| 194 | { |
| 195 | QuantizationInfo(0.031778190285f, 156), // conv0 |
| 196 | QuantizationInfo(0.00604454148561f, 66) // conv14 |
| 197 | }; |
| 198 | |
| 199 | const std::vector<QuantizationInfo> depth_weights_quant_info = |
| 200 | { |
| 201 | QuantizationInfo(0.254282623529f, 129), // dwsc1 |
| 202 | QuantizationInfo(0.12828284502f, 172), // dwsc2 |
| 203 | QuantizationInfo(0.265911251307f, 83), // dwsc3 |
| 204 | QuantizationInfo(0.0985597148538f, 30), // dwsc4 |
| 205 | QuantizationInfo(0.0631204470992f, 54), // dwsc5 |
| 206 | QuantizationInfo(0.0137207424268f, 141), // dwsc6 |
| 207 | QuantizationInfo(0.0817828401923f, 125), // dwsc7 |
| 208 | QuantizationInfo(0.0393880493939f, 164), // dwsc8 |
| 209 | QuantizationInfo(0.211694166064f, 129), // dwsc9 |
| 210 | QuantizationInfo(0.158015936613f, 103), // dwsc10 |
| 211 | QuantizationInfo(0.0182712618262f, 137), // dwsc11 |
| 212 | QuantizationInfo(0.0127998134121f, 134), // dwsc12 |
| 213 | QuantizationInfo(0.299285322428f, 161) // dwsc13 |
| 214 | }; |
| 215 | |
| 216 | const std::vector<QuantizationInfo> point_weights_quant_info = |
| 217 | { |
| 218 | QuantizationInfo(0.0425766184926f, 129), // dwsc1 |
| 219 | QuantizationInfo(0.0250773020089f, 94), // dwsc2 |
| 220 | QuantizationInfo(0.015851572156f, 93), // dwsc3 |
| 221 | QuantizationInfo(0.0167811904103f, 98), // dwsc4 |
| 222 | QuantizationInfo(0.00951790809631f, 135), // dwsc5 |
| 223 | QuantizationInfo(0.00999817531556f, 128), // dwsc6 |
| 224 | QuantizationInfo(0.00590536883101f, 126), // dwsc7 |
| 225 | QuantizationInfo(0.00576109671965f, 133), // dwsc8 |
| 226 | QuantizationInfo(0.00830461271107f, 142), // dwsc9 |
| 227 | QuantizationInfo(0.0152327232063f, 72), // dwsc10 |
| 228 | QuantizationInfo(0.00741417845711f, 125), // dwsc11 |
| 229 | QuantizationInfo(0.0135628981516f, 142), // dwsc12 |
| 230 | QuantizationInfo(0.0338749065995f, 140) // dwsc13 |
| 231 | }; |
| 232 | |
| 233 | graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info), |
| 234 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/" + common_params.image)) |
| 235 | << ConvolutionLayer( |
| 236 | 3U, 3U, 32U, |
| 237 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_weights.npy"), |
| 238 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Conv2d_0_bias.npy"), |
| 239 | PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), |
| 240 | 1, conv_weights_quant_info.at(0), mid_quant_info) |
| 241 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)); |
| 242 | 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)); |
| 243 | 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), |
| 244 | point_weights_quant_info.at(1)); |
| 245 | 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), |
| 246 | point_weights_quant_info.at(2)); |
| 247 | 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), |
| 248 | point_weights_quant_info.at(3)); |
| 249 | 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), |
| 250 | point_weights_quant_info.at(4)); |
| 251 | 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), |
| 252 | point_weights_quant_info.at(5)); |
| 253 | 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), |
| 254 | point_weights_quant_info.at(6)); |
| 255 | 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), |
| 256 | point_weights_quant_info.at(7)); |
| 257 | 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), |
| 258 | point_weights_quant_info.at(8)); |
| 259 | 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), |
| 260 | point_weights_quant_info.at(9)); |
| 261 | 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), |
| 262 | point_weights_quant_info.at(10)); |
| 263 | 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), |
| 264 | point_weights_quant_info.at(11)); |
| 265 | 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), |
| 266 | point_weights_quant_info.at(12)) |
| 267 | << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)) |
| 268 | << ConvolutionLayer( |
| 269 | 1U, 1U, 1001U, |
| 270 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_weights.npy"), |
| 271 | get_weights_accessor(data_path, "/cnn_data/mobilenet_qasymm8_model/Logits_Conv2d_1c_1x1_bias.npy"), |
| 272 | PadStrideInfo(1U, 1U, 0U, 0U), 1, conv_weights_quant_info.at(1)); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 275 | BranchLayer get_dwsc_node_float(const std::string &data_path, std::string &¶m_path, |
| 276 | unsigned int conv_filt, |
| 277 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 278 | { |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 279 | std::string total_path = param_path + "_"; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 280 | SubStream sg(graph); |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 281 | sg << DepthwiseConvolutionLayer( |
| 282 | 3U, 3U, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 283 | 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] | 284 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 285 | dwc_pad_stride_info) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 286 | .set_name(total_path + "depthwise/depthwise") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 287 | << BatchNormalizationLayer( |
| 288 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_mean.npy"), |
| 289 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_moving_variance.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 290 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_gamma.npy"), |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 291 | get_weights_accessor(data_path, total_path + "depthwise_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 292 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 293 | .set_name(total_path + "depthwise/BatchNorm") |
| 294 | << 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] | 295 | << ConvolutionLayer( |
| 296 | 1U, 1U, conv_filt, |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 297 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy", DataLayout::NCHW), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 298 | std::unique_ptr<arm_compute::graph::ITensorAccessor>(nullptr), |
| 299 | conv_pad_stride_info) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 300 | .set_name(total_path + "pointwise/Conv2D") |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 301 | << BatchNormalizationLayer( |
| 302 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_mean.npy"), |
| 303 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_moving_variance.npy"), |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 304 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_gamma.npy"), |
Georgios Pinitas | 7f530b3 | 2018-01-22 11:20:44 +0000 | [diff] [blame] | 305 | get_weights_accessor(data_path, total_path + "pointwise_BatchNorm_beta.npy"), |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 306 | 0.001f) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 307 | .set_name(total_path + "pointwise/BatchNorm") |
| 308 | << 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] | 309 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 310 | return BranchLayer(std::move(sg)); |
| 311 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 312 | |
| 313 | BranchLayer get_dwsc_node_qasymm(const std::string &data_path, std::string &¶m_path, |
| 314 | const unsigned int conv_filt, |
| 315 | PadStrideInfo dwc_pad_stride_info, PadStrideInfo conv_pad_stride_info, |
| 316 | QuantizationInfo depth_weights_quant_info, QuantizationInfo point_weights_quant_info) |
| 317 | { |
| 318 | std::string total_path = "/cnn_data/mobilenet_qasymm8_model/" + param_path + "_"; |
| 319 | SubStream sg(graph); |
| 320 | |
| 321 | sg << DepthwiseConvolutionLayer( |
| 322 | 3U, 3U, |
| 323 | get_weights_accessor(data_path, total_path + "depthwise_weights.npy"), |
| 324 | get_weights_accessor(data_path, total_path + "depthwise_bias.npy"), |
| 325 | dwc_pad_stride_info, depth_weights_quant_info) |
| 326 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)) |
| 327 | << ConvolutionLayer( |
| 328 | 1U, 1U, conv_filt, |
| 329 | get_weights_accessor(data_path, total_path + "pointwise_weights.npy"), |
| 330 | get_weights_accessor(data_path, total_path + "pointwise_bias.npy"), |
| 331 | conv_pad_stride_info, 1, point_weights_quant_info) |
| 332 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)); |
| 333 | |
| 334 | return BranchLayer(std::move(sg)); |
| 335 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 336 | }; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 337 | |
| 338 | /** Main program for MobileNetV1 |
| 339 | * |
Georgios Pinitas | 9f28b39 | 2018-07-18 20:01:53 +0100 | [diff] [blame] | 340 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 341 | * |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 342 | * @param[in] argc Number of arguments |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 343 | * @param[in] argv Arguments |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 344 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 345 | int main(int argc, char **argv) |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 346 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 347 | return arm_compute::utils::run_example<GraphMobilenetExample>(argc, argv); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 348 | } |