FLImaging 6.5.16.1
ComputationalGraphObjectDetection.h
1#pragma once
2
3#if _MSC_VER >= 1900 && defined(_M_X64)
4
5#include "DefinitionsAI.h"
6#include "ComputationalGraph.h"
7#include <vector>
8#include <utility>
9#include <functional>
10
11namespace FLImaging
12{
13 namespace AI
14 {
15 template<typename T>
16 class CComputationalGraphObjectDetectionProcessor;
17
18 template<typename T>
19 class CValidatorForObjectDetection;
20
21 template <typename T>
22 class CObjectBox
23 {
24 public:
25 int64_t i64ClassId;
26 T tCenterX;
27 T tCenterY;
28 T tWidth;
29 T tHeight;
30 T tAngle;
31
32 public:
33 CObjectBox();
34 };
35
36 template <typename T>
37 class FL_EXPORT CComputationalGraphObjectDetection : public CComputationalGraph<T>
38 {
39 public:
40 class CIoUInfo
41 {
42 public:
43 CIoUInfo();
44 public:
45 T tIoU, tGIoU, tDIoU, tCIoU;
46 Base::TRect<T> trDxIoU;
47 Base::TRect<T> trDxGIoU;
48 };
49
50 class FL_EXPORT CObjectDetectionDLTrainResult
51 {
52 public:
53 CObjectDetectionDLTrainResult();
54 public:
55 T tTotalIoU, tTotalGIoU, tTotalDIoU, tTotalCIoU;
56 T tIoULoss, tGIoULoss, tDIoULoss, tCIoULoss;
57 T tTotalLoss, tTotalIoULoss, tTotalGIoULoss, tTotalDIoULoss, tTotalCIoULoss, tTotalClassificationLoss;
58 T tCost;
59 int64_t i64Count;
60 int64_t i64ClassCount;
61 int64_t i64ReWrittenBbox;
62 };
63
64 private:
65 CComputationalGraphObjectDetection();
66
67 protected:
68 CComputationalGraphObjectDetection(const CComputationalGraphObjectDetection<T>& cg);
69
70 public:
71 CComputationalGraphObjectDetection(const CComputationalBase<T>& cbOperand);
72 virtual ~CComputationalGraphObjectDetection();
73
74 virtual CTensor<T>& Forward() override;
75 virtual CTensor<T>* Backward() override;
76 virtual CComputationalBase<T>* Clone() const override;
77
78 virtual const std::vector<int64_t>& GetEstimatedShape(bool bRecursive = true) const override;
79
80 virtual const CResult SetInputSize(const std::vector<int64_t>& vctInputSize);
81 virtual const std::vector<int64_t>& GetInputSize() const;
82 virtual const CResult SetLearningAnchorMask(const std::vector<int64_t>& vctAnchorMask);
83 virtual const std::vector<int64_t>& GetLearningAnchorMask() const;
84 virtual const CResult SetLearningAnchor(const std::vector<std::pair<int64_t, int64_t>>& vctAnchor);
85 virtual const std::vector<std::pair<int64_t, int64_t>>& GetLearningAnchor() const;
86 virtual const CResult SetNumerOfClasses(int64_t i64NumerOfClasses = 20);
87 virtual int64_t GetNumerOfClasses() const;
88 virtual const CResult SetIgnoreIoUThreshold(T tIgnoreIoUThreshold = .5);
89 virtual T GetIgnoreIoUThreshold() const;
90 virtual const CResult SetTruthThresh(T tTruthThresh = 1.);
91 virtual T GetTruthThresh() const;
92 virtual const CResult SetScaleRatio(T tScaleRatio = 1.);
93 virtual T GetScaleRatio() const;
94 virtual const CResult SetLearningIoUThreshold(T tIoUThreshold = .213);
95 virtual T GetLearningIoUThreshold() const;
96 virtual const CResult SetClassNormalizer(T tClsNormalizer = 1.);
97 virtual T GetClassNormalizer() const;
98 virtual const CResult SetIoUNormalizer(T tIoUNormalizer = .75);
99 virtual T GetIoUNormalizer() const;
100 virtual const CResult SetObjectNormalizer(T tObjectNormalizer = 1.);
101 virtual T GetObjectNormalizer() const;
102 virtual const CResult SetIoULoss(EIoU eIoULoss = EIoU_CIoU);
103 virtual EIoU GetIoULoss() const;
104 virtual const CResult SetMaxDelta(T tMaxDelta = 10.);
105 virtual T GetMaxDelta() const;
106
107 virtual const CResult SetMaxBox(int64_t i64MaxBox = 200);
108 virtual int64_t GetMaxBox() const;
109 virtual const CResult SetLabelSmoothEpsilon(T tEpsilon = 0.);
110 virtual T GetLabelSmoothEpsilon() const;
111 virtual const CResult SetObjectSmooth(bool bObjectSmooth = false);
112 virtual bool GetObjectSmooth() const;
113 virtual const CResult SetNewCoordinate(bool bNewCoord = false);
114 virtual bool GetNewCoordinate() const;
115 virtual const CResult SetLearningIoUThresholdType(EIoU eIoUThresh = EIoU_CIoU);
116 virtual EIoU GetLearningIoUThresholdType() const;
117 virtual const CResult SetFocalLoss(bool bFocalLoss = false);
118 virtual bool GetFocalLoss() const;
119
120 // 실제 유효한 class 개수가 label class num 과 다른 경우를 위해 존재
121 virtual const CResult SetRealClassIdToValidClassId(const std::vector<int64_t>& vctRealClassId);
122 virtual const CResult GetRealClassIdToValidClassId(std::vector<int64_t>& vctRealClassId);
123
124 virtual const CResult GetBinaryData(Base::CFLData& fldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
125 virtual const CResult GetBinaryData(Base::CFLData* pFldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
126
127 virtual const CResult SetBinaryData(const Base::CFLData& fldBinary, int64_t* pI64Offset = nullptr) override;
128 virtual const CResult SetBinaryData(const Base::CFLData* pFldBinary, int64_t* pI64Offset = nullptr) override;
129
130 //virtual int64_t GetRequiredTemporaryMemory(bool bTraining = false, bool bRecursively = true, int64_t i64BatchSize = 1, int64_t i64MemoryIndex = 0) const override;
131
132
133 DeclareGetClassType();
134 SupportToDuplicateObjectWithoutCreateNewObject(CComputationalGraphObjectDetection, *this);
135
136 public:
137 virtual const CResult PrintNodeParamInfo() const;
138
139 protected:
140 CObjectBox<T> GetObjectDetectionBox(T* x, int64_t i64Mask, int64_t i64Index, int64_t w, int64_t h);
141 static T GetOverlapSize(T tVal1, T tRange1, T tVal2, T tRange2);
142 static T GetIoU(const CObjectBox<T>& ybBox1, const CObjectBox<T>& ybBox2);
143 static T GetGIoU(const CObjectBox<T>& ybBox1, const CObjectBox<T>& ybBox2);
144 static T GetDIoU(const CObjectBox<T>& ybBox1, const CObjectBox<T>& ybBox2, T tBeta = (T)0.6);
145 static T GetCIoU(const CObjectBox<T>& ybBox1, const CObjectBox<T>& ybBox2);
146 bool CompareObjectDetectionClass(T* pTOutput, int64_t i64ClassIndex, T tObjectness, int64_t i64ClassId);
147 const CResult GetDeltaObjectDetectionClass(int64_t i64Index, int64_t i64ClassIndex, T* pAvgCat = nullptr);
148 CIoUInfo GetDeltaObjectDetectionBox(CObjectBox<T>& ybTruth, int64_t i64Mask, int64_t i64Index, int64_t w, int64_t h);
149 const CResult GetDxIoU(CObjectBox<T>& ybPred, CObjectBox<T>& ybTruth, Base::TRect<T>& trReturn);
150 const CResult CalcAverageObjectDetectionDeltas(int64_t class_index, int64_t box_index);
151 const CResult CreateTruthLabel();
152
153 const T GetLoss();
154 const CObjectDetectionDLTrainResult& GetTrainResult() const;
155
156 protected:
157 std::vector<int64_t>& m_vctRealClassIdToValidClassId;
158 std::vector<int64_t>& m_vctInputSize;
159 std::vector<int64_t>& m_vctCounterPerClass;
160 std::vector<int64_t>& m_vctAnchorMask;
161 std::vector<std::pair<int64_t, int64_t> >& m_vctAnchors;
162
163 int64_t m_i64NumerOfClasses;
164 int64_t m_i64AnchorNum;
165 T m_tIgnoreIoUThreshold;
166 T m_tTruthThresh;
167 T m_tScaleRatio;
168 T m_tIoUThreshold;
169 T m_tClassNormalizer;
170 T m_tIoUNormalizer;
171 T m_tObjectNormalizer;
172 EIoU m_eIoULoss;
173 T m_tMaxDelta;
174
175 int64_t m_i64MaxBox;
176 T m_tLabelSmoothEpsilon;
177 bool m_bObjectSmooth;
178 bool m_bNewCoords;
179 EIoU m_eIoUThresh;
180 bool m_bFocalLoss;
181
182
183 protected:
184 T* m_pTTruth;
185 int64_t* m_pI64ClassIds;
186 int64_t m_i64ClassIdsCount;
187 T* m_pTClassMultiplier;
188
189 int64_t m_i64TruthBufferSize;
190 int64_t m_i64Stride;
191 T m_tConfidenceThresh;
192
193 T m_tCurIouNormalizer;
194
195 CObjectDetectionDLTrainResult m_odResult;
196
197 template<typename T> friend class CComputationalGraphObjectDetectionProcessor;
198 template<typename T> friend class CValidatorForObjectDetection;
199 public:
200 DeclareGetSignletonObject(CComputationalGraphObjectDetection);
201 };
202
203 #define CCGFObjectDetection(...) (*(new CComputationalGraphObjectDetection<float>(__VA_ARGS__)))
204 #define CCGDObjectDetection(...) (*(new CComputationalGraphObjectDetection<double>(__VA_ARGS__)))
205 #define CCGTObjectDetection(T, ...) (*(new CComputationalGraphObjectDetection<T>(__VA_ARGS__)))
206
207 }
208}
209
210#endif