3 #include "TrainNodeNaiveBayes.h" 4 #include "TrainNodeGM.h" 5 #include "TrainNodeGMM.h" 6 #include "TrainNodeCvGM.h" 7 #include "TrainNodeCvGMM.h" 8 #include "TrainNodeKNN.h" 9 #include "TrainNodeCvKNN.h" 10 #include "TrainNodeCvRF.h" 11 #include "TrainNodeMsRF.h" 12 #include "TrainNodeCvANN.h" 13 #include "TrainNodeCvSVM.h" 20 std::shared_ptr<CTrainNode>
CTrainNode::create(byte nodeRandomModel, byte nStates, word nFeatures)
22 switch (nodeRandomModel)
24 case NodeRandomModel::Bayes:
return std::make_shared<CTrainNodeBayes>(nStates, nFeatures);
25 case NodeRandomModel::GM:
return std::make_shared<CTrainNodeGM>(nStates, nFeatures);
26 case NodeRandomModel::GMM:
return std::make_shared<CTrainNodeGMM>(nStates, nFeatures);
27 case NodeRandomModel::CvGM:
return std::make_shared<CTrainNodeCvGM>(nStates, nFeatures);
28 case NodeRandomModel::CvGMM:
return std::make_shared<CTrainNodeCvGMM>(nStates, nFeatures);
29 case NodeRandomModel::KNN:
return std::make_shared<CTrainNodeKNN>(nStates, nFeatures);
30 case NodeRandomModel::CvKNN:
return std::make_shared<CTrainNodeCvKNN>(nStates, nFeatures);
31 case NodeRandomModel::CvRF:
return std::make_shared<CTrainNodeCvRF>(nStates, nFeatures);
33 case NodeRandomModel::MsRF:
return std::make_shared<CTrainNodeMsRF>(nStates, nFeatures);
35 case NodeRandomModel::CvANN:
return std::make_shared<CTrainNodeCvANN>(nStates, nFeatures);
36 case NodeRandomModel::CvSVM:
return std::make_shared<CTrainNodeCvSVM>(nStates, nFeatures);
38 DGM_ASSERT_MSG(
false,
"Unknown type of the node random model");
44 DGM_ASSERT_MSG(featureVectors.channels() ==
getNumFeatures(),
"Number of features in the <featureVectors> (%d) does not correspond to the specified (%d)", featureVectors.channels(),
getNumFeatures());
45 DGM_VECTORWISE1<CTrainNode, &CTrainNode::addFeatureVec>(*
this, featureVectors, gt);
50 DGM_ASSERT_MSG(featureVectors.size() ==
getNumFeatures(),
"Number of features in the <featureVectors> (%zu) does not correspond to the specified (%d)", featureVectors.size(),
getNumFeatures());
51 DGM_VECTORWISE1<CTrainNode, &CTrainNode::addFeatureVec>(*
this, featureVectors, gt);
57 DGM_ASSERT_MSG(featureVectors.channels() ==
getNumFeatures(),
"Number of features in the <featureVectors> (%d) does not correspond to the specified (%d)", featureVectors.channels(),
getNumFeatures());
58 DGM_ASSERT(featureVectors.depth() == CV_8U);
59 if (!weights.empty()) {
60 DGM_ASSERT(featureVectors.size() == weights.size());
61 DGM_ASSERT(weights.type() == CV_32FC1);
64 Mat res(featureVectors.size(), CV_32FC(
m_nStates));
66 concurrency::parallel_for(0, res.rows, [&] (
int y) {
68 Mat vec(getNumFeatures(), 1, CV_8UC1);
71 Mat vec(getNumFeatures(), 1, CV_8UC1);
72 for (int y = 0; y < res.rows; y++) {
74 const byte *pFv = featureVectors.ptr<byte>(y);
75 const float *pW = weights.empty() ? NULL : weights.ptr<float>(y);
76 float *pRes = res.ptr<float>(y);
77 for (int x = 0; x < res.cols; x++) {
78 float weight = pW ? pW[x] : 1.0f;
79 for (int f = 0; f < getNumFeatures(); f++) vec.at<byte>(f, 0) = pFv[getNumFeatures() * x + f];
80 pot = getNodePotentials(vec, weight, Z);
81 for (int s = 0; s < m_nStates; s++) pRes[m_nStates * x + s] = pot.at<float>(s, 0);
93 DGM_ASSERT_MSG(featureVectors.size() ==
getNumFeatures(),
"Number of features in the <featureVectors> (%zu) does not correspond to the specified (%d)", featureVectors.size(),
getNumFeatures());
94 DGM_ASSERT(featureVectors[0].depth() == CV_8U);
95 if (!weights.empty()) {
96 DGM_ASSERT(featureVectors[0].size() == weights.size());
97 DGM_ASSERT(weights.type() == CV_32FC1);
100 Mat res(featureVectors[0].size(), CV_32FC(
m_nStates));
102 concurrency::parallel_for(0, res.rows, [&](
int y) {
104 Mat vec(getNumFeatures(), 1, CV_8UC1);
107 Mat vec(getNumFeatures(), 1, CV_8UC1);
108 for (int y = 0; y < res.rows; y++) {
110 const byte **pFv = new const byte *[getNumFeatures()];
111 for (word f = 0; f < getNumFeatures(); f++) pFv[f] = featureVectors[f].ptr<byte>(y);
112 const float *pW = weights.empty() ? NULL : weights.ptr<float>(y);
113 float *pRes = res.ptr<float>(y);
114 for (int x = 0; x < res.cols; x++) {
115 float weight = pW ? pW[x] : 1.0f;
116 for (int f = 0; f < getNumFeatures(); f++) vec.at<byte>(f, 0) = pFv[f][x];
117 pot = getNodePotentials(vec, weight, Z);
118 for (int s = 0; s < m_nStates; s++) pRes[m_nStates * x + s] = pot.at<float>(s, 0);
132 DGM_ASSERT_MSG(featureVector.type() == CV_8UC1,
133 "The input feature vector has either wrong depth or more than one channel");
134 DGM_ASSERT_MSG((featureVector.size().width == 1) && (featureVector.size().height ==
getNumFeatures()),
135 "The input feature vector has wrong size:(%d, %d)", featureVector.size().width, featureVector.size().height);
137 Mat res(
m_nStates, 1, CV_32FC1, Scalar(0));
138 const_cast<Mat &
>(
m_mask).setTo(1);
140 if (weight != 1.0f) pow(res, weight, res);
143 float Sum =
static_cast<float>(sum(res).val[0]);
144 if (Sum < FLT_EPSILON) {
145 res.setTo(FLT_EPSILON,
m_mask);
void addFeatureVecs(const Mat &featureVectors, const Mat >)
Adds a block of new feature vectors.
virtual void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const =0
Calculates the node potential, based on the feature vector.
word getNumFeatures(void) const
Returns number of features.
Mat getNodePotentials(const Mat &featureVectors, const Mat &weights=Mat(), float Z=0.0f) const
Returns a block of node potentials, based on the block of feature vector.
static std::shared_ptr< CTrainNode > create(byte nodeRandomModel, byte nStates, word nFeatures)
Factory method returning node trainer object.
byte m_nStates
The number of states (classes)