2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "luci/Import/Nodes/CircleNonMaxSuppressionV5.h"
19 #include <luci/IR/Nodes/CircleNonMaxSuppressionV5.h>
20 #include <luci/IR/Nodes/CircleNonMaxSuppressionV5Out.h>
23 #include <oops/UserExn.h>
28 bool CircleNonMaxSuppressionV5GraphBuilder::validate(const ValidateArgs &args) const
30 const auto &inputs = args.op.inputs;
31 const auto &outputs = args.op.outputs;
33 if (inputs.size() != 6)
35 if (outputs.size() != 3)
38 const auto &tensors = args.reader.tensors();
39 const auto &boxes_tensor = tensors.at(inputs[0]);
40 if (boxes_tensor->shape.size() != 2)
42 if (boxes_tensor->shape.at(1) != 4)
44 if (boxes_tensor->shape.at(0) != tensors.at(inputs[1])->shape.at(0))
47 if (tensors.at(inputs[2])->type != circle::TensorType_INT32)
49 if (tensors.at(inputs[3])->type != circle::TensorType_FLOAT32)
51 if (tensors.at(inputs[4])->type != circle::TensorType_FLOAT32)
53 if (tensors.at(inputs[5])->type != circle::TensorType_FLOAT32)
60 * @brief NonMaxSuppressionV5 Node builder
62 * @note Current loco does not provide multiple outputs
63 * We will create multiple NonMasSuppressionV5Oout nodes to emulate this
66 void CircleNonMaxSuppressionV5GraphBuilder::build(const circle::OperatorT &op,
67 GraphBuilderContext *context) const
69 assert(context != nullptr);
71 auto graph = context->graph();
73 const std::vector<int32_t> &inputs = op.inputs;
74 const std::vector<int32_t> &outputs = op.outputs;
75 const auto &tensors = context->reader()->tensors();
76 const auto &opcodes = context->reader()->opcodes();
77 auto tensors_ptr = context->reader()->tensors_ptr();
78 assert(tensors_ptr != nullptr);
80 std::vector<CircleNode *> input_nodes;
81 for (const int32_t input_tensor_index : inputs)
83 input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
86 // Create CircleNonMaxSuppressionV5
87 auto node = graph->nodes()->create<CircleNonMaxSuppressionV5>();
88 node->boxes(input_nodes[0]);
89 node->scores(input_nodes[1]);
90 node->max_output_size(input_nodes[2]);
91 node->iou_threshold(input_nodes[3]);
92 node->score_threshold(input_nodes[4]);
93 node->soft_nms_sigma(input_nodes[5]);
95 assert(outputs.size() == 3);
97 // Let's use name of output 0 as NonMaxSuppressionV5 name
98 const circle::TensorT &output_tensor = *tensors[outputs[0]];
99 node->name(tensor_name(output_tensor));
100 node->op_version(opcodes[op.opcode_index].get()->version);
102 // NOTE We don't set quantization for NonMaxSuppressionV5 itself but to virtual outputs
105 // Create virtual outputs of NonMaxSuppressionV5
106 for (size_t n = 0; n < outputs.size(); ++n)
108 const circle::TensorT &output_tensor = *tensors[outputs[n]];
110 auto *nodeout = graph->nodes()->create<CircleNonMaxSuppressionV5Out>();
111 copy_tensor_attributes(output_tensor, nodeout);
114 if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
115 nodeout->shape_status(ShapeStatus::NOSHAPE);
117 nodeout->shape_status(ShapeStatus::VALID);
119 nodeout->input(node);
122 context->nodefinder()->enroll(outputs[n], nodeout);