Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / DetectionPostProcessLayer.h
1 /*
2  * Copyright (c) 2021 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 #ifndef __ONERT_BACKEND_CPU_OPS_DPP_H__
18 #define __ONERT_BACKEND_CPU_OPS_DPP_H__
19
20 #include <exec/IFunction.h>
21
22 #include "OperationUtils.h"
23
24 namespace onert
25 {
26 namespace backend
27 {
28 namespace cpu
29 {
30 namespace ops
31 {
32
33 class DetectionPostProcessLayer : public ::onert::exec::IFunction
34 {
35 public:
36   struct CornerBox
37   {
38     float y1, x1;
39     float y2, x2;
40   };
41
42   struct CenterSizeBox
43   {
44     float y, x;
45     float h, w;
46   };
47
48   struct DetectionPostProcessParameters
49   {
50     const IPortableTensor *boxes_input;
51     const IPortableTensor *scores_input;
52     const IPortableTensor *anchors_input;
53     IPortableTensor *box_coords_output;
54     IPortableTensor *box_classes_output;
55     IPortableTensor *box_scores_output;
56     IPortableTensor *num_selections_output;
57     std::vector<int32_t> boxes_descr;
58     std::vector<int32_t> scrores_descr;
59
60     uint32_t max_detections;
61     float score_threshold;
62     float iou_threshold; // intersection-over-union
63     uint32_t max_boxes_per_class;
64     bool center_box_format = false;
65     int32_t num_classes;
66     int32_t max_classes_per_detection;
67     CenterSizeBox scales;
68   };
69
70   enum SelectionFormat
71   {
72     BOX_INDEX = 1,
73     CLASS_INDEX = 0
74   };
75
76   struct Allocations
77   {
78     int *selections_buffer = nullptr;
79     // TODO move all dynamic allocations here, and into configure phase
80   };
81
82   DetectionPostProcessLayer() : _parameters{}
83   {
84     // DO NOTHING
85   }
86
87   virtual ~DetectionPostProcessLayer();
88
89 public:
90   void configure(DetectionPostProcessParameters parameters);
91
92   void run() override;
93
94 private:
95   DetectionPostProcessParameters _parameters;
96
97   Allocations _allocations;
98 };
99
100 } // namespace ops
101 } // namespace cpu
102 } // namespace backend
103 } // namespace onert
104
105 #endif // __ONERT_BACKEND_CPU_OPS_DPP_H__