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