Direct Graphical Models  v.1.7.0
TrainNodeNaiveBayes.cpp
1 #include "TrainNodeNaiveBayes.h"
2 #include "PDFHistogram.h"
3 #include "PDFHistogram2D.h"
4 #include "PDFGaussian.h"
5 #include "macroses.h"
6 
7 namespace DirectGraphicalModels
8 {
9  // Constructor
10  CTrainNodeBayes::CTrainNodeBayes(byte nStates, word nFeatures)
11  : CBaseRandomModel(nStates)
12  , CTrainNode(nStates, nFeatures)
13  , CPriorNode(nStates)
14  , m_prior(Mat())
15  {
16  m_pPDF = new IPDF**[m_nStates];
17  for (byte s = 0; s < m_nStates; s++) {
18  m_pPDF[s] = new IPDF*[getNumFeatures()];
19  for (word f = 0; f < getNumFeatures(); f++)
20  m_pPDF[s][f] = new CPDFHistogram();
21  // m_pPDF[s][f] = new CPDFGaussian();
22  } // s
23 
24  if (getNumFeatures() == 2) {
25  m_pPDF2D = new IPDF*[m_nStates];
26  for (byte s = 0; s < m_nStates; s++)
27  m_pPDF2D[s] = new CPDFHistogram2D();
28  } else m_pPDF2D = NULL;
29  }
30 
31  // Destructor
33  {
34  if (!m_prior.empty()) m_prior.release();
35  for (byte s = 0; s < m_nStates; s++) {
36  for (word f = 0; f < getNumFeatures(); f++)
37  delete m_pPDF[s][f];
38  delete m_pPDF[s];
39  } // s
40  delete m_pPDF;
41 
42  if (m_pPDF2D) {
43  for (byte s = 0; s < m_nStates; s++)
44  delete[] m_pPDF2D[s];
45  delete m_pPDF2D;
46  }
47  }
48 
50  {
51  CPriorNode::reset(); // resetting the prior histogram vector
52  if (!m_prior.empty()) m_prior.release(); // resetting the prior
53 
54  for (byte s = 0; s < m_nStates; s++)
55  for (word f = 0; f < getNumFeatures(); f++)
56  m_pPDF[s][f]->reset();
57 
58  if (m_pPDF2D)
59  for (byte s = 0; s < m_nStates; s++)
60  m_pPDF2D[s]->reset();
61  }
62 
63  void CTrainNodeBayes::addFeatureVec(const Mat &featureVector, byte gt)
64  {
65  // Assertions
66  DGM_ASSERT_MSG(gt < m_nStates, "The groundtruth value %d is out of range %d", gt, m_nStates);
67  DGM_ASSERT_MSG(featureVector.type() == CV_8UC1, "The feature vector has incorrect type");
68 
70 
71  for (word f = 0; f < getNumFeatures(); f++) {
72  // byte feature = featureVector.ptr<byte>(f)[0];
73  byte feature = featureVector.at<byte>(f, 0);
74  m_pPDF[gt][f]->addPoint(feature);
75  }
76 
77  if (m_pPDF2D) {
78  byte x = featureVector.at<byte>(0, 0);
79  byte y = featureVector.at<byte>(1, 0);
80  m_pPDF2D[gt]->addPoint(Scalar(x, y));
81  }
82  }
83 
85  {
86  m_prior = getPrior(FLT_MAX);
87  }
88 
90  {
91  if (typeid(*** m_pPDF) != typeid(CPDFHistogram)) return;
92  for (byte s = 0; s < m_nStates; s++)
93  for (word f = 0; f < getNumFeatures(); f++)
94  dynamic_cast<CPDFHistogram *>(m_pPDF[s][f])->smooth(nIt);
95  if (m_pPDF2D)
96  for (byte s = 0; s < m_nStates; s++)
97  dynamic_cast<CPDFHistogram2D *>(m_pPDF2D[s])->smooth(nIt);
98  }
99 
100  void CTrainNodeBayes::saveFile(FILE *pFile) const
101  {
102  CPriorNode::saveFile(pFile);
103 
104  for (byte s = 0; s < m_nStates; s++)
105  for (word f = 0; f < getNumFeatures(); f++)
106  m_pPDF[s][f]->saveFile(pFile);
107  if (m_pPDF2D)
108  for (byte s = 0; s < m_nStates; s++)
109  m_pPDF2D[s]->saveFile(pFile);
110  }
111 
112  void CTrainNodeBayes::loadFile(FILE *pFile)
113  {
114  CPriorNode::loadFile(pFile);
115  m_prior = getPrior(FLT_MAX); // loads m_prior from the CPriorNode class
116 
117  for (byte s = 0; s < m_nStates; s++)
118  for (word f = 0; f < getNumFeatures(); f++)
119  m_pPDF[s][f]->loadFile(pFile);
120  if (m_pPDF2D)
121  for (byte s = 0; s < m_nStates; s++)
122  m_pPDF2D[s]->loadFile(pFile);
123  }
124 
125  void CTrainNodeBayes::calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
126  {
127  m_prior.copyTo(potential);
128  for (byte s = 0; s < m_nStates; s++) { // state
129  float * pPot = potential.ptr<float>(s);
130  byte * pMask = mask.ptr<byte>(s);
131  for (word f = 0; f < getNumFeatures(); f++) { // feature
132  byte feature = featureVector.ptr<byte>(f)[0];
133  if (m_pPDF[s][f]->isEstimated())
134  pPot[0] *= static_cast<float>(m_pPDF[s][f]->getDensity(feature));
135  else {
136  pPot[0] = 0;
137  pMask[0] = 0;
138  }
139  } // f
140  } // s
141  }
142 }
void smooth(int nIt=1)
Smothes the underlying Probability Density Functions (PDFs)
virtual void loadFile(FILE *pFile)
Loads the random model from the file.
Definition: Prior.cpp:55
IPDF *** m_pPDF
The 1D PDF for node potentials [state][feature].
virtual void train(bool doClean=false)
Random model training.
virtual void addPoint(Scalar point)=0
Adds a sample point for PDF estimation.
Histogram-based PDF class (2D)
word getNumFeatures(void) const
Returns number of features.
Definition: ITrain.h:37
void addNodeGroundTruth(const Mat &gt)
Adds ground truth values to the co-occurance histogram vector.
Definition: PriorNode.cpp:7
virtual void reset(void)
Resets class variables.
Mat m_prior
The class prior probability vector.
virtual void saveFile(FILE *pFile) const
Saves the random model into the file.
virtual void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
CTrainNodeBayes(byte nStates, word nFeatures)
Constructor.
Base abstract class for random model training.
Histogram-based PDF class (1D)
Definition: PDFHistogram.h:16
void reset(void)
Resets class variables.
Definition: Prior.cpp:19
virtual void loadFile(FILE *pFile)
Loads the random model from the file.
Node prior probability estimation class
Definition: PriorNode.h:14
Interface class for Probability Density Function (PDF)
Definition: IPDF.h:16
virtual void saveFile(FILE *pFile) const
Saves the random model into the file.
Definition: Prior.cpp:34
Mat getPrior(float weight=1.0f) const
Returns the prior probabilies.
Definition: Prior.cpp:24
IPDF ** m_pPDF2D
The 2D data histogram for node potentials and 2 features[state].
Base abstract class for node potentials training.
Definition: TrainNode.h:47
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
byte m_nStates
The number of states (classes)