@Namespace(value="cv::ml") @Properties(inherit=opencv_ml.class) public class SVMSGD extends StatModel
SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach, as presented in \cite bottou2010large.
The classifier has following parameters:
- model type,
- margin type,
- margin regularization (\lambda
),
- initial step size (\gamma_0
),
- step decreasing power (c
),
- and termination criteria.
The model type may have one of the following values: \ref SGD and \ref ASGD.
- \ref SGD is the classic version of SVMSGD classifier: every next step is calculated by the formula
\[w_{t+1} = w_t - \gamma(t) \frac{dQ_i}{dw} |_{w = w_t}\]
where
- w_t
is the weights vector for decision function at step t
,
- \gamma(t)
is the step size of model parameters at the iteration t
, it is decreased on each step by the formula
\gamma(t) = \gamma_0 (1 + \lambda \gamma_0 t) ^ {-c}
- Q_i
is the target functional from SVM task for sample with number i
, this sample is chosen stochastically on each step of the algorithm.
- \ref ASGD is Average Stochastic Gradient Descent SVM Classifier. ASGD classifier averages weights vector on each step of algorithm by the formula
\widehat{w}_{t+1} = \frac{t}{1+t}\widehat{w}_{t} + \frac{1}{1+t}w_{t+1}
The recommended model type is ASGD (following \cite bottou2010large).
The margin type may have one of the following values: \ref SOFT_MARGIN or \ref HARD_MARGIN.
- You should use \ref HARD_MARGIN type, if you have linearly separable sets. - You should use \ref SOFT_MARGIN type, if you have non-linearly separable sets or sets with outliers. - In the general case (if you know nothing about linear separability of your sets), use SOFT_MARGIN.
The other parameters may be described as follows: - Margin regularization parameter is responsible for weights decreasing at each step and for the strength of restrictions on outliers (the less the parameter, the less probability that an outlier will be ignored). Recommended value for SGD model is 0.0001, for ASGD model is 0.00001.
- Initial step size parameter is the initial value for the step size \gamma(t)
.
You will have to find the best initial step for your problem.
- Step decreasing power is the power parameter for \gamma(t)
decreasing by the formula, mentioned above.
Recommended value for SGD model is 1, for ASGD model is 0.75.
- Termination criteria can be TermCriteria::COUNT, TermCriteria::EPS or TermCriteria::COUNT + TermCriteria::EPS. You will have to find the best termination criteria for your problem.
Note that the parameters margin regularization, initial step size, and step decreasing power should be positive.
To use SVMSGD algorithm do as follows:
- first, create the SVMSGD object. The algorithm will set optimal parameters by default, but you can set your own parameters via functions setSvmsgdType(), setMarginType(), setMarginRegularization(), setInitialStepSize(), and setStepDecreasingPower().
- then the SVM model can be trained using the train features and the correspondent labels by the method train().
- after that, the label of a new feature vector can be predicted using the method predict().
// Create empty object
cv::Ptr<SVMSGD> svmsgd = SVMSGD::create();
// Train the Stochastic Gradient Descent SVM
svmsgd->train(trainData);
// Predict labels for the new samples
svmsgd->predict(samples, responses);
Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter
Modifier and Type | Field and Description |
---|---|
static int |
ASGD
enum cv::ml::SVMSGD::SvmsgdType
|
static int |
HARD_MARGIN
enum cv::ml::SVMSGD::MarginType
|
static int |
SGD
enum cv::ml::SVMSGD::SvmsgdType
|
static int |
SOFT_MARGIN
enum cv::ml::SVMSGD::MarginType
|
COMPRESSED_INPUT, PREPROCESSED_INPUT, RAW_OUTPUT, UPDATE_MODEL
Constructor and Description |
---|
SVMSGD(Pointer p)
Pointer cast constructor.
|
Modifier and Type | Method and Description |
---|---|
static SVMSGD |
create()
\brief Creates empty model.
|
float |
getInitialStepSize() |
float |
getMarginRegularization() |
int |
getMarginType() |
float |
getShift() |
float |
getStepDecreasingPower() |
int |
getSvmsgdType() |
TermCriteria |
getTermCriteria() |
Mat |
getWeights() |
static SVMSGD |
load(BytePointer filepath) |
static SVMSGD |
load(BytePointer filepath,
BytePointer nodeName)
\brief Loads and creates a serialized SVMSGD from a file
Use SVMSGD::save to serialize and store an SVMSGD to disk.
|
static SVMSGD |
load(String filepath) |
static SVMSGD |
load(String filepath,
String nodeName) |
void |
setInitialStepSize(float InitialStepSize)
\copybrief getInitialStepSize @see getInitialStepSize
|
void |
setMarginRegularization(float marginRegularization)
\copybrief getMarginRegularization @see getMarginRegularization
|
void |
setMarginType(int marginType)
\copybrief getMarginType @see getMarginType
|
void |
setOptimalParameters() |
void |
setOptimalParameters(int svmsgdType,
int marginType)
\brief Function sets optimal parameters values for chosen SVM SGD model.
|
void |
setStepDecreasingPower(float stepDecreasingPower)
\copybrief getStepDecreasingPower @see getStepDecreasingPower
|
void |
setSvmsgdType(int svmsgdType)
\copybrief getSvmsgdType @see getSvmsgdType
|
void |
setTermCriteria(TermCriteria val)
\copybrief getTermCriteria @see getTermCriteria
|
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, 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 SGD
public static final int ASGD
public static final int SOFT_MARGIN
public static final int HARD_MARGIN
public SVMSGD(Pointer p)
Pointer.Pointer(Pointer)
.@ByVal public Mat getWeights()
public float getShift()
@opencv_core.Ptr public static SVMSGD create()
@opencv_core.Ptr public static SVMSGD load(@opencv_core.Str BytePointer filepath, @opencv_core.Str BytePointer nodeName)
filepath
- path to serialized SVMSGDnodeName
- name of node containing the classifier@opencv_core.Ptr public static SVMSGD load(@opencv_core.Str BytePointer filepath)
@opencv_core.Ptr public static SVMSGD load(@opencv_core.Str String filepath, @opencv_core.Str String nodeName)
@opencv_core.Ptr public static SVMSGD load(@opencv_core.Str String filepath)
public void setOptimalParameters(int svmsgdType, int marginType)
svmsgdType
- is the type of SVMSGD classifier.marginType
- is the type of margin constraint.public void setOptimalParameters()
public int getSvmsgdType()
setSvmsgdType
public void setSvmsgdType(int svmsgdType)
public int getMarginType()
setMarginType
public void setMarginType(int marginType)
public float getMarginRegularization()
setMarginRegularization
public void setMarginRegularization(float marginRegularization)
public float getInitialStepSize()
setInitialStepSize
public void setInitialStepSize(float InitialStepSize)
public float getStepDecreasingPower()
setStepDecreasingPower
public void setStepDecreasingPower(float stepDecreasingPower)
@ByVal public TermCriteria getTermCriteria()
setTermCriteria
public void setTermCriteria(@Const @ByRef TermCriteria val)
Copyright © 2020. All rights reserved.