Originally, support vector machines (SVM) was a technique for building an optimal binary (2-class) classifier. Later the technique was extended to regression and clustering problems. SVM is a partial case of kernel-based methods. It maps feature vectors into a higher-dimensional space using a kernel function and builds an optimal linear discriminating function in this space or an optimal hyper-plane that fits into the training data. In case of SVM, the kernel is not defined explicitly. Instead, a distance between any 2 points in the hyper-space needs to be defined.
The solution is optimal, which means that the margin between the separating hyper-plane and the nearest feature vectors from both classes (in case of 2-class classifier) is maximal. The feature vectors that are the closest to the hyper-plane are called support vectors, which means that the position of other vectors does not affect the hyper-plane (the decision function).
SVM implementation in OpenCV is based on [LibSVM].
[Burges98] |
|
[LibSVM] | (1, 2) C.-C. Chang and C.-J. Lin. LIBSVM: a library for support vector machines, ACM Transactions on Intelligent Systems and Technology, 2:27:1–27:27, 2011. (http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf) |
CvParamGrid
¶The structure represents the logarithmic grid range of statmodel parameters. It is used for optimizing statmodel accuracy by varying model parameters, the accuracy estimate being computed by cross-validation.
CvParamGrid::
min_val
¶Minimum value of the statmodel parameter.
CvParamGrid::
max_val
¶Maximum value of the statmodel parameter.
CvParamGrid::
step
¶Logarithmic step for iterating the statmodel parameter.
The grid determines the following iteration sequence of the statmodel parameter values:
where is the maximal index satisfying
The grid is logarithmic, so step
must always be greater then 1.
The constructors.
CvParamGrid::
CvParamGrid
()¶
CvParamGrid::
CvParamGrid
(double min_val, double max_val, double log_step)¶The full constructor initializes corresponding members. The default constructor creates a dummy grid:
CvParamGrid::CvParamGrid()
{
min_val = max_val = step = 0;
}
Checks validness of the grid.
bool CvParamGrid::
check
()¶Returns true
if the grid is valid and false
otherwise. The grid is valid if and only if:
CvSVMParams
¶SVM training parameters.
The structure must be initialized and passed to the training method of CvSVM
.
The constructors.
CvSVMParams::
CvSVMParams
()¶
CvSVMParams::
CvSVMParams
(int svm_type, int kernel_type, double degree, double gamma, double coef0, double Cvalue, double nu, double p, CvMat* class_weights, CvTermCriteria term_crit)¶Parameters: |
|
---|
The default constructor initialize the structure with following values:
CvSVMParams::CvSVMParams() :
svm_type(CvSVM::C_SVC), kernel_type(CvSVM::RBF), degree(0),
gamma(1), coef0(0), C(1), nu(0), p(0), class_weights(0)
{
term_crit = cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
}
CvSVM
: public CvStatModel
¶Support Vector Machines.
Note
Default and training constructors.
CvSVM::
CvSVM
()¶
CvSVM::
CvSVM
(const Mat& trainData, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), CvSVMParams params=CvSVMParams() )¶
CvSVM::
CvSVM
(const CvMat* trainData, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, CvSVMParams params=CvSVMParams() )¶
cv2.
SVM
([trainData, responses[, varIdx[, sampleIdx[, params]]]]) → <SVM object>¶The constructors follow conventions of CvStatModel::CvStatModel()
. See CvStatModel::train()
for parameters descriptions.
Trains an SVM.
bool CvSVM::
train
(const Mat& trainData, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), CvSVMParams params=CvSVMParams() )¶
bool CvSVM::
train
(const CvMat* trainData, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, CvSVMParams params=CvSVMParams() )¶
cv2.SVM.
train
(trainData, responses[, varIdx[, sampleIdx[, params]]]) → retval¶The method trains the SVM model. It follows the conventions of the generic CvStatModel::train()
approach with the following limitations:
CV_ROW_SAMPLE
data layout is supported.params.svm_type=CvSVM::C_SVC
or params.svm_type=CvSVM::NU_SVC
), or ordered (params.svm_type=CvSVM::EPS_SVR
or params.svm_type=CvSVM::NU_SVR
), or not required at all (params.svm_type=CvSVM::ONE_CLASS
).All the other parameters are gathered in the
CvSVMParams
structure.
Trains an SVM with optimal parameters.
bool CvSVM::
train_auto
(const Mat& trainData, const Mat& responses, const Mat& varIdx, const Mat& sampleIdx, CvSVMParams params, int k_fold=10, CvParamGrid Cgrid=CvSVM::get_default_grid(CvSVM::C), CvParamGrid gammaGrid=CvSVM::get_default_grid(CvSVM::GAMMA), CvParamGrid pGrid=CvSVM::get_default_grid(CvSVM::P), CvParamGrid nuGrid=CvSVM::get_default_grid(CvSVM::NU), CvParamGrid coeffGrid=CvSVM::get_default_grid(CvSVM::COEF), CvParamGrid degreeGrid=CvSVM::get_default_grid(CvSVM::DEGREE), bool balanced=false)¶
bool CvSVM::
train_auto
(const CvMat* trainData, const CvMat* responses, const CvMat* varIdx, const CvMat* sampleIdx, CvSVMParams params, int kfold=10, CvParamGrid Cgrid=get_default_grid(CvSVM::C), CvParamGrid gammaGrid=get_default_grid(CvSVM::GAMMA), CvParamGrid pGrid=get_default_grid(CvSVM::P), CvParamGrid nuGrid=get_default_grid(CvSVM::NU), CvParamGrid coeffGrid=get_default_grid(CvSVM::COEF), CvParamGrid degreeGrid=get_default_grid(CvSVM::DEGREE), bool balanced=false )¶
cv2.SVM.
train_auto
(trainData, responses, varIdx, sampleIdx, params[, k_fold[, Cgrid[, gammaGrid[, pGrid[, nuGrid[, coeffGrid[, degreeGrid[, balanced]]]]]]]]) → retval¶Parameters: |
|
---|
The method trains the SVM model automatically by choosing the optimal
parameters C
, gamma
, p
, nu
, coef0
, degree
from
CvSVMParams
. Parameters are considered optimal
when the cross-validation estimate of the test set error
is minimal.
If there is no need to optimize a parameter, the corresponding grid step should be set to any value less than or equal to 1. For example, to avoid optimization in gamma
, set gamma_grid.step = 0
, gamma_grid.min_val
, gamma_grid.max_val
as arbitrary numbers. In this case, the value params.gamma
is taken for gamma
.
And, finally, if the optimization in a parameter is required but
the corresponding grid is unknown, you may call the function CvSVM::get_default_grid()
. To generate a grid, for example, for gamma
, call CvSVM::get_default_grid(CvSVM::GAMMA)
.
This function works for the classification
(params.svm_type=CvSVM::C_SVC
or params.svm_type=CvSVM::NU_SVC
)
as well as for the regression
(params.svm_type=CvSVM::EPS_SVR
or params.svm_type=CvSVM::NU_SVR
). If params.svm_type=CvSVM::ONE_CLASS
, no optimization is made and the usual SVM with parameters specified in params
is executed.
Predicts the response for input sample(s).
float CvSVM::
predict
(const Mat& sample, bool returnDFVal=false ) const
¶
float CvSVM::
predict
(const CvMat* sample, bool returnDFVal=false ) const
¶
float CvSVM::
predict
(const CvMat* samples, CvMat* results) const
¶
cv2.SVM.
predict
(sample[, returnDFVal]) → retval¶
cv2.SVM.
predict_all
(samples[, results]) → results¶Parameters: |
|
---|
If you pass one sample then prediction result is returned. If you want to get responses for several samples then you should pass the results
matrix where prediction results will be stored.
The function is parallelized with the TBB library.
Generates a grid for SVM parameters.
CvParamGrid CvSVM::
get_default_grid
(int param_id)¶Parameters: |
|
---|
The function generates a grid for the specified parameter of the SVM algorithm. The grid may be passed to the function CvSVM::train_auto()
.
Returns the current SVM parameters.
CvSVMParams CvSVM::
get_params
() const
¶This function may be used to get the optimal parameters obtained while automatically training CvSVM::train_auto()
.
Retrieves a number of support vectors and the particular vector.
int CvSVM::
get_support_vector_count
() const
¶
const float* CvSVM::
get_support_vector
(int i) const
¶
cv2.SVM.
get_support_vector_count
() → retval¶Parameters: | i – Index of the particular support vector. |
---|
The methods can be used to retrieve a set of support vectors.