Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 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 | } |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 379 | NodeID GraphBuilder::add_dequantization_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 380 | { |
| 381 | return create_simple_single_input_output_node<DequantizationLayerNode>(g, params, input); |
| 382 | } |
| 383 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 384 | 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] | 385 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 386 | check_nodeidx_pair(input_loc, g); |
| 387 | check_nodeidx_pair(input_conf, g); |
| 388 | check_nodeidx_pair(input_priorbox, g); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 389 | |
| 390 | // Create detection_output node and connect |
| 391 | NodeID detect_nid = g.add_node<DetectionOutputLayerNode>(detect_info); |
| 392 | g.add_connection(input_loc.node_id, input_loc.index, detect_nid, 0); |
| 393 | g.add_connection(input_conf.node_id, input_conf.index, detect_nid, 1); |
| 394 | g.add_connection(input_priorbox.node_id, input_priorbox.index, detect_nid, 2); |
| 395 | |
| 396 | set_node_params(g, detect_nid, params); |
| 397 | |
| 398 | return detect_nid; |
| 399 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 400 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 401 | NodeID GraphBuilder::add_detection_post_process_node(Graph &g, NodeParams params, NodeIdxPair input_box_encoding, NodeIdxPair input_class_prediction, const DetectionPostProcessLayerInfo &detect_info, |
| 402 | ITensorAccessorUPtr anchors_accessor, const QuantizationInfo &anchor_quant_info) |
| 403 | { |
| 404 | check_nodeidx_pair(input_box_encoding, g); |
| 405 | check_nodeidx_pair(input_class_prediction, g); |
| 406 | |
| 407 | // Get input tensor descriptor |
| 408 | const TensorDescriptor input_box_encoding_tensor_desc = get_tensor_descriptor(g, g.node(input_box_encoding.node_id)->outputs()[0]); |
| 409 | |
| 410 | // Calculate anchor descriptor |
| 411 | TensorDescriptor anchor_desc = input_box_encoding_tensor_desc; |
| 412 | if(!anchor_quant_info.empty()) |
| 413 | { |
| 414 | anchor_desc.quant_info = anchor_quant_info; |
| 415 | } |
| 416 | |
| 417 | // Create anchors nodes |
| 418 | auto anchors_nid = add_const_node_with_name(g, params, "Anchors", anchor_desc, std::move(anchors_accessor)); |
| 419 | |
| 420 | // Create detection_output node and connect |
| 421 | NodeID detect_nid = g.add_node<DetectionPostProcessLayerNode>(detect_info); |
| 422 | g.add_connection(input_box_encoding.node_id, input_box_encoding.index, detect_nid, 0); |
| 423 | g.add_connection(input_class_prediction.node_id, input_class_prediction.index, detect_nid, 1); |
| 424 | g.add_connection(anchors_nid, 0, detect_nid, 2); |
| 425 | |
| 426 | set_node_params(g, detect_nid, params); |
| 427 | |
| 428 | return detect_nid; |
| 429 | } |
| 430 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 431 | NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 432 | { |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 433 | return create_simple_single_input_output_node<DummyNode>(g, params, input, shape); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 436 | NodeID GraphBuilder::add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
| 437 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 438 | check_nodeidx_pair(input0, g); |
| 439 | check_nodeidx_pair(input1, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 440 | |
| 441 | NodeID nid = g.add_node<EltwiseLayerNode>(operation); |
| 442 | |
| 443 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 444 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 445 | |
| 446 | set_node_params(g, nid, params); |
| 447 | |
| 448 | return nid; |
| 449 | } |
| 450 | |
| 451 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 452 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 453 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | 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] | 457 | NodeID weights_nid, NodeID bias_nid, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 458 | const FullyConnectedLayerInfo fc_info, const QuantizationInfo &out_quant_info) |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 459 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 460 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 461 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 462 | ARM_COMPUTE_ERROR_ON(weights_nid == EmptyNodeID); |
| 463 | |
| 464 | const bool has_bias = (bias_nid != EmptyNodeID); |
| 465 | |
| 466 | // Get input tensor descriptor |
| 467 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 468 | |
| 469 | // Create fully connected node and connect |
| 470 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info); |
| 471 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 472 | g.add_connection(weights_nid, 0, fc_nid, 1); |
| 473 | if(has_bias) |
| 474 | { |
| 475 | g.add_connection(bias_nid, 0, fc_nid, 2); |
| 476 | } |
| 477 | |
| 478 | set_node_params(g, fc_nid, params); |
| 479 | |
| 480 | return fc_nid; |
| 481 | } |
| 482 | |
| 483 | 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] | 484 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 485 | const FullyConnectedLayerInfo fc_info, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 486 | const QuantizationInfo &weights_quant_info, const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 487 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 488 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 489 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 490 | |
| 491 | bool has_bias = (bias_accessor != nullptr); |
| 492 | |
| 493 | // Get input tensor descriptor |
| 494 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 495 | |
| 496 | // Create weights node |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 497 | 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] | 498 | 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] | 499 | |
| 500 | // Create bias nodes |
| 501 | NodeID b_nid = EmptyNodeID; |
| 502 | if(has_bias) |
| 503 | { |
| 504 | TensorDescriptor b_desc = input_tensor_desc; |
| 505 | b_desc.shape = TensorShape(num_outputs); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 506 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 507 | { |
| 508 | b_desc.data_type = DataType::S32; |
| 509 | } |
| 510 | 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] | 511 | } |
| 512 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 513 | // Create fully connected node and connect |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 514 | 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] | 515 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 516 | g.add_connection(w_nid, 0, fc_nid, 1); |
| 517 | if(has_bias) |
| 518 | { |
| 519 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 520 | } |
| 521 | |
| 522 | set_node_params(g, fc_nid, params); |
| 523 | |
| 524 | return fc_nid; |
| 525 | } |
| 526 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 527 | NodeID GraphBuilder::add_generate_proposals_node(Graph &g, NodeParams params, NodeIdxPair scores, NodeIdxPair deltas, NodeIdxPair anchors, GenerateProposalsInfo info) |
| 528 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 529 | check_nodeidx_pair(scores, g); |
| 530 | check_nodeidx_pair(deltas, g); |
| 531 | check_nodeidx_pair(anchors, g); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 532 | |
| 533 | NodeID nid = g.add_node<GenerateProposalsLayerNode>(info); |
| 534 | |
| 535 | g.add_connection(scores.node_id, scores.index, nid, 0); |
| 536 | g.add_connection(deltas.node_id, deltas.index, nid, 1); |
| 537 | g.add_connection(anchors.node_id, anchors.index, nid, 2); |
| 538 | |
| 539 | set_node_params(g, nid, params); |
| 540 | return nid; |
| 541 | } |
| 542 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 543 | NodeID GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
| 544 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 545 | 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] | 546 | } |
| 547 | |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 548 | NodeID GraphBuilder::add_normalize_planar_yuv_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 549 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr std_accessor) |
| 550 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 551 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 552 | |
| 553 | // Get input tensor descriptor |
| 554 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 555 | |
| 556 | // Calculate Common Descriptor |
| 557 | TensorDescriptor common_desc = input_tensor_desc; |
| 558 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
| 559 | |
| 560 | // Create mean and std nodes |
| 561 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 562 | auto std_nid = add_const_node_with_name(g, params, "Std", common_desc, std::move(std_accessor)); |
| 563 | |
| 564 | // Create normalize planar YUV node and add connections |
| 565 | NodeID norm_planar_yuv_nid = g.add_node<NormalizePlanarYUVLayerNode>(); |
| 566 | g.add_connection(input.node_id, input.index, norm_planar_yuv_nid, 0); |
| 567 | g.add_connection(mean_nid, 0, norm_planar_yuv_nid, 1); |
| 568 | g.add_connection(std_nid, 0, norm_planar_yuv_nid, 2); |
| 569 | set_node_params(g, norm_planar_yuv_nid, params); |
| 570 | |
| 571 | return norm_planar_yuv_nid; |
| 572 | } |
| 573 | |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 574 | NodeID GraphBuilder::add_pad_node(Graph &g, NodeParams params, NodeIdxPair input, PaddingList padding) |
| 575 | { |
| 576 | return create_simple_single_input_output_node<PadLayerNode>(g, params, input, padding); |
| 577 | } |
| 578 | |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 579 | NodeID GraphBuilder::add_permute_node(Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout) |
| 580 | { |
| 581 | return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout); |
| 582 | } |
| 583 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 584 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 585 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 586 | 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] | 587 | } |
| 588 | |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame^] | 589 | NodeID GraphBuilder::add_print_node(Graph &g, NodeParams params, NodeIdxPair input, std::ostream &stream, const IOFormatInfo &format_info, const std::function<ITensor *(ITensor *)> transform) |
| 590 | { |
| 591 | return create_simple_single_input_output_node<PrintLayerNode>(g, params, input, stream, format_info, transform); |
| 592 | } |
| 593 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 594 | 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] | 595 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 596 | check_nodeidx_pair(input0, g); |
| 597 | check_nodeidx_pair(input1, g); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 598 | |
| 599 | // Create priorbox node and connect |
| 600 | NodeID prior_nid = g.add_node<PriorBoxLayerNode>(prior_info); |
| 601 | g.add_connection(input0.node_id, input0.index, prior_nid, 0); |
| 602 | g.add_connection(input1.node_id, input1.index, prior_nid, 1); |
| 603 | |
| 604 | set_node_params(g, prior_nid, params); |
| 605 | |
| 606 | return prior_nid; |
| 607 | } |
| 608 | |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 609 | 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] | 610 | { |
| 611 | return create_simple_single_input_output_node<QuantizationLayerNode>(g, params, input, out_quant_info); |
| 612 | } |
| 613 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 614 | NodeID GraphBuilder::add_reorg_node(Graph &g, NodeParams params, NodeIdxPair input, int stride) |
| 615 | { |
| 616 | return create_simple_single_input_output_node<ReorgLayerNode>(g, params, input, stride); |
| 617 | } |
| 618 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 619 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 620 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 621 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 624 | NodeID GraphBuilder::add_resize_node(Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, |
| 625 | float width_scale, float height_scale) |
| 626 | { |
| 627 | return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale); |
| 628 | } |
| 629 | |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 630 | NodeID GraphBuilder::add_roi_align_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info) |
| 631 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 632 | check_nodeidx_pair(input, g); |
| 633 | check_nodeidx_pair(rois, g); |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 634 | |
| 635 | NodeID nid = g.add_node<ROIAlignLayerNode>(pool_info); |
| 636 | |
| 637 | g.add_connection(input.node_id, input.index, nid, 0); |
| 638 | g.add_connection(rois.node_id, rois.index, nid, 1); |
| 639 | |
| 640 | set_node_params(g, nid, params); |
| 641 | return nid; |
| 642 | } |
| 643 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 644 | NodeID GraphBuilder::add_scale_layer(Graph &g, const NodeParams ¶ms, NodeIdxPair input, ITensorAccessorUPtr mul_accessor, ITensorAccessorUPtr add_accessor) |
| 645 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 646 | check_nodeidx_pair(input, g); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 647 | |
| 648 | // Get input tensor descriptor |
| 649 | 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] | 650 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 651 | |
| 652 | // Create mul node |
| 653 | TensorDescriptor mul_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 654 | const size_t C = input_tensor_desc.shape[get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL)]; |
| 655 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), 1); |
| 656 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), 1); |
| 657 | 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] | 658 | NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor)); |
| 659 | NodeIdxPair mul_const_nidxp = { mul_const_nid, 0 }; |
| 660 | |
| 661 | // Create add node |
| 662 | TensorDescriptor add_desc = mul_desc; |
| 663 | NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor)); |
| 664 | NodeIdxPair add_const_nidxp = { add_const_nid, 0 }; |
| 665 | |
| 666 | // Create node and connect |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 667 | 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] | 668 | NodeIdxPair mulnode_nidxp = { mul_node, 0 }; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 669 | 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] | 670 | |
| 671 | return add_node; |
| 672 | } |
| 673 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 674 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 675 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 676 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 677 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 678 | |
Michele Di Giorgio | c30b668 | 2018-09-12 17:44:08 +0100 | [diff] [blame] | 679 | NodeID GraphBuilder::add_slice_node(Graph &g, NodeParams params, NodeIdxPair input, Coordinates &starts, Coordinates &ends) |
| 680 | { |
| 681 | return create_simple_single_input_output_node<SliceLayerNode>(g, params, input, starts, ends); |
| 682 | } |
| 683 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 684 | NodeID GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
| 685 | { |
| 686 | 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] | 687 | } |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 688 | |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 689 | NodeID GraphBuilder::add_stack_node(Graph &g, NodeParams params, const std::vector<NodeIdxPair> &inputs, int axis) |
| 690 | { |
| 691 | return create_simple_multiple_input_single_output_node<StackLayerNode>(g, params, inputs, inputs.size(), axis); |
| 692 | } |
| 693 | |
Michalis Spyrou | 4e1c3f3 | 2018-09-20 17:14:03 +0100 | [diff] [blame] | 694 | NodeID GraphBuilder::add_upsample_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D info, InterpolationPolicy upsampling_policy) |
| 695 | { |
| 696 | return create_simple_single_input_output_node<UpsampleLayerNode>(g, params, input, info, upsampling_policy); |
| 697 | } |
| 698 | |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 699 | NodeID GraphBuilder::add_yolo_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info, int32_t num_classes) |
| 700 | { |
| 701 | return create_simple_single_input_output_node<YOLOLayerNode>(g, params, input, act_info, num_classes); |
| 702 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 703 | } // namespace graph |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 704 | } // namespace arm_compute |