FLImaging 6.5.16.1
ComputationalGraphRegionProposalNetwork.h
1#pragma once
2
3#if _MSC_VER >= 1900 && defined(_M_X64)
4
5#include "DefinitionsAI.h"
6#include "ComputationalGraph.h"
7#include "Tensor.h"
8#include "BackendBCEWithLogistLoss.h"
9#include "BackendSmoothL1Loss.h"
10#include "BackendConcatenate.h"
11#include "BackendLabel.h"
12
13#include <vector>
14#include <utility>
15#include <functional>
16#include <map>
17
18//#define RPN_TEST
19
20namespace FLImaging
21{
22 namespace AI
23 {
24 template <typename T>
25 class FL_EXPORT CComputationalGraphRegionProposalNetwork : public CComputationalGraph<T>
26 {
27 private:
28 CComputationalGraphRegionProposalNetwork();
29
30 protected:
31 CComputationalGraphRegionProposalNetwork(const CComputationalGraphRegionProposalNetwork<T>& cg);
32
33 public:
34 CComputationalGraphRegionProposalNetwork(const CComputationalBase<T>& cbOperand, const std::vector<int64_t>* pAnchorSizeOrNull = nullptr, const std::vector<float>* pAspectRatioOrNull = nullptr);
35 CComputationalGraphRegionProposalNetwork(const std::initializer_list<const CComputationalBase<T>*>& cbOperands, const std::vector<int64_t>* pAnchorSizeOrNull = nullptr, const std::vector<float>* pAspectRatioOrNull = nullptr);
36 CComputationalGraphRegionProposalNetwork(const std::vector<const CComputationalBase<T>*>& vctOperands, const std::vector<int64_t>* pAnchorSizeOrNull = nullptr, const std::vector<float>* pAspectRatioOrNull = nullptr);
37
38 virtual ~CComputationalGraphRegionProposalNetwork();
39
40 virtual CTensor<T>& Forward() override;
41 virtual CTensor<T>* Backward() override;
42 virtual CComputationalBase<T>* Clone() const override;
43
44 virtual const std::vector<int64_t>& GetEstimatedShape(bool bRecursive = true) const override;
45
46 virtual const CResult SetForegroundIoUThreshold(T tThreshold);
47 virtual T GetForegroundIoUThreshold() const;
48
49 virtual const CResult SetBackgroundIoUThreshold(T tThreshold);
50 virtual T GetBackgroundIoUThreshold() const;
51
52 virtual const CResult SetBatchSizePerImage(int64_t i64BatchSizePerImage);
53 virtual int64_t GetBatchSizePerImage() const;
54
55 virtual const CResult SetPositiveFraction(T tPositiveFraction);
56 virtual T GetPositiveFraction() const;
57
58 virtual const CResult SetPreNMSTopN(int64_t i64PreNMSTopN);
59 virtual T GetPreNMSTopN() const;
60 virtual const CResult SetPostNMSTopN(int64_t i64PostNMSTopN);
61 virtual T GetPostNMSTopN() const;
62
63 virtual const CResult SetNMSThreshold(T tNMSThreshold);
64 virtual T GetNMSThreshold() const;
65 virtual const CResult SetScoreThreshold(T tScoreThreshold);
66 virtual T GetScoreThreshold() const;
67
68 virtual const CResult SetAreaThreshold(T tAreaThreshold);
69 virtual T GetAreaThreshold() const;
70
71 virtual const CResult SetInputSize(const std::vector<int64_t>& vctInputSize);
72 virtual const std::vector<int64_t>& GetInputSize() const;
73
74 virtual const CResult SetAnchorSize(const std::vector<int64_t>& vctAnchorSize);
75 virtual const std::vector<int64_t>& GetAnchorSize() const;
76
77 virtual const CResult SetAspectRatio(const std::vector<float>& vctAspectRatio);
78 virtual const std::vector<float>& GetAspectRatio() const;
79
80 virtual const CResult EnableRevertAngle(bool bRevertAngle);
81 virtual bool IsRevertAngleEnable() const;
82
83 virtual const CResult EnableClipBox(bool bClipBox);
84 virtual bool IsClipBoxEnable() const;
85
86 virtual const std::vector<CTensor<T>>& GetTargetBoxes() const;
87 virtual const std::vector<CTensor<T>>& GetTargetLabels() const;
88 virtual const CResult SetAddGradientCount(int64_t i64AddGradientCount) override;
89
90 virtual T GetBoxLoss() const;
91 virtual T GetScoreLoss() const;
92
93 #ifdef RPN_TEST
94 virtual CTensor<T>& GetFilterProposalResultBoxes();
95 #endif
96 virtual CTensor<T>& GetFilterProposalResultScores();
97 #ifdef RPN_TEST
98 virtual const CResult SetTargetBoxes(const std::vector<CTensor<T>>& tsrTraget);
99 virtual const CResult SetObjectnessTsr(const CTensor<T>* pTsrObjectness);
100 virtual const CResult SetProposalTsr(const CTensor<T>* pTsrProposal);
101 #endif
102 DeclareGetClassType();
103 SupportToDuplicateObjectWithoutCreateNewObject(CComputationalGraphRegionProposalNetwork, *this);
104
105 public:
106 virtual const CResult PrintNodeParamInfo() const;
107 virtual const CResult GetBinaryData(Base::CFLData& fldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
108 virtual const CResult GetBinaryData(Base::CFLData* pFldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
109
110 virtual const CResult SetBinaryData(const Base::CFLData& fldBinary, int64_t* pI64Offset = nullptr) override;
111 virtual const CResult SetBinaryData(const Base::CFLData* pFldBinary, int64_t* pI64Offset = nullptr) override;
112
113 virtual int64_t GetRequiredDedicatedMemory(bool bTraining = false, bool bRecursively = true, int64_t i64BatchSize = 1) const override;
114
115 protected:
116 virtual const CResult RPNConstructor(const std::vector<const CComputationalBase<T>*>& vctOperands);
117 virtual const CResult GenerateAnchors();
118 virtual const CResult ForwardOfAnchor();
119 virtual const CResult GenerateHead(const std::vector<const CComputationalBase<T>*>& vctOperands);
120 virtual const CResult ConcatBoxPredictionLayers(const std::vector<CComputationalBase<T>*>& vctObjectness, const std::vector<CComputationalBase<T>*>& vctPredictionBBoxDeltas, CTensor<T>* pTsrResultObjectness, CTensor<T>* pTsrResultBoxRegression);
121
122 virtual const CResult ConcatBoxPredictionLayersBackward(const CTensor<T>* pTsrConcatObjectnessDy, const CTensor<T>* pTsrConcatBBoxDeltasDy, std::vector<CComputationalBase<T>*>& vctObjectness, std::vector<CComputationalBase<T>*>& vctPredictionBBoxDeltas);
123
124 virtual const CResult FilterProposals(const CTensor<T>* pTsrProposals, const CTensor<T>* pTsrObjectness, const std::vector<std::pair<int64_t, int64_t>>& vctImageSizes, const std::vector<int64_t>& vctAnchors, CTensor<T>* pTsrResultBoxes, CTensor<T>* pTsrResultScores, CTensor<T>* pTsrBuffer);
125 virtual const CResult AssignTargetsToAnchors(const CTensor<T>* pTsrAnchor, const std::vector<CTensor<T>>& vctTsrTargets, CTensor<T>* pTsrResultMatchedLabel, CTensor<T>* pTsrResultAnchorMatchedTarget);
126 virtual const CResult ComputeLoss(const CTensor<T>* pTsrObjenctness, const CTensor<T>* pTsrPredBBoxDeltas, const CTensor<T>* pTsrLabel, const CTensor<T>* pTsrRegressionTargets);
127 virtual const CResult ComputeLossBackward(CTensor<T>* pTsrObjectness, CTensor<T>* pTsrPredBBoxDeltas, CTensor<T>* pTsrObjectnessDx, CTensor<T>* pTsrPredBBoxDeltasDx);
128
129 virtual const CResult IntializeMemberTensor();
130 virtual std::vector<int64_t> GetAnchorShape() const;
131
132 virtual const CResult CreateTargetBuffer();
133
134 virtual const CResult GetFeaturesVector(std::vector<CComputationalBase<T>*>& vctFeatures);
135
136 #ifdef RPN_TEST
137 public:
138 CTensor<T>* m_pTsrObjecntessPy;
139 CTensor<T>* m_pTsrProposalPy;
140 #else
141 protected:
142 #endif
143 T m_tForegroundIoUThreshold;
144 T m_tBackgroundIoUThreshold;
145 int64_t m_i64BatchSizePerImage;
146 T m_tPositiveFraction;
147 int64_t m_i64PreNMSTopN;
148 int64_t m_i64PostNMSTopN;
149
150 T m_tNMSThreshold;
151 T m_tScoreThreshold;
152 T m_tAreaThreshold;
153 bool m_bRevertAngle;
154 bool m_bClipBox;
155
156 std::vector<int64_t>& m_vctImageSize;
157 std::vector<int64_t>& m_vctAnchorSize;
158 std::vector<float>& m_vctAspectRatio;
159
160 std::vector<CTensor<T>>& m_vctTsrTargetBoxes;
161 std::vector<CTensor<T>>& m_vctTsrTargetLabels;
162
163 CTensor<T> m_tsrBaseAnchor;
164 CTensor<T> m_tsrAnchor;
165 CTensor<T> m_tsrConcatResultObjectness;
166 CTensor<T> m_tsrConcatResultBoxDeltas;
167 CTensor<T> m_tsrDecodeResultProposal;
168 CTensor<T> m_tsrFilterProposalResultScore;
169 #ifdef RPN_TEST
170 CTensor<T> m_tsrFilterProposalResultBoxes;
171 #endif
172 CTensor<T> m_tsrBuffer;
173 CTensor<T> m_tsrAssignTargetResultLabels;
174 CTensor<T> m_tsrAssignTargetResultGTBoxes;
175 CTensor<T> m_tsrEncodeResultRegressionTarget;
176
177 CBackendBCEWithLogistLoss<T> m_backendBCEWithLogistLoss;
178 CBackendSmoothL1Loss<T> m_backendSmoothL1Loss;
179 CBackendConcatenate<T> m_backendConcatenate;
180 CBackendLabel<T> m_backendLabel;
181
182 std::vector<uint32_t>& m_vctPositiveIndexes;
183 std::vector<uint32_t>& m_vctPositiveNegativeIndexes;
184
185 CTensor<T> m_tsrObjectnessSelected;
186 CTensor<T> m_tsrPredBBoxDeltasSelected;
187 CTensor<T> m_tsrLabelSelected;
188 CTensor<T> m_tsrTargetRegressionSelected;
189 CTensor<T> m_tsrObjectnessLoss;
190 CTensor<T> m_tsrBoxLoss;
191
192 CTensor<T> m_tsrTargetBuffer;
193
194 T m_tBoxLoss;
195 T m_tScoreLoss;
196
197 std::vector<CTensor<T>>& m_vctObjectnessPermute;
198 std::vector<CTensor<T>>& m_vctBBoxDeltasPermute;
199
200 public:
201 DeclareGetSignletonObject(CComputationalGraphRegionProposalNetwork);
202 };
203
204 #define CCGFRegionProposalNetwork(...) (*(new CComputationalGraphRegionProposalNetwork<float>(__VA_ARGS__)))
205 #define CCGDRegionProposalNetwork(...) (*(new CComputationalGraphRegionProposalNetwork<double>(__VA_ARGS__)))
206 #define CCGTRegionProposalNetwork(T, ...) (*(new CComputationalGraphRegionProposalNetwork<T>(__VA_ARGS__)))
207
208 }
209}
210
211#endif