Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Giuseppe Rossini | bb365de | 2019-02-15 10:24:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +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/GraphBuilder.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | #include "arm_compute/graph/Graph.h" |
| 27 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/algorithms/TopologicalSort.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/nodes/Nodes.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 30 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 31 | #include "support/ToolchainSupport.h" |
| 32 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 35 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 36 | { |
| 37 | namespace |
| 38 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 39 | inline void check_nodeidx_pair(const NodeIdxPair &pair, const Graph &g) |
| 40 | { |
| 41 | ARM_COMPUTE_UNUSED(pair); |
| 42 | ARM_COMPUTE_UNUSED(g); |
| 43 | ARM_COMPUTE_ERROR_ON((pair.node_id >= g.nodes().size()) || (g.node((pair).node_id) == nullptr) || (pair.index >= g.node(pair.node_id)->num_outputs())); |
| 44 | } |
| 45 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 46 | Status set_node_params(Graph &g, NodeID nid, NodeParams ¶ms) |
| 47 | { |
| 48 | INode *node = g.node(nid); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 50 | |
| 51 | node->set_common_node_parameters(params); |
| 52 | |
| 53 | return Status{}; |
| 54 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 55 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 56 | Status set_accessor_on_node(Graph &g, NodeID nid, bool is_output, size_t idx, ITensorAccessorUPtr accessor) |
| 57 | { |
| 58 | INode *node = g.node(nid); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 60 | |
| 61 | Tensor *tensor = is_output ? node->output(idx) : node->input(idx); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(!tensor); |
| 63 | |
| 64 | tensor->set_accessor(std::move(accessor)); |
| 65 | |
| 66 | return Status{}; |
| 67 | } |
| 68 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 69 | NodeID add_const_node_with_name(Graph &g, NodeParams params, const std::string &name, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 70 | { |
| 71 | params.name = params.name.empty() ? "" : params.name + name; |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 72 | auto nid = GraphBuilder::add_const_node(g, params, desc, std::move(accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 73 | set_node_params(g, nid, params); |
| 74 | return nid; |
| 75 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 76 | |
| 77 | template <typename NT, typename... Args> |
| 78 | NodeID create_simple_single_input_output_node(Graph &g, NodeParams ¶ms, NodeIdxPair input, Args &&... args) |
| 79 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 80 | check_nodeidx_pair(input, g); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 81 | |
| 82 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 83 | g.add_connection(input.node_id, input.index, nid, 0); |
| 84 | set_node_params(g, nid, params); |
| 85 | |
| 86 | return nid; |
| 87 | } |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 88 | |
| 89 | template <typename NT, typename... Args> |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 90 | NodeID create_simple_multiple_input_single_output_node(Graph &g, NodeParams ¶ms, const std::vector<NodeIdxPair> &inputs, Args &&... args) |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 91 | { |
| 92 | ARM_COMPUTE_ERROR_ON(inputs.size() == 0); |
| 93 | |
| 94 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 95 | |
| 96 | unsigned int i = 0; |
| 97 | for(const auto &input : inputs) |
| 98 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 99 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 100 | g.add_connection(input.node_id, input.index, nid, i++); |
| 101 | } |
| 102 | set_node_params(g, nid, params); |
| 103 | |
| 104 | return nid; |
| 105 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 106 | } // namespace |
| 107 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 108 | NodeID GraphBuilder::add_const_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 109 | { |
| 110 | auto nid = g.add_node<ConstNode>(desc); |
| 111 | set_node_params(g, nid, params); |
| 112 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 113 | return nid; |
| 114 | } |
| 115 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 116 | NodeID GraphBuilder::add_input_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 117 | { |
| 118 | auto nid = g.add_node<InputNode>(desc); |
| 119 | set_node_params(g, nid, params); |
| 120 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 121 | return nid; |
| 122 | } |
| 123 | |
| 124 | NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor) |
| 125 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 126 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 127 | |
| 128 | NodeID nid = g.add_node<OutputNode>(); |
| 129 | g.add_connection(input.node_id, input.index, nid, 0); |
| 130 | set_node_params(g, nid, params); |
| 131 | set_accessor_on_node(g, nid, false, 0, std::move(accessor)); |
| 132 | |
| 133 | return nid; |
| 134 | } |
| 135 | |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 136 | NodeID GraphBuilder::add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 137 | const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 138 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 139 | return create_simple_single_input_output_node<ActivationLayerNode>(g, params, input, act_info, out_quant_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | NodeID GraphBuilder::add_batch_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, float epsilon, |
| 143 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr var_accessor, |
| 144 | ITensorAccessorUPtr beta_accessor, ITensorAccessorUPtr gamma_accessor) |
| 145 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 146 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 147 | |
| 148 | bool has_beta = (beta_accessor != nullptr); |
| 149 | bool has_gamma = (gamma_accessor != nullptr); |
| 150 | |
| 151 | // Get input tensor descriptor |
| 152 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 153 | |
| 154 | // Calculate Common Descriptor |
| 155 | TensorDescriptor common_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 156 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 157 | |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 158 | // Create mean and var nodes |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 159 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 160 | auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor)); |
| 161 | |
| 162 | // Create beta node |
| 163 | NodeID beta_nid = EmptyNodeID; |
| 164 | if(has_beta) |
| 165 | { |
| 166 | beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor)); |
| 167 | } |
| 168 | |
| 169 | // Create gamma node |
| 170 | NodeID gamma_nid = EmptyNodeID; |
| 171 | if(has_gamma) |
| 172 | { |
| 173 | gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor)); |
| 174 | } |
| 175 | |
| 176 | // Create batch normalization node and add connections |
| 177 | NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon); |
| 178 | g.add_connection(input.node_id, input.index, batch_norm_nid, 0); |
| 179 | g.add_connection(mean_nid, 0, batch_norm_nid, 1); |
| 180 | g.add_connection(var_nid, 0, batch_norm_nid, 2); |
| 181 | if(has_beta) |
| 182 | { |
| 183 | g.add_connection(beta_nid, 0, batch_norm_nid, 3); |
| 184 | } |
| 185 | if(has_gamma) |
| 186 | { |
| 187 | g.add_connection(gamma_nid, 0, batch_norm_nid, 4); |
| 188 | } |
| 189 | set_node_params(g, batch_norm_nid, params); |
| 190 | |
| 191 | return batch_norm_nid; |
| 192 | } |
| 193 | |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 194 | NodeID GraphBuilder::add_bounding_box_transform_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair deltas, BoundingBoxTransformInfo info) |
| 195 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 196 | check_nodeidx_pair(input, g); |
| 197 | check_nodeidx_pair(deltas, g); |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 198 | |
| 199 | NodeID nid = g.add_node<BoundingBoxTransformLayerNode>(info); |
| 200 | |
| 201 | g.add_connection(input.node_id, input.index, nid, 0); |
| 202 | g.add_connection(deltas.node_id, deltas.index, nid, 1); |
| 203 | |
| 204 | set_node_params(g, nid, params); |
| 205 | return nid; |
| 206 | } |
| 207 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 208 | NodeID GraphBuilder::add_channel_shuffle_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_groups) |
| 209 | { |
| 210 | return create_simple_single_input_output_node<ChannelShuffleLayerNode>(g, params, input, num_groups); |
| 211 | } |
| 212 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 213 | NodeID GraphBuilder::add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 214 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info, |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 215 | unsigned int num_groups, ConvolutionMethod method, FastMathHint fast_math_hint, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 216 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 217 | const QuantizationInfo &weights_quant_info, |
| 218 | const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 219 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 220 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 221 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 222 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 223 | |
| 224 | bool has_bias = (bias_accessor != nullptr); |
| 225 | |
| 226 | // Get input tensor descriptor |
| 227 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 228 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 229 | |
| 230 | // Create weights node |
| 231 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 232 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 233 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 234 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 235 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 236 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 237 | if(!weights_quant_info.empty()) |
| 238 | { |
| 239 | w_desc.quant_info = weights_quant_info; |
| 240 | } |
| 241 | |
| 242 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 243 | |
| 244 | // Create bias nodes |
| 245 | NodeID b_nid = EmptyNodeID; |
| 246 | if(has_bias) |
| 247 | { |
| 248 | TensorDescriptor b_desc = input_tensor_desc; |
| 249 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 250 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 251 | { |
| 252 | b_desc.data_type = DataType::S32; |
| 253 | } |
| 254 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 257 | // Create convolution node and connect |
| 258 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, num_groups, method, fast_math_hint, out_quant_info); |
| 259 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 260 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 261 | if(has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 262 | { |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 263 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 264 | } |
| 265 | set_node_params(g, conv_nid, params); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 266 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 267 | return conv_nid; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 270 | NodeID GraphBuilder::add_deconvolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 271 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo deconv_info, |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 272 | ITensorAccessorUPtr weights_accessor, |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 273 | ITensorAccessorUPtr bias_accessor) |
| 274 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 275 | check_nodeidx_pair(input, g); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 276 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 277 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 278 | |
| 279 | bool has_bias = (bias_accessor != nullptr); |
| 280 | |
| 281 | // Get input tensor descriptor |
| 282 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 283 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 284 | |
| 285 | // Create weights node |
| 286 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 287 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 288 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 289 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 290 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 291 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 292 | |
| 293 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
| 294 | |
| 295 | // Create bias nodes |
| 296 | NodeID b_nid = EmptyNodeID; |
| 297 | if(has_bias) |
| 298 | { |
| 299 | TensorDescriptor b_desc = input_tensor_desc; |
| 300 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 301 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 302 | { |
| 303 | b_desc.data_type = DataType::S32; |
| 304 | } |
| 305 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // Create convolution node and connect |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 309 | NodeID deconv_nid = g.add_node<DeconvolutionLayerNode>(deconv_info); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 310 | g.add_connection(input.node_id, input.index, deconv_nid, 0); |
| 311 | g.add_connection(w_nid, 0, deconv_nid, 1); |
| 312 | if(has_bias) |
| 313 | { |
| 314 | g.add_connection(b_nid, 0, deconv_nid, 2); |
| 315 | } |
| 316 | set_node_params(g, deconv_nid, params); |
| 317 | |
| 318 | return deconv_nid; |
| 319 | } |
| 320 | |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 321 | NodeID GraphBuilder::add_concatenate_node(Graph &g, NodeParams params, const std::vector<NodeIdxPair> &inputs, const descriptors::ConcatLayerDescriptor &concat_descriptor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 322 | { |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 323 | return create_simple_multiple_input_single_output_node<ConcatenateLayerNode>(g, params, inputs, inputs.size(), concat_descriptor); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 326 | NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, |
| 327 | PadStrideInfo conv_info, int depth_multiplier, DepthwiseConvolutionMethod method, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 328 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, const QuantizationInfo &quant_info, const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 329 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 330 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 331 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 332 | |
| 333 | bool has_bias = (bias_accessor != nullptr); |
| 334 | |
| 335 | // Get input tensor descriptor |
| 336 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 337 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 338 | |
| 339 | // Create weights node |
| 340 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 341 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 342 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 343 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 344 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 345 | if(!quant_info.empty()) |
| 346 | { |
| 347 | w_desc.quant_info = quant_info; |
| 348 | } |
| 349 | |
| 350 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 351 | |
| 352 | // Create bias nodes |
| 353 | NodeID b_nid = EmptyNodeID; |
| 354 | if(has_bias) |
| 355 | { |
| 356 | TensorDescriptor b_desc = input_tensor_desc; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 357 | b_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); |
Giorgio Arena | 2aa0ec4 | 2018-08-29 14:28:38 +0100 | [diff] [blame] | 358 | |
| 359 | if(is_data_type_quantized_asymmetric(b_desc.data_type)) |
| 360 | { |
| 361 | b_desc.data_type = DataType::S32; |
| 362 | } |
| 363 | |
| 364 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Create convolution node and connect |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 368 | NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, depth_multiplier, method, out_quant_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 369 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 370 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 371 | if(has_bias) |
| 372 | { |
| 373 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 374 | } |
| 375 | set_node_params(g, conv_nid, params); |
| 376 | |
| 377 | return conv_nid; |
| 378 | } |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 379 | NodeID GraphBuilder::add_detection_output_node(Graph &g, NodeParams params, NodeIdxPair input_loc, NodeIdxPair input_conf, NodeIdxPair input_priorbox, const DetectionOutputLayerInfo &detect_info) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 380 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 381 | check_nodeidx_pair(input_loc, g); |
| 382 | check_nodeidx_pair(input_conf, g); |
| 383 | check_nodeidx_pair(input_priorbox, g); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 384 | |
| 385 | // Create detection_output node and connect |
| 386 | NodeID detect_nid = g.add_node<DetectionOutputLayerNode>(detect_info); |
| 387 | g.add_connection(input_loc.node_id, input_loc.index, detect_nid, 0); |
| 388 | g.add_connection(input_conf.node_id, input_conf.index, detect_nid, 1); |
| 389 | g.add_connection(input_priorbox.node_id, input_priorbox.index, detect_nid, 2); |
| 390 | |
| 391 | set_node_params(g, detect_nid, params); |
| 392 | |
| 393 | return detect_nid; |
| 394 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 395 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 396 | NodeID GraphBuilder::add_detection_post_process_node(Graph &g, NodeParams params, NodeIdxPair input_box_encoding, NodeIdxPair input_class_prediction, const DetectionPostProcessLayerInfo &detect_info, |
| 397 | ITensorAccessorUPtr anchors_accessor, const QuantizationInfo &anchor_quant_info) |
| 398 | { |
| 399 | check_nodeidx_pair(input_box_encoding, g); |
| 400 | check_nodeidx_pair(input_class_prediction, g); |
| 401 | |
| 402 | // Get input tensor descriptor |
| 403 | const TensorDescriptor input_box_encoding_tensor_desc = get_tensor_descriptor(g, g.node(input_box_encoding.node_id)->outputs()[0]); |
| 404 | |
| 405 | // Calculate anchor descriptor |
| 406 | TensorDescriptor anchor_desc = input_box_encoding_tensor_desc; |
| 407 | if(!anchor_quant_info.empty()) |
| 408 | { |
| 409 | anchor_desc.quant_info = anchor_quant_info; |
| 410 | } |
| 411 | |
| 412 | // Create anchors nodes |
| 413 | auto anchors_nid = add_const_node_with_name(g, params, "Anchors", anchor_desc, std::move(anchors_accessor)); |
| 414 | |
| 415 | // Create detection_output node and connect |
| 416 | NodeID detect_nid = g.add_node<DetectionPostProcessLayerNode>(detect_info); |
| 417 | g.add_connection(input_box_encoding.node_id, input_box_encoding.index, detect_nid, 0); |
| 418 | g.add_connection(input_class_prediction.node_id, input_class_prediction.index, detect_nid, 1); |
| 419 | g.add_connection(anchors_nid, 0, detect_nid, 2); |
| 420 | |
| 421 | set_node_params(g, detect_nid, params); |
| 422 | |
| 423 | return detect_nid; |
| 424 | } |
| 425 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 426 | NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 427 | { |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 428 | return create_simple_single_input_output_node<DummyNode>(g, params, input, shape); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 431 | NodeID GraphBuilder::add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
| 432 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 433 | check_nodeidx_pair(input0, g); |
| 434 | check_nodeidx_pair(input1, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 435 | |
| 436 | NodeID nid = g.add_node<EltwiseLayerNode>(operation); |
| 437 | |
| 438 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 439 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 440 | |
| 441 | set_node_params(g, nid, params); |
| 442 | |
| 443 | return nid; |
| 444 | } |
| 445 | |
| 446 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 447 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 448 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | NodeID GraphBuilder::add_fully_connected_layer(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_outputs, |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 452 | NodeID weights_nid, NodeID bias_nid, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 453 | const FullyConnectedLayerInfo fc_info, const QuantizationInfo &out_quant_info) |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 454 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 455 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 456 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 457 | ARM_COMPUTE_ERROR_ON(weights_nid == EmptyNodeID); |
| 458 | |
| 459 | const bool has_bias = (bias_nid != EmptyNodeID); |
| 460 | |
| 461 | // Get input tensor descriptor |
| 462 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 463 | |
| 464 | // Create fully connected node and connect |
| 465 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info); |
| 466 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 467 | g.add_connection(weights_nid, 0, fc_nid, 1); |
| 468 | if(has_bias) |
| 469 | { |
| 470 | g.add_connection(bias_nid, 0, fc_nid, 2); |
| 471 | } |
| 472 | |
| 473 | set_node_params(g, fc_nid, params); |
| 474 | |
| 475 | return fc_nid; |
| 476 | } |
| 477 | |
| 478 | NodeID GraphBuilder::add_fully_connected_layer(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_outputs, |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 479 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 480 | const FullyConnectedLayerInfo fc_info, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 481 | const QuantizationInfo &weights_quant_info, const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 482 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 483 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 484 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 485 | |
| 486 | bool has_bias = (bias_accessor != nullptr); |
| 487 | |
| 488 | // Get input tensor descriptor |
| 489 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 490 | |
| 491 | // Create weights node |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 492 | TensorDescriptor w_desc = FullyConnectedLayerNode::compute_weights_descriptor(input_tensor_desc, num_outputs, fc_info, weights_quant_info); |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 493 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 494 | |
| 495 | // Create bias nodes |
| 496 | NodeID b_nid = EmptyNodeID; |
| 497 | if(has_bias) |
| 498 | { |
| 499 | TensorDescriptor b_desc = input_tensor_desc; |
| 500 | b_desc.shape = TensorShape(num_outputs); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 501 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 502 | { |
| 503 | b_desc.data_type = DataType::S32; |
| 504 | } |
| 505 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 508 | // Create fully connected node and connect |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 509 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 510 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 511 | g.add_connection(w_nid, 0, fc_nid, 1); |
| 512 | if(has_bias) |
| 513 | { |
| 514 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 515 | } |
| 516 | |
| 517 | set_node_params(g, fc_nid, params); |
| 518 | |
| 519 | return fc_nid; |
| 520 | } |
| 521 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 522 | NodeID GraphBuilder::add_generate_proposals_node(Graph &g, NodeParams params, NodeIdxPair scores, NodeIdxPair deltas, NodeIdxPair anchors, GenerateProposalsInfo info) |
| 523 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 524 | check_nodeidx_pair(scores, g); |
| 525 | check_nodeidx_pair(deltas, g); |
| 526 | check_nodeidx_pair(anchors, g); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 527 | |
| 528 | NodeID nid = g.add_node<GenerateProposalsLayerNode>(info); |
| 529 | |
| 530 | g.add_connection(scores.node_id, scores.index, nid, 0); |
| 531 | g.add_connection(deltas.node_id, deltas.index, nid, 1); |
| 532 | g.add_connection(anchors.node_id, anchors.index, nid, 2); |
| 533 | |
| 534 | set_node_params(g, nid, params); |
| 535 | return nid; |
| 536 | } |
| 537 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 538 | NodeID GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
| 539 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 540 | return create_simple_single_input_output_node<NormalizationLayerNode>(g, params, input, norm_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 543 | NodeID GraphBuilder::add_normalize_planar_yuv_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 544 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr std_accessor) |
| 545 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 546 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 547 | |
| 548 | // Get input tensor descriptor |
| 549 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 550 | |
| 551 | // Calculate Common Descriptor |
| 552 | TensorDescriptor common_desc = input_tensor_desc; |
| 553 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
| 554 | |
| 555 | // Create mean and std nodes |
| 556 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 557 | auto std_nid = add_const_node_with_name(g, params, "Std", common_desc, std::move(std_accessor)); |
| 558 | |
| 559 | // Create normalize planar YUV node and add connections |
| 560 | NodeID norm_planar_yuv_nid = g.add_node<NormalizePlanarYUVLayerNode>(); |
| 561 | g.add_connection(input.node_id, input.index, norm_planar_yuv_nid, 0); |
| 562 | g.add_connection(mean_nid, 0, norm_planar_yuv_nid, 1); |
| 563 | g.add_connection(std_nid, 0, norm_planar_yuv_nid, 2); |
| 564 | set_node_params(g, norm_planar_yuv_nid, params); |
| 565 | |
| 566 | return norm_planar_yuv_nid; |
| 567 | } |
| 568 | |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 569 | NodeID GraphBuilder::add_pad_node(Graph &g, NodeParams params, NodeIdxPair input, PaddingList padding) |
| 570 | { |
| 571 | return create_simple_single_input_output_node<PadLayerNode>(g, params, input, padding); |
| 572 | } |
| 573 | |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 574 | NodeID GraphBuilder::add_permute_node(Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout) |
| 575 | { |
| 576 | return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout); |
| 577 | } |
| 578 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 579 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 580 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 581 | return create_simple_single_input_output_node<PoolingLayerNode>(g, params, input, pool_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 584 | NodeID GraphBuilder::add_priorbox_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, const PriorBoxLayerInfo &prior_info) |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 585 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 586 | check_nodeidx_pair(input0, g); |
| 587 | check_nodeidx_pair(input1, g); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 588 | |
| 589 | // Create priorbox node and connect |
| 590 | NodeID prior_nid = g.add_node<PriorBoxLayerNode>(prior_info); |
| 591 | g.add_connection(input0.node_id, input0.index, prior_nid, 0); |
| 592 | g.add_connection(input1.node_id, input1.index, prior_nid, 1); |
| 593 | |
| 594 | set_node_params(g, prior_nid, params); |
| 595 | |
| 596 | return prior_nid; |
| 597 | } |
| 598 | |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 599 | NodeID GraphBuilder::add_quantization_node(Graph &g, NodeParams params, NodeIdxPair input, const QuantizationInfo &out_quant_info) |
Isabella Gottardi | 3db1ba9 | 2019-05-17 12:35:20 +0100 | [diff] [blame] | 600 | { |
| 601 | return create_simple_single_input_output_node<QuantizationLayerNode>(g, params, input, out_quant_info); |
| 602 | } |
| 603 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 604 | NodeID GraphBuilder::add_reorg_node(Graph &g, NodeParams params, NodeIdxPair input, int stride) |
| 605 | { |
| 606 | return create_simple_single_input_output_node<ReorgLayerNode>(g, params, input, stride); |
| 607 | } |
| 608 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 609 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 610 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 611 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 614 | NodeID GraphBuilder::add_resize_node(Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, |
| 615 | float width_scale, float height_scale) |
| 616 | { |
| 617 | return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale); |
| 618 | } |
| 619 | |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 620 | NodeID GraphBuilder::add_roi_align_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info) |
| 621 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 622 | check_nodeidx_pair(input, g); |
| 623 | check_nodeidx_pair(rois, g); |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 624 | |
| 625 | NodeID nid = g.add_node<ROIAlignLayerNode>(pool_info); |
| 626 | |
| 627 | g.add_connection(input.node_id, input.index, nid, 0); |
| 628 | g.add_connection(rois.node_id, rois.index, nid, 1); |
| 629 | |
| 630 | set_node_params(g, nid, params); |
| 631 | return nid; |
| 632 | } |
| 633 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 634 | NodeID GraphBuilder::add_scale_layer(Graph &g, const NodeParams ¶ms, NodeIdxPair input, ITensorAccessorUPtr mul_accessor, ITensorAccessorUPtr add_accessor) |
| 635 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 636 | check_nodeidx_pair(input, g); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 637 | |
| 638 | // Get input tensor descriptor |
| 639 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 640 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 641 | |
| 642 | // Create mul node |
| 643 | TensorDescriptor mul_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 644 | const size_t C = input_tensor_desc.shape[get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL)]; |
| 645 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), 1); |
| 646 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), 1); |
| 647 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), C); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 648 | NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor)); |
| 649 | NodeIdxPair mul_const_nidxp = { mul_const_nid, 0 }; |
| 650 | |
| 651 | // Create add node |
| 652 | TensorDescriptor add_desc = mul_desc; |
| 653 | NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor)); |
| 654 | NodeIdxPair add_const_nidxp = { add_const_nid, 0 }; |
| 655 | |
| 656 | // Create node and connect |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 657 | NodeID mul_node = GraphBuilder::add_elementwise_node(g, params, input, mul_const_nidxp, EltwiseOperation::Mul); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 658 | NodeIdxPair mulnode_nidxp = { mul_node, 0 }; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 659 | NodeID add_node = GraphBuilder::add_elementwise_node(g, params, mulnode_nidxp, add_const_nidxp, EltwiseOperation::Add); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 660 | |
| 661 | return add_node; |
| 662 | } |
| 663 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 664 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 665 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 666 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 667 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 668 | |
Michele Di Giorgio | c30b668 | 2018-09-12 17:44:08 +0100 | [diff] [blame] | 669 | NodeID GraphBuilder::add_slice_node(Graph &g, NodeParams params, NodeIdxPair input, Coordinates &starts, Coordinates &ends) |
| 670 | { |
| 671 | return create_simple_single_input_output_node<SliceLayerNode>(g, params, input, starts, ends); |
| 672 | } |
| 673 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 674 | NodeID GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
| 675 | { |
| 676 | return create_simple_single_input_output_node<SplitLayerNode>(g, params, input, num_splits, axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 677 | } |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 678 | |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 679 | NodeID GraphBuilder::add_stack_node(Graph &g, NodeParams params, const std::vector<NodeIdxPair> &inputs, int axis) |
| 680 | { |
| 681 | return create_simple_multiple_input_single_output_node<StackLayerNode>(g, params, inputs, inputs.size(), axis); |
| 682 | } |
| 683 | |
Michalis Spyrou | 4e1c3f3 | 2018-09-20 17:14:03 +0100 | [diff] [blame] | 684 | NodeID GraphBuilder::add_upsample_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D info, InterpolationPolicy upsampling_policy) |
| 685 | { |
| 686 | return create_simple_single_input_output_node<UpsampleLayerNode>(g, params, input, info, upsampling_policy); |
| 687 | } |
| 688 | |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 689 | NodeID GraphBuilder::add_yolo_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info, int32_t num_classes) |
| 690 | { |
| 691 | return create_simple_single_input_output_node<YOLOLayerNode>(g, params, input, act_info, num_classes); |
| 692 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 693 | } // namespace graph |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 694 | } // namespace arm_compute |