1 #include "TrainNodeCvSVM.h" 2 #include "SamplesAccumulator.h" 23 m_pSVM = ml::SVM::create();
24 m_pSVM->setType(ml::SVM::C_SVC);
26 m_pSVM->setKernel(ml::SVM::INTER);
44 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvSVM" : name, idx);
45 m_pSVM->save(fileName.c_str());
50 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvSVM" : name, idx);
51 m_pSVM = Algorithm::load<ml::SVM>(fileName.c_str());
61 #ifdef DEBUG_PRINT_INFO 68 #ifdef DEBUG_PRINT_INFO 72 classes.push_back(Mat(nSamples, 1, CV_32SC1, Scalar(s)));
75 samples.convertTo(samples, CV_32FC1);
77 m_pSVM->train(samples, ml::ROW_SAMPLE, classes);
83 featureVector.convertTo(fv, CV_32FC1);
84 float res =
m_pSVM->predict(fv.t());
85 byte s =
static_cast<byte
>(res);
86 potential.at<
float>(s, 0) = 1.0f;
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
void reset(void)
Resets class variables.
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.
double epsilon
The desired accuracy or change in parameters at which the iterative algorithm stops.
int getNumSamples(byte state) const
Returns the number of stored samples in container for the state (class) state.
const TrainNodeCvSVMParams TRAIN_NODE_CV_SVM_PARAMS_DEFAULT
Base abstract class for random model training.
Ptr< ml::SVM > m_pSVM
Support Vector Machine.
void release(byte state)
Releases memory of container for the state (class) state.
void load(const std::string &path, const std::string &name=std::string(), short idx=-1)
Loads the training data.
void save(const std::string &path, const std::string &name=std::string(), short idx=-1) const
Saves the training data.
OpenCV Support Vector machine parameters.
virtual ~CTrainNodeCvSVM(void)
void addSample(const Mat &featureVector, byte state)
Adds new sample to the accumulator.
void reset(void)
Resets the accumulator.
int maxCount
The maximum number of iterations (time / accuracy)
int term_criteria_type
Termination cirteria type (according the the two previous parameters)
double C
Parameter C of a SVM optimization problem.
Samples accumulator abstract class.
void init(TrainNodeCvSVMParams params)
void train(bool doClean=false)
Random model training.
Base abstract class for node potentials training.
Mat getSamplesContainer(byte state) const
Returns samples container for the state (class) state.
int getNumInputSamples(byte state) const
Returns the number of input samples in container for the state (class) state.
CTrainNodeCvSVM(byte nStates, word nFeatures, TrainNodeCvSVMParams params=TRAIN_NODE_CV_SVM_PARAMS_DEFAULT)
Constructor.
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
size_t maxSamples
Maximum number of samples to be used in training. 0 means using all the samples.
byte m_nStates
The number of states (classes)