1 #include "TrainNodeKNN.h" 3 #include "SamplesAccumulator.h" 44 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeKNN" : name, idx);
50 std::string fileName =
generateFileName(path, name.empty() ?
"TrainNodeKNN" : name, idx);
61 #ifdef DEBUG_PRINT_INFO 69 #ifdef DEBUG_PRINT_INFO 73 classes.push_back(Mat(nSamples, 1, CV_8UC1, Scalar(s)));
86 size_t n = nearestNeighbors.size();
87 for (
auto node : nearestNeighbors) {
88 byte s = node->getValue();
94 potential.at<
float>(s, 0) += 1.0f;
96 if (n) potential /=
static_cast<double>(n);
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.
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.
size_t maxNeighbors
Max number of neighbors to be used for calculating potentials.
CKDTree * m_pTree
k-D Tree
void addFeatureVec(const Mat &featureVector, byte gt)
Adds new feature vector.
Base abstract class for random model training.
void release(byte state)
Releases memory of container for the state (class) state.
TrainNodeKNNParams m_params
size_t maxSamples
Maximum number of samples to be used in training. 0 means using all the samples.
Class implementing k-D Tree data structure.
void build(Mat &keys, Mat &values)
Builds a k-d tree on keys with corresponding values.
void addSample(const Mat &featureVector, byte state)
Adds new sample to the accumulator.
void reset(void)
Resets the tree.
void reset(void)
Resets the accumulator.
k-Nearest Neighbors parameters
void train(bool doClean=false)
Random model training.
std::vector< std::shared_ptr< const CKDNode > > findNearestNeighbors(const Mat &key, size_t maxNeighbors) const
Finds up to maxNeighbors nearest neighbors to the key.
void calculateNodePotentials(const Mat &featureVector, Mat &potential, Mat &mask) const
Calculates the node potential, based on the feature vector.
Samples accumulator abstract class.
float bias
Regularization CRF parameter: bias is added to all potential values.
void load(const std::string &fileName)
Loads a tree from the file.
Base abstract class for node potentials training.
CTrainNodeKNN(byte nStates, word nFeatures, TrainNodeKNNParams params=TRAIN_NODE_KNN_PARAMS_DEFAULT)
Constructor.
void reset(void)
Resets class variables.
void save(const std::string &fileName) const
Saves the tree into a file.
CSamplesAccumulator * m_pSamplesAcc
Samples Accumulator.
Mat getSamplesContainer(byte state) const
Returns samples container for the state (class) state.
const TrainNodeKNNParams TRAIN_NODE_KNN_PARAMS_DEFAULT
int getNumInputSamples(byte state) const
Returns the number of input samples in container for the state (class) state.
void init(TrainNodeKNNParams params)
byte m_nStates
The number of states (classes)