@Namespace(value="cv::ml") @Properties(inherit=opencv_ml.class) public class SVM extends StatModel
ml_intro_svm
Modifier and Type | Class and Description |
---|---|
static class |
SVM.Kernel |
Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter
Modifier and Type | Field and Description |
---|---|
static int |
C
enum cv::ml::SVM::ParamTypes
|
static int |
C_SVC
enum cv::ml::SVM::Types
|
static int |
CHI2
enum cv::ml::SVM::KernelTypes
|
static int |
COEF
enum cv::ml::SVM::ParamTypes
|
static int |
CUSTOM
enum cv::ml::SVM::KernelTypes
|
static int |
DEGREE
enum cv::ml::SVM::ParamTypes
|
static int |
EPS_SVR
enum cv::ml::SVM::Types
|
static int |
GAMMA
enum cv::ml::SVM::ParamTypes
|
static int |
INTER
enum cv::ml::SVM::KernelTypes
|
static int |
LINEAR
enum cv::ml::SVM::KernelTypes
|
static int |
NU
enum cv::ml::SVM::ParamTypes
|
static int |
NU_SVC
enum cv::ml::SVM::Types
|
static int |
NU_SVR
enum cv::ml::SVM::Types
|
static int |
ONE_CLASS
enum cv::ml::SVM::Types
|
static int |
P
enum cv::ml::SVM::ParamTypes
|
static int |
POLY
enum cv::ml::SVM::KernelTypes
|
static int |
RBF
enum cv::ml::SVM::KernelTypes
|
static int |
SIGMOID
enum cv::ml::SVM::KernelTypes
|
COMPRESSED_INPUT, PREPROCESSED_INPUT, RAW_OUTPUT, UPDATE_MODEL
Constructor and Description |
---|
SVM(Pointer p)
Pointer cast constructor.
|
Modifier and Type | Method and Description |
---|---|
static SVM |
create()
Creates empty model.
|
double |
getC() |
Mat |
getClassWeights() |
double |
getCoef0() |
double |
getDecisionFunction(int i,
GpuMat alpha,
GpuMat svidx) |
double |
getDecisionFunction(int i,
Mat alpha,
Mat svidx)
\brief Retrieves the decision function
|
double |
getDecisionFunction(int i,
UMat alpha,
UMat svidx) |
static ParamGrid |
getDefaultGrid(int param_id)
\brief Generates a grid for %SVM parameters.
|
static ParamGrid |
getDefaultGridPtr(int param_id)
\brief Generates a grid for %SVM parameters.
|
double |
getDegree() |
double |
getGamma() |
int |
getKernelType()
Type of a %SVM kernel.
|
double |
getNu() |
double |
getP() |
Mat |
getSupportVectors()
\brief Retrieves all the support vectors
|
TermCriteria |
getTermCriteria() |
int |
getType() |
Mat |
getUncompressedSupportVectors()
\brief Retrieves all the uncompressed support vectors of a linear %SVM
|
static SVM |
load(BytePointer filepath)
\brief Loads and creates a serialized svm from a file
Use SVM::save to serialize and store an SVM to disk.
|
static SVM |
load(String filepath) |
void |
setC(double val)
\copybrief getC @see getC
|
void |
setClassWeights(Mat val)
\copybrief getClassWeights @see getClassWeights
|
void |
setCoef0(double val)
\copybrief getCoef0 @see getCoef0
|
void |
setCustomKernel(SVM.Kernel _kernel)
Initialize with custom kernel.
|
void |
setDegree(double val)
\copybrief getDegree @see getDegree
|
void |
setGamma(double val)
\copybrief getGamma @see getGamma
|
void |
setKernel(int kernelType)
Initialize with one of predefined kernels.
|
void |
setNu(double val)
\copybrief getNu @see getNu
|
void |
setP(double val)
\copybrief getP @see getP
|
void |
setTermCriteria(TermCriteria val)
\copybrief getTermCriteria @see getTermCriteria
|
void |
setType(int val)
\copybrief getType @see getType
|
boolean |
trainAuto(GpuMat samples,
int layout,
GpuMat responses) |
boolean |
trainAuto(GpuMat samples,
int layout,
GpuMat responses,
int kFold,
ParamGrid Cgrid,
ParamGrid gammaGrid,
ParamGrid pGrid,
ParamGrid nuGrid,
ParamGrid coeffGrid,
ParamGrid degreeGrid,
boolean balanced) |
boolean |
trainAuto(Mat samples,
int layout,
Mat responses) |
boolean |
trainAuto(Mat samples,
int layout,
Mat responses,
int kFold,
ParamGrid Cgrid,
ParamGrid gammaGrid,
ParamGrid pGrid,
ParamGrid nuGrid,
ParamGrid coeffGrid,
ParamGrid degreeGrid,
boolean balanced)
\brief Trains an %SVM with optimal parameters
|
boolean |
trainAuto(TrainData data) |
boolean |
trainAuto(TrainData data,
int kFold,
ParamGrid Cgrid,
ParamGrid gammaGrid,
ParamGrid pGrid,
ParamGrid nuGrid,
ParamGrid coeffGrid,
ParamGrid degreeGrid,
boolean balanced)
\brief Trains an %SVM with optimal parameters.
|
boolean |
trainAuto(UMat samples,
int layout,
UMat responses) |
boolean |
trainAuto(UMat samples,
int layout,
UMat responses,
int kFold,
ParamGrid Cgrid,
ParamGrid gammaGrid,
ParamGrid pGrid,
ParamGrid nuGrid,
ParamGrid coeffGrid,
ParamGrid degreeGrid,
boolean balanced) |
calcError, calcError, calcError, empty, getVarCount, isClassifier, isTrained, predict, predict, predict, predict, predict, predict, train, train, train, train, train
loadANN_MLP, loadANN_MLP, loadBoost, loadBoost, loadDTrees, loadDTrees, loadEM, loadEM, loadKNearest, loadKNearest, loadLogisticRegression, loadLogisticRegression, loadNormalBayesClassifier, loadNormalBayesClassifier, loadRTrees, loadRTrees, loadSVM, loadSVM
clear, getDefaultName, position, read, save, save, write, write, write
address, asBuffer, asByteBuffer, availablePhysicalBytes, calloc, capacity, capacity, close, deallocate, deallocate, deallocateReferences, deallocator, deallocator, equals, fill, formatBytes, free, hashCode, isNull, limit, limit, malloc, maxBytes, maxPhysicalBytes, memchr, memcmp, memcpy, memmove, memset, offsetof, parseBytes, physicalBytes, position, put, realloc, referenceCount, releaseReference, retainReference, setNull, sizeof, toString, totalBytes, totalPhysicalBytes, withDeallocator, zero
public static final int C_SVC
public static final int NU_SVC
public static final int ONE_CLASS
public static final int EPS_SVR
public static final int NU_SVR
public static final int CUSTOM
public static final int LINEAR
public static final int POLY
public static final int RBF
public static final int SIGMOID
public static final int CHI2
public static final int INTER
public static final int C
public static final int GAMMA
public static final int P
public static final int NU
public static final int COEF
public static final int DEGREE
public SVM(Pointer p)
Pointer.Pointer(Pointer)
.public int getType()
setType
public void setType(int val)
public double getGamma()
setGamma
public void setGamma(double val)
public double getCoef0()
setCoef0
public void setCoef0(double val)
public double getDegree()
setDegree
public void setDegree(double val)
public double getC()
setC
public void setC(double val)
public double getNu()
setNu
public void setNu(double val)
public double getP()
setP
public void setP(double val)
public void setClassWeights(@Const @ByRef Mat val)
@ByVal public TermCriteria getTermCriteria()
setTermCriteria
public void setTermCriteria(@Const @ByRef TermCriteria val)
public int getKernelType()
public void setKernel(int kernelType)
public void setCustomKernel(@opencv_core.Ptr SVM.Kernel _kernel)
@Cast(value="bool") public boolean trainAuto(@opencv_core.Ptr TrainData data, int kFold, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::C))") ParamGrid Cgrid, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::GAMMA))") ParamGrid gammaGrid, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::P))") ParamGrid pGrid, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::NU))") ParamGrid nuGrid, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::COEF))") ParamGrid coeffGrid, @ByVal(nullValue="cv::ml::ParamGrid(cv::ml::SVM::getDefaultGrid(cv::ml::SVM::DEGREE))") ParamGrid degreeGrid, @Cast(value="bool") boolean balanced)
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 CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nucoeffGrid
- grid for coeffdegreeGrid
- grid for degreebalanced
- 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 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.
@Cast(value="bool") public boolean trainAuto(@opencv_core.Ptr TrainData data)
@Cast(value="bool") public boolean trainAuto(@ByVal Mat samples, int layout, @ByVal Mat responses, int kFold, @opencv_core.Ptr ParamGrid Cgrid, @opencv_core.Ptr ParamGrid gammaGrid, @opencv_core.Ptr ParamGrid pGrid, @opencv_core.Ptr ParamGrid nuGrid, @opencv_core.Ptr ParamGrid coeffGrid, @opencv_core.Ptr ParamGrid degreeGrid, @Cast(value="bool") boolean balanced)
samples
- training sampleslayout
- See ml::SampleTypes.responses
- vector of responses associated with the training samples.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 isCgrid
- grid for CgammaGrid
- grid for gammapGrid
- grid for pnuGrid
- grid for nucoeffGrid
- grid for coeffdegreeGrid
- grid for degreebalanced
- 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.
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.
@Cast(value="bool") public boolean trainAuto(@ByVal Mat samples, int layout, @ByVal Mat responses)
@Cast(value="bool") public boolean trainAuto(@ByVal UMat samples, int layout, @ByVal UMat responses, int kFold, @opencv_core.Ptr ParamGrid Cgrid, @opencv_core.Ptr ParamGrid gammaGrid, @opencv_core.Ptr ParamGrid pGrid, @opencv_core.Ptr ParamGrid nuGrid, @opencv_core.Ptr ParamGrid coeffGrid, @opencv_core.Ptr ParamGrid degreeGrid, @Cast(value="bool") boolean balanced)
@Cast(value="bool") public boolean trainAuto(@ByVal UMat samples, int layout, @ByVal UMat responses)
@Cast(value="bool") public boolean trainAuto(@ByVal GpuMat samples, int layout, @ByVal GpuMat responses, int kFold, @opencv_core.Ptr ParamGrid Cgrid, @opencv_core.Ptr ParamGrid gammaGrid, @opencv_core.Ptr ParamGrid pGrid, @opencv_core.Ptr ParamGrid nuGrid, @opencv_core.Ptr ParamGrid coeffGrid, @opencv_core.Ptr ParamGrid degreeGrid, @Cast(value="bool") boolean balanced)
@Cast(value="bool") public boolean trainAuto(@ByVal GpuMat samples, int layout, @ByVal GpuMat responses)
@ByVal public Mat getSupportVectors()
The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows.
@ByVal public Mat getUncompressedSupportVectors()
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.
public double getDecisionFunction(int i, @ByVal Mat alpha, @ByVal Mat svidx)
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 N(N-1)/2
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.
public double getDecisionFunction(int i, @ByVal GpuMat alpha, @ByVal GpuMat svidx)
@ByVal public static ParamGrid getDefaultGrid(int param_id)
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.
@opencv_core.Ptr public static ParamGrid getDefaultGridPtr(int param_id)
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 pointer for the specified parameter of the %SVM algorithm. The grid may be passed to the function SVM::trainAuto.
@opencv_core.Ptr public static SVM create()
@opencv_core.Ptr public static SVM load(@opencv_core.Str BytePointer filepath)
filepath
- path to serialized svm@opencv_core.Ptr public static SVM load(@opencv_core.Str String filepath)
Copyright © 2019. All rights reserved.