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/CircleNonMaxSuppressionV4.h"
19 #include <luci/IR/Nodes/CircleNonMaxSuppressionV4.h>
20 #include <luci/IR/Nodes/CircleNonMaxSuppressionV4Out.h>
23 #include <oops/UserExn.h>
28 bool CircleNonMaxSuppressionV4GraphBuilder::validate(const ValidateArgs &args) const
30 const auto &inputs = args.op.inputs;
31 const auto &outputs = args.op.outputs;
33 if (inputs.size() != 5)
35 if (outputs.size() != 2)
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)
58 * @brief NonMaxSuppressionV4 Node builder
60 * @note Current loco does not provide multiple outputs
61 * We will create multiple NonMasSuppressionV4Oout nodes to emulate this
64 void CircleNonMaxSuppressionV4GraphBuilder::build(const circle::OperatorT &op,
65 GraphBuilderContext *context) const
67 assert(context != nullptr);
69 auto graph = context->graph();
71 const std::vector<int32_t> &inputs = op.inputs;
72 const std::vector<int32_t> &outputs = op.outputs;
73 const auto &tensors = context->reader()->tensors();
74 const auto &opcodes = context->reader()->opcodes();
75 auto tensors_ptr = context->reader()->tensors_ptr();
76 assert(tensors_ptr != nullptr);
78 std::vector<CircleNode *> input_nodes;
79 for (const int32_t input_tensor_index : inputs)
81 input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
84 // Create CircleNonMaxSuppressionV4
85 auto node = graph->nodes()->create<CircleNonMaxSuppressionV4>();
86 node->boxes(input_nodes[0]);
87 node->scores(input_nodes[1]);
88 node->max_output_size(input_nodes[2]);
89 node->iou_threshold(input_nodes[3]);
90 node->score_threshold(input_nodes[4]);
92 assert(outputs.size() == 2);
94 // Let's use name of output 0 as NonMaxSuppressionV4 name
95 const circle::TensorT &output_tensor = *tensors[outputs[0]];
96 node->name(tensor_name(output_tensor));
97 node->op_version(opcodes[op.opcode_index].get()->version);
99 // NOTE We don't set quantization for NonMaxSuppressionV4 itself but to virtual outputs
102 // Create virtual outputs of NonMaxSuppressionV4
103 for (size_t n = 0; n < outputs.size(); ++n)
105 const circle::TensorT &output_tensor = *tensors[outputs[n]];
107 auto *nodeout = graph->nodes()->create<CircleNonMaxSuppressionV4Out>();
108 copy_tensor_attributes(output_tensor, nodeout);
111 if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
112 nodeout->shape_status(ShapeStatus::NOSHAPE);
114 nodeout->shape_status(ShapeStatus::VALID);
116 nodeout->input(node);
119 context->nodefinder()->enroll(outputs[n], nodeout);