Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleNonMaxSuppressionV4.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/CircleNonMaxSuppressionV4.h"
18
19 #include <luci/IR/Nodes/CircleNonMaxSuppressionV4.h>
20 #include <luci/IR/Nodes/CircleNonMaxSuppressionV4Out.h>
21
22 #include <loco.h>
23 #include <oops/UserExn.h>
24
25 namespace luci
26 {
27
28 bool CircleNonMaxSuppressionV4GraphBuilder::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() != 5)
34     return false;
35   if (outputs.size() != 2)
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
54   return true;
55 }
56
57 /**
58  * @brief  NonMaxSuppressionV4 Node builder
59  *
60  * @note   Current loco does not provide multiple outputs
61  *         We will create multiple NonMasSuppressionV4Oout nodes to emulate this
62  */
63
64 void CircleNonMaxSuppressionV4GraphBuilder::build(const circle::OperatorT &op,
65                                                   GraphBuilderContext *context) const
66 {
67   assert(context != nullptr);
68
69   auto graph = context->graph();
70
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);
77
78   std::vector<CircleNode *> input_nodes;
79   for (const int32_t input_tensor_index : inputs)
80   {
81     input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
82   }
83
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]);
91
92   assert(outputs.size() == 2);
93   {
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);
98
99     // NOTE We don't set quantization for NonMaxSuppressionV4 itself but to virtual outputs
100   }
101
102   // Create virtual outputs of NonMaxSuppressionV4
103   for (size_t n = 0; n < outputs.size(); ++n)
104   {
105     const circle::TensorT &output_tensor = *tensors[outputs[n]];
106
107     auto *nodeout = graph->nodes()->create<CircleNonMaxSuppressionV4Out>();
108     copy_tensor_attributes(output_tensor, nodeout);
109
110     // mark shape_status
111     if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
112       nodeout->shape_status(ShapeStatus::NOSHAPE);
113     else
114       nodeout->shape_status(ShapeStatus::VALID);
115
116     nodeout->input(node);
117     nodeout->index(n);
118
119     context->nodefinder()->enroll(outputs[n], nodeout);
120   }
121 }
122
123 } // namespace luci