OpenCV 5.0.0-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
cv::ml::ANN_MLP Class Referenceabstract

Artificial Neural Networks - Multi-Layer Perceptrons. More...

#include <opencv2/ml.hpp>

Collaboration diagram for cv::ml::ANN_MLP:

Public Types

enum  ActivationFunctions {
  IDENTITY = 0 ,
  SIGMOID_SYM = 1 ,
  GAUSSIAN = 2 ,
  RELU = 3 ,
  LEAKYRELU = 4
}
 
enum  TrainFlags {
  UPDATE_WEIGHTS = 1 ,
  NO_INPUT_SCALE = 2 ,
  NO_OUTPUT_SCALE = 4
}
 
enum  TrainingMethods {
  BACKPROP =0 ,
  RPROP = 1 ,
  ANNEAL = 2
}
 
- Public Types inherited from cv::ml::StatModel
enum  Flags {
  UPDATE_MODEL = 1 ,
  RAW_OUTPUT =1 ,
  COMPRESSED_INPUT =2 ,
  PREPROCESSED_INPUT =4
}
 

Public Member Functions

virtual double getAnnealCoolingRatio () const =0
 
virtual double getAnnealFinalT () const =0
 
virtual double getAnnealInitialT () const =0
 
virtual int getAnnealItePerStep () const =0
 
virtual double getBackpropMomentumScale () const =0
 
virtual double getBackpropWeightScale () const =0
 
virtual cv::Mat getLayerSizes () const =0
 
virtual double getRpropDW0 () const =0
 
virtual double getRpropDWMax () const =0
 
virtual double getRpropDWMin () const =0
 
virtual double getRpropDWMinus () const =0
 
virtual double getRpropDWPlus () const =0
 
virtual TermCriteria getTermCriteria () const =0
 
virtual int getTrainMethod () const =0
 
virtual Mat getWeights (int layerIdx) const =0
 
virtual void setActivationFunction (int type, double param1=0, double param2=0)=0
 
virtual void setAnnealCoolingRatio (double val)=0
 
virtual void setAnnealEnergyRNG (const RNG &rng)=0
 Set/initialize anneal RNG.
 
virtual void setAnnealFinalT (double val)=0
 
virtual void setAnnealInitialT (double val)=0
 
virtual void setAnnealItePerStep (int val)=0
 
virtual void setBackpropMomentumScale (double val)=0
 
virtual void setBackpropWeightScale (double val)=0
 
virtual void setLayerSizes (InputArray _layer_sizes)=0
 
virtual void setRpropDW0 (double val)=0
 
virtual void setRpropDWMax (double val)=0
 
virtual void setRpropDWMin (double val)=0
 
virtual void setRpropDWMinus (double val)=0
 
virtual void setRpropDWPlus (double val)=0
 
virtual void setTermCriteria (TermCriteria val)=0
 
virtual void setTrainMethod (int method, double param1=0, double param2=0)=0
 
- Public Member Functions inherited from cv::ml::StatModel
virtual float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 Computes error on the training or test dataset.
 
virtual bool empty () const CV_OVERRIDE
 Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read.
 
virtual int getVarCount () const =0
 Returns the number of variables in training samples.
 
virtual bool isClassifier () const =0
 Returns true if the model is classifier.
 
virtual bool isTrained () const =0
 Returns true if the model is trained.
 
virtual float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 Predicts response(s) for the provided sample(s)
 
virtual bool train (const Ptr< TrainData > &trainData, int flags=0)
 Trains the statistical model.
 
virtual bool train (InputArray samples, int layout, InputArray responses)
 Trains the statistical model.
 
- Public Member Functions inherited from cv::Algorithm
 Algorithm ()
 
virtual ~Algorithm ()
 
virtual void clear ()
 Clears the algorithm state.
 
virtual String getDefaultName () const
 
virtual void read (const FileNode &fn)
 Reads algorithm parameters from a file storage.
 
virtual void save (const String &filename) const
 
virtual void write (FileStorage &fs) const
 Stores algorithm parameters in a file storage.
 
void write (FileStorage &fs, const String &name) const
 

Static Public Member Functions

static Ptr< ANN_MLPcreate ()
 Creates empty model.
 
static Ptr< ANN_MLPload (const String &filepath)
 Loads and creates a serialized ANN from a file.
 
- Static Public Member Functions inherited from cv::ml::StatModel
template<typename _Tp >
static Ptr< _Tptrain (const Ptr< TrainData > &data, int flags=0)
 Create and train model with default parameters.
 
- Static Public Member Functions inherited from cv::Algorithm
template<typename _Tp >
static Ptr< _Tpload (const String &filename, const String &objname=String())
 Loads algorithm from the file.
 
template<typename _Tp >
static Ptr< _TploadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String.
 
template<typename _Tp >
static Ptr< _Tpread (const FileNode &fn)
 Reads algorithm from the file node.
 

Additional Inherited Members

- Protected Member Functions inherited from cv::Algorithm
void writeFormat (FileStorage &fs) const
 

Detailed Description

Artificial Neural Networks - Multi-Layer Perceptrons.

Unlike many other models in ML that are constructed and trained at once, in the MLP model these steps are separated. First, a network with the specified topology is created using the non-default constructor or the method ANN_MLP::create. All the weights are set to zeros. Then, the network is trained using a set of input and output vectors. The training procedure can be repeated more than once, that is, the weights can be adjusted based on the new training data.

Additional flags for StatModel::train are available: ANN_MLP::TrainFlags.

See also
Neural Networks

Member Enumeration Documentation

◆ ActivationFunctions

possible activation functions

Enumerator
IDENTITY 

Identity function: \(f(x)=x\)

SIGMOID_SYM 

Symmetrical sigmoid: \(f(x)=\beta*(1-e^{-\alpha x})/(1+e^{-\alpha x})\)

Note
If you are using the default sigmoid activation function with the default parameter values fparam1=0 and fparam2=0 then the function used is y = 1.7159*tanh(2/3 * x), so the output will range from [-1.7159, 1.7159], instead of [0,1].
GAUSSIAN 

Gaussian function: \(f(x)=\beta e^{-\alpha x*x}\)

RELU 

ReLU function: \(f(x)=max(0,x)\)

LEAKYRELU 

Leaky ReLU function: for x>0 \(f(x)=x \) and x<=0 \(f(x)=\alpha x \)

◆ TrainFlags

Train options

Enumerator
UPDATE_WEIGHTS 

Update the network weights, rather than compute them from scratch. In the latter case the weights are initialized using the Nguyen-Widrow algorithm.

NO_INPUT_SCALE 

Do not normalize the input vectors. If this flag is not set, the training algorithm normalizes each input feature independently, shifting its mean value to 0 and making the standard deviation equal to 1. If the network is assumed to be updated frequently, the new training data could be much different from original one. In this case, you should take care of proper normalization.

NO_OUTPUT_SCALE 

Do not normalize the output vectors. If the flag is not set, the training algorithm normalizes each output feature independently, by transforming it to the certain range depending on the used activation function.

◆ TrainingMethods

Available training methods

Enumerator
BACKPROP 

The back-propagation algorithm.

RPROP 

The RPROP algorithm. See [227] for details.

ANNEAL 

The simulated annealing algorithm. See [147] for details.

Member Function Documentation

◆ create()

static Ptr< ANN_MLP > cv::ml::ANN_MLP::create ( )
static
Python:
cv.ml.ANN_MLP.create() -> retval
cv.ml.ANN_MLP_create() -> retval

Creates empty model.

Use StatModel::train to train the model, Algorithm::load<ANN_MLP>(filename) to load the pre-trained model. Note that the train method has optional flags: ANN_MLP::TrainFlags.

◆ getAnnealCoolingRatio()

virtual double cv::ml::ANN_MLP::getAnnealCoolingRatio ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getAnnealCoolingRatio() -> retval

ANNEAL: Update cooling ratio. It must be >0 and less than 1. Default value is 0.95.

See also
setAnnealCoolingRatio

◆ getAnnealFinalT()

virtual double cv::ml::ANN_MLP::getAnnealFinalT ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getAnnealFinalT() -> retval

ANNEAL: Update final temperature. It must be >=0 and less than initialT. Default value is 0.1.

See also
setAnnealFinalT

◆ getAnnealInitialT()

virtual double cv::ml::ANN_MLP::getAnnealInitialT ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getAnnealInitialT() -> retval

ANNEAL: Update initial temperature. It must be >=0. Default value is 10.

See also
setAnnealInitialT

◆ getAnnealItePerStep()

virtual int cv::ml::ANN_MLP::getAnnealItePerStep ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getAnnealItePerStep() -> retval

ANNEAL: Update iteration per step. It must be >0 . Default value is 10.

See also
setAnnealItePerStep

◆ getBackpropMomentumScale()

virtual double cv::ml::ANN_MLP::getBackpropMomentumScale ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getBackpropMomentumScale() -> retval

BPROP: Strength of the momentum term (the difference between weights on the 2 previous iterations). This parameter provides some inertia to smooth the random fluctuations of the weights. It can vary from 0 (the feature is disabled) to 1 and beyond. The value 0.1 or so is good enough. Default value is 0.1.

See also
setBackpropMomentumScale

◆ getBackpropWeightScale()

virtual double cv::ml::ANN_MLP::getBackpropWeightScale ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getBackpropWeightScale() -> retval

BPROP: Strength of the weight gradient term. The recommended value is about 0.1. Default value is 0.1.

See also
setBackpropWeightScale

◆ getLayerSizes()

virtual cv::Mat cv::ml::ANN_MLP::getLayerSizes ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getLayerSizes() -> retval

Integer vector specifying the number of neurons in each layer including the input and output layers. The very first element specifies the number of elements in the input layer. The last element - number of elements in the output layer.

See also
setLayerSizes

◆ getRpropDW0()

virtual double cv::ml::ANN_MLP::getRpropDW0 ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getRpropDW0() -> retval

RPROP: Initial value \(\Delta_0\) of update-values \(\Delta_{ij}\). Default value is 0.1.

See also
setRpropDW0

◆ getRpropDWMax()

virtual double cv::ml::ANN_MLP::getRpropDWMax ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getRpropDWMax() -> retval

RPROP: Update-values upper limit \(\Delta_{max}\). It must be >1. Default value is 50.

See also
setRpropDWMax

◆ getRpropDWMin()

virtual double cv::ml::ANN_MLP::getRpropDWMin ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getRpropDWMin() -> retval

RPROP: Update-values lower limit \(\Delta_{min}\). It must be positive. Default value is FLT_EPSILON.

See also
setRpropDWMin

◆ getRpropDWMinus()

virtual double cv::ml::ANN_MLP::getRpropDWMinus ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getRpropDWMinus() -> retval

RPROP: Decrease factor \(\eta^-\). It must be <1. Default value is 0.5.

See also
setRpropDWMinus

◆ getRpropDWPlus()

virtual double cv::ml::ANN_MLP::getRpropDWPlus ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getRpropDWPlus() -> retval

RPROP: Increase factor \(\eta^+\). It must be >1. Default value is 1.2.

See also
setRpropDWPlus

◆ getTermCriteria()

virtual TermCriteria cv::ml::ANN_MLP::getTermCriteria ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getTermCriteria() -> retval

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). Default value is TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, 0.01).

See also
setTermCriteria

◆ getTrainMethod()

virtual int cv::ml::ANN_MLP::getTrainMethod ( ) const
pure virtual
Python:
cv.ml.ANN_MLP.getTrainMethod() -> retval

Returns current training method

◆ getWeights()

virtual Mat cv::ml::ANN_MLP::getWeights ( int layerIdx) const
pure virtual
Python:
cv.ml.ANN_MLP.getWeights(layerIdx) -> retval

◆ load()

static Ptr< ANN_MLP > cv::ml::ANN_MLP::load ( const String & filepath)
static
Python:
cv.ml.ANN_MLP.load(filepath) -> retval
cv.ml.ANN_MLP_load(filepath) -> retval

Loads and creates a serialized ANN from a file.

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

Parameters
filepathpath to serialized ANN

◆ setActivationFunction()

virtual void cv::ml::ANN_MLP::setActivationFunction ( int type,
double param1 = 0,
double param2 = 0 )
pure virtual
Python:
cv.ml.ANN_MLP.setActivationFunction(type[, param1[, param2]]) -> None

Initialize the activation function for each neuron. Currently the default and the only fully supported activation function is ANN_MLP::SIGMOID_SYM.

Parameters
typeThe type of activation function. See ANN_MLP::ActivationFunctions.
param1The first parameter of the activation function, \(\alpha\). Default value is 0.
param2The second parameter of the activation function, \(\beta\). Default value is 0.

◆ setAnnealCoolingRatio()

virtual void cv::ml::ANN_MLP::setAnnealCoolingRatio ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setAnnealCoolingRatio(val) -> None

◆ setAnnealEnergyRNG()

virtual void cv::ml::ANN_MLP::setAnnealEnergyRNG ( const RNG & rng)
pure virtual

Set/initialize anneal RNG.

◆ setAnnealFinalT()

virtual void cv::ml::ANN_MLP::setAnnealFinalT ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setAnnealFinalT(val) -> None

See also
getAnnealFinalT

◆ setAnnealInitialT()

virtual void cv::ml::ANN_MLP::setAnnealInitialT ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setAnnealInitialT(val) -> None

◆ setAnnealItePerStep()

virtual void cv::ml::ANN_MLP::setAnnealItePerStep ( int val)
pure virtual
Python:
cv.ml.ANN_MLP.setAnnealItePerStep(val) -> None

◆ setBackpropMomentumScale()

virtual void cv::ml::ANN_MLP::setBackpropMomentumScale ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setBackpropMomentumScale(val) -> None

◆ setBackpropWeightScale()

virtual void cv::ml::ANN_MLP::setBackpropWeightScale ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setBackpropWeightScale(val) -> None

◆ setLayerSizes()

virtual void cv::ml::ANN_MLP::setLayerSizes ( InputArray _layer_sizes)
pure virtual
Python:
cv.ml.ANN_MLP.setLayerSizes(_layer_sizes) -> None

Integer vector specifying the number of neurons in each layer including the input and output layers. The very first element specifies the number of elements in the input layer. The last element - number of elements in the output layer. Default value is empty Mat.

See also
getLayerSizes

◆ setRpropDW0()

virtual void cv::ml::ANN_MLP::setRpropDW0 ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setRpropDW0(val) -> None

See also
getRpropDW0

◆ setRpropDWMax()

virtual void cv::ml::ANN_MLP::setRpropDWMax ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setRpropDWMax(val) -> None

See also
getRpropDWMax

◆ setRpropDWMin()

virtual void cv::ml::ANN_MLP::setRpropDWMin ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setRpropDWMin(val) -> None

See also
getRpropDWMin

◆ setRpropDWMinus()

virtual void cv::ml::ANN_MLP::setRpropDWMinus ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setRpropDWMinus(val) -> None

See also
getRpropDWMinus

◆ setRpropDWPlus()

virtual void cv::ml::ANN_MLP::setRpropDWPlus ( double val)
pure virtual
Python:
cv.ml.ANN_MLP.setRpropDWPlus(val) -> None

See also
getRpropDWPlus

◆ setTermCriteria()

virtual void cv::ml::ANN_MLP::setTermCriteria ( TermCriteria val)
pure virtual
Python:
cv.ml.ANN_MLP.setTermCriteria(val) -> None

See also
getTermCriteria

◆ setTrainMethod()

virtual void cv::ml::ANN_MLP::setTrainMethod ( int method,
double param1 = 0,
double param2 = 0 )
pure virtual
Python:
cv.ml.ANN_MLP.setTrainMethod(method[, param1[, param2]]) -> None

Sets training method and common parameters.

Parameters
methodDefault value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
param1passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.
param2passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.

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