Add yolo5s model on SNPE
[platform/core/api/mediavision.git] / mv_machine_learning / inference / include / DecodeInfo.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 __DECODE_INFO_H__
18 #define __DECODE_INFO_H__
19
20 #include <string>
21 #include <vector>
22 #include <map>
23 #include <memory>
24
25 #include <MetadataType.h>
26 #include <mv_inference_type.h>
27 #include <opencv2/core.hpp>
28 #include "Utils.h"
29
30 namespace mediavision
31 {
32 namespace inference
33 {
34 namespace box
35 {
36 struct AnchorParam {
37         int mode;
38         int numLayers;
39         float minScale;
40         float maxScale;
41         int inputSizeHeight;
42         int inputSizeWidth;
43         float anchorOffsetX;
44         float anchorOffsetY;
45         std::vector<int> strides;
46         std::vector<float> aspectRatios;
47         bool isReduceBoxedInLowestLayer;
48         float interpolatedScaleAspectRatio;
49         bool isFixedAnchorSize;
50         bool isExponentialBoxScale;
51         float xScale;
52         float yScale;
53         float wScale;
54         float hScale;
55
56         // Yolo
57         int offsetAnchors;
58         inference_score_type_e type;
59         std::map<std::string, inference_score_type_e> supportedCellType;
60         std::vector<double> vxScales;
61         std::vector<double> vyScales;
62         unsigned int totalAnchors;
63 };
64
65 struct NMSParam {
66         inference_box_nms_type_e mode; /**< 0: standard */
67         float iouThreshold;
68         std::map<std::string, inference_box_nms_type_e> supportedBoxNmsTypes;
69 };
70
71 struct RotateParam {
72         int startPointIndex;
73         int endPointIndex;
74         cv::Point2f startPoint;
75         cv::Point2f endPoint;
76         float baseAngle;
77 };
78
79 struct RoiOptionParam {
80         int startPointIndex;
81         int endPointIndex;
82         int centerPointIndex;
83         cv::Point2f centerPoint;
84         float shiftX;
85         float shiftY;
86         float scaleX;
87         float scaleY;
88         int mode;
89 };
90
91 struct DecodeInfo {
92         AnchorParam anchorParam;
93         std::vector<cv::Rect2f> anchorBoxes;
94         std::vector<std::vector<cv::Rect2f> > vAnchorBoxes; // (stride) * (H * W * B) * (rect)
95         NMSParam nmsParam;
96         RotateParam rotParam;
97         RoiOptionParam roiOptParam;
98
99         DecodeInfo()
100         {
101                 nmsParam.mode = INFERENCE_BOX_NMS_TYPE_NONE;
102                 nmsParam.iouThreshold = 0.2f;
103                 nmsParam.supportedBoxNmsTypes.insert({ "STANDARD", INFERENCE_BOX_NMS_TYPE_STANDARD });
104
105                 rotParam.startPointIndex = -1;
106                 rotParam.endPointIndex = -1;
107                 rotParam.startPoint = cv::Point2f(0.f, 0.f);
108                 rotParam.endPoint = cv::Point2f(0.f, 0.f);
109                 rotParam.baseAngle = 0.f;
110
111                 roiOptParam.startPointIndex = -1;
112                 roiOptParam.endPointIndex = -1;
113                 roiOptParam.centerPointIndex = -1;
114                 roiOptParam.centerPoint = cv::Point2f(0.f, 0.f);
115                 roiOptParam.shiftX = 0.f;
116                 roiOptParam.shiftY = 0.f;
117                 roiOptParam.scaleX = 1.f;
118                 roiOptParam.scaleY = 1.f;
119                 roiOptParam.mode = -1;
120         }
121
122         ~DecodeInfo() = default;
123
124         std::vector<cv::Rect2f> &GetAnchorBoxAll();
125         bool IsAnchorBoxEmpty();
126         void AddAnchorBox(cv::Rect2f &ahcnor);
127         void ClearAnchorBox();
128
129         // Anchor param
130         int ParseAnchorParam(JsonObject *root);
131         int GenerateAnchor();
132         int GenerateYOLOAnchor();
133         bool IsFixedAnchorSize();
134         bool IsExponentialBoxScale();
135         float GetAnchorXscale();
136         float GetAnchorYscale();
137         float GetAnchorWscale();
138         float GetAnchorHscale();
139         float CalculateScale(float min, float max, int index, int maxStride);
140
141         // Nms param
142         int ParseNms(JsonObject *root);
143         int GetNmsMode();
144         float GetNmsIouThreshold();
145
146         // Rotate param
147         int ParseRotate(JsonObject *root);
148         int GetRotStartPointIndex();
149         int GetRotEndPointIndex();
150         float GetBaseAngle();
151
152         // Roi option param
153         int ParseRoiOption(JsonObject *root);
154         int GetRoiMode();
155         int GetRoiCenterPointIndex();
156         int GetRoiStartPointIndex();
157         int GetRoiEndPointIndex();
158         float GetShiftX();
159         float GetShiftY();
160         float GetScaleX();
161         float GetScaleY();
162 };
163 } /* box */
164 } /* Inference */
165 } /* MediaVision */
166
167 #endif