39 #ifndef PCL_SVM_WRAPPER_H_ 40 #define PCL_SVM_WRAPPER_H_ 49 #include <pcl/common/eigen.h> 52 #include <pcl/console/time.h> 54 #include <pcl/ml/svm.h> 55 #define Malloc(type,n) static_cast<type *> (malloc((n)*sizeof(type))) 120 std::vector<pcl::SVMDataPoint>
SV;
122 SVMData () : label (
std::numeric_limits<double>::signaling_NaN())
148 readline (FILE *input);
153 fprintf (stderr,
"Wrong input format at line %d\n", line_num);
158 inline const std::string&
161 return (class_name_);
165 void adaptInputToLibSVM (std::vector<SVMData> training_set,
svm_problem &prob);
168 void adaptLibSVMToInput (std::vector<SVMData> &training_set,
svm_problem prob);
171 bool loadProblem (
const char *filename,
svm_problem &prob);
174 bool saveProblem (
const char *filename,
bool labelled);
177 bool saveProblemNorm (
const char *filename,
svm_problem prob_,
bool labelled);
182 training_set_ (), prob_ (), model_ (), scaling_ (), param_ (),
183 class_name_ (), line_ (NULL), max_line_len_ (10000), labelled_training_set_ (1)
190 svm_destroy_param (¶m_);
192 if (scaling_.
max > 0)
207 int nr_class = svm_get_nr_class (&model_);
208 int *labels_ =
static_cast<int *
> (malloc (nr_class *
sizeof (
int)));
209 svm_get_labels (&model_, labels_);
211 for (
int j = 0 ; j < nr_class; j++)
212 labels.push_back (labels_[j]);
224 if (svm_save_model (filename, &model_))
226 fprintf (stderr,
"can't save model to file %s\n", filename);
260 scaleFactors (std::vector<SVMData> training_set,
svm_scaling &scaling);
264 SVMTrain() : debug_ (0), cross_validation_ (0), nr_fold_ (0)
266 class_name_ =
"SVMTrain";
267 svm_set_print_string_function (&printNull);
274 svm_free_model_content (&model_);
302 training_set_.insert (training_set_.end(), training_set.begin(), training_set.end());
309 return training_set_;
316 training_set_.clear();
339 svm_set_print_string_function (NULL);
341 svm_set_print_string_function (&printNull);
386 SVMClassify () : model_extern_copied_ (0), predict_probability_ (0)
388 class_name_ =
"SvmClassify";
394 if (!model_extern_copied_ && model_.l > 0)
395 svm_free_model_content (&model_);
402 assert (training_set.size() > 0);
404 if (scaling_.max == 0)
407 PCL_ERROR (
"[pcl::%s::setInputTrainingSet] Classifier model not loaded!\n", getClassName ().c_str ());
411 training_set_.insert (training_set_.end(), training_set.begin(), training_set.end());
419 return training_set_;
426 training_set_.clear();
432 loadClassifierModel (
const char *filename);
439 out.insert (out.begin(), prediction_.begin(), prediction_.end());
444 saveClassificationResult (
const char *filename);
454 while (model_.scaling[i].index != -1)
458 scaling_.obj = Malloc (
struct svm_node, i + 1);
459 scaling_.obj[i].index = -1;
462 for (
int j = 0; j < i; j++)
464 scaling_.obj[j] = model_.scaling[j];
467 model_extern_copied_ = 1;
476 assert (model_.l != 0);
480 scaleProblem (prob_, scaling_);
500 predict_probability_ =
set;
507 classificationTest ();
537 #endif // PCL_SVM_WRAPPER_H_
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 saveNormClassProblem(const char *filename)
Save the normalized classification problem in a file (in svmlight format).
bool saveProblemNorm(const char *filename, svm_problem prob_, bool labelled)
Save the problem (with normalized values) in an extern file.
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
The structure stores the features and the label of a single sample which has to be used for the train...
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.
SVMParam getParameters()
Return the current training parameters.
bool loadClassProblem(const char *filename)
Read in a raw classification problem (in svmlight format).
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.
bool predict_probability_
SVM (Support Vector Machines) classification of a dataset.
The structure initialize a model crated by the SVM (Support Vector Machines) classifier (pcl::SVMTrai...
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.
bool saveClassProblem(const char *filename)
Save the raw classification problem in a file (in svmlight format).
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.
std::vector< std::vector< double > > prediction_
bool loadProblem(const char *filename, svm_problem &prob)
Load a problem from an extern file.
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 adaptLibSVMToInput(std::vector< SVMData > &training_set, svm_problem prob)
Convert the libSVM format (svm_problem) into a easier output format.
void getLabel(std::vector< int > &labels)
Return the labels order from the classifier model.
std::vector< SVMData > training_set_
void getClassificationResult(std::vector< std::vector< double > > &out)
Get the result of the classification.
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 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.