1 #include "TrainNodeCvKNN.h" 2 #include "SamplesAccumulator.h" 23 m_pKNN = ml::KNearest::create();
27 m_pKNN->setAlgorithmType(ml::KNearest::BRUTE_FORCE);
45 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvKNN" : name, idx);
46 m_pKNN->save(fileName.c_str());
51 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeCvKNN" : name, idx);
52 m_pKNN = Algorithm::load<ml::KNearest>(fileName.c_str());
62 #ifdef DEBUG_PRINT_INFO 70 #ifdef DEBUG_PRINT_INFO 74 classes.push_back(Mat(nSamples, 1, CV_32FC1, Scalar(s)));
77 samples.convertTo(samples, CV_32FC1);
80 Mat var_type(
getNumFeatures() + 1, 1, CV_8UC1, Scalar(ml::VAR_NUMERICAL));
85 m_pKNN->train(ml::TrainData::create(samples, ml::ROW_SAMPLE, classes, noArray(), noArray(), noArray(), var_type));
87 catch (std::exception &e) {
88 printf(
"EXCEPTION: %s\n", e.what());
97 featureVector.convertTo(fv, CV_32FC1);
103 Mat result, neighborResponses;
106 float *pResponse = neighborResponses.ptr<
float>(0);
107 int n = neighborResponses.cols;
108 for (
int i = 0; i < n; i++) {
109 byte s =
static_cast<byte
>(pResponse[i]);
110 potential.at<
float>(s, 0) += 1.0f;
112 if (n) potential /= n;
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.
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.
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
Base abstract class for random model training.
void release(byte state)
Releases memory of container for the state (class) state.
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
void train(bool doClean=false)
Random model training.
float bias
Regularization CRF parameter: bias is added to all potential values.
void addSample(const Mat &featureVector, byte state)
Adds new sample to the accumulator.
void reset(void)
Resets the accumulator.
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.
Mat getSamplesContainer(byte state) const
Returns samples container for the state (class) state.
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
int getNumInputSamples(byte state) const
Returns the number of input samples in container for the state (class) state.
virtual ~CTrainNodeCvKNN(void)
TrainNodeCvKNNParams m_params
CTrainNodeCvKNN(byte nStates, word nFeatures, TrainNodeCvKNNParams params=TRAIN_NODE_CV_KNN_PARAMS_DEFAULT)
Constructor.
byte m_nStates
The number of states (classes)