92f4b85b0eb4f81f9425546525666af085467b0d
[platform/core/multimedia/inference-engine-interface.git] / tools / include / OutputMetadata.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 __INFERENCE_ENGINE_OUTPUTMETADATA_H__
18 #define __INFERENCE_ENGINE_OUTPUTMETADATA_H__
19
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 #include "inference_engine_error.h"
26 #include "inference_engine_private_type.h"
27 #include "inference_engine_type.h"
28 #include <dlog.h>
29
30 #include "OutputMetadataTypes.h"
31 #include <json-glib/json-glib.h>
32
33 /**
34  * @file OutputMetadata.h
35  * @brief This file contains the metadata class definition which
36  *        provides metadata of a model.
37  */
38
39 namespace InferenceEngineInterface
40 {
41 namespace Cltuner
42 {
43         class DimInfo
44         {
45         private:
46                 std::vector<int> dims;
47         public:
48                 std::vector<int> GetValidIndexAll() const;
49                 void SetValidIndex(int index);
50         };
51
52         class DeQuantization
53         {
54         private:
55                 double scale;
56                 double zeropoint;
57         public:
58                 DeQuantization(double s, double z) : scale(s), zeropoint(z) {};
59                 ~DeQuantization() = default;
60
61                 double GetScale() { return scale; }
62                 double GetZeroPoint() { return zeropoint; }
63         };
64
65         class ScoreInfo
66         {
67         private:
68                 std::string name;
69                 DimInfo dimInfo;
70                 double threshold;
71                 inference_score_type_e type;
72                 int topNumber;
73                 std::shared_ptr<DeQuantization> deQuantization;
74
75                 std::map<std::string, inference_score_type_e> supportedScoreTypes;
76
77         public:
78                 ScoreInfo();
79                 ~ScoreInfo() = default;
80
81                 std::string GetName() { return name; }
82                 DimInfo GetDimInfo() { return dimInfo; }
83                 double GetThresHold() { return threshold; }
84                 inference_score_type_e GetType() { return type; }
85                 int GetTopNumber() { return topNumber; }
86                 std::shared_ptr<DeQuantization> GetDeQuant() { return deQuantization; }
87
88                 int ParseScore(JsonObject *root);
89         };
90
91         class BoxInfo
92         {
93         private:
94                 std::string name;
95                 DimInfo dimInfo;
96                 inference_box_type_e type;
97                 std::vector<int> order;
98                 inference_box_coordinate_type_e coordinate;
99                 inference_box_decoding_type_e decodingType;
100
101                 std::map<std::string, inference_box_type_e> supportedBoxTypes;
102                 std::map<std::string, inference_box_coordinate_type_e> supportedBoxCoordinateTypes;
103                 std::map<std::string, inference_box_decoding_type_e> supportedBoxDecodingTypes;
104
105         public:
106                 BoxInfo();
107                 ~BoxInfo() = default;
108
109                 std::string GetName() { return name; }
110                 DimInfo GetDimInfo() { return dimInfo; }
111                 inference_box_type_e GetType() { return type; }
112                 inference_box_decoding_type_e GetDecodingType() { return decodingType; }
113                 std::vector<int> GetOrder() { return order; }
114                 int GetCoordinate() { return coordinate; }
115
116                 int ParseBox(JsonObject *root);
117         };
118
119         class Label
120         {
121         private:
122                 std::string name;
123                 DimInfo dimInfo;
124                 inference_box_decoding_type_e decodingType;
125
126         public:
127                 Label() = default;
128                 ~Label() = default;
129                 std::string GetName() { return name; }
130                 DimInfo GetDimInfo() { return dimInfo; }
131
132                 int ParseLabel(JsonObject *root);
133         };
134
135         class Number
136         {
137         private:
138                 std::string name;
139                 DimInfo dimInfo;
140
141         public:
142                 Number() = default;
143                 ~Number() = default;
144                 std::string GetName() { return name; }
145                 DimInfo GetDimInfo() { return dimInfo; }
146
147                 int ParseNumber(JsonObject *root);
148         };
149
150         class Landmark
151         {
152         public:
153                 class DecodeInfo {
154                 public:
155                         class HeatMapInfo {
156                         public:
157                                 int wIdx;
158                                 int hIdx;
159                                 int cIdx;
160                                 inference_tensor_shape_type_e shapeType;
161                                 float nmsRadius;
162                                 HeatMapInfo() = default;
163                                 ~HeatMapInfo() = default;
164                         };
165                         HeatMapInfo heatMap;
166                         DecodeInfo() = default;
167                         ~DecodeInfo() = default;
168                 };
169         private:
170                 std::string name;
171                 DimInfo dimInfo;
172                 inference_landmark_type_e type;
173                 int offset;
174                 inference_landmark_coorindate_type_e coordinate;
175                 inference_landmark_decoding_type_e decodingType;
176                 DecodeInfo decodingInfo;
177
178                 std::map<std::string, inference_landmark_type_e> supportedLandmarkTypes;
179                 std::map<std::string, inference_landmark_coorindate_type_e> supportedLandmarkCoordinateTypes;
180                 std::map<std::string, inference_landmark_decoding_type_e> supportedLandmarkDecodingTypes;
181
182         public:
183                 Landmark();
184                 ~Landmark() = default;
185                 std::string GetName() { return name; }
186                 DimInfo GetDimInfo() { return dimInfo; }
187                 inference_landmark_type_e GetType();
188                 int GetOffset();
189                 inference_landmark_coorindate_type_e GetCoordinate();
190                 inference_landmark_decoding_type_e GetDecodingType();
191                 DecodeInfo& GetDecodingInfo();
192
193                 int ParseLandmark(JsonObject *root);
194         };
195
196         class OffsetVec
197         {
198         private:
199                 std::string name;
200                 DimInfo dimInfo;
201                 int shapeType;
202         public:
203                 OffsetVec() = default;
204                 ~OffsetVec() = default;
205                 std::string GetName() { return name; }
206                 DimInfo GetDimInfo() { return dimInfo; }
207                 int GetShapeType() { return shapeType; }
208
209                 int ParseOffset(JsonObject *root);
210         };
211
212         class DispVec
213         {
214         private:
215                 std::string name;
216                 DimInfo dimInfo;
217                 inference_displacement_type_e type;
218                 int shapeType;
219                 std::map<std::string, inference_displacement_type_e> supportedDispTypes;
220         public:
221                 DispVec();
222                 ~DispVec() = default;
223                 std::string GetName() { return name; }
224                 DimInfo GetDimInfo() { return dimInfo; }
225                 inference_displacement_type_e GetType() { return type; }
226                 int GetShapeType() { return shapeType; }
227
228                 int ParseDisplacement(JsonObject *root);
229         };
230
231         class Edge
232         {
233         private:
234                 std::vector<std::pair<int, int>> edges;
235         public:
236                 Edge() = default;
237                 ~Edge() = default;
238                 int ParseEdge(JsonObject *root);
239                 std::vector<std::pair<int, int>>& GetEdgesAll();
240         };
241
242         class OutputMetadata
243         {
244         private:
245                 bool parsed;
246                 ScoreInfo score;
247                 BoxInfo box;
248                 Label label;
249                 Number number;
250                 Landmark landmark;
251                 OffsetVec offsetVec;
252                 std::vector<DispVec> dispVecs;
253                 Edge edgeMap;
254
255                 int ParseScore(JsonObject *root);
256                 int ParseBox(JsonObject *root);
257                 int ParseLabel(JsonObject *root);
258                 int ParseNumber(JsonObject *root);
259                 int ParseLandmark(JsonObject *root);
260                 int ParseLandmarkDecodeInfo(JsonObject *root);
261                 int ParseOffset(JsonObject *root);
262                 int ParseDisplacement(JsonObject *root);
263                 int ParseEdgeMap(JsonObject *root);
264
265         public:
266                 static std::map<std::string, inference_tensor_shape_type_e> supportedTensorShapes;
267                 /**
268                  * @brief   Creates an OutputMetadata class instance.
269                  *
270                  * @since_tizen 6.5
271                  */
272                 OutputMetadata();
273
274                 /**
275                  * @brief   Destroys an OutputMetadata class instance including
276                  *          its all resources.
277                  *
278                  * @since_tizen 6.5
279                  */
280                 ~OutputMetadata() = default;
281
282                 /** @brief Parses an OutputMetadata
283                  *
284                  * @since_tizen 6.5
285                  */
286                 int Parse(JsonObject *root);
287
288                 bool IsParsed();
289                 ScoreInfo& GetScore();
290                 BoxInfo& GetBox();
291                 Label& GetLabel();
292                 Number& GetNumber();
293                 Landmark& GetLandmark();
294                 OffsetVec& GetOffset();
295                 std::vector<DispVec>& GetDispVecAll();
296
297                 template <typename T>
298                 static T GetSupportedType(JsonObject* root, std::string typeName,
299                                                                 std::map<std::string, T>& supportedTypes);
300         };
301
302 } /* Inference */
303 } /* MediaVision */
304
305 #endif /* __INFERENCE_ENGINE_OUTPUTMETADATA_H__ */