1 #include "TrainNodeCvRF.h" 2 #include "SamplesAccumulator.h" 25 m_pRF = ml::RTrees::create();
50 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvRF" : name, idx);
51 m_pRF->save(fileName.c_str());
56 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvRF" : name, idx);
57 m_pRF = Algorithm::load<ml::RTrees>(fileName.c_str());
67 #ifdef DEBUG_PRINT_INFO 75 #ifdef DEBUG_PRINT_INFO 79 classes.push_back(Mat(nSamples, 1, CV_32FC1, Scalar(s)));
82 samples.convertTo(samples, CV_32FC1);
85 Mat var_type(
getNumFeatures() + 1, 1, CV_8UC1, Scalar(ml::VAR_NUMERICAL));
90 m_pRF->train(ml::TrainData::create(samples, ml::ROW_SAMPLE, classes, noArray(), noArray(), noArray(), var_type));
91 }
catch (std::exception &e) {
92 printf(
"EXCEPTION: %s\n", e.what());
93 printf(
"Try to reduce the maximal depth of the forest or switch to x64.\n");
101 return m_pRF->getVarImportance();
107 featureVector.convertTo(fv, CV_32FC1);
108 float res =
m_pRF->predict(fv.t());
109 byte s =
static_cast<byte
>(res);
110 potential.at<
float>(s, 0) = 1.0f;
void init(TrainNodeCvRFParams params)
void load(const std::string &path, const std::string &name=std::string(), short idx=-1)
Loads the training data.
CTrainNodeCvRF(byte nStates, word nFeatures, TrainNodeCvRFParams params=TRAIN_NODE_CV_RF_PARAMS_DEFAULT)
Constructor.
size_t maxSamples
Maximum number of samples to be used in training. 0 means using all the samples.
Ptr< ml::RTrees > m_pRF
Random Forest.
OpenCV Random Forest parameters.
word getNumFeatures(void) const
Returns number of features.
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.
int getNumSamples(byte state) const
Returns the number of stored samples in container for the state (class) state.
int max_categories
Max number of categories (use sub-optimal algorithm for larger numbers)
bool use_surrogates
Compute surrogate split, no missing data.
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
Mat getFeatureImportance(void) const
Returns the feature importance vector.
void save(const std::string &path, const std::string &name=std::string(), short idx=-1) const
Saves the training data.
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
int min_sample_count
Min sample count (1% of all data)
void reset(void)
Resets class variables.
Base abstract class for random model training.
void release(byte state)
Releases memory of container for the state (class) state.
void addSample(const Mat &featureVector, byte state)
Adds new sample to the accumulator.
void reset(void)
Resets the accumulator.
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
Samples accumulator abstract class.
const TrainNodeCvRFParams TRAIN_NODE_CV_RF_PARAMS_DEFAULT
int term_criteria_type
Termination cirteria type (according the the two previous parameters)
Base abstract class for node potentials training.
bool calc_var_importance
Calculate variable importance (must be true in order to use CTrainNodeCvRF::getFeatureImportance func...
void train(bool doClean=false)
Random model training.
Mat getSamplesContainer(byte state) const
Returns samples container for the state (class) state.
double epsilon
Forest accuracy.
int getNumInputSamples(byte state) const
Returns the number of input samples in container for the state (class) state.
int maxCount
Max number of trees in the forest (time / accuracy)
float regression_accuracy
Regression accuracy (0 means N/A here)
byte m_nStates
The number of states (classes)
int nactive_vars
Number of variables randomly selected at node and used to find the best split(s). (0 means the ) ...