FLImaging 6.5.16.1
ComputationalGraphMaskRCNN.h
1#pragma once
2
3#if _MSC_VER >= 1900 && defined(_M_X64)
4
5#include "DefinitionsAI.h"
6#include "ComputationalGraph.h"
7#include "ComputationalGraphPlaceholder.h"
8#include "Tensor.h"
9#include "BackendSoftmax.h"
10#include "BackendSoftmaxWithCrossEntropy.h"
11#include "BackendBCEWithLogistLoss.h"
12#include "BackendSmoothL1Loss.h"
13#include "BackendMultiScaleRoIAlign.h"
14#include "BackendLabel.h"
15#include "Tensor.h"
16#include <vector>
17#include <utility>
18#include <functional>
19
20//#define MASKRCNN_TEST
21
22namespace FLImaging
23{
24 namespace AI
25 {
26 template <typename T>
27 class CInternalMaskRCNNParameters;
28
29 template <typename T>
30 class FL_EXPORT CInstanceSegmentationResult
31 {
32 public:
33 CInstanceSegmentationResult();
34 ~CInstanceSegmentationResult();
35 void Clear();
36 public:
37 std::vector<CTensor<T>>& vctTsrBoxes;
38 std::vector<CTensor<T>>& vctTsrLabels;
39 std::vector<CTensor<T>>& vctTsrScores;
40 std::vector<CTensor<T>>& vctTsrMasks;
41 };
42
43 template <typename T>
44 class FL_EXPORT CComputationalGraphMaskRCNN : public CComputationalGraph<T>
45 {
46 private:
47 CComputationalGraphMaskRCNN();
48
49 protected:
50 CComputationalGraphMaskRCNN(const CComputationalGraphMaskRCNN<T>& cg);
51
52 public:
53 CComputationalGraphMaskRCNN(const CComputationalBase<T>& cgFeature, const CComputationalBase<T>& cbProposals, int64_t i64ClassCount);
54 CComputationalGraphMaskRCNN(const std::initializer_list<const CComputationalBase<T>*>& liFeatures, const CComputationalBase<T>& cbProposals, int64_t i64ClassCount);
55 CComputationalGraphMaskRCNN(const std::vector<const CComputationalBase<T>*>& vctFeatures, const CComputationalBase<T>& cbProposals, int64_t i64ClassCount);
56 CComputationalGraphMaskRCNN(const std::vector<const CComputationalBase<T>*>& vctHeadIntermediateProposalMaskGraph);
57
58 virtual ~CComputationalGraphMaskRCNN();
59
60 virtual CTensor<T>& Forward() override;
61 virtual CTensor<T>* Backward() override;
62 virtual CComputationalBase<T>* Clone() const override;
63
64 virtual const std::vector<int64_t>& GetEstimatedShape(bool bRecursive = true) const override;
65
66 virtual const CResult SetForegroundIoUThreshold(T tForegroundIoUThreshold);
67 virtual T GetForegroundIoUThreshold() const;
68
69 virtual const CResult SetBackgroundIoUThreshold(T tBackgroundIoUThreshold);
70 virtual T GetBackgroundIoUThreshold() const;
71
72 virtual const CResult SetImageWidth(int64_t i64ImageWidth);
73 virtual int64_t GetImageWidth() const;
74
75 virtual const CResult SetImageHeight(int64_t i64ImageHeight);
76 virtual int64_t GetImageHeight() const;
77
78 virtual const CResult SetNMSType(ENonMaximumSuppressionType eNMS);
79 virtual int64_t GetNMSType() const;
80
81 virtual const CResult SetNMSIoUThreshold(T tNMSIoUThreshold);
82 virtual T GetNMSIoUThreshold() const;
83
84 virtual const CResult SetObjectnessThreshold(T tObjectnessThreshold);
85 virtual T GetObjectnessThreshold() const;
86
87 virtual const CResult SetDetectionPerBatch(int64_t i64DetectionPerBatch);
88 virtual int64_t GetDetectionPerBatch() const;
89
90 virtual const CResult SetSamplerResultBoxCount(int64_t i64SamplerResultBoxCount);
91 virtual int64_t GetSamplerResultBoxCount() const;
92
93 virtual const CResult SetSamplerPositiveFraction(T tSamplerPositiveFraction);
94 virtual T GetSamplerPositiveFraction() const;
95
96 virtual T GetFasterRCNNBoxLoss() const;
97 virtual T GetFasterRCNNScoreLoss() const;
98 virtual T GetRPNBoxLoss() const;
99 virtual T GetRPNScoreLoss() const;
100 virtual T GetMaskLoss() const;
101 virtual T GetTotalLoss() const;
102
103 virtual const CInstanceSegmentationResult<T>* GetMaskRCNNInferenceResult() const;
104
105 static CComputationalBase<T>* MakeMaskRCNNHead(const std::vector<const CComputationalBase<T>*>& vctFetures, const CComputationalBase<T>& cbProposals, int64_t i64OutputSize = 7);
106 static CComputationalBase<T>* MakeMaskRCNNIntermediate(const std::vector<const CComputationalBase<T>*>& vctFetures, const CComputationalBase<T>& cgMaskRCNNHead, const CComputationalBase<T>& cbClassLogist, const CComputationalBase<T>& cbBoxRegression, const CComputationalBase<T>& cbProposals, int64_t i64OutputSize = 14);
107
108 DeclareGetClassType();
109 SupportToDuplicateObjectWithoutCreateNewObject(CComputationalGraphMaskRCNN, *this);
110 public:
111 virtual const CResult PrintNodeParamInfo() const;
112
113 virtual const CResult GetBinaryData(Base::CFLData& fldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
114 virtual const CResult GetBinaryData(Base::CFLData* pFldBinary, bool bSuperClass = false, int32_t i32Version = -1, bool bDumpMode = false) const override;
115
116 virtual const CResult SetBinaryData(const Base::CFLData& fldBinary, int64_t* pI64Offset = nullptr) override;
117 virtual const CResult SetBinaryData(const Base::CFLData* pFldBinary, int64_t* pI64Offset = nullptr) override;
118
119 virtual int64_t GetRequiredDedicatedMemory(bool bTraining = false, bool bRecursively = true, int64_t i64BatchSize = 1) const override;
120 virtual int64_t GetOperandIndexForNextBatchSize() const override;
121 protected:
122 virtual const CResult GenerateGraph(const std::vector<const CComputationalBase<T>*>& vctFeatures, const CComputationalBase<T>& cbProposals, int64_t i64ClassCount);
123 virtual const CResult MaskRCNNLoss(const CTensor<T>* pTsrMaskLogist, const std::vector<CTensor<T>>& vctTsrMaskProposal, const std::vector<CTensor<T>>& vctTsrGTMasks, const std::vector<CTensor<T>>& vctTsrGTLabels, const std::vector<std::vector<uint32_t>>& vctMatchedIndexes, CTensor<T>& tsrResultMaskLoss);
124 virtual const CResult MaskRCNNInference(const CTensor<T>* pTsrMaskLogist, const std::vector<CTensor<T>>& m_vctLabel, std::vector<CTensor<T>>& vctResultMask);
125 virtual const CResult MaskRCNNLoss_Backward(const CTensor<T>* pTsrMaskLogist, const CTensor<T>* pTsrMaskLossDy, CTensor<T>* pTsrMaskLogistDx, bool bMaskLogistAddGraident);
126 virtual const CResult CreateTargetBuffer();
127
128 virtual CInternalMaskRCNNParameters<T>& GetMaskRCNNParameters() const;
129 protected:
130 CBackendBCEWithLogistLoss<T> m_backendBCELoss;
131 CBackendMultiScaleRoIAlign<T> m_backendRoIAlign;
132 CBackendLabel<T> m_backendLabel;
133 T m_tRPNBoxLoss;
134 T m_tRPNScoreLoss;
135 T m_tFasterRCNNBoxLoss;
136 T m_tFasterRCNNScoreLoss;
137 T m_tMaskLoss;
138 T m_tTotalLoss;
139
140 #ifdef MASKRCNN_TEST
141 public:
142 #endif
143 CTensor<T> m_tsrMaskLogistSelected;
144 CTensor<T> m_tsrMaskTargetSelected;
145 CTensor<T> m_tsrMaskLogistDxSelected;
146 CTensor<T> m_tsrSelectedLabel;
147 CTensor<T> m_tsrMaskLogistSigmoid;
148
149 CTensor<T> m_tsrMaskTargetSelectedBuffer;
150 CTensor<T> m_tsrMaskLogistSelectedBuffer;
151 CTensor<T> m_tsrMaskLogistSelectedDxBuffer;
152
153 std::vector<CTensor<T>>& m_vctTsrTargetMasks;
154 CTensor<T> m_tsrTargetMasksBuffer;
155
156 bool m_bBackwardEnabled;
157 public:
158 DeclareGetSignletonObject(CComputationalGraphMaskRCNN);
159 };
160
161 #define CCGFMaskRCNN(...) (*(new CComputationalGraphMaskRCNN<float>(__VA_ARGS__)))
162 #define CCGDMaskRCNN(...) (*(new CComputationalGraphMaskRCNN<double>(__VA_ARGS__)))
163 #define CCGTMaskRCNN(T, ...) (*(new CComputationalGraphMaskRCNN<T>(__VA_ARGS__)))
164
165 }
166}
167
168#endif
@ Clear
Clear all the figure objects.