41 #include <pcl/common/eigen.h>
42 #include <pcl/ml/svm.h>
52 #define Malloc(type, n) static_cast<type*>(malloc((n) * sizeof(type)))
117 std::vector<pcl::SVMDataPoint>
SV;
119 SVMData() : label(
std::numeric_limits<double>::signaling_NaN()) {}
149 fprintf(stderr,
"Wrong input format at line %d\n", line_num);
154 inline const std::string&
157 return (class_name_);
182 SVM() : prob_(), line_(nullptr), max_line_len_(10000), labelled_training_set_(true) {}
187 svm_destroy_param(¶m_);
189 if (scaling_.
max > 0)
203 int nr_class = svm_get_nr_class(&model_);
204 int* labels_ =
static_cast<int*
>(malloc(nr_class *
sizeof(
int)));
205 svm_get_labels(&model_, labels_);
207 for (
int j = 0; j < nr_class; j++)
208 labels.push_back(labels_[j]);
221 if (svm_save_model(filename, &model_)) {
222 fprintf(stderr,
"can't save model to file %s\n", filename);
268 SVMTrain() : debug_(false), cross_validation_(0), nr_fold_(0)
271 svm_set_print_string_function(
279 svm_free_model_content(&
model_);
348 svm_set_print_string_function(
nullptr);
350 svm_set_print_string_function(&
printNull);
400 SVMClassify() : model_extern_copied_(false), predict_probability_(false)
408 if (!model_extern_copied_ &&
model_.
l > 0)
409 svm_free_model_content(&
model_);
416 assert(training_set.size() > 0);
420 PCL_ERROR(
"[pcl::%s::setInputTrainingSet] Classifier model not loaded!\n",
455 out.insert(out.begin(), prediction_.begin(), prediction_.end());
478 for (
int j = 0; j < i; j++) {
482 model_extern_copied_ =
true;
521 predict_probability_ = set;
float value
The value assigned to the correspondent feature.
std::vector< SVMData > getInputTrainingSet()
Return the current training set.
void setInputTrainingSet(std::vector< SVMData > training_set)
It adds/store the training set with labelled data.
bool loadClassifierModel(const char *filename)
Read in a classifier model (in svmlight format).
bool saveNormClassProblem(const char *filename)
Save the normalized classification problem in a file (in svmlight format).
void adaptLibSVMToInput(std::vector< SVMData > &training_set, svm_problem prob) const
Convert the libSVM format (svm_problem) into a easier output format.
void getClassificationResult(std::vector< std::vector< double >> &out)
Get the result of the classification.
bool saveProblemNorm(const char *filename, svm_problem prob_, bool labelled)
Save the problem (with normalized values) in an extern file.
bool classificationTest()
Start the classification on labelled input dataset.
bool loadNormClassProblem(const char *filename)
Read in a normalized classification problem (in svmlight format).
The structure initialize a single feature value for the classification using SVM (Support Vector Mach...
std::vector< pcl::SVMDataPoint > SV
Vector of features for the specific sample.
int idx
It's the feature index. It has to be an integer number greater or equal to zero.
void scaleFactors(std::vector< SVMData > training_set, svm_scaling &scaling)
It extracts scaling factors from the input training_set.
The structure stores the features and the label of a single sample which has to be used for the train...
void doCrossValidation()
To cross validate the classifier.
SVMClassify()
Constructor.
Base class for SVM SVM (Support Vector Machines).
bool model_extern_copied_
void exitInputError(int line_num)
Outputs an error in file reading.
void setDebugMode(bool in)
Set to 1 for debugging info.
std::vector< std::vector< double > > prediction_
SVMParam getParameters()
Return the current training parameters.
bool loadClassProblem(const char *filename)
Read in a raw classification problem (in svmlight format).
int nr_fold_
Number of folds to be used during cross validation.
bool saveTrainingSet(const char *filename)
Save the raw training set in a file (in svmlight format).
void resetTrainingSet()
Reset the training set.
bool saveProblem(const char *filename, bool labelled)
Save the raw problem in an extern file.
double label
Pointer to the label value. It is a mandatory to train the classifier.
bool predict_probability_
SVM (Support Vector Machines) classification of a dataset.
The structure initialize a model created by the SVM (Support Vector Machines) classifier (pcl::SVMTra...
bool saveNormTrainingSet(const char *filename)
Save the normalized training set in a file (in svmlight format).
bool loadProblem(const char *filename)
Read in a problem (in svmlight format).
SVM (Support Vector Machines) training class for the SVM machine learning.
void saveClassificationResult(const char *filename)
Save the classification result in an extern file.
bool saveClassProblem(const char *filename)
Save the raw classification problem in a file (in svmlight format).
int cross_validation_
Set too 1 for cross validating the classifier.
void saveClassifierModel(const char *filename)
Save the classifier model in an extern file (in svmlight format).
void setParameters(SVMParam param)
Change default training parameters (pcl::SVMParam).
~SVMClassify()
Destructor.
void adaptInputToLibSVM(std::vector< SVMData > training_set, svm_problem &prob)
Convert the input format (vector of SVMData) into a readable format for libSVM.
bool labelled_training_set_
void resetTrainingSet()
Reset the training set.
static void printNull(const char *)
Set for output printings during classification.
struct svm_node * scaling
char * readline(FILE *input)
To read a line from the input file.
bool loadProblem(const char *filename, svm_problem &prob)
Load a problem from an extern file.
bool trainClassifier()
Start the training of the SVM classifier.
void setInputTrainingSet(std::vector< SVMData > training_set)
It adds/store the training set with labelled data.
void setClassifierModel(SVMModel model)
Set the classifier model.
void getLabel(std::vector< int > &labels)
Return the labels order from the classifier model.
std::vector< SVMData > training_set_
bool classification()
Start the classification on un-labelled input dataset.
const std::string & getClassName() const
Get a string representation of the name of this class.
The structure stores the parameters for the classificationa nd must be initialized and passed to the ...
void scaleProblem(svm_problem &input, svm_scaling scaling)
It scales the input dataset using the model information.
bool debug_
Set to 1 to see the training output.
void setProbabilityEstimates(bool set)
Set whether the classification has to be done with the probability estimate.
SVMModel getClassifierModel()
Return the result of the training.
std::vector< SVMData > getInputTrainingSet()
Return the current training set.