Direct Graphical Models  v.1.7.0
TrainNodeCvKNN.h
1 // k-Nearest Neighbors (based on OpenCV) training class interface
2 // Written by Sergey G. Kosov in 2017 for Project X
3 #pragma once
4 
5 #include "TrainNode.h"
6 
7 namespace DirectGraphicalModels
8 {
9  class CSamplesAccumulator;
10 
12  typedef struct TrainNodeCvKNNParams {
13  float bias;
14  size_t maxNeighbors;
15  size_t maxSamples;
16 
18  TrainNodeCvKNNParams(float _bias, size_t _maxNeighbors, size_t _maxSamples) : bias(_bias), maxNeighbors(_maxNeighbors), maxSamples(_maxSamples) {}
20 
22  0.1f, // Regularization CRF parameter: bias is added to all potential values
23  100, // Max number of neighbors to be used for calculating potentials
24  0 // Maximum number of samples to be used in training. 0 means using all the samples
25  );
26 
27  // ====================== OpenCV k-Nearest Neighbors Train Class =====================
36  class CTrainNodeCvKNN : public CTrainNode {
37  public:
44  DllExport CTrainNodeCvKNN(byte nStates, word nFeatures, TrainNodeCvKNNParams params = TRAIN_NODE_CV_KNN_PARAMS_DEFAULT);
53  CTrainNodeCvKNN(byte nStates, word nFeatures, size_t maxSamples);
54  DllExport virtual ~CTrainNodeCvKNN(void);
55 
56  DllExport void reset(void);
57  DllExport void save(const std::string &path, const std::string &name = std::string(), short idx = -1) const;
58  DllExport void load(const std::string &path, const std::string &name = std::string(), short idx = -1);
59 
60  DllExport void addFeatureVec(const Mat &featureVector, byte gt);
61 
62  DllExport void train(bool doClean = false);
63 
64 
65  protected:
66  DllExport void saveFile(FILE *pFile) const { }
67  DllExport void loadFile(FILE *pFile) { }
68  DllExport void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const;
69 
70 
71  protected:
72  Ptr<ml::KNearest> m_pKNN;
74 
75 
76  private:
77  void init(TrainNodeCvKNNParams params); // This function is called by both constructors
78 
79 
80  private:
82  };
83 }
void loadFile(FILE *pFile)
Loads the random model from the file.
OpenCV k-Nearest Neighbors parameters.
Ptr< ml::KNearest > m_pKNN
k-Nearest Neighbors
void reset(void)
Resets class variables.
size_t maxNeighbors
Max number of neighbors to be used for calculating potentials.
void save(const std::string &path, const std::string &name=std::string(), short idx=-1) const
Saves the training data.
void init(TrainNodeCvKNNParams params)
const TrainNodeCvKNNParams TRAIN_NODE_CV_KNN_PARAMS_DEFAULT
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
OpenCV Nearest Neighbor training class.
void train(bool doClean=false)
Random model training.
struct DirectGraphicalModels::TrainNodeCvKNNParams TrainNodeCvKNNParams
OpenCV k-Nearest Neighbors parameters.
float bias
Regularization CRF parameter: bias is added to all potential values.
Samples accumulator abstract class.
size_t maxSamples
Maximum number of samples to be used in training. 0 means using all the samples.
void load(const std::string &path, const std::string &name=std::string(), short idx=-1)
Loads the training data.
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.
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
CTrainNodeCvKNN(byte nStates, word nFeatures, TrainNodeCvKNNParams params=TRAIN_NODE_CV_KNN_PARAMS_DEFAULT)
Constructor.
TrainNodeCvKNNParams(float _bias, size_t _maxNeighbors, size_t _maxSamples)
void saveFile(FILE *pFile) const
Saves the random model into the file.