-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSupportVectorMachine.h
More file actions
113 lines (90 loc) · 3.46 KB
/
SupportVectorMachine.h
File metadata and controls
113 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef SUPPORT_VECTOR_MACHINE_H
#define SUPPORT_VECTOR_MACHINE_H
#include "Common.h"
#include "Feature.h"
#include "PascalImageDatabase.h"
//! Support Vector Machine Class
/*!
This class is a wrapper for LIBSVM that allows the SVM training from the image database.
It creates a primal form of the weights so it can be used along with the OPENCV framework.
*/
class SupportVectorMachine
{
private:
struct svm_parameter _param; // set by parse_command_line
struct svm_problem _prob; // set by read_problem
struct svm_model *_model;
struct svm_node *_x_space;
svm_node *_data;
private:
//! De allocate memory
void _deinit();
public:
//! Constructor
SupportVectorMachine();
//! Loads SVM with user-defined parameters
/*!
\param params ParameterMap containing the SVM configuration
*/
SupportVectorMachine(const ParametersMap ¶ms);
//! Loads SVM model from file
/*!
\param modelFName Path to a file containing the SVM configuration
*/
SupportVectorMachine(const std::string &modelFName);
//! Destructor
~SupportVectorMachine();
//! Train the SVM model
void train(const std::vector<float> &labels, FeatureCollection &features, std::string svmModelFName);
//! Predict the decision value of a feature
/*!
Run classifier on feature, size of feature must match one used for model training.
\param feature HOG calculated features from an image
*/
float predict(const Feature &feature) const;
//! Predict the decision value of a feature
/*!
Run classifier on feature, size of feature must match one used for model training.
\param feature HOG calculated features from an image
*/
//float predict(const vector<float> &feature) const;
//! Predict the label of a feature
/*!
Run classifier on feature, size of feature must match one used for model training.
\param feature HOG calculated features from an image
*/
float predictLabel(const Feature &feature) const;
//! Predict the label of a feature
/*!
Run classifier on feature, size of feature must match one used for model training.
\param feature HOG calculated features from an image
*/
float predictLabel(const vector<float> &feature, double& decisionValue) const;
//! Gets a collection of predictions given a collection of features
std::vector<float> predict(const FeatureCollection &fset);
std::vector<float> predictLabel(const FeatureCollection &fset) const;
//! Get the primal form for the svm
std::vector<float> getDetector() const;
//! Print the parameters chosen for the SVM
void printSVMParameters();
//! Get SVM weights in the shape of the original features
//vector<float> getWeights() const;
double getBiasTerm() const;
//! Get default parameters
static ParametersMap getDefaultParameters();
ParametersMap getParameters();
//Mat renderSVMWeights(const FeatureExtractor *featExtractor);
//! Load model to file
/*!
\param filename Path where the configuration file is located.
*/
svm_model * load(const std::string &filename);
//! Save model to file
/*!
\param filename Path where the configuration file will be located.
*/
void save(const std::string &filename) const;
//! Verify if the svm is initiallized
bool initialized() const { return _model != NULL; }
};
#endif // SUPPORT_VECTOR_MACHINE_H