Direct Graphical Models
v.1.7.0
|
Bayes training class. More...
#include <TrainNodeNaiveBayes.h>
Public Member Functions | |
CTrainNodeBayes (byte nStates, word nFeatures) | |
Constructor. More... | |
virtual | ~CTrainNodeBayes (void) |
virtual void | reset (void) |
Resets class variables. More... | |
virtual void | addFeatureVec (const Mat &featureVector, byte gt) |
Adds new feature vector. More... | |
virtual void | train (bool doClean=false) |
Random model training. More... | |
IPDF * | getPDF (byte state, word feature) const |
Returns the normalized probability density function (PDF) for specific state (class) and feature. More... | |
IPDF * | getPDF2D (byte state) const |
Returns the 2D normalized probability density function (PDF) for specific state (class) More... | |
void | smooth (int nIt=1) |
Smothes the underlying Probability Density Functions (PDFs) More... | |
Public Member Functions inherited from DirectGraphicalModels::CTrainNode | |
CTrainNode (byte nStates, word nFeatures) | |
Constructor. More... | |
virtual | ~CTrainNode (void)=default |
void | addFeatureVecs (const Mat &featureVectors, const Mat >) |
Adds a block of new feature vectors. More... | |
void | addFeatureVecs (const vec_mat_t &featureVectors, const Mat >) |
Adds a block of new feature vectors. More... | |
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. More... | |
Mat | getNodePotentials (const vec_mat_t &featureVectors, const Mat &weights=Mat(), float Z=0.0f) const |
Returns a block of node potentials, based on the block of feature vector. More... | |
Mat | getNodePotentials (const Mat &featureVector, float weight, float Z=0.0f) const |
Returns the node potential, based on the feature vector. More... | |
Public Member Functions inherited from DirectGraphicalModels::ITrain | |
ITrain (byte nStates, word nFeatures) | |
Constructor. More... | |
virtual | ~ITrain (void)=default |
word | getNumFeatures (void) const |
Returns number of features. More... | |
Public Member Functions inherited from DirectGraphicalModels::CBaseRandomModel | |
CBaseRandomModel (byte nStates) | |
Constructor. More... | |
virtual | ~CBaseRandomModel (void) |
virtual void | save (const std::string &path, const std::string &name=std::string(), short idx=-1) const |
Saves the training data. More... | |
virtual void | load (const std::string &path, const std::string &name=std::string(), short idx=-1) |
Loads the training data. More... | |
byte | getNumStates (void) const |
Returns number of states (classes) More... | |
Protected Member Functions | |
virtual void | saveFile (FILE *pFile) const |
Saves the random model into the file. More... | |
virtual void | loadFile (FILE *pFile) |
Loads the random model from the file. More... | |
void | calculateNodePotentials (const Mat &featureVector, Mat &potential, Mat &mask) const |
Calculates the node potential, based on the feature vector. More... | |
Protected Member Functions inherited from DirectGraphicalModels::CBaseRandomModel | |
std::string | generateFileName (const std::string &path, const std::string &name, short idx) const |
Generates name of the data file for storing random model parameters. More... | |
Private Attributes | |
IPDF *** | m_pPDF |
The 1D PDF for node potentials [state][feature]. More... | |
IPDF ** | m_pPDF2D |
The 2D data histogram for node potentials and 2 features[state]. More... | |
Mat | m_prior |
The class prior probability vector. More... | |
Private Attributes inherited from DirectGraphicalModels::CPrior | |
Mat | m_histogramPrior |
The class cooccurance histogram. More... | |
Private Attributes inherited from DirectGraphicalModels::CBaseRandomModel | |
byte | m_nStates |
The number of states (classes) More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from DirectGraphicalModels::CTrainNode | |
static std::shared_ptr< CTrainNode > | create (byte nodeRandomModel, byte nStates, word nFeatures) |
Factory method returning node trainer object. More... | |
Protected Attributes inherited from DirectGraphicalModels::CBaseRandomModel | |
byte | m_nStates |
The number of states (classes) More... | |
Private Member Functions inherited from DirectGraphicalModels::CPriorNode | |
CPriorNode (byte nStates) | |
Constructor. More... | |
~CPriorNode (void) | |
void | addNodeGroundTruth (const Mat >) |
Adds ground truth values to the co-occurance histogram vector. More... | |
void | addNodeGroundTruth (byte gt) |
Adds a ground truth value to the co-occurance histogram vector. More... | |
Mat | calculatePrior (void) const |
Calculates the prior probabilies. More... | |
Private Member Functions inherited from DirectGraphicalModels::CPrior | |
CPrior (byte nStates, RandomModelType type) | |
Constructor. More... | |
~CPrior (void) | |
Mat | getPrior (float weight=1.0f) const |
Returns the prior probabilies. More... | |
Private Member Functions inherited from DirectGraphicalModels::CBaseRandomModel | |
CBaseRandomModel (byte nStates) | |
Constructor. More... | |
virtual | ~CBaseRandomModel (void) |
virtual void | save (const std::string &path, const std::string &name=std::string(), short idx=-1) const |
Saves the training data. More... | |
virtual void | load (const std::string &path, const std::string &name=std::string(), short idx=-1) |
Loads the training data. More... | |
byte | getNumStates (void) const |
Returns number of states (classes) More... | |
std::string | generateFileName (const std::string &path, const std::string &name, short idx) const |
Generates name of the data file for storing random model parameters. More... | |
Bayes training class.
This class implements the naive Bayes classifier, which is based on strong (naive) independence assumptions between the features.
Definition at line 20 of file TrainNodeNaiveBayes.h.
DirectGraphicalModels::CTrainNodeBayes::CTrainNodeBayes | ( | byte | nStates, |
word | nFeatures | ||
) |
Constructor.
nStates | Number of states (classes) |
nFeatures | Number of features |
Definition at line 10 of file TrainNodeNaiveBayes.cpp.
|
virtual |
|
virtual |
Adds new feature vector.
Used to add a featureVector, corresponding to the ground-truth state (class) gt for training
featureVector | Multi-dimensinal point: Mat(size: nFeatures x 1; type: CV_8UC1) |
gt | Corresponding ground-truth state (class) |
Implements DirectGraphicalModels::CTrainNode.
Definition at line 63 of file TrainNodeNaiveBayes.cpp.
|
protectedvirtual |
Calculates the node potential, based on the feature vector.
This function calculates the potentials of the node, described with the sample featureVector ( \( \textbf{f} \)):
\[ nodePot_s = prior_s\cdot\prod_{f\in\mathbb{F}} (H_{s,f}.data[\textbf{f}_f] / H_{s,f}.n); \forall s\in\mathbb{S}, \]
where \(\mathbb{S}\) and \(\mathbb{F}\) are sets of all states (classes) and features correspondently. In other words, the indexes: \( s \in [0; nStates) \) and \( f \in [0; nFeatures) \). Here \( H.data[256] \) is a 1D histogram, \( H.n \) is the number of entries in histogram, i.e. \( H.n = \sum^{255}_{i = 0} H.data[i] \). And \( \textbf{f}_f \in [0; 255], \forall f \in [0; nFeatures) \), i.e. has (type: CV_8UC1).
[in] | featureVector | Multi-dimensinal point \(\textbf{f}\): Mat(size: nFeatures x 1; type: CV_{XX}C1) |
[in,out] | potential | Node potentials: Mat(size: nStates x 1; type: CV_32FC1). This parameter should be preinitialized and set to value 0. |
[in,out] | mask | Relevant Node potentials: Mat(size: nStates x 1; type: CV_8UC1). This parameter should be preinitialized and set to value 1 (all potentials are relevant). |
Implements DirectGraphicalModels::CTrainNode.
Definition at line 125 of file TrainNodeNaiveBayes.cpp.
|
inline |
Returns the normalized probability density function (PDF) for specific state (class) and feature.
state | The state (class) |
feature | The feature |
Definition at line 42 of file TrainNodeNaiveBayes.h.
|
inline |
Returns the 2D normalized probability density function (PDF) for specific state (class)
state | The state (class) |
Definition at line 49 of file TrainNodeNaiveBayes.h.
|
protectedvirtual |
Loads the random model from the file.
Allows to re-use the class.
pFile | Pointer to the file, opened for reading. |
Reimplemented from DirectGraphicalModels::CPrior.
Definition at line 112 of file TrainNodeNaiveBayes.cpp.
|
virtual |
Resets class variables.
Allows to re-use the class.
Reimplemented from DirectGraphicalModels::CPrior.
Definition at line 49 of file TrainNodeNaiveBayes.cpp.
|
protectedvirtual |
Saves the random model into the file.
Allows to re-use the class.
pFile | Pointer to the file, opened for writing. |
Reimplemented from DirectGraphicalModels::CPrior.
Definition at line 100 of file TrainNodeNaiveBayes.cpp.
void DirectGraphicalModels::CTrainNodeBayes::smooth | ( | int | nIt = 1 | ) |
Smothes the underlying Probability Density Functions (PDFs)
nIt | Number of smooth iterations |
Definition at line 89 of file TrainNodeNaiveBayes.cpp.
|
virtual |
Random model training.
Auxilary function for training - some derived classes may use this function inbetween training and classification phases
doClean | Flag indicating if the memory, keeping the trining data should be released after training |
Reimplemented from DirectGraphicalModels::CTrainNode.
Definition at line 84 of file TrainNodeNaiveBayes.cpp.
|
private |
The 1D PDF for node potentials [state][feature].
Definition at line 75 of file TrainNodeNaiveBayes.h.
|
private |
The 2D data histogram for node potentials and 2 features[state].
Definition at line 76 of file TrainNodeNaiveBayes.h.
|
private |
The class prior probability vector.
Definition at line 77 of file TrainNodeNaiveBayes.h.