Direct Graphical Models  v.1.7.0
TrainNodeCvANN.h
1 // Artificial Neural Network (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 TrainNodeCvANNParams {
13  word numLayers;
14  double weightScale;
15  double momentumScale;
16  int maxCount;
17  double epsilon;
19  size_t maxSamples;
20 
22  TrainNodeCvANNParams(word _numLayers, double _weightScale, double _momentumScale, int _maxCount, double _epsilon, int _term_criteria_type, int _maxSamples) : numLayers(_numLayers), weightScale(_weightScale), maxCount(_maxCount), epsilon(_epsilon), term_criteria_type(_term_criteria_type), maxSamples(_maxSamples) {}
24 
26  5, // Num layers
27  0.0001, // Backpropagation Weight Scale
28  0.1, // Backpropagation Momentum Scale
29  100, // The maximum number of iterations (time / accuracy)
30  0.01, // The desired accuracy or change in parameters at which the iterative algorithm stops
31  TermCriteria::MAX_ITER | TermCriteria::EPS, // Termination cirteria (according the the two previous parameters)
32  0 // Maximum number of samples to be used in training. 0 means using all the samples
33  );
34 
35  // ====================== OpenCV Artificial Neural Network Train Class =====================
42  class CTrainNodeCvANN : public CTrainNode {
43  public:
50  DllExport CTrainNodeCvANN(byte nStates, word nFeatures, TrainNodeCvANNParams params = TRAIN_NODE_CV_ANN_PARAMS_DEFAULT);
59  DllExport CTrainNodeCvANN(byte nStates, word nFeatures, size_t maxSamples);
60  DllExport virtual ~CTrainNodeCvANN(void);
61 
62  DllExport void reset(void);
63  DllExport void save(const std::string &path, const std::string &name = std::string(), short idx = -1) const;
64  DllExport void load(const std::string &path, const std::string &name = std::string(), short idx = -1);
65 
66  DllExport void addFeatureVec(const Mat &featureVector, byte gt);
67 
68  DllExport void train(bool doClean = false);
69 
70 
71  protected:
72  DllExport void saveFile(FILE *pFile) const { }
73  DllExport void loadFile(FILE *pFile) { }
74  DllExport void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const;
75 
76 
77  private:
78  void init(TrainNodeCvANNParams params); // This function is called by both constructors
79 
80 
81  protected:
82  Ptr<ml::ANN_MLP> m_pANN;
84  };
85 }
Ptr< ml::ANN_MLP > m_pANN
Artificial Neural Network.
int term_criteria_type
Termination cirteria type (according the the two previous parameters)
void init(TrainNodeCvANNParams params)
double weightScale
Strength of the weight gradient term. The recommended value is about 0.1. Default value is 0...
void train(bool doClean=false)
Random model training.
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
double epsilon
The desired accuracy or change in parameters at which the iterative algorithm stops.
const TrainNodeCvANNParams TRAIN_NODE_CV_ANN_PARAMS_DEFAULT
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
OpenCV Artificial neural network parameters.
void loadFile(FILE *pFile)
Loads the random model from the file.
TrainNodeCvANNParams(word _numLayers, double _weightScale, double _momentumScale, int _maxCount, double _epsilon, int _term_criteria_type, int _maxSamples)
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
Samples accumulator abstract class.
int maxCount
The maximum number of iterations (time / accuracy)
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 reset(void)
Resets class variables.
word numLayers
Number of layers of neurons.
void saveFile(FILE *pFile) const
Saves the random model into the file.
struct DirectGraphicalModels::TrainNodeCvANNParams TrainNodeCvANNParams
OpenCV Artificial neural network parameters.
OpenCV Artificial neural network training class.
CTrainNodeCvANN(byte nStates, word nFeatures, TrainNodeCvANNParams params=TRAIN_NODE_CV_ANN_PARAMS_DEFAULT)
Constructor.
double momentumScale
Strength of the momentum term (the difference between weights on the 2 previous iterations). This parameter provides some inertia to smooth the random fluctuations of the weights. It can vary from 0 (the feature is disabled) to 1 and beyond. The value 0.1 or so is good enough. Default value is 0.1.
void save(const std::string &path, const std::string &name=std::string(), short idx=-1) const
Saves the training data.
size_t maxSamples
Maximum number of samples to be used in training. 0 means using all the samples.