Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleNonMaxSuppressionV5.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "luci/Import/Nodes/CircleNonMaxSuppressionV5.h"
18
19 #include <luci/IR/Nodes/CircleNonMaxSuppressionV5.h>
20 #include <luci/IR/Nodes/CircleNonMaxSuppressionV5Out.h>
21
22 #include <loco.h>
23 #include <oops/UserExn.h>
24
25 namespace luci
26 {
27
28 bool CircleNonMaxSuppressionV5GraphBuilder::validate(const ValidateArgs &args) const
29 {
30   const auto &inputs = args.op.inputs;
31   const auto &outputs = args.op.outputs;
32
33   if (inputs.size() != 6)
34     return false;
35   if (outputs.size() != 3)
36     return false;
37
38   const auto &tensors = args.reader.tensors();
39   const auto &boxes_tensor = tensors.at(inputs[0]);
40   if (boxes_tensor->shape.size() != 2)
41     return false;
42   if (boxes_tensor->shape.at(1) != 4)
43     return false;
44   if (boxes_tensor->shape.at(0) != tensors.at(inputs[1])->shape.at(0))
45     return false;
46
47   if (tensors.at(inputs[2])->type != circle::TensorType_INT32)
48     return false;
49   if (tensors.at(inputs[3])->type != circle::TensorType_FLOAT32)
50     return false;
51   if (tensors.at(inputs[4])->type != circle::TensorType_FLOAT32)
52     return false;
53   if (tensors.at(inputs[5])->type != circle::TensorType_FLOAT32)
54     return false;
55
56   return true;
57 }
58
59 /**
60  * @brief  NonMaxSuppressionV5 Node builder
61  *
62  * @note   Current loco does not provide multiple outputs
63  *         We will create multiple NonMasSuppressionV5Oout nodes to emulate this
64  */
65
66 void CircleNonMaxSuppressionV5GraphBuilder::build(const circle::OperatorT &op,
67                                                   GraphBuilderContext *context) const
68 {
69   assert(context != nullptr);
70
71   auto graph = context->graph();
72
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);
79
80   std::vector<CircleNode *> input_nodes;
81   for (const int32_t input_tensor_index : inputs)
82   {
83     input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
84   }
85
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]);
94
95   assert(outputs.size() == 3);
96   {
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);
101
102     // NOTE We don't set quantization for NonMaxSuppressionV5 itself but to virtual outputs
103   }
104
105   // Create virtual outputs of NonMaxSuppressionV5
106   for (size_t n = 0; n < outputs.size(); ++n)
107   {
108     const circle::TensorT &output_tensor = *tensors[outputs[n]];
109
110     auto *nodeout = graph->nodes()->create<CircleNonMaxSuppressionV5Out>();
111     copy_tensor_attributes(output_tensor, nodeout);
112
113     // mark shape_status
114     if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
115       nodeout->shape_status(ShapeStatus::NOSHAPE);
116     else
117       nodeout->shape_status(ShapeStatus::VALID);
118
119     nodeout->input(node);
120     nodeout->index(n);
121
122     context->nodefinder()->enroll(outputs[n], nodeout);
123   }
124 }
125
126 } // namespace luci