opencv on mbed
SVM Class Reference
[Machine Learning]
Support Vector Machines. More...
#include <ml.hpp>
Inherits cv::ml::StatModel.
Public Types | |
enum | Types { C_SVC = 100, NU_SVC = 101, ONE_CLASS = 102, EPS_SVR = 103, NU_SVR = 104 } |
SVM type More... | |
enum | KernelTypes { CUSTOM = -1, LINEAR = 0, POLY = 1, RBF = 2, SIGMOID = 3, CHI2 = 4, INTER = 5 } |
SVM kernel type More... | |
enum | ParamTypes |
SVM params type More... | |
enum | Flags { , RAW_OUTPUT = 1 } |
Predict options. More... | |
Public Member Functions | |
virtual CV_WRAP int | getType () const =0 |
Type of a SVM formulation. | |
virtual CV_WRAP void | setType (int val)=0 |
Type of a SVM formulation. | |
virtual CV_WRAP double | getGamma () const =0 |
Parameter of a kernel function. | |
virtual CV_WRAP void | setGamma (double val)=0 |
Parameter of a kernel function. | |
virtual CV_WRAP double | getCoef0 () const =0 |
Parameter _coef0_ of a kernel function. | |
virtual CV_WRAP void | setCoef0 (double val)=0 |
Parameter _coef0_ of a kernel function. | |
virtual CV_WRAP double | getDegree () const =0 |
Parameter _degree_ of a kernel function. | |
virtual CV_WRAP void | setDegree (double val)=0 |
Parameter _degree_ of a kernel function. | |
virtual CV_WRAP double | getC () const =0 |
Parameter _C_ of a SVM optimization problem. | |
virtual CV_WRAP void | setC (double val)=0 |
Parameter _C_ of a SVM optimization problem. | |
virtual CV_WRAP double | getNu () const =0 |
Parameter of a SVM optimization problem. | |
virtual CV_WRAP void | setNu (double val)=0 |
Parameter of a SVM optimization problem. | |
virtual CV_WRAP double | getP () const =0 |
Parameter of a SVM optimization problem. | |
virtual CV_WRAP void | setP (double val)=0 |
Parameter of a SVM optimization problem. | |
virtual CV_WRAP cv::Mat | getClassWeights () const =0 |
Optional weights in the SVM::C_SVC problem, assigned to particular classes. | |
virtual CV_WRAP void | setClassWeights (const cv::Mat &val)=0 |
Optional weights in the SVM::C_SVC problem, assigned to particular classes. | |
virtual CV_WRAP cv::TermCriteria | getTermCriteria () const =0 |
Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem. | |
virtual CV_WRAP 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. | |
virtual CV_WRAP int | getKernelType () const =0 |
Type of a SVM kernel. | |
virtual CV_WRAP void | setKernel (int kernelType)=0 |
Initialize with one of predefined kernels. | |
virtual void | setCustomKernel (const Ptr< Kernel > &_kernel)=0 |
Initialize with custom kernel. | |
virtual bool | trainAuto (const Ptr< TrainData > &data, int kFold=10, ParamGrid Cgrid=SVM::getDefaultGrid(SVM::C), ParamGrid gammaGrid=SVM::getDefaultGrid(SVM::GAMMA), ParamGrid pGrid=SVM::getDefaultGrid(SVM::P), ParamGrid nuGrid=SVM::getDefaultGrid(SVM::NU), ParamGrid coeffGrid=SVM::getDefaultGrid(SVM::COEF), ParamGrid degreeGrid=SVM::getDefaultGrid(SVM::DEGREE), bool balanced=false)=0 |
Trains an SVM with optimal parameters. | |
virtual CV_WRAP Mat | getSupportVectors () const =0 |
Retrieves all the support vectors. | |
CV_WRAP Mat | getUncompressedSupportVectors () const |
Retrieves all the uncompressed support vectors of a linear SVM. | |
virtual CV_WRAP double | getDecisionFunction (int i, OutputArray alpha, OutputArray svidx) const =0 |
Retrieves the decision function. | |
virtual CV_WRAP int | getVarCount () const =0 |
Returns the number of variables in training samples. | |
virtual CV_WRAP bool | empty () const |
Returns true if the Algorithm is empty (e.g. | |
virtual CV_WRAP bool | isTrained () const =0 |
Returns true if the model is trained. | |
virtual CV_WRAP bool | isClassifier () const =0 |
Returns true if the model is classifier. | |
virtual CV_WRAP bool | train (const Ptr< TrainData > &trainData, int flags=0) |
Trains the statistical model. | |
virtual CV_WRAP bool | train (InputArray samples, int layout, InputArray responses) |
Trains the statistical model. | |
virtual CV_WRAP float | calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const |
Computes error on the training or test dataset. | |
virtual CV_WRAP float | predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0 |
Predicts response(s) for the provided sample(s) | |
virtual CV_WRAP void | clear () |
Clears the algorithm state. | |
virtual void | write (FileStorage &fs) const |
Stores algorithm parameters in a file storage. | |
virtual void | read (const FileNode &fn) |
Reads algorithm parameters from a file storage. | |
virtual CV_WRAP void | save (const String &filename) const |
Saves the algorithm to a file. | |
virtual CV_WRAP String | getDefaultName () const |
Returns the algorithm string identifier. | |
Static Public Member Functions | |
static ParamGrid | getDefaultGrid (int param_id) |
Generates a grid for SVM parameters. | |
static CV_WRAP Ptr< SVM > | create () |
Creates empty model. | |
template<typename _Tp > | |
static Ptr< _Tp > | train (const Ptr< TrainData > &data, int flags=0) |
Create and train model with default parameters. | |
template<typename _Tp > | |
static Ptr< _Tp > | read (const FileNode &fn) |
Reads algorithm from the file node. | |
template<typename _Tp > | |
static Ptr< _Tp > | load (const String &filename, const String &objname=String()) |
Loads algorithm from the file. | |
template<typename _Tp > | |
static Ptr< _Tp > | loadFromString (const String &strModel, const String &objname=String()) |
Loads algorithm from a String. |
Detailed Description
Support Vector Machines.
- See also:
- ml_intro_svm
Definition at line 481 of file ml.hpp.
Member Enumeration Documentation
enum Flags [inherited] |
enum 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. ![image](pics/SVM_Comparison.png)
- 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. .
POLY Polynomial kernel: .
RBF Radial basis function (RBF), a good choice in most cases.
.
SIGMOID Sigmoid kernel: .
CHI2 Exponential Chi2 kernel, similar to the RBF kernel: .
INTER Histogram intersection kernel.
A fast kernel. .
enum ParamTypes |
enum Types |
SVM type
- Enumerator:
Member Function Documentation
virtual CV_WRAP float calcError | ( | const Ptr< TrainData > & | data, |
bool | test, | ||
OutputArray | resp | ||
) | const [virtual, inherited] |
Computes error on the training or test dataset.
- Parameters:
-
data the training data test if 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. resp the 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%).
virtual CV_WRAP void clear | ( | ) | [virtual, inherited] |
Clears the algorithm state.
Reimplemented in DescriptorMatcher, and FlannBasedMatcher.
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.
virtual CV_WRAP bool empty | ( | ) | const [virtual, inherited] |
virtual CV_WRAP double 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
virtual CV_WRAP cv::Mat 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
virtual CV_WRAP double getCoef0 | ( | ) | const [pure virtual] |
Parameter _coef0_ of a kernel function.
For SVM::POLY or SVM::SIGMOID. Default value is 0.
- See also:
- setCoef0
virtual CV_WRAP double getDecisionFunction | ( | int | i, |
OutputArray | alpha, | ||
OutputArray | svidx | ||
) | const [pure virtual] |
Retrieves the decision function.
- Parameters:
-
i the 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 decision functions. alpha the optional output vector for weights, corresponding to different support vectors. In the case of linear SVM all the alpha's will be 1's. svidx the 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.
static ParamGrid getDefaultGrid | ( | int | param_id ) | [static] |
Generates a grid for SVM parameters.
- Parameters:
-
param_id SVM 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.
virtual CV_WRAP String getDefaultName | ( | ) | const [virtual, inherited] |
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.
virtual CV_WRAP double getDegree | ( | ) | const [pure virtual] |
virtual CV_WRAP double getGamma | ( | ) | const [pure virtual] |
Parameter of a kernel function.
For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1.
- See also:
- setGamma
virtual CV_WRAP int getKernelType | ( | ) | const [pure virtual] |
Type of a SVM kernel.
See SVM::KernelTypes. Default value is SVM::RBF.
virtual CV_WRAP double getNu | ( | ) | const [pure virtual] |
Parameter of a SVM optimization problem.
For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0.
- See also:
- setNu
virtual CV_WRAP double getP | ( | ) | const [pure virtual] |
virtual CV_WRAP Mat 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.
virtual CV_WRAP cv::TermCriteria 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
virtual CV_WRAP int getType | ( | ) | const [pure virtual] |
CV_WRAP Mat getUncompressedSupportVectors | ( | ) | const |
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.
virtual CV_WRAP int getVarCount | ( | ) | const [pure virtual, inherited] |
Returns the number of variables in training samples.
virtual CV_WRAP bool isClassifier | ( | ) | const [pure virtual, inherited] |
Returns true if the model is classifier.
virtual CV_WRAP bool isTrained | ( | ) | const [pure virtual, inherited] |
Returns true if the model is trained.
static Ptr<_Tp> load | ( | const String & | filename, |
const String & | objname = String() |
||
) | [static, inherited] |
Loads algorithm from the file.
- Parameters:
-
filename Name of the file to read. objname The 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).
static Ptr<_Tp> loadFromString | ( | const String & | strModel, |
const String & | objname = String() |
||
) | [static, inherited] |
Loads algorithm from a String.
- Parameters:
-
strModel The string variable containing the model you want to load. objname The 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);
virtual CV_WRAP float predict | ( | InputArray | samples, |
OutputArray | results = noArray() , |
||
int | flags = 0 |
||
) | const [pure virtual, inherited] |
Predicts response(s) for the provided sample(s)
- Parameters:
-
samples The input samples, floating-point matrix results The optional output matrix of results. flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
Implemented in LogisticRegression.
virtual void read | ( | const FileNode & | fn ) | [virtual, inherited] |
Reads algorithm parameters from a file storage.
Reimplemented in DescriptorMatcher, and FlannBasedMatcher.
Reads algorithm from the file node.
This is static template method of Algorithm. It's usage is following (in the case of SVM):
Ptr<SVM> svm = Algorithm::read<SVM>(fn);
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)
Reimplemented in DescriptorMatcher, and FlannBasedMatcher.
virtual CV_WRAP void save | ( | const String & | filename ) | const [virtual, inherited] |
Saves the algorithm to a file.
In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).
virtual CV_WRAP void setC | ( | double | val ) | [pure virtual] |
Parameter _C_ of a SVM optimization problem.
- See also:
- getC
virtual CV_WRAP void setClassWeights | ( | const cv::Mat & | val ) | [pure virtual] |
Optional weights in the SVM::C_SVC problem, assigned to particular classes.
- See also:
- getClassWeights
virtual CV_WRAP void setCoef0 | ( | double | val ) | [pure virtual] |
Parameter _coef0_ of a kernel function.
- See also:
- getCoef0
virtual void setCustomKernel | ( | const Ptr< Kernel > & | _kernel ) | [pure virtual] |
Initialize with custom kernel.
See SVM::Kernel class for implementation details
virtual CV_WRAP void setDegree | ( | double | val ) | [pure virtual] |
Parameter _degree_ of a kernel function.
- See also:
- getDegree
virtual CV_WRAP void setGamma | ( | double | val ) | [pure virtual] |
Parameter of a kernel function.
- See also:
- getGamma
virtual CV_WRAP void setKernel | ( | int | kernelType ) | [pure virtual] |
Initialize with one of predefined kernels.
See SVM::KernelTypes.
virtual CV_WRAP void setNu | ( | double | val ) | [pure virtual] |
Parameter of a SVM optimization problem.
- See also:
- getNu
virtual CV_WRAP void setP | ( | double | val ) | [pure virtual] |
Parameter of a SVM optimization problem.
- See also:
- getP
virtual CV_WRAP void 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
virtual CV_WRAP void setType | ( | int | val ) | [pure virtual] |
Type of a SVM formulation.
- See also:
- getType
virtual CV_WRAP bool train | ( | InputArray | samples, |
int | layout, | ||
InputArray | responses | ||
) | [virtual, inherited] |
Trains the statistical model.
- Parameters:
-
samples training samples layout See ml::SampleTypes. responses vector of responses associated with the training samples.
virtual CV_WRAP bool train | ( | const Ptr< TrainData > & | trainData, |
int | flags = 0 |
||
) | [virtual, inherited] |
Trains the statistical model.
- Parameters:
-
trainData training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create. flags optional 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).
virtual bool trainAuto | ( | const Ptr< TrainData > & | data, |
int | kFold = 10 , |
||
ParamGrid | Cgrid = SVM::getDefaultGrid(SVM::C) , |
||
ParamGrid | gammaGrid = SVM::getDefaultGrid(SVM::GAMMA) , |
||
ParamGrid | pGrid = SVM::getDefaultGrid(SVM::P) , |
||
ParamGrid | nuGrid = SVM::getDefaultGrid(SVM::NU) , |
||
ParamGrid | coeffGrid = SVM::getDefaultGrid(SVM::COEF) , |
||
ParamGrid | degreeGrid = SVM::getDefaultGrid(SVM::DEGREE) , |
||
bool | balanced = false |
||
) | [pure virtual] |
Trains an SVM with optimal parameters.
- Parameters:
-
data the training data that can be constructed using TrainData::create or TrainData::loadFromCSV. kFold Cross-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. Cgrid grid for C gammaGrid grid for gamma pGrid grid for p nuGrid grid for nu coeffGrid grid for coeff degreeGrid grid for degree balanced If 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 `SVMgetDefaultGrid(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.
virtual void write | ( | FileStorage & | fs ) | const [virtual, inherited] |
Stores algorithm parameters in a file storage.
Reimplemented in DescriptorMatcher, and FlannBasedMatcher.
Generated on Tue Jul 12 2022 16:42:45 by 1.7.2