OpenCV  4.1.1-pre
Open Source Computer Vision
Looking for a C++ dev who knows OpenCV?
I'm looking for work. Hire me!
cv::ml::SVM Class Referenceabstract

Support Vector Machines. More...

#include <opencv2/ml.hpp>

Inheritance diagram for cv::ml::SVM:
Collaboration diagram for cv::ml::SVM:

Classes

class  Kernel
 

Public Types

enum  Flags {
  UPDATE_MODEL = 1,
  RAW_OUTPUT =1,
  COMPRESSED_INPUT =2,
  PREPROCESSED_INPUT =4
}
 Predict options. More...
 
enum  KernelTypes {
  CUSTOM =-1,
  LINEAR =0,
  POLY =1,
  RBF =2,
  SIGMOID =3,
  CHI2 =4,
  INTER =5
}
 SVM kernel type More...
 
enum  ParamTypes {
  C =0,
  GAMMA =1,
  P =2,
  NU =3,
  COEF =4,
  DEGREE =5
}
 SVM params type More...
 
enum  Types {
  C_SVC =100,
  NU_SVC =101,
  ONE_CLASS =102,
  EPS_SVR =103,
  NU_SVR =104
}
 SVM type More...
 

Public Member Functions

virtual float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 Computes error on the training or test dataset. More...
 
virtual void clear ()
 Clears the algorithm state. More...
 
virtual bool empty () const CV_OVERRIDE
 Returns true if the Algorithm is empty (e.g. More...
 
virtual double getC () const =0
 Parameter C of a SVM optimization problem. More...
 
virtual cv::Mat getClassWeights () const =0
 Optional weights in the SVM::C_SVC problem, assigned to particular classes. More...
 
virtual double getCoef0 () const =0
 Parameter coef0 of a kernel function. More...
 
virtual double getDecisionFunction (int i, OutputArray alpha, OutputArray svidx) const =0
 Retrieves the decision function. More...
 
virtual String getDefaultName () const
 Returns the algorithm string identifier. More...
 
virtual double getDegree () const =0
 Parameter degree of a kernel function. More...
 
virtual double getGamma () const =0
 Parameter \(\gamma\) of a kernel function. More...
 
virtual int getKernelType () const =0
 Type of a SVM kernel. More...
 
virtual double getNu () const =0
 Parameter \(\nu\) of a SVM optimization problem. More...
 
virtual double getP () const =0
 Parameter \(\epsilon\) of a SVM optimization problem. More...
 
virtual Mat getSupportVectors () const =0
 Retrieves all the support vectors. More...
 
virtual cv::TermCriteria getTermCriteria () const =0
 Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem. More...
 
virtual int getType () const =0
 Type of a SVM formulation. More...
 
virtual Mat getUncompressedSupportVectors () const =0
 Retrieves all the uncompressed support vectors of a linear SVM. More...
 
virtual int getVarCount () const =0
 Returns the number of variables in training samples. More...
 
virtual bool isClassifier () const =0
 Returns true if the model is classifier. More...
 
virtual bool isTrained () const =0
 Returns true if the model is trained. More...
 
virtual float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 Predicts response(s) for the provided sample(s) More...
 
virtual void read (const FileNode &fn)
 Reads algorithm parameters from a file storage. More...
 
virtual void save (const String &filename) const
 Saves the algorithm to a file. More...
 
virtual void setC (double val)=0
 Parameter C of a SVM optimization problem. More...
 
virtual void setClassWeights (const cv::Mat &val)=0
 Optional weights in the SVM::C_SVC problem, assigned to particular classes. More...
 
virtual void setCoef0 (double val)=0
 Parameter coef0 of a kernel function. More...
 
virtual void setCustomKernel (const Ptr< Kernel > &_kernel)=0
 Initialize with custom kernel. More...
 
virtual void setDegree (double val)=0
 Parameter degree of a kernel function. More...
 
virtual void setGamma (double val)=0
 Parameter \(\gamma\) of a kernel function. More...
 
virtual void setKernel (int kernelType)=0
 Initialize with one of predefined kernels. More...
 
virtual void setNu (double val)=0
 Parameter \(\nu\) of a SVM optimization problem. More...
 
virtual void setP (double val)=0
 Parameter \(\epsilon\) of a SVM optimization problem. More...
 
virtual void setTermCriteria (const cv::TermCriteria &val)=0
 Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem. More...
 
virtual void setType (int val)=0
 Type of a SVM formulation. More...
 
virtual bool train (const Ptr< TrainData > &trainData, int flags=0)
 Trains the statistical model. More...
 
virtual bool train (InputArray samples, int layout, InputArray responses)
 Trains the statistical model. More...
 
virtual bool trainAuto (const Ptr< TrainData > &data, int kFold=10, ParamGrid Cgrid=getDefaultGrid(C), ParamGrid gammaGrid=getDefaultGrid(GAMMA), ParamGrid pGrid=getDefaultGrid(P), ParamGrid nuGrid=getDefaultGrid(NU), ParamGrid coeffGrid=getDefaultGrid(COEF), ParamGrid degreeGrid=getDefaultGrid(DEGREE), bool balanced=false)=0
 Trains an SVM with optimal parameters. More...
 
virtual bool trainAuto (InputArray samples, int layout, InputArray responses, int kFold=10, Ptr< ParamGrid > Cgrid=SVM::getDefaultGridPtr(SVM::C), Ptr< ParamGrid > gammaGrid=SVM::getDefaultGridPtr(SVM::GAMMA), Ptr< ParamGrid > pGrid=SVM::getDefaultGridPtr(SVM::P), Ptr< ParamGrid > nuGrid=SVM::getDefaultGridPtr(SVM::NU), Ptr< ParamGrid > coeffGrid=SVM::getDefaultGridPtr(SVM::COEF), Ptr< ParamGrid > degreeGrid=SVM::getDefaultGridPtr(SVM::DEGREE), bool balanced=false)=0
 Trains an SVM with optimal parameters. More...
 
virtual void write (FileStorage &fs) const
 Stores algorithm parameters in a file storage. More...
 
void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 simplified API for language bindings This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 

Static Public Member Functions

static Ptr< SVMcreate ()
 Creates empty model. More...
 
static ParamGrid getDefaultGrid (int param_id)
 Generates a grid for SVM parameters. More...
 
static Ptr< ParamGridgetDefaultGridPtr (int param_id)
 Generates a grid for SVM parameters. More...
 
static Ptr< SVMload (const String &filepath)
 Loads and creates a serialized svm from a file. More...
 
template<typename _Tp >
static Ptr< _Tp > load (const String &filename, const String &objname=String())
 Loads algorithm from the file. More...
 
template<typename _Tp >
static Ptr< _Tp > loadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String. More...
 
template<typename _Tp >
static Ptr< _Tp > read (const FileNode &fn)
 Reads algorithm from the file node. More...
 
template<typename _Tp >
static Ptr< _Tp > train (const Ptr< TrainData > &data, int flags=0)
 Create and train model with default parameters. More...
 

Protected Member Functions

void writeFormat (FileStorage &fs) const
 

Detailed Description

Support Vector Machines.

See also
Support Vector Machines

Member Enumeration Documentation

◆ Flags

enum cv::ml::StatModel::Flags
inherited

Predict options.

Enumerator
UPDATE_MODEL 
RAW_OUTPUT 

makes the method return the raw results (the sum), not the class label

COMPRESSED_INPUT 
PREPROCESSED_INPUT 

◆ KernelTypes

SVM kernel type

A comparison of different kernels on the following 2D test case with four classes. Four SVM::C_SVC SVMs have been trained (one against rest) with auto_train. Evaluation on three different kernels (SVM::CHI2, SVM::INTER, SVM::RBF). The color depicts the class with max score. Bright means max-score > 0, dark means max-score < 0.

SVM_Comparison.png
image
Enumerator
CUSTOM 

Returned by SVM::getKernelType in case when custom kernel has been set.

LINEAR 

Linear kernel.

No mapping is done, linear discrimination (or regression) is done in the original feature space. It is the fastest option. \(K(x_i, x_j) = x_i^T x_j\).

POLY 

Polynomial kernel: \(K(x_i, x_j) = (\gamma x_i^T x_j + coef0)^{degree}, \gamma > 0\).

RBF 

Radial basis function (RBF), a good choice in most cases.

\(K(x_i, x_j) = e^{-\gamma ||x_i - x_j||^2}, \gamma > 0\).

SIGMOID 

Sigmoid kernel: \(K(x_i, x_j) = \tanh(\gamma x_i^T x_j + coef0)\).

CHI2 

Exponential Chi2 kernel, similar to the RBF kernel: \(K(x_i, x_j) = e^{-\gamma \chi^2(x_i,x_j)}, \chi^2(x_i,x_j) = (x_i-x_j)^2/(x_i+x_j), \gamma > 0\).

INTER 

Histogram intersection kernel.

A fast kernel. \(K(x_i, x_j) = min(x_i,x_j)\).

◆ ParamTypes

SVM params type

Enumerator
GAMMA 
NU 
COEF 
DEGREE 

◆ Types

SVM type

Enumerator
C_SVC 

C-Support Vector Classification.

n-class classification (n \(\geq\) 2), allows imperfect separation of classes with penalty multiplier C for outliers.

NU_SVC 

\(\nu\)-Support Vector Classification.

n-class classification with possible imperfect separation. Parameter \(\nu\) (in the range 0..1, the larger the value, the smoother the decision boundary) is used instead of C.

ONE_CLASS 

Distribution Estimation (One-class SVM).

All the training data are from the same class, SVM builds a boundary that separates the class from the rest of the feature space.

EPS_SVR 

\(\epsilon\)-Support Vector Regression.

The distance between feature vectors from the training set and the fitting hyper-plane must be less than p. For outliers the penalty multiplier C is used.

NU_SVR 

\(\nu\)-Support Vector Regression.

\(\nu\) is used instead of p. See [19] for details.

Member Function Documentation

◆ calcError()

virtual float cv::ml::StatModel::calcError ( const Ptr< TrainData > &  data,
bool  test,
OutputArray  resp 
) const
virtualinherited

Computes error on the training or test dataset.

Parameters
datathe training data
testif true, the error is computed over the test subset of the data, otherwise it's computed over the training subset of the data. Please note that if you loaded a completely different dataset to evaluate already trained classifier, you will probably want not to set the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so that the error is computed for the whole new set. Yes, this sounds a bit confusing.
respthe optional output responses.

The method uses StatModel::predict to compute the error. For regression models the error is computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).

◆ clear()

virtual void cv::Algorithm::clear ( )
inlinevirtualinherited

Clears the algorithm state.

Reimplemented in cv::FlannBasedMatcher, and cv::DescriptorMatcher.

◆ create()

static Ptr<SVM> cv::ml::SVM::create ( )
static

Creates empty model.

Use StatModel::train to train the model. Since SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto.

Examples:
samples/cpp/train_HOG.cpp.

◆ empty()

virtual bool cv::ml::StatModel::empty ( ) const
virtualinherited

Returns true if the Algorithm is empty (e.g.

in the very beginning or after unsuccessful read

Reimplemented from cv::Algorithm.

◆ getC()

virtual double cv::ml::SVM::getC ( ) const
pure virtual

Parameter C of a SVM optimization problem.

For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0.

See also
setC

◆ getClassWeights()

virtual cv::Mat cv::ml::SVM::getClassWeights ( ) const
pure virtual

Optional weights in the SVM::C_SVC problem, assigned to particular classes.

They are multiplied by C so the parameter C of class i becomes classWeights(i) * C. Thus these weights affect the misclassification penalty for different classes. The larger weight, the larger penalty on misclassification of data from the corresponding class. Default value is empty Mat.

See also
setClassWeights

◆ getCoef0()

virtual double cv::ml::SVM::getCoef0 ( ) const
pure virtual

Parameter coef0 of a kernel function.

For SVM::POLY or SVM::SIGMOID. Default value is 0.

See also
setCoef0

◆ getDecisionFunction()

virtual double cv::ml::SVM::getDecisionFunction ( int  i,
OutputArray  alpha,
OutputArray  svidx 
) const
pure virtual

Retrieves the decision function.

Parameters
ithe index of the decision function. If the problem solved is regression, 1-class or 2-class classification, then there will be just one decision function and the index should always be 0. Otherwise, in the case of N-class classification, there will be \(N(N-1)/2\) decision functions.
alphathe optional output vector for weights, corresponding to different support vectors. In the case of linear SVM all the alpha's will be 1's.
svidxthe optional output vector of indices of support vectors within the matrix of support vectors (which can be retrieved by SVM::getSupportVectors). In the case of linear SVM each decision function consists of a single "compressed" support vector.

The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.

◆ getDefaultGrid()

static ParamGrid cv::ml::SVM::getDefaultGrid ( int  param_id)
static

Generates a grid for SVM parameters.

Parameters
param_idSVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.

The function generates a grid for the specified parameter of the SVM algorithm. The grid may be passed to the function SVM::trainAuto.

◆ getDefaultGridPtr()

static Ptr<ParamGrid> cv::ml::SVM::getDefaultGridPtr ( int  param_id)
static

Generates a grid for SVM parameters.

Parameters
param_idSVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.

The function generates a grid pointer for the specified parameter of the SVM algorithm. The grid may be passed to the function SVM::trainAuto.

◆ getDefaultName()

virtual String cv::Algorithm::getDefaultName ( ) const
virtualinherited

Returns the algorithm string identifier.

This string is used as top level xml/yml node tag when the object is saved to a file or string.

Reimplemented in cv::AKAZE, cv::KAZE, cv::SimpleBlobDetector, cv::GFTTDetector, cv::AgastFeatureDetector, cv::FastFeatureDetector, cv::MSER, cv::ORB, cv::BRISK, and cv::Feature2D.

◆ getDegree()

virtual double cv::ml::SVM::getDegree ( ) const
pure virtual

Parameter degree of a kernel function.

For SVM::POLY. Default value is 0.

See also
setDegree

◆ getGamma()

virtual double cv::ml::SVM::getGamma ( ) const
pure virtual

Parameter \(\gamma\) of a kernel function.

For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1.

See also
setGamma

◆ getKernelType()

virtual int cv::ml::SVM::getKernelType ( ) const
pure virtual

Type of a SVM kernel.

See SVM::KernelTypes. Default value is SVM::RBF.

◆ getNu()

virtual double cv::ml::SVM::getNu ( ) const
pure virtual

Parameter \(\nu\) of a SVM optimization problem.

For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0.

See also
setNu

◆ getP()

virtual double cv::ml::SVM::getP ( ) const
pure virtual

Parameter \(\epsilon\) of a SVM optimization problem.

For SVM::EPS_SVR. Default value is 0.

See also
setP

◆ getSupportVectors()

virtual Mat cv::ml::SVM::getSupportVectors ( ) const
pure virtual

Retrieves all the support vectors.

The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows.

◆ getTermCriteria()

virtual cv::TermCriteria cv::ml::SVM::getTermCriteria ( ) const
pure virtual

Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem.

You can specify tolerance and/or the maximum number of iterations. Default value is TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON );

See also
setTermCriteria

◆ getType()

virtual int cv::ml::SVM::getType ( ) const
pure virtual

Type of a SVM formulation.

See SVM::Types. Default value is SVM::C_SVC.

See also
setType

◆ getUncompressedSupportVectors()

virtual Mat cv::ml::SVM::getUncompressedSupportVectors ( ) const
pure virtual

Retrieves all the uncompressed support vectors of a linear SVM.

The method returns all the uncompressed support vectors of a linear SVM that the compressed support vector, used for prediction, was derived from. They are returned in a floating-point matrix, where the support vectors are stored as matrix rows.

◆ getVarCount()

virtual int cv::ml::StatModel::getVarCount ( ) const
pure virtualinherited

Returns the number of variables in training samples.

◆ isClassifier()

virtual bool cv::ml::StatModel::isClassifier ( ) const
pure virtualinherited

Returns true if the model is classifier.

◆ isTrained()

virtual bool cv::ml::StatModel::isTrained ( ) const
pure virtualinherited

Returns true if the model is trained.

◆ load() [1/2]

static Ptr<SVM> cv::ml::SVM::load ( const String filepath)
static

Loads and creates a serialized svm from a file.

Use SVM::save to serialize and store an SVM to disk. Load the SVM from this file again, by calling this function with the path to the file.

Parameters
filepathpath to serialized svm

◆ load() [2/2]

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::load ( const String filename,
const String objname = String() 
)
inlinestaticinherited

Loads algorithm from the file.

Parameters
filenameName of the file to read.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

Ptr<SVM> svm = Algorithm::load<SVM>("my_svm_model.xml");

In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn).

References CV_Assert, cv::FileNode::empty(), cv::FileStorage::getFirstTopLevelNode(), cv::FileStorage::isOpened(), and cv::FileStorage::READ.

Here is the call graph for this function:

◆ loadFromString()

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::loadFromString ( const String strModel,
const String objname = String() 
)
inlinestaticinherited

Loads algorithm from a String.

Parameters
strModelThe string variable containing the model you want to load.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

Ptr<SVM> svm = Algorithm::loadFromString<SVM>(myStringModel);

References CV_WRAP, cv::FileNode::empty(), cv::FileStorage::getFirstTopLevelNode(), cv::FileStorage::MEMORY, and cv::FileStorage::READ.

Here is the call graph for this function:

◆ predict()

virtual float cv::ml::StatModel::predict ( InputArray  samples,
OutputArray  results = noArray(),
int  flags = 0 
) const
pure virtualinherited

Predicts response(s) for the provided sample(s)

Parameters
samplesThe input samples, floating-point matrix
resultsThe optional output matrix of results.
flagsThe optional flags, model-dependent. See cv::ml::StatModel::Flags.

Implemented in cv::ml::LogisticRegression, and cv::ml::EM.

◆ read() [1/2]

virtual void cv::Algorithm::read ( const FileNode fn)
inlinevirtualinherited

Reads algorithm parameters from a file storage.

Reimplemented in cv::FlannBasedMatcher, cv::DescriptorMatcher, and cv::Feature2D.

◆ read() [2/2]

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::read ( const FileNode fn)
inlinestaticinherited

Reads algorithm from the file node.

This is static template method of Algorithm. It's usage is following (in the case of SVM):

cv::FileStorage fsRead("example.xml", FileStorage::READ);
Ptr<SVM> svm = Algorithm::read<SVM>(fsRead.root());

In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn) and also have static create() method without parameters (or with all the optional parameters)

◆ save()

virtual void cv::Algorithm::save ( const String filename) const
virtualinherited

Saves the algorithm to a file.

In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

◆ setC()

virtual void cv::ml::SVM::setC ( double  val)
pure virtual

Parameter C of a SVM optimization problem.

See also
getC

◆ setClassWeights()

virtual void cv::ml::SVM::setClassWeights ( const cv::Mat val)
pure virtual

Optional weights in the SVM::C_SVC problem, assigned to particular classes.

See also
getClassWeights

◆ setCoef0()

virtual void cv::ml::SVM::setCoef0 ( double  val)
pure virtual

Parameter coef0 of a kernel function.

See also
getCoef0

◆ setCustomKernel()

virtual void cv::ml::SVM::setCustomKernel ( const Ptr< Kernel > &  _kernel)
pure virtual

Initialize with custom kernel.

See SVM::Kernel class for implementation details

◆ setDegree()

virtual void cv::ml::SVM::setDegree ( double  val)
pure virtual

Parameter degree of a kernel function.

See also
getDegree

◆ setGamma()

virtual void cv::ml::SVM::setGamma ( double  val)
pure virtual

Parameter \(\gamma\) of a kernel function.

See also
getGamma

◆ setKernel()

virtual void cv::ml::SVM::setKernel ( int  kernelType)
pure virtual

Initialize with one of predefined kernels.

See SVM::KernelTypes.

◆ setNu()

virtual void cv::ml::SVM::setNu ( double  val)
pure virtual

Parameter \(\nu\) of a SVM optimization problem.

See also
getNu

◆ setP()

virtual void cv::ml::SVM::setP ( double  val)
pure virtual

Parameter \(\epsilon\) of a SVM optimization problem.

See also
getP

◆ setTermCriteria()

virtual void cv::ml::SVM::setTermCriteria ( const cv::TermCriteria val)
pure virtual

Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem.

See also
getTermCriteria

◆ setType()

virtual void cv::ml::SVM::setType ( int  val)
pure virtual

Type of a SVM formulation.

See also
getType

◆ train() [1/3]

virtual bool cv::ml::StatModel::train ( const Ptr< TrainData > &  trainData,
int  flags = 0 
)
virtualinherited

Trains the statistical model.

Parameters
trainDatatraining data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.
flagsoptional flags, depending on the model. Some of the models can be updated with the new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).

◆ train() [2/3]

virtual bool cv::ml::StatModel::train ( InputArray  samples,
int  layout,
InputArray  responses 
)
virtualinherited

Trains the statistical model.

Parameters
samplestraining samples
layoutSee ml::SampleTypes.
responsesvector of responses associated with the training samples.

◆ train() [3/3]

template<typename _Tp >
static Ptr<_Tp> cv::ml::StatModel::train ( const Ptr< TrainData > &  data,
int  flags = 0 
)
inlinestaticinherited

Create and train model with default parameters.

The class must implement static create() method with no parameters or with all default parameter values

◆ trainAuto() [1/2]

virtual bool cv::ml::SVM::trainAuto ( const Ptr< TrainData > &  data,
int  kFold = 10,
ParamGrid  Cgrid = getDefaultGrid(C),
ParamGrid  gammaGrid = getDefaultGrid(GAMMA),
ParamGrid  pGrid = getDefaultGrid(P),
ParamGrid  nuGrid = getDefaultGrid(NU),
ParamGrid  coeffGrid = getDefaultGrid(COEF),
ParamGrid  degreeGrid = getDefaultGrid(DEGREE),
bool  balanced = false 
)
pure virtual

Trains an SVM with optimal parameters.

Parameters
datathe training data that can be constructed using TrainData::create or TrainData::loadFromCSV.
kFoldCross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the SVM algorithm is executed kFold times.
Cgridgrid for C
gammaGridgrid for gamma
pGridgrid for p
nuGridgrid for nu
coeffGridgrid for coeff
degreeGridgrid for degree
balancedIf true and the problem is 2-class classification then the method creates more balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset.

The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. 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 gammaGrid.step = 0, gammaGrid.minVal, gamma_grid.maxVal as arbitrary numbers. In this case, the value 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 SVM::getDefaultGrid. To generate a grid, for example, for gamma, call SVM::getDefaultGrid(SVM::GAMMA).

This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.

◆ trainAuto() [2/2]

virtual bool cv::ml::SVM::trainAuto ( InputArray  samples,
int  layout,
InputArray  responses,
int  kFold = 10,
Ptr< ParamGrid Cgrid = SVM::getDefaultGridPtr(SVM::C),
Ptr< ParamGrid gammaGrid = SVM::getDefaultGridPtr(SVM::GAMMA),
Ptr< ParamGrid pGrid = SVM::getDefaultGridPtr(SVM::P),
Ptr< ParamGrid nuGrid = SVM::getDefaultGridPtr(SVM::NU),
Ptr< ParamGrid coeffGrid = SVM::getDefaultGridPtr(SVM::COEF),
Ptr< ParamGrid degreeGrid = SVM::getDefaultGridPtr(SVM::DEGREE),
bool  balanced = false 
)
pure virtual

Trains an SVM with optimal parameters.

Parameters
samplestraining samples
layoutSee ml::SampleTypes.
responsesvector of responses associated with the training samples.
kFoldCross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the SVM algorithm is
Cgridgrid for C
gammaGridgrid for gamma
pGridgrid for p
nuGridgrid for nu
coeffGridgrid for coeff
degreeGridgrid for degree
balancedIf true and the problem is 2-class classification then the method creates more balanced cross-validation subsets that is proportions between classes in subsets are close to such proportion in the whole train dataset.

The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal.

This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options.

This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.

◆ write() [1/2]

virtual void cv::Algorithm::write ( FileStorage fs) const
inlinevirtualinherited

Stores algorithm parameters in a file storage.

Reimplemented in cv::FlannBasedMatcher, cv::DescriptorMatcher, and cv::Feature2D.

References CV_WRAP.

Referenced by cv::Feature2D::write(), and cv::DescriptorMatcher::write().

Here is the caller graph for this function:

◆ write() [2/2]

void cv::Algorithm::write ( const Ptr< FileStorage > &  fs,
const String name = String() 
) const
inherited

simplified API for language bindings This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ writeFormat()

void cv::Algorithm::writeFormat ( FileStorage fs) const
protectedinherited

The documentation for this class was generated from the following file: