Skip to content

ml_SVMSGD

mlclassOpenCV 5.0.0
import { ml_SVMSGD } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSConstructor or factory
CLASSml_SVMSGD
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Native object: release it with using or delete(). Factories can return null; check before calling methods. Inherits ml_StatModel.


Stochastic Gradient Descent SVM Classifier * *************************************************************************************** Stochastic Gradient Descent SVM classifier

SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach, as presented in [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: SGD and ASGD.

  • 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.

  • 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 [bottou2010large]).

The margin type may have one of the following values: SOFT_MARGIN or HARD_MARGIN.

  • You should use HARD_MARGIN type, if you have linearly separable sets.
  • You should use 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);

Constructors and members

static create

Creates empty model. Use StatModel::train to train the model. Since %SVMSGD has several parameters, you may want to find the best parameters for your problem or use setOptimalParameters() to set some default parameters.

create(): ml_SVMSGD | null;
Returns

The ml_SVMSGD | null result.

static load

Loads and creates a serialized SVMSGD from a file

Use SVMSGD::save to serialize and store an SVMSGD to disk. Load the SVMSGD from this file again, by calling this function with the path to the file. Optionally specify the node for the file containing the classifier

load(filepath: EmbindString, nodeName: EmbindString): ml_SVMSGD | null;
2 available overloads
load(filepath: EmbindString): ml_SVMSGD | null;
load(filepath: EmbindString, nodeName: EmbindString): ml_SVMSGD | null;
filepath

path to serialized SVMSGD

nodeName

name of node containing the classifier

Returns

The ml_SVMSGD | null result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

getWeights

Return the weights configured on this ml_SVMSGD object.

getWeights(): Mat;
Returns

the weights of the trained model (decision function f(x) = weights * x + shift). Release returned native handles with using or delete(), including handles nested in results.

getShift

Return the shift configured on this ml_SVMSGD object.

getShift(): number;
Returns

the shift of the trained model (decision function f(x) = weights * x + shift).

setOptimalParameters

Function sets optimal parameters values for chosen SVM SGD model.

setOptimalParameters(svmsgdType: number, marginType: number): void;
3 available overloads
setOptimalParameters(): void;
setOptimalParameters(svmsgdType: number): void;
setOptimalParameters(svmsgdType: number, marginType: number): void;
svmsgdType

is the type of SVMSGD classifier.

marginType

is the type of margin constraint.

getSvmsgdType

%Algorithm type, one of SVMSGD::SvmsgdType. See: setSvmsgdType

getSvmsgdType(): number;
Returns

The number result.

setSvmsgdType

%Algorithm type, one of SVMSGD::SvmsgdType. See: setSvmsgdType See: getSvmsgdType

setSvmsgdType(svmsgdType: number): void;
svmsgdType

svmsgd type argument (number).

getMarginType

%Margin type, one of SVMSGD::MarginType. See: setMarginType

getMarginType(): number;
Returns

The number result.

setMarginType

%Margin type, one of SVMSGD::MarginType. See: setMarginType See: getMarginType

setMarginType(marginType: number): void;
marginType

margin type argument (number).

getMarginRegularization

Parameter marginRegularization of a %SVMSGD optimization problem. See: setMarginRegularization

getMarginRegularization(): number;
Returns

The number result.

setMarginRegularization

Parameter marginRegularization of a %SVMSGD optimization problem. See: setMarginRegularization See: getMarginRegularization

setMarginRegularization(marginRegularization: number): void;
marginRegularization

margin regularization argument (number).

getInitialStepSize

Parameter initialStepSize of a %SVMSGD optimization problem. See: setInitialStepSize

getInitialStepSize(): number;
Returns

The number result.

setInitialStepSize

Parameter initialStepSize of a %SVMSGD optimization problem. See: setInitialStepSize See: getInitialStepSize

setInitialStepSize(InitialStepSize: number): void;
InitialStepSize

initial step size argument (number).

getStepDecreasingPower

Parameter stepDecreasingPower of a %SVMSGD optimization problem. See: setStepDecreasingPower

getStepDecreasingPower(): number;
Returns

The number result.

setStepDecreasingPower

Parameter stepDecreasingPower of a %SVMSGD optimization problem. See: setStepDecreasingPower See: getStepDecreasingPower

setStepDecreasingPower(stepDecreasingPower: number): void;
stepDecreasingPower

step decreasing power argument (number).

getTermCriteria

Termination criteria of the training algorithm. You can specify the maximum number of iterations (maxCount) and/or how much the error could change between the iterations to make the algorithm continue (epsilon). See: setTermCriteria

getTermCriteria(): TermCriteria;
Returns

The TermCriteria result.

setTermCriteria

Termination criteria of the training algorithm. You can specify the maximum number of iterations (maxCount) and/or how much the error could change between the iterations to make the algorithm continue (epsilon). See: setTermCriteria See: getTermCriteria

setTermCriteria(val: TermCriteria): void;
val

val argument (TermCriteria).

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.