2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
2014-07-30 03:54:23 +08:00
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
2010-05-12 01:44:00 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
2014-07-30 03:54:23 +08:00
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
2014-08-19 02:40:31 +08:00
|
|
|
// Copyright (C) 2014, Itseez Inc, all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
2014-07-30 03:54:23 +08:00
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
2010-05-12 01:44:00 +08:00
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2016-09-30 17:46:29 +08:00
|
|
|
#ifndef OPENCV_ML_HPP
|
|
|
|
#define OPENCV_ML_HPP
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-29 16:59:55 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
# include "opencv2/core.hpp"
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
#include <float.h>
|
2012-04-14 05:50:59 +08:00
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/**
|
|
|
|
@defgroup ml Machine Learning
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The Machine Learning Library (MLL) is a set of classes and functions for statistical
|
|
|
|
classification, regression, and clustering of data.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
Most of the classification and regression algorithms are implemented as C++ classes. As the
|
|
|
|
algorithms have different sets of features (like an ability to handle missing measurements or
|
|
|
|
categorical input variables), there is a little common ground between the classes. This common
|
|
|
|
ground is defined by the class cv::ml::StatModel that all the other ML classes are derived from.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
See detailed overview here: @ref ml_intro.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
namespace ml
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
//! @addtogroup ml
|
|
|
|
//! @{
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Variable types */
|
|
|
|
enum VariableTypes
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-12-31 19:18:50 +08:00
|
|
|
VAR_NUMERICAL =0, //!< same as VAR_ORDERED
|
|
|
|
VAR_ORDERED =0, //!< ordered variables
|
|
|
|
VAR_CATEGORICAL =1 //!< categorical variables
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief %Error types */
|
|
|
|
enum ErrorTypes
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
TEST_ERROR = 0,
|
|
|
|
TRAIN_ERROR = 1
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Sample types */
|
|
|
|
enum SampleTypes
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-12-31 19:18:50 +08:00
|
|
|
ROW_SAMPLE = 0, //!< each training sample is a row of samples
|
|
|
|
COL_SAMPLE = 1 //!< each training sample occupies a column of samples
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief The structure represents the logarithmic grid range of statmodel parameters.
|
|
|
|
|
|
|
|
It is used for optimizing statmodel accuracy by varying model parameters, the accuracy estimate
|
|
|
|
being computed by cross-validation.
|
|
|
|
*/
|
2017-03-23 21:00:19 +08:00
|
|
|
class CV_EXPORTS_W ParamGrid
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
public:
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Default constructor */
|
2014-07-30 03:54:23 +08:00
|
|
|
ParamGrid();
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Constructor with parameters */
|
2014-07-30 03:54:23 +08:00
|
|
|
ParamGrid(double _minVal, double _maxVal, double _logStep);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-03-23 21:00:19 +08:00
|
|
|
CV_PROP_RW double minVal; //!< Minimum value of the statmodel parameter. Default value is 0.
|
|
|
|
CV_PROP_RW double maxVal; //!< Maximum value of the statmodel parameter. Default value is 0.
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Logarithmic step for iterating the statmodel parameter.
|
|
|
|
|
|
|
|
The grid determines the following iteration sequence of the statmodel parameter values:
|
|
|
|
\f[(minVal, minVal*step, minVal*{step}^2, \dots, minVal*{logStep}^n),\f]
|
|
|
|
where \f$n\f$ is the maximal index satisfying
|
|
|
|
\f[\texttt{minVal} * \texttt{logStep} ^n < \texttt{maxVal}\f]
|
|
|
|
The grid is logarithmic, so logStep must always be greater then 1. Default value is 1.
|
|
|
|
*/
|
2017-03-23 21:00:19 +08:00
|
|
|
CV_PROP_RW double logStep;
|
|
|
|
|
|
|
|
/** @brief Creates a ParamGrid Ptr that can be given to the %SVM::trainAuto method
|
|
|
|
|
|
|
|
@param minVal minimum value of the parameter grid
|
|
|
|
@param maxVal maximum value of the parameter grid
|
|
|
|
@param logstep Logarithmic step for iterating the statmodel parameter
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<ParamGrid> create(double minVal=0., double maxVal=0., double logstep=1.);
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Class encapsulating training data.
|
|
|
|
|
|
|
|
Please note that the class only specifies the interface of training data, but not implementation.
|
2014-12-31 19:18:50 +08:00
|
|
|
All the statistical model classes in _ml_ module accepts Ptr\<TrainData\> as parameter. In other
|
|
|
|
words, you can create your own class derived from TrainData and pass smart pointer to the instance
|
|
|
|
of this class into StatModel::train.
|
|
|
|
|
|
|
|
@sa @ref ml_intro_data
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
class CV_EXPORTS_W TrainData
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-07-30 03:54:23 +08:00
|
|
|
static inline float missingValue() { return FLT_MAX; }
|
|
|
|
virtual ~TrainData();
|
|
|
|
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getLayout() const = 0;
|
|
|
|
CV_WRAP virtual int getNTrainSamples() const = 0;
|
|
|
|
CV_WRAP virtual int getNTestSamples() const = 0;
|
|
|
|
CV_WRAP virtual int getNSamples() const = 0;
|
|
|
|
CV_WRAP virtual int getNVars() const = 0;
|
|
|
|
CV_WRAP virtual int getNAllVars() const = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void getSample(InputArray varIdx, int sidx, float* buf) const = 0;
|
|
|
|
CV_WRAP virtual Mat getSamples() const = 0;
|
|
|
|
CV_WRAP virtual Mat getMissing() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Returns matrix of train samples
|
|
|
|
|
|
|
|
@param layout The requested layout. If it's different from the initial one, the matrix is
|
2014-12-31 19:18:50 +08:00
|
|
|
transposed. See ml::SampleTypes.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param compressSamples if true, the function returns only the training samples (specified by
|
2014-12-31 19:18:50 +08:00
|
|
|
sampleIdx)
|
2014-11-19 22:55:13 +08:00
|
|
|
@param compressVars if true, the function returns the shorter training samples, containing only
|
2014-12-31 19:18:50 +08:00
|
|
|
the active variables.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
In current implementation the function tries to avoid physical data copying and returns the
|
|
|
|
matrix stored inside TrainData (unless the transposition or compression is needed).
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getTrainSamples(int layout=ROW_SAMPLE,
|
2014-07-30 03:54:23 +08:00
|
|
|
bool compressSamples=true,
|
|
|
|
bool compressVars=true) const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Returns the vector of responses
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The function returns ordered or the original categorical responses. Usually it's used in
|
|
|
|
regression algorithms.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getTrainResponses() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Returns the vector of normalized categorical responses
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The function returns vector of responses. Each response is integer from `0` to `<number of
|
|
|
|
classes>-1`. The actual label value can be retrieved then from the class label vector, see
|
2014-11-19 22:55:13 +08:00
|
|
|
TrainData::getClassLabels.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getTrainNormCatResponses() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTestResponses() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTestNormCatResponses() const = 0;
|
|
|
|
CV_WRAP virtual Mat getResponses() const = 0;
|
|
|
|
CV_WRAP virtual Mat getNormCatResponses() const = 0;
|
|
|
|
CV_WRAP virtual Mat getSampleWeights() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTrainSampleWeights() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTestSampleWeights() const = 0;
|
|
|
|
CV_WRAP virtual Mat getVarIdx() const = 0;
|
|
|
|
CV_WRAP virtual Mat getVarType() const = 0;
|
2016-08-26 20:25:46 +08:00
|
|
|
CV_WRAP Mat getVarSymbolFlags() const;
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getResponseType() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTrainSampleIdx() const = 0;
|
|
|
|
CV_WRAP virtual Mat getTestSampleIdx() const = 0;
|
|
|
|
CV_WRAP virtual void getValues(int vi, InputArray sidx, float* values) const = 0;
|
2015-03-19 20:45:34 +08:00
|
|
|
virtual void getNormCatValues(int vi, InputArray sidx, int* values) const = 0;
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getDefaultSubstValues() const = 0;
|
|
|
|
|
|
|
|
CV_WRAP virtual int getCatCount(int vi) const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Returns the vector of class labels
|
|
|
|
|
|
|
|
The function returns vector of unique labels occurred in the responses.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getClassLabels() const = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getCatOfs() const = 0;
|
|
|
|
CV_WRAP virtual Mat getCatMap() const = 0;
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Splits the training data into the training and test parts
|
|
|
|
@sa TrainData::setTrainTestSplitRatio
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTrainTestSplit(int count, bool shuffle=true) = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Splits the training data into the training and test parts
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The function selects a subset of specified relative size and then returns it as the training
|
|
|
|
set. If the function is not called, all the data is used for training. Please, note that for
|
|
|
|
each of TrainData::getTrain\* there is corresponding TrainData::getTest\*, so that the test
|
|
|
|
subset can be retrieved and processed as well.
|
|
|
|
@sa TrainData::setTrainTestSplit
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTrainTestSplitRatio(double ratio, bool shuffle=true) = 0;
|
|
|
|
CV_WRAP virtual void shuffleTrainTest() = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2016-02-12 17:32:26 +08:00
|
|
|
/** @brief Returns matrix of test samples */
|
|
|
|
CV_WRAP Mat getTestSamples() const;
|
2015-12-05 19:48:02 +08:00
|
|
|
|
2016-08-26 20:25:46 +08:00
|
|
|
/** @brief Returns vector of symbolic names captured in loadFromCSV() */
|
|
|
|
CV_WRAP void getNames(std::vector<String>& names) const;
|
|
|
|
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Mat getSubVector(const Mat& vec, const Mat& idx);
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Reads the dataset from a .csv file and returns the ready-to-use training data.
|
|
|
|
|
|
|
|
@param filename The input file name
|
|
|
|
@param headerLineCount The number of lines in the beginning to skip; besides the header, the
|
2014-12-31 19:18:50 +08:00
|
|
|
function also skips empty lines and lines staring with `#`
|
|
|
|
@param responseStartIdx Index of the first output variable. If -1, the function considers the
|
|
|
|
last variable as the response
|
|
|
|
@param responseEndIdx Index of the last output variable + 1. If -1, then there is single
|
|
|
|
response variable at responseStartIdx.
|
|
|
|
@param varTypeSpec The optional text string that specifies the variables' types. It has the
|
|
|
|
format `ord[n1-n2,n3,n4-n5,...]cat[n6,n7-n8,...]`. That is, variables from `n1 to n2`
|
|
|
|
(inclusive range), `n3`, `n4 to n5` ... are considered ordered and `n6`, `n7 to n8` ... are
|
|
|
|
considered as categorical. The range `[n1..n2] + [n3] + [n4..n5] + ... + [n6] + [n7..n8]`
|
|
|
|
should cover all the variables. If varTypeSpec is not specified, then algorithm uses the
|
|
|
|
following rules:
|
|
|
|
- all input variables are considered ordered by default. If some column contains has non-
|
|
|
|
numerical values, e.g. 'apple', 'pear', 'apple', 'apple', 'mango', the corresponding
|
|
|
|
variable is considered categorical.
|
|
|
|
- if there are several output variables, they are all considered as ordered. Error is
|
|
|
|
reported when non-numerical values are used.
|
|
|
|
- if there is a single output variable, then if its values are non-numerical or are all
|
|
|
|
integers, then it's considered categorical. Otherwise, it's considered ordered.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param delimiter The character used to separate values in each line.
|
|
|
|
@param missch The character used to specify missing measurements. It should not be a digit.
|
2014-12-31 19:18:50 +08:00
|
|
|
Although it's a non-numerical value, it surely does not affect the decision of whether the
|
|
|
|
variable ordered or categorical.
|
2015-09-03 11:52:04 +08:00
|
|
|
@note If the dataset only contains input variables and no responses, use responseStartIdx = -2
|
|
|
|
and responseEndIdx = 0. The output variables vector will just contain zeros.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
static Ptr<TrainData> loadFromCSV(const String& filename,
|
|
|
|
int headerLineCount,
|
|
|
|
int responseStartIdx=-1,
|
|
|
|
int responseEndIdx=-1,
|
|
|
|
const String& varTypeSpec=String(),
|
|
|
|
char delimiter=',',
|
|
|
|
char missch='?');
|
2014-12-31 19:18:50 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Creates training data from in-memory arrays.
|
|
|
|
|
2014-11-21 16:28:14 +08:00
|
|
|
@param samples matrix of samples. It should have CV_32F type.
|
2014-12-31 19:18:50 +08:00
|
|
|
@param layout see ml::SampleTypes.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param responses matrix of responses. If the responses are scalar, they should be stored as a
|
2014-12-31 19:18:50 +08:00
|
|
|
single row or as a single column. The matrix should have type CV_32F or CV_32S (in the
|
|
|
|
former case the responses are considered as ordered by default; in the latter case - as
|
|
|
|
categorical)
|
2014-11-19 22:55:13 +08:00
|
|
|
@param varIdx vector specifying which variables to use for training. It can be an integer vector
|
2014-12-31 19:18:50 +08:00
|
|
|
(CV_32S) containing 0-based variable indices or byte vector (CV_8U) containing a mask of
|
|
|
|
active variables.
|
|
|
|
@param sampleIdx vector specifying which samples to use for training. It can be an integer
|
|
|
|
vector (CV_32S) containing 0-based sample indices or byte vector (CV_8U) containing a mask
|
|
|
|
of training samples.
|
2014-11-21 16:28:14 +08:00
|
|
|
@param sampleWeights optional vector with weights for each sample. It should have CV_32F type.
|
2014-12-31 19:18:50 +08:00
|
|
|
@param varType optional vector of type CV_8U and size `<number_of_variables_in_samples> +
|
|
|
|
<number_of_variables_in_responses>`, containing types of each input and output variable. See
|
|
|
|
ml::VariableTypes.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2016-09-30 21:21:04 +08:00
|
|
|
CV_WRAP static Ptr<TrainData> create(InputArray samples, int layout, InputArray responses,
|
2014-07-30 03:54:23 +08:00
|
|
|
InputArray varIdx=noArray(), InputArray sampleIdx=noArray(),
|
|
|
|
InputArray sampleWeights=noArray(), InputArray varType=noArray());
|
|
|
|
};
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Base class for statistical models in OpenCV ML.
|
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W StatModel : public Algorithm
|
|
|
|
{
|
|
|
|
public:
|
2014-12-31 19:18:50 +08:00
|
|
|
/** Predict options */
|
|
|
|
enum Flags {
|
|
|
|
UPDATE_MODEL = 1,
|
|
|
|
RAW_OUTPUT=1, //!< makes the method return the raw results (the sum), not the class label
|
|
|
|
COMPRESSED_INPUT=2,
|
|
|
|
PREPROCESSED_INPUT=4
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Returns the number of variables in training samples */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getVarCount() const = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2015-04-07 21:44:26 +08:00
|
|
|
CV_WRAP virtual bool empty() const;
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Returns true if the model is trained */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool isTrained() const = 0;
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Returns true if the model is classifier */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool isClassifier() const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Trains the statistical model
|
|
|
|
|
|
|
|
@param trainData training data that can be loaded from file using TrainData::loadFromCSV or
|
2014-12-31 19:18:50 +08:00
|
|
|
created with TrainData::create.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param flags optional flags, depending on the model. Some of the models can be updated with the
|
2014-12-31 19:18:50 +08:00
|
|
|
new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2016-09-30 21:21:04 +08:00
|
|
|
CV_WRAP virtual bool train( const Ptr<TrainData>& trainData, int flags=0 );
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
/** @brief Trains the statistical model
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
@param samples training samples
|
2014-12-31 19:18:50 +08:00
|
|
|
@param layout See ml::SampleTypes.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param responses vector of responses associated with the training samples.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool train( InputArray samples, int layout, InputArray responses );
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Computes error on the training or test dataset
|
|
|
|
|
|
|
|
@param data the training data
|
|
|
|
@param test if true, the error is computed over the test subset of the data, otherwise it's
|
2014-12-31 19:18:50 +08:00
|
|
|
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.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param resp the optional output responses.
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
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%).
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2016-09-30 21:21:04 +08:00
|
|
|
CV_WRAP virtual float calcError( const Ptr<TrainData>& data, bool test, OutputArray resp ) const;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Predicts response(s) for the provided sample(s)
|
|
|
|
|
|
|
|
@param samples The input samples, floating-point matrix
|
|
|
|
@param results The optional output matrix of results.
|
2014-12-31 19:18:50 +08:00
|
|
|
@param flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** @brief Create and train model with default parameters
|
2014-12-31 19:18:50 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
The class must implement static `create()` method with no parameters or with all default parameter values
|
2014-12-31 19:18:50 +08:00
|
|
|
*/
|
2016-09-30 21:21:04 +08:00
|
|
|
template<typename _Tp> static Ptr<_Tp> train(const Ptr<TrainData>& data, int flags=0)
|
2014-08-03 05:41:09 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
Ptr<_Tp> model = _Tp::create();
|
|
|
|
return !model.empty() && model->train(data, flags) ? model : Ptr<_Tp>();
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* Normal Bayes Classifier *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Bayes classifier for normally distributed data.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
@sa @ref ml_intro_bayes
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W NormalBayesClassifier : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Predicts the response for sample(s).
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The method estimates the most probable classes for input vectors. Input vectors (one or more)
|
|
|
|
are stored as rows of the matrix inputs. In case of multiple input vectors, there should be one
|
|
|
|
output vector outputs. The predicted class for a single input vector is returned by the method.
|
|
|
|
The vector outputProbs contains the output probabilities corresponding to each element of
|
|
|
|
result.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual float predictProb( InputArray inputs, OutputArray outputs,
|
2014-07-30 03:54:23 +08:00
|
|
|
OutputArray outputProbs, int flags=0 ) const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Creates empty model
|
|
|
|
Use StatModel::train to train the model after creation. */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<NormalBayesClassifier> create();
|
2017-01-29 21:21:55 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized NormalBayesClassifier from a file
|
|
|
|
*
|
|
|
|
* Use NormalBayesClassifier::save to serialize and store an NormalBayesClassifier to disk.
|
|
|
|
* Load the NormalBayesClassifier from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized NormalBayesClassifier
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<NormalBayesClassifier> load(const String& filepath , const String& nodeName = String());
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* K-Nearest Neighbour Classifier *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class implements K-Nearest Neighbors model
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@sa @ref ml_intro_knn
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W KNearest : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-08-03 05:41:09 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Default number of neighbors to use in predict method. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setDefaultK */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getDefaultK() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getDefaultK @see getDefaultK */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setDefaultK(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Whether classification or regression model should be trained. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setIsClassifier */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool getIsClassifier() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getIsClassifier @see getIsClassifier */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setIsClassifier(bool val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter for KDTree implementation. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setEmax */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getEmax() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getEmax @see getEmax */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setEmax(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** %Algorithm type, one of KNearest::Types. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setAlgorithmType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getAlgorithmType() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getAlgorithmType @see getAlgorithmType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setAlgorithmType(int val) = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Finds the neighbors and predicts responses for input vectors.
|
|
|
|
|
|
|
|
@param samples Input samples stored by rows. It is a single-precision floating-point matrix of
|
2014-12-31 19:18:50 +08:00
|
|
|
`<number_of_samples> * k` size.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param k Number of used nearest neighbors. Should be greater than 1.
|
|
|
|
@param results Vector with results of prediction (regression or classification) for each input
|
2014-12-31 19:18:50 +08:00
|
|
|
sample. It is a single-precision floating-point vector with `<number_of_samples>` elements.
|
|
|
|
@param neighborResponses Optional output values for corresponding neighbors. It is a single-
|
|
|
|
precision floating-point matrix of `<number_of_samples> * k` size.
|
|
|
|
@param dist Optional output distances from the input vectors to the corresponding neighbors. It
|
|
|
|
is a single-precision floating-point matrix of `<number_of_samples> * k` size.
|
|
|
|
|
|
|
|
For each input vector (a row of the matrix samples), the method finds the k nearest neighbors.
|
|
|
|
In case of regression, the predicted result is a mean value of the particular vector's neighbor
|
2014-11-19 22:55:13 +08:00
|
|
|
responses. In case of classification, the class is determined by voting.
|
|
|
|
|
|
|
|
For each input vector, the neighbors are sorted by their distances to the vector.
|
|
|
|
|
|
|
|
In case of C++ interface you can use output pointers to empty matrices and the function will
|
|
|
|
allocate memory itself.
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
If only a single input vector is passed, all output matrices are optional and the predicted
|
|
|
|
value is returned by the method.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
The function is parallelized with the TBB library.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual float findNearest( InputArray samples, int k,
|
2014-07-30 03:54:23 +08:00
|
|
|
OutputArray results,
|
|
|
|
OutputArray neighborResponses=noArray(),
|
|
|
|
OutputArray dist=noArray() ) const = 0;
|
2014-08-23 22:41:32 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** @brief Implementations of KNearest algorithm
|
|
|
|
*/
|
|
|
|
enum Types
|
|
|
|
{
|
|
|
|
BRUTE_FORCE=1,
|
|
|
|
KDTREE=2
|
|
|
|
};
|
2014-08-23 22:41:32 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Creates the empty model
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<KNearest> create();
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* Support Vector Machines *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Support Vector Machines.
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@sa @ref ml_intro_svm
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W SVM : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS Kernel : public Algorithm
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual int getType() const = 0;
|
|
|
|
virtual void calc( int vcount, int n, const float* vecs, const float* another, float* results ) = 0;
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Type of a %SVM formulation.
|
|
|
|
See SVM::Types. Default value is SVM::C_SVC. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getType() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getType @see getType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setType(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter \f$\gamma\f$ of a kernel function.
|
|
|
|
For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setGamma */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getGamma() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getGamma @see getGamma */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setGamma(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter _coef0_ of a kernel function.
|
|
|
|
For SVM::POLY or SVM::SIGMOID. Default value is 0.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setCoef0 */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getCoef0() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getCoef0 @see getCoef0 */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setCoef0(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter _degree_ of a kernel function.
|
|
|
|
For SVM::POLY. Default value is 0. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setDegree */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getDegree() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getDegree @see getDegree */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setDegree(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter _C_ of a %SVM optimization problem.
|
|
|
|
For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setC */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getC() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getC @see getC */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setC(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter \f$\nu\f$ of a %SVM optimization problem.
|
|
|
|
For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setNu */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getNu() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getNu @see getNu */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setNu(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Parameter \f$\epsilon\f$ of a %SVM optimization problem.
|
|
|
|
For SVM::EPS_SVR. Default value is 0. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setP */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getP() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getP @see getP */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setP(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setClassWeights */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual cv::Mat getClassWeights() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getClassWeights @see getClassWeights */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setClassWeights(const cv::Mat &val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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 )`; */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual cv::TermCriteria getTermCriteria() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTermCriteria(const cv::TermCriteria &val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Type of a %SVM kernel.
|
|
|
|
See SVM::KernelTypes. Default value is SVM::RBF. */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getKernelType() const = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Initialize with one of predefined kernels.
|
|
|
|
See SVM::KernelTypes. */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setKernel(int kernelType) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Initialize with custom kernel.
|
|
|
|
See SVM::Kernel class for implementation details */
|
|
|
|
virtual void setCustomKernel(const Ptr<Kernel> &_kernel) = 0;
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! %SVM type
|
|
|
|
enum Types {
|
|
|
|
/** C-Support Vector Classification. n-class classification (n \f$\geq\f$ 2), allows
|
|
|
|
imperfect separation of classes with penalty multiplier C for outliers. */
|
|
|
|
C_SVC=100,
|
|
|
|
/** \f$\nu\f$-Support Vector Classification. n-class classification with possible
|
|
|
|
imperfect separation. Parameter \f$\nu\f$ (in the range 0..1, the larger the value, the smoother
|
|
|
|
the decision boundary) is used instead of C. */
|
|
|
|
NU_SVC=101,
|
|
|
|
/** Distribution Estimation (One-class %SVM). All the training data are from
|
|
|
|
the same class, %SVM builds a boundary that separates the class from the rest of the feature
|
|
|
|
space. */
|
|
|
|
ONE_CLASS=102,
|
|
|
|
/** \f$\epsilon\f$-Support Vector Regression. The distance between feature vectors
|
|
|
|
from the training set and the fitting hyper-plane must be less than p. For outliers the
|
|
|
|
penalty multiplier C is used. */
|
|
|
|
EPS_SVR=103,
|
|
|
|
/** \f$\nu\f$-Support Vector Regression. \f$\nu\f$ is used instead of p.
|
|
|
|
See @cite LibSVM for details. */
|
|
|
|
NU_SVR=104
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief %SVM kernel type
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
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)
|
|
|
|
*/
|
|
|
|
enum KernelTypes {
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Returned by SVM::getKernelType in case when custom kernel has been set */
|
2014-12-31 19:18:50 +08:00
|
|
|
CUSTOM=-1,
|
|
|
|
/** Linear kernel. No mapping is done, linear discrimination (or regression) is
|
|
|
|
done in the original feature space. It is the fastest option. \f$K(x_i, x_j) = x_i^T x_j\f$. */
|
|
|
|
LINEAR=0,
|
|
|
|
/** Polynomial kernel:
|
|
|
|
\f$K(x_i, x_j) = (\gamma x_i^T x_j + coef0)^{degree}, \gamma > 0\f$. */
|
|
|
|
POLY=1,
|
|
|
|
/** Radial basis function (RBF), a good choice in most cases.
|
|
|
|
\f$K(x_i, x_j) = e^{-\gamma ||x_i - x_j||^2}, \gamma > 0\f$. */
|
|
|
|
RBF=2,
|
|
|
|
/** Sigmoid kernel: \f$K(x_i, x_j) = \tanh(\gamma x_i^T x_j + coef0)\f$. */
|
|
|
|
SIGMOID=3,
|
|
|
|
/** Exponential Chi2 kernel, similar to the RBF kernel:
|
|
|
|
\f$K(x_i, x_j) = e^{-\gamma \chi^2(x_i,x_j)}, \chi^2(x_i,x_j) = (x_i-x_j)^2/(x_i+x_j), \gamma > 0\f$. */
|
|
|
|
CHI2=4,
|
|
|
|
/** Histogram intersection kernel. A fast kernel. \f$K(x_i, x_j) = min(x_i,x_j)\f$. */
|
|
|
|
INTER=5
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! %SVM params type
|
|
|
|
enum ParamTypes {
|
|
|
|
C=0,
|
|
|
|
GAMMA=1,
|
|
|
|
P=2,
|
|
|
|
NU=3,
|
|
|
|
COEF=4,
|
|
|
|
DEGREE=5
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Trains an %SVM with optimal parameters.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
@param data the training data that can be constructed using TrainData::create or
|
2014-12-31 19:18:50 +08:00
|
|
|
TrainData::loadFromCSV.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param kFold Cross-validation parameter. The training set is divided into kFold subsets. One
|
2014-12-31 19:18:50 +08:00
|
|
|
subset is used to test the model, the others form the train set. So, the %SVM algorithm is
|
|
|
|
executed kFold times.
|
|
|
|
@param Cgrid grid for C
|
|
|
|
@param gammaGrid grid for gamma
|
|
|
|
@param pGrid grid for p
|
|
|
|
@param nuGrid grid for nu
|
|
|
|
@param coeffGrid grid for coeff
|
|
|
|
@param degreeGrid grid for degree
|
2014-11-19 22:55:13 +08:00
|
|
|
@param balanced If true and the problem is 2-class classification then the method creates more
|
2014-12-31 19:18:50 +08:00
|
|
|
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,
|
2015-02-11 18:24:14 +08:00
|
|
|
nu, coef0, degree. Parameters are considered optimal when the cross-validation
|
2014-12-31 19:18:50 +08:00
|
|
|
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
|
2015-02-11 18:24:14 +08:00
|
|
|
`Gamma` is taken for gamma.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
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.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2016-09-30 21:21:04 +08:00
|
|
|
virtual bool trainAuto( const Ptr<TrainData>& data, int kFold = 10,
|
2017-03-07 21:02:19 +08:00
|
|
|
ParamGrid Cgrid = getDefaultGrid(C),
|
|
|
|
ParamGrid gammaGrid = getDefaultGrid(GAMMA),
|
|
|
|
ParamGrid pGrid = getDefaultGrid(P),
|
|
|
|
ParamGrid nuGrid = getDefaultGrid(NU),
|
|
|
|
ParamGrid coeffGrid = getDefaultGrid(COEF),
|
|
|
|
ParamGrid degreeGrid = getDefaultGrid(DEGREE),
|
2014-07-30 03:54:23 +08:00
|
|
|
bool balanced=false) = 0;
|
|
|
|
|
2017-03-23 21:00:19 +08:00
|
|
|
/** @brief Trains an %SVM with optimal parameters
|
|
|
|
|
|
|
|
@param samples training samples
|
|
|
|
@param layout See ml::SampleTypes.
|
|
|
|
@param responses vector of responses associated with the training samples.
|
|
|
|
@param 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
|
|
|
|
@param Cgrid grid for C
|
|
|
|
@param gammaGrid grid for gamma
|
|
|
|
@param pGrid grid for p
|
|
|
|
@param nuGrid grid for nu
|
|
|
|
@param coeffGrid grid for coeff
|
|
|
|
@param degreeGrid grid for degree
|
|
|
|
@param 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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
CV_WRAP bool trainAuto(InputArray samples,
|
|
|
|
int layout,
|
|
|
|
InputArray responses,
|
|
|
|
int kFold = 10,
|
|
|
|
Ptr<ParamGrid> Cgrid = SVM::getDefaultGridPtr(SVM::C),
|
|
|
|
Ptr<ParamGrid> gammaGrid = SVM::getDefaultGridPtr(SVM::GAMMA),
|
|
|
|
Ptr<ParamGrid> pGrid = SVM::getDefaultGridPtr(SVM::P),
|
|
|
|
Ptr<ParamGrid> nuGrid = SVM::getDefaultGridPtr(SVM::NU),
|
|
|
|
Ptr<ParamGrid> coeffGrid = SVM::getDefaultGridPtr(SVM::COEF),
|
|
|
|
Ptr<ParamGrid> degreeGrid = SVM::getDefaultGridPtr(SVM::DEGREE),
|
|
|
|
bool balanced=false);
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Retrieves all the support vectors
|
|
|
|
|
2015-04-19 09:32:29 +08:00
|
|
|
The method returns all the support vectors as a floating-point matrix, where support vectors are
|
2014-12-31 19:18:50 +08:00
|
|
|
stored as matrix rows.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_WRAP virtual Mat getSupportVectors() const = 0;
|
|
|
|
|
2015-04-19 09:32:29 +08:00
|
|
|
/** @brief 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.
|
|
|
|
*/
|
|
|
|
CV_WRAP Mat getUncompressedSupportVectors() const;
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Retrieves the decision function
|
|
|
|
|
|
|
|
@param i the index of the decision function. If the problem solved is regression, 1-class or
|
2014-12-31 19:18:50 +08:00
|
|
|
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 \f$N(N-1)/2\f$
|
|
|
|
decision functions.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param alpha the optional output vector for weights, corresponding to different support vectors.
|
2014-12-31 19:18:50 +08:00
|
|
|
In the case of linear %SVM all the alpha's will be 1's.
|
|
|
|
@param 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.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The method returns rho parameter of the decision function, a scalar subtracted from the weighted
|
|
|
|
sum of kernel responses.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getDecisionFunction(int i, OutputArray alpha, OutputArray svidx) const = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Generates a grid for %SVM parameters.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@param param_id %SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is
|
2015-02-11 18:24:14 +08:00
|
|
|
generated for the parameter with this ID.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The function generates a grid for the specified parameter of the %SVM algorithm. The grid may be
|
2014-11-19 22:55:13 +08:00
|
|
|
passed to the function SVM::trainAuto.
|
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
static ParamGrid getDefaultGrid( int param_id );
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2017-03-23 21:00:19 +08:00
|
|
|
/** @brief Generates a grid for %SVM parameters.
|
|
|
|
|
|
|
|
@param 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.
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<ParamGrid> getDefaultGridPtr( int param_id );
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** 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. */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<SVM> create();
|
2016-01-04 22:47:26 +08:00
|
|
|
|
2016-01-07 15:00:01 +08:00
|
|
|
/** @brief Loads and creates a serialized svm from a file
|
|
|
|
*
|
|
|
|
* Use SVM::save to serialize and store an SVM to disk.
|
|
|
|
* Load the SVM from this file again, by calling this function with the path to the file.
|
|
|
|
*
|
2016-01-07 15:19:06 +08:00
|
|
|
* @param filepath path to serialized svm
|
2016-01-07 15:00:01 +08:00
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<SVM> load(const String& filepath);
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* Expectation - Maximization *
|
|
|
|
\****************************************************************************************/
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class implements the Expectation Maximization algorithm.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@sa @ref ml_intro_em
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W EM : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-12-31 19:18:50 +08:00
|
|
|
//! Type of covariation matrices
|
|
|
|
enum Types {
|
|
|
|
/** A scaled identity matrix \f$\mu_k * I\f$. There is the only
|
|
|
|
parameter \f$\mu_k\f$ to be estimated for each matrix. The option may be used in special cases,
|
|
|
|
when the constraint is relevant, or as a first step in the optimization (for example in case
|
|
|
|
when the data is preprocessed with PCA). The results of such preliminary estimation may be
|
|
|
|
passed again to the optimization procedure, this time with
|
|
|
|
covMatType=EM::COV_MAT_DIAGONAL. */
|
|
|
|
COV_MAT_SPHERICAL=0,
|
|
|
|
/** A diagonal matrix with positive diagonal elements. The number of
|
|
|
|
free parameters is d for each matrix. This is most commonly used option yielding good
|
|
|
|
estimation results. */
|
|
|
|
COV_MAT_DIAGONAL=1,
|
|
|
|
/** A symmetric positively defined matrix. The number of free
|
|
|
|
parameters in each matrix is about \f$d^2/2\f$. It is not recommended to use this option, unless
|
|
|
|
there is pretty accurate initial estimation of the parameters and/or a huge number of
|
|
|
|
training samples. */
|
|
|
|
COV_MAT_GENERIC=2,
|
|
|
|
COV_MAT_DEFAULT=COV_MAT_DIAGONAL
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! Default parameters
|
2012-04-30 22:33:52 +08:00
|
|
|
enum {DEFAULT_NCLUSTERS=5, DEFAULT_MAX_ITERS=100};
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! The initial step
|
2012-04-14 05:50:59 +08:00
|
|
|
enum {START_E_STEP=1, START_M_STEP=2, START_AUTO_STEP=0};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** The number of mixture components in the Gaussian mixture model.
|
|
|
|
Default value of the parameter is EM::DEFAULT_NCLUSTERS=5. Some of %EM implementation could
|
|
|
|
determine the optimal number of mixtures within a specified value range, but that is not the
|
|
|
|
case in ML yet. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setClustersNumber */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getClustersNumber() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getClustersNumber @see getClustersNumber */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setClustersNumber(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Constraint on covariance matrices which defines type of matrices.
|
|
|
|
See EM::Types. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setCovarianceMatrixType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getCovarianceMatrixType() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getCovarianceMatrixType @see getCovarianceMatrixType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setCovarianceMatrixType(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** The termination criteria of the %EM algorithm.
|
|
|
|
The %EM algorithm can be terminated by the number of iterations termCrit.maxCount (number of
|
|
|
|
M-steps) or when relative change of likelihood logarithm is less than termCrit.epsilon. Default
|
|
|
|
maximum number of iterations is EM::DEFAULT_MAX_ITERS=100. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTermCriteria(const TermCriteria &val) = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns weights of the mixtures
|
|
|
|
|
|
|
|
Returns vector with the number of elements equal to the number of mixtures.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getWeights() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns the cluster centers (means of the Gaussian mixture)
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
Returns matrix with the number of rows equal to the number of mixtures and number of columns
|
|
|
|
equal to the space dimensionality.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getMeans() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns covariation matrices
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
Returns vector of covariation matrices. Number of matrices is the number of gaussian mixtures,
|
|
|
|
each matrix is a square floating-point matrix NxN, where N is the space dimensionality.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-07-24 00:38:18 +08:00
|
|
|
CV_WRAP virtual void getCovs(CV_OUT std::vector<Mat>& covs) const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-01-30 22:11:19 +08:00
|
|
|
/** @brief Returns posterior probabilities for the provided samples
|
|
|
|
|
|
|
|
@param samples The input samples, floating-point matrix
|
|
|
|
@param results The optional output \f$ nSamples \times nClusters\f$ matrix of results. It contains
|
|
|
|
posterior probabilities for each sample from the input
|
|
|
|
@param flags This parameter will be ignored
|
|
|
|
*/
|
|
|
|
CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Returns a likelihood logarithm value and an index of the most probable mixture component
|
|
|
|
for the given sample.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@param sample A sample for classification. It should be a one-channel matrix of
|
|
|
|
\f$1 \times dims\f$ or \f$dims \times 1\f$ size.
|
|
|
|
@param probs Optional output matrix that contains posterior probabilities of each component
|
|
|
|
given the sample. It has \f$1 \times nclusters\f$ size and CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The method returns a two-element double vector. Zero element is a likelihood logarithm value for
|
|
|
|
the sample. First element is an index of the most probable mixture component for the given
|
|
|
|
sample.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-07-24 00:38:18 +08:00
|
|
|
CV_WRAP virtual Vec2d predict2(InputArray sample, OutputArray probs) const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** @brief Estimate the Gaussian mixture parameters from a samples set.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
This variation starts with Expectation step. Initial values of the model parameters will be
|
|
|
|
estimated by the k-means algorithm.
|
|
|
|
|
|
|
|
Unlike many of the ML models, %EM is an unsupervised learning algorithm and it does not take
|
|
|
|
responses (class labels or function values) as input. Instead, it computes the *Maximum
|
|
|
|
Likelihood Estimate* of the Gaussian mixture parameters from an input sample set, stores all the
|
|
|
|
parameters inside the structure: \f$p_{i,k}\f$ in probs, \f$a_k\f$ in means , \f$S_k\f$ in
|
|
|
|
covs[k], \f$\pi_k\f$ in weights , and optionally computes the output "class label" for each
|
|
|
|
sample: \f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most
|
|
|
|
probable mixture component for each sample).
|
|
|
|
|
|
|
|
The trained model can be used further for prediction, just like any other classifier. The
|
|
|
|
trained model is similar to the NormalBayesClassifier.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
@param samples Samples from which the Gaussian mixture model will be estimated. It should be a
|
2014-12-31 19:18:50 +08:00
|
|
|
one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
|
|
|
|
it will be converted to the inner matrix of such type for the further computing.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
|
2014-12-31 19:18:50 +08:00
|
|
|
each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param labels The optional output "class label" for each sample:
|
2014-12-31 19:18:50 +08:00
|
|
|
\f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
|
|
|
|
mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param probs The optional output matrix that contains posterior probabilities of each Gaussian
|
2014-12-31 19:18:50 +08:00
|
|
|
mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
|
|
|
|
CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool trainEM(InputArray samples,
|
2015-02-11 18:24:14 +08:00
|
|
|
OutputArray logLikelihoods=noArray(),
|
|
|
|
OutputArray labels=noArray(),
|
|
|
|
OutputArray probs=noArray()) = 0;
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** @brief Estimate the Gaussian mixture parameters from a samples set.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
This variation starts with Expectation step. You need to provide initial means \f$a_k\f$ of
|
|
|
|
mixture components. Optionally you can pass initial weights \f$\pi_k\f$ and covariance matrices
|
2014-11-19 22:55:13 +08:00
|
|
|
\f$S_k\f$ of mixture components.
|
|
|
|
|
|
|
|
@param samples Samples from which the Gaussian mixture model will be estimated. It should be a
|
2014-12-31 19:18:50 +08:00
|
|
|
one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
|
|
|
|
it will be converted to the inner matrix of such type for the further computing.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param means0 Initial means \f$a_k\f$ of mixture components. It is a one-channel matrix of
|
2014-12-31 19:18:50 +08:00
|
|
|
\f$nclusters \times dims\f$ size. If the matrix does not have CV_64F type it will be
|
|
|
|
converted to the inner matrix of such type for the further computing.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param covs0 The vector of initial covariance matrices \f$S_k\f$ of mixture components. Each of
|
2014-12-31 19:18:50 +08:00
|
|
|
covariance matrices is a one-channel matrix of \f$dims \times dims\f$ size. If the matrices
|
|
|
|
do not have CV_64F type they will be converted to the inner matrices of such type for the
|
|
|
|
further computing.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param weights0 Initial weights \f$\pi_k\f$ of mixture components. It should be a one-channel
|
2014-12-31 19:18:50 +08:00
|
|
|
floating-point matrix with \f$1 \times nclusters\f$ or \f$nclusters \times 1\f$ size.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
|
2014-12-31 19:18:50 +08:00
|
|
|
each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param labels The optional output "class label" for each sample:
|
2014-12-31 19:18:50 +08:00
|
|
|
\f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
|
|
|
|
mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param probs The optional output matrix that contains posterior probabilities of each Gaussian
|
2014-12-31 19:18:50 +08:00
|
|
|
mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
|
|
|
|
CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool trainE(InputArray samples, InputArray means0,
|
2015-02-11 18:24:14 +08:00
|
|
|
InputArray covs0=noArray(),
|
|
|
|
InputArray weights0=noArray(),
|
|
|
|
OutputArray logLikelihoods=noArray(),
|
|
|
|
OutputArray labels=noArray(),
|
|
|
|
OutputArray probs=noArray()) = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** @brief Estimate the Gaussian mixture parameters from a samples set.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
This variation starts with Maximization step. You need to provide initial probabilities
|
|
|
|
\f$p_{i,k}\f$ to use this option.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
@param samples Samples from which the Gaussian mixture model will be estimated. It should be a
|
2014-12-31 19:18:50 +08:00
|
|
|
one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
|
|
|
|
it will be converted to the inner matrix of such type for the further computing.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param probs0
|
|
|
|
@param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
|
2014-12-31 19:18:50 +08:00
|
|
|
each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param labels The optional output "class label" for each sample:
|
2014-12-31 19:18:50 +08:00
|
|
|
\f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
|
|
|
|
mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param probs The optional output matrix that contains posterior probabilities of each Gaussian
|
2014-12-31 19:18:50 +08:00
|
|
|
mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
|
|
|
|
CV_64FC1 type.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool trainM(InputArray samples, InputArray probs0,
|
2015-02-11 18:24:14 +08:00
|
|
|
OutputArray logLikelihoods=noArray(),
|
|
|
|
OutputArray labels=noArray(),
|
|
|
|
OutputArray probs=noArray()) = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Creates empty %EM model.
|
2014-11-19 22:55:13 +08:00
|
|
|
The model should be trained then using StatModel::train(traindata, flags) method. Alternatively, you
|
2015-04-07 21:44:26 +08:00
|
|
|
can use one of the EM::train\* methods or load it from file using Algorithm::load\<EM\>(filename).
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<EM> create();
|
2017-01-29 21:21:55 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized EM from a file
|
|
|
|
*
|
|
|
|
* Use EM::save to serialize and store an EM to disk.
|
|
|
|
* Load the EM from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized EM
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<EM> load(const String& filepath , const String& nodeName = String());
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
2014-07-30 03:54:23 +08:00
|
|
|
* Decision Tree *
|
2010-05-12 01:44:00 +08:00
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class represents a single decision tree or a collection of decision trees.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
The current public interface of the class allows user to train only a single decision tree, however
|
|
|
|
the class is capable of storing multiple decision trees and using them for prediction (by summing
|
|
|
|
responses or using a voting schemes), and the derived from DTrees classes (such as RTrees and Boost)
|
|
|
|
use this capability to implement decision tree ensembles.
|
|
|
|
|
|
|
|
@sa @ref ml_intro_trees
|
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W DTrees : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-12-31 19:18:50 +08:00
|
|
|
/** Predict options */
|
|
|
|
enum Flags { PREDICT_AUTO=0, PREDICT_SUM=(1<<8), PREDICT_MAX_VOTE=(2<<8), PREDICT_MASK=(3<<8) };
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Cluster possible values of a categorical variable into K\<=maxCategories clusters to
|
|
|
|
find a suboptimal split.
|
|
|
|
If a discrete variable, on which the training procedure tries to make a split, takes more than
|
|
|
|
maxCategories values, the precise best subset estimation may take a very long time because the
|
|
|
|
algorithm is exponential. Instead, many decision trees engines (including our implementation)
|
|
|
|
try to find sub-optimal split in this case by clustering all the samples into maxCategories
|
|
|
|
clusters that is some categories are merged together. The clustering is applied only in n \>
|
|
|
|
2-class classification problems for categorical variables with N \> max_categories possible
|
|
|
|
values. In case of regression and 2-class classification the optimal split can be found
|
|
|
|
efficiently without employing clustering, thus the parameter is not used in these cases.
|
|
|
|
Default value is 10.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setMaxCategories */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getMaxCategories() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getMaxCategories @see getMaxCategories */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setMaxCategories(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** The maximum possible depth of the tree.
|
|
|
|
That is the training algorithms attempts to split a node while its depth is less than maxDepth.
|
|
|
|
The root node has zero depth. The actual depth may be smaller if the other termination criteria
|
|
|
|
are met (see the outline of the training procedure @ref ml_intro_trees "here"), and/or if the
|
|
|
|
tree is pruned. Default value is INT_MAX.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setMaxDepth */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getMaxDepth() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getMaxDepth @see getMaxDepth */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setMaxDepth(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** If the number of samples in a node is less than this parameter then the node will not be split.
|
|
|
|
|
|
|
|
Default value is 10.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setMinSampleCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getMinSampleCount() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getMinSampleCount @see getMinSampleCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setMinSampleCount(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** If CVFolds \> 1 then algorithms prunes the built decision tree using K-fold
|
|
|
|
cross-validation procedure where K is equal to CVFolds.
|
|
|
|
Default value is 10.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setCVFolds */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getCVFolds() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getCVFolds @see getCVFolds */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setCVFolds(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** If true then surrogate splits will be built.
|
|
|
|
These splits allow to work with missing data and compute variable importance correctly.
|
|
|
|
Default value is false.
|
|
|
|
@note currently it's not implemented.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setUseSurrogates */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool getUseSurrogates() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getUseSurrogates @see getUseSurrogates */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setUseSurrogates(bool val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** If true then a pruning will be harsher.
|
|
|
|
This will make a tree more compact and more resistant to the training data noise but a bit less
|
|
|
|
accurate. Default value is true.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setUse1SERule */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool getUse1SERule() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getUse1SERule @see getUse1SERule */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setUse1SERule(bool val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** If true then pruned branches are physically removed from the tree.
|
|
|
|
Otherwise they are retained and it is possible to get results from the original unpruned (or
|
|
|
|
pruned less aggressively) tree. Default value is true.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTruncatePrunedTree */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool getTruncatePrunedTree() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTruncatePrunedTree @see getTruncatePrunedTree */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTruncatePrunedTree(bool val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Termination criteria for regression trees.
|
|
|
|
If all absolute differences between an estimated value in a node and values of train samples
|
|
|
|
in this node are less than this parameter then the node will not be split further. Default
|
|
|
|
value is 0.01f*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRegressionAccuracy */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual float getRegressionAccuracy() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRegressionAccuracy @see getRegressionAccuracy */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setRegressionAccuracy(float val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** @brief The array of a priori class probabilities, sorted by the class label value.
|
|
|
|
|
|
|
|
The parameter can be used to tune the decision tree preferences toward a certain class. For
|
|
|
|
example, if you want to detect some rare anomaly occurrence, the training base will likely
|
|
|
|
contain much more normal cases than anomalies, so a very good classification performance
|
|
|
|
will be achieved just by considering every case as normal. To avoid this, the priors can be
|
|
|
|
specified, where the anomaly probability is artificially increased (up to 0.5 or even
|
|
|
|
greater), so the weight of the misclassified anomalies becomes much bigger, and the tree is
|
|
|
|
adjusted properly.
|
|
|
|
|
|
|
|
You can also think about this parameter as weights of prediction categories which determine
|
|
|
|
relative weights that you give to misclassification. That is, if the weight of the first
|
|
|
|
category is 1 and the weight of the second category is 10, then each mistake in predicting
|
|
|
|
the second category is equivalent to making 10 mistakes in predicting the first category.
|
|
|
|
Default value is empty Mat.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setPriors */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual cv::Mat getPriors() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getPriors @see getPriors */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setPriors(const cv::Mat &val) = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class represents a decision tree node.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Node();
|
2014-12-31 19:18:50 +08:00
|
|
|
double value; //!< Value at the node: a class label in case of classification or estimated
|
|
|
|
//!< function value in case of regression.
|
|
|
|
int classIdx; //!< Class index normalized to 0..class_count-1 range and assigned to the
|
|
|
|
//!< node. It is used internally in classification trees and tree ensembles.
|
|
|
|
int parent; //!< Index of the parent node
|
|
|
|
int left; //!< Index of the left child node
|
|
|
|
int right; //!< Index of right child node
|
|
|
|
int defaultDir; //!< Default direction where to go (-1: left or +1: right). It helps in the
|
|
|
|
//!< case of missing values.
|
|
|
|
int split; //!< Index of the first split
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class represents split in a decision tree.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS Split
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Split();
|
2014-12-31 19:18:50 +08:00
|
|
|
int varIdx; //!< Index of variable on which the split is created.
|
|
|
|
bool inversed; //!< If true, then the inverse split rule is used (i.e. left and right
|
|
|
|
//!< branches are exchanged in the rule expressions below).
|
|
|
|
float quality; //!< The split quality, a positive number. It is used to choose the best split.
|
|
|
|
int next; //!< Index of the next split in the list of splits for the node
|
|
|
|
float c; /**< The threshold value in case of split on an ordered variable.
|
|
|
|
The rule is:
|
|
|
|
@code{.none}
|
|
|
|
if var_value < c
|
|
|
|
then next_node <- left
|
|
|
|
else next_node <- right
|
|
|
|
@endcode */
|
|
|
|
int subsetOfs; /**< Offset of the bitset used by the split on a categorical variable.
|
|
|
|
The rule is:
|
|
|
|
@code{.none}
|
|
|
|
if bitset[var_value] == 1
|
|
|
|
then next_node <- left
|
|
|
|
else next_node <- right
|
|
|
|
@endcode */
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns indices of root nodes
|
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual const std::vector<int>& getRoots() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns all the nodes
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
all the node indices are indices in the returned vector
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual const std::vector<Node>& getNodes() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns all the splits
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
all the split indices are indices in the returned vector
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual const std::vector<Split>& getSplits() const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Returns all the bitsets for categorical splits
|
|
|
|
|
|
|
|
Split::subsetOfs is an offset in the returned vector
|
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual const std::vector<int>& getSubsets() const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Creates the empty model
|
|
|
|
|
|
|
|
The static method creates empty decision tree with the specified parameters. It should be then
|
2014-12-31 19:18:50 +08:00
|
|
|
trained using train method (see StatModel::train). Alternatively, you can load the model from
|
2015-04-07 21:44:26 +08:00
|
|
|
file using Algorithm::load\<DTrees\>(filename).
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<DTrees> create();
|
2017-01-29 18:13:01 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized DTrees from a file
|
|
|
|
*
|
|
|
|
* Use DTree::save to serialize and store an DTree to disk.
|
|
|
|
* Load the DTree from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized DTree
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<DTrees> load(const String& filepath , const String& nodeName = String());
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
2014-07-30 03:54:23 +08:00
|
|
|
* Random Trees Classifier *
|
2010-05-12 01:44:00 +08:00
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief The class implements the random forest predictor.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
@sa @ref ml_intro_rtrees
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W RTrees : public DTrees
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.
|
|
|
|
Default value is false.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setCalculateVarImportance */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual bool getCalculateVarImportance() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getCalculateVarImportance @see getCalculateVarImportance */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setCalculateVarImportance(bool val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** The size of the randomly selected subset of features at each tree node and that are used
|
|
|
|
to find the best split(s).
|
|
|
|
If you set it to 0 then the size will be set to the square root of the total number of
|
|
|
|
features. Default value is 0.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setActiveVarCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getActiveVarCount() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getActiveVarCount @see getActiveVarCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setActiveVarCount(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** The termination criteria that specifies when the training algorithm stops.
|
|
|
|
Either when the specified number of trees is trained and added to the ensemble or when
|
|
|
|
sufficient accuracy (measured as OOB error) is achieved. Typically the more trees you have the
|
|
|
|
better the accuracy. However, the improvement in accuracy generally diminishes and asymptotes
|
|
|
|
pass a certain number of trees. Also to keep in mind, the number of tree increases the
|
|
|
|
prediction time linearly. Default value is TermCriteria(TermCriteria::MAX_ITERS +
|
|
|
|
TermCriteria::EPS, 50, 0.1)*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTermCriteria(const TermCriteria &val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Returns the variable importance array.
|
2014-11-19 22:55:13 +08:00
|
|
|
The method returns the variable importance vector, computed at the training stage when
|
2015-02-11 18:24:14 +08:00
|
|
|
CalculateVarImportance is set to true. If this flag was set to false, the empty matrix is
|
2014-11-19 22:55:13 +08:00
|
|
|
returned.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat getVarImportance() const = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-02-01 13:31:10 +08:00
|
|
|
/** Returns the result of each individual tree in the forest.
|
|
|
|
In case the model is a regression problem, the method will return each of the trees'
|
|
|
|
results for each of the sample cases. If the model is a classifier, it will return
|
|
|
|
a Mat with samples + 1 rows, where the first row gives the class number and the
|
|
|
|
following rows return the votes each class had for each sample.
|
2018-02-12 23:15:36 +08:00
|
|
|
@param samples Array containing the samples for which votes will be calculated.
|
2017-02-01 13:31:10 +08:00
|
|
|
@param results Array where the result of the calculation will be written.
|
|
|
|
@param flags Flags for defining the type of RTrees.
|
|
|
|
*/
|
|
|
|
CV_WRAP void getVotes(InputArray samples, OutputArray results, int flags) const;
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Creates the empty model.
|
2014-12-31 19:18:50 +08:00
|
|
|
Use StatModel::train to train the model, StatModel::train to create and train the model,
|
2015-04-07 21:44:26 +08:00
|
|
|
Algorithm::load to load the pre-trained model.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<RTrees> create();
|
2017-01-13 04:19:49 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized RTree from a file
|
|
|
|
*
|
|
|
|
* Use RTree::save to serialize and store an RTree to disk.
|
|
|
|
* Load the RTree from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized RTree
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<RTrees> load(const String& filepath , const String& nodeName = String());
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Boosted tree classifier *
|
|
|
|
\****************************************************************************************/
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Boosted tree classifier derived from DTrees
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
@sa @ref ml_intro_boost
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2014-07-30 03:54:23 +08:00
|
|
|
class CV_EXPORTS_W Boost : public DTrees
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Type of the boosting algorithm.
|
|
|
|
See Boost::Types. Default value is Boost::REAL. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setBoostType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getBoostType() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getBoostType @see getBoostType */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setBoostType(int val) = 0;
|
2014-12-31 19:18:50 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** The number of weak classifiers.
|
|
|
|
Default value is 100. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setWeakCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getWeakCount() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getWeakCount @see getWeakCount */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setWeakCount(int val) = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** A threshold between 0 and 1 used to save computational time.
|
|
|
|
Samples with summary weight \f$\leq 1 - weight_trim_rate\f$ do not participate in the *next*
|
|
|
|
iteration of training. Set this parameter to 0 to turn off this functionality. Default value is 0.95.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setWeightTrimRate */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getWeightTrimRate() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getWeightTrimRate @see getWeightTrimRate */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setWeightTrimRate(double val) = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Boosting type.
|
|
|
|
Gentle AdaBoost and Real AdaBoost are often the preferable choices. */
|
2014-12-31 19:18:50 +08:00
|
|
|
enum Types {
|
|
|
|
DISCRETE=0, //!< Discrete AdaBoost.
|
|
|
|
REAL=1, //!< Real AdaBoost. It is a technique that utilizes confidence-rated predictions
|
|
|
|
//!< and works well with categorical data.
|
|
|
|
LOGIT=2, //!< LogitBoost. It can produce good regression fits.
|
|
|
|
GENTLE=3 //!< Gentle AdaBoost. It puts less weight on outlier data points and for that
|
|
|
|
//!<reason is often good with regression data.
|
|
|
|
};
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Creates the empty model.
|
2015-04-07 21:44:26 +08:00
|
|
|
Use StatModel::train to train the model, Algorithm::load\<Boost\>(filename) to load the pre-trained model. */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<Boost> create();
|
2017-01-29 18:13:01 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized Boost from a file
|
|
|
|
*
|
|
|
|
* Use Boost::save to serialize and store an RTree to disk.
|
|
|
|
* Load the Boost from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized Boost
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<Boost> load(const String& filepath , const String& nodeName = String());
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-10-14 04:18:12 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Gradient Boosted Trees *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-08-04 01:21:11 +08:00
|
|
|
/*class CV_EXPORTS_W GBTrees : public DTrees
|
2010-10-14 04:18:12 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-07-30 03:54:23 +08:00
|
|
|
struct CV_EXPORTS_W_MAP Params : public DTrees::Params
|
|
|
|
{
|
|
|
|
CV_PROP_RW int weakCount;
|
|
|
|
CV_PROP_RW int lossFunctionType;
|
|
|
|
CV_PROP_RW float subsamplePortion;
|
|
|
|
CV_PROP_RW float shrinkage;
|
|
|
|
|
|
|
|
Params();
|
|
|
|
Params( int lossFunctionType, int weakCount, float shrinkage,
|
|
|
|
float subsamplePortion, int maxDepth, bool useSurrogates );
|
|
|
|
};
|
2010-10-14 04:18:12 +08:00
|
|
|
|
|
|
|
enum {SQUARED_LOSS=0, ABSOLUTE_LOSS, HUBER_LOSS=3, DEVIANCE_LOSS};
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual void setK(int k) = 0;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
virtual float predictSerial( InputArray samples,
|
2014-08-04 01:21:11 +08:00
|
|
|
OutputArray weakResponses, int flags) const = 0;
|
2010-10-14 04:18:12 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
static Ptr<GBTrees> create(const Params& p);
|
2014-08-04 01:21:11 +08:00
|
|
|
};*/
|
2010-10-14 04:18:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Artificial Neural Networks (ANN) *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
|
|
|
/////////////////////////////////// Multi-Layer Perceptrons //////////////////////////////
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Artificial Neural Networks - Multi-Layer Perceptrons.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
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
|
2014-11-21 16:28:14 +08:00
|
|
|
constructor or the method ANN_MLP::create. All the weights are set to zeros. Then, the network is
|
2014-11-19 22:55:13 +08:00
|
|
|
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.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
Additional flags for StatModel::train are available: ANN_MLP::TrainFlags.
|
|
|
|
|
|
|
|
@sa @ref ml_intro_ann
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-05-06 20:21:23 +08:00
|
|
|
class CV_EXPORTS_W ANN_MLP : public StatModel
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Available training methods */
|
|
|
|
enum TrainingMethods {
|
|
|
|
BACKPROP=0, //!< The back-propagation algorithm.
|
2017-12-15 18:57:39 +08:00
|
|
|
RPROP = 1, //!< The RPROP algorithm. See @cite RPROP93 for details.
|
|
|
|
ANNEAL = 2 //!< The simulated annealing algorithm. See @cite Kirkpatrick83 for details.
|
2014-07-30 03:54:23 +08:00
|
|
|
};
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
/** Sets training method and common parameters.
|
|
|
|
@param method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
|
2017-12-15 18:57:39 +08:00
|
|
|
@param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.
|
|
|
|
@param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.
|
2015-02-11 18:24:14 +08:00
|
|
|
*/
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Returns current training method */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual int getTrainMethod() const = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Initialize the activation function for each neuron.
|
|
|
|
Currently the default and the only fully supported activation function is ANN_MLP::SIGMOID_SYM.
|
|
|
|
@param type The type of activation function. See ANN_MLP::ActivationFunctions.
|
|
|
|
@param param1 The first parameter of the activation function, \f$\alpha\f$. Default value is 0.
|
|
|
|
@param param2 The second parameter of the activation function, \f$\beta\f$. Default value is 0.
|
|
|
|
*/
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setActivationFunction(int type, double param1 = 0, double param2 = 0) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
@sa getLayerSizes */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setLayerSizes(InputArray _layer_sizes) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
@sa setLayerSizes */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual cv::Mat getLayerSizes() const = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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).*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTermCriteria */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setTermCriteria(TermCriteria val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** BPROP: Strength of the weight gradient term.
|
|
|
|
The recommended value is about 0.1. Default value is 0.1.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setBackpropWeightScale */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getBackpropWeightScale() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getBackpropWeightScale @see getBackpropWeightScale */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setBackpropWeightScale(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** 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.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setBackpropMomentumScale */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getBackpropMomentumScale() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getBackpropMomentumScale @see getBackpropMomentumScale */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setBackpropMomentumScale(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** RPROP: Initial value \f$\Delta_0\f$ of update-values \f$\Delta_{ij}\f$.
|
|
|
|
Default value is 0.1.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRpropDW0 */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getRpropDW0() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRpropDW0 @see getRpropDW0 */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setRpropDW0(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** RPROP: Increase factor \f$\eta^+\f$.
|
|
|
|
It must be \>1. Default value is 1.2.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRpropDWPlus */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getRpropDWPlus() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRpropDWPlus @see getRpropDWPlus */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setRpropDWPlus(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** RPROP: Decrease factor \f$\eta^-\f$.
|
|
|
|
It must be \<1. Default value is 0.5.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRpropDWMinus */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getRpropDWMinus() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRpropDWMinus @see getRpropDWMinus */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setRpropDWMinus(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** RPROP: Update-values lower limit \f$\Delta_{min}\f$.
|
|
|
|
It must be positive. Default value is FLT_EPSILON.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRpropDWMin */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getRpropDWMin() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRpropDWMin @see getRpropDWMin */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setRpropDWMin(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** RPROP: Update-values upper limit \f$\Delta_{max}\f$.
|
|
|
|
It must be \>1. Default value is 50.*/
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRpropDWMax */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual double getRpropDWMax() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRpropDWMax @see getRpropDWMax */
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual void setRpropDWMax(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
2017-12-15 18:57:39 +08:00
|
|
|
/** ANNEAL: Update initial temperature.
|
|
|
|
It must be \>=0. Default value is 10.*/
|
|
|
|
/** @see setAnnealInitialT */
|
|
|
|
CV_WRAP double getAnnealInitialT() const;
|
|
|
|
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
|
|
|
|
CV_WRAP void setAnnealInitialT(double val);
|
|
|
|
|
|
|
|
/** ANNEAL: Update final temperature.
|
|
|
|
It must be \>=0 and less than initialT. Default value is 0.1.*/
|
|
|
|
/** @see setAnnealFinalT */
|
|
|
|
CV_WRAP double getAnnealFinalT() const;
|
|
|
|
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
|
|
|
|
CV_WRAP void setAnnealFinalT(double val);
|
|
|
|
|
|
|
|
/** ANNEAL: Update cooling ratio.
|
|
|
|
It must be \>0 and less than 1. Default value is 0.95.*/
|
|
|
|
/** @see setAnnealCoolingRatio */
|
|
|
|
CV_WRAP double getAnnealCoolingRatio() const;
|
|
|
|
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
|
|
|
|
CV_WRAP void setAnnealCoolingRatio(double val);
|
|
|
|
|
|
|
|
/** ANNEAL: Update iteration per step.
|
|
|
|
It must be \>0 . Default value is 10.*/
|
|
|
|
/** @see setAnnealItePerStep */
|
|
|
|
CV_WRAP int getAnnealItePerStep() const;
|
|
|
|
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
|
|
|
|
CV_WRAP void setAnnealItePerStep(int val);
|
|
|
|
|
2017-12-15 22:40:08 +08:00
|
|
|
/** @brief Set/initialize anneal RNG */
|
|
|
|
void setAnnealEnergyRNG(const RNG& rng);
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** possible activation functions */
|
|
|
|
enum ActivationFunctions {
|
|
|
|
/** Identity function: \f$f(x)=x\f$ */
|
|
|
|
IDENTITY = 0,
|
2017-11-23 05:07:23 +08:00
|
|
|
/** Symmetrical sigmoid: \f$f(x)=\beta*(1-e^{-\alpha x})/(1+e^{-\alpha x})\f$
|
2014-12-31 19:18:50 +08:00
|
|
|
@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].*/
|
|
|
|
SIGMOID_SYM = 1,
|
|
|
|
/** Gaussian function: \f$f(x)=\beta e^{-\alpha x*x}\f$ */
|
2017-11-23 05:07:23 +08:00
|
|
|
GAUSSIAN = 2,
|
|
|
|
/** ReLU function: \f$f(x)=max(0,x)\f$ */
|
|
|
|
RELU = 3,
|
|
|
|
/** Leaky ReLU function: for x>0 \f$f(x)=x \f$ and x<=0 \f$f(x)=\alpha x \f$*/
|
|
|
|
LEAKYRELU= 4
|
2014-12-31 19:18:50 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** Train options */
|
|
|
|
enum TrainFlags {
|
|
|
|
/** Update the network weights, rather than compute them from scratch. In the latter case
|
|
|
|
the weights are initialized using the Nguyen-Widrow algorithm. */
|
|
|
|
UPDATE_WEIGHTS = 1,
|
|
|
|
/** 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_INPUT_SCALE = 2,
|
|
|
|
/** 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. */
|
|
|
|
NO_OUTPUT_SCALE = 4
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP virtual Mat getWeights(int layerIdx) const = 0;
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Creates empty model
|
|
|
|
|
2015-04-07 21:44:26 +08:00
|
|
|
Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
|
2014-12-31 19:18:50 +08:00
|
|
|
Note that the train method has optional flags: ANN_MLP::TrainFlags.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-05-06 20:21:23 +08:00
|
|
|
CV_WRAP static Ptr<ANN_MLP> create();
|
2016-01-11 17:59:15 +08:00
|
|
|
|
|
|
|
/** @brief 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.
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized ANN
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<ANN_MLP> load(const String& filepath);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2013-08-05 21:42:07 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Logistic Regression *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Implements Logistic Regression classifier.
|
2014-12-31 19:18:50 +08:00
|
|
|
|
|
|
|
@sa @ref ml_intro_lr
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
class CV_EXPORTS_W LogisticRegression : public StatModel
|
2013-08-05 21:42:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Learning rate. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setLearningRate */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual double getLearningRate() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getLearningRate @see getLearningRate */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setLearningRate(double val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Number of iterations. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setIterations */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getIterations() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getIterations @see getIterations */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setIterations(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Kind of regularization to be applied. See LogisticRegression::RegKinds. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setRegularization */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getRegularization() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getRegularization @see getRegularization */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setRegularization(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Kind of training method used. See LogisticRegression::Methods. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTrainMethod */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getTrainMethod() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTrainMethod @see getTrainMethod */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTrainMethod(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Specifies the number of training samples taken in each step of Mini-Batch Gradient
|
|
|
|
Descent. Will only be used if using LogisticRegression::MINI_BATCH training algorithm. It
|
|
|
|
has to take values less than the total number of training samples. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setMiniBatchSize */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual int getMiniBatchSize() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getMiniBatchSize @see getMiniBatchSize */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setMiniBatchSize(int val) = 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
|
|
|
|
/** Termination criteria of the algorithm. */
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @see setTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
|
2015-03-18 22:23:42 +08:00
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual void setTermCriteria(TermCriteria val) = 0;
|
2013-08-05 21:42:07 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! Regularization kinds
|
|
|
|
enum RegKinds {
|
2015-03-11 23:32:17 +08:00
|
|
|
REG_DISABLE = -1, //!< Regularization disabled
|
2014-12-31 19:18:50 +08:00
|
|
|
REG_L1 = 0, //!< %L1 norm
|
2015-02-11 18:24:14 +08:00
|
|
|
REG_L2 = 1 //!< %L2 norm
|
2014-12-31 19:18:50 +08:00
|
|
|
};
|
2013-08-05 21:42:07 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
//! Training methods
|
|
|
|
enum Methods {
|
|
|
|
BATCH = 0,
|
2015-02-11 18:24:14 +08:00
|
|
|
MINI_BATCH = 1 //!< Set MiniBatchSize to a positive integer when using this method.
|
2014-12-31 19:18:50 +08:00
|
|
|
};
|
2014-11-19 22:55:13 +08:00
|
|
|
|
|
|
|
/** @brief Predicts responses for input samples and returns a float type.
|
|
|
|
|
|
|
|
@param samples The input data for the prediction algorithm. Matrix [m x n], where each row
|
2014-12-31 19:18:50 +08:00
|
|
|
contains variables (features) of one object being classified. Should have data type CV_32F.
|
2014-11-21 16:28:14 +08:00
|
|
|
@param results Predicted labels as a column matrix of type CV_32S.
|
2014-11-19 22:55:13 +08:00
|
|
|
@param flags Not used.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
|
2013-10-05 02:00:38 +08:00
|
|
|
|
2018-02-12 23:15:36 +08:00
|
|
|
/** @brief This function returns the trained parameters arranged across rows.
|
2014-11-19 22:55:13 +08:00
|
|
|
|
2018-02-12 23:15:36 +08:00
|
|
|
For a two class classifcation problem, it returns a row matrix. It returns learnt parameters of
|
2014-12-31 19:18:50 +08:00
|
|
|
the Logistic Regression as a matrix of type CV_32F.
|
2014-11-19 22:55:13 +08:00
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP virtual Mat get_learnt_thetas() const = 0;
|
2013-10-05 02:00:38 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
/** @brief Creates empty model.
|
|
|
|
|
|
|
|
Creates Logistic Regression model with parameters given.
|
|
|
|
*/
|
2015-03-19 18:22:30 +08:00
|
|
|
CV_WRAP static Ptr<LogisticRegression> create();
|
2017-01-29 21:21:55 +08:00
|
|
|
|
|
|
|
/** @brief Loads and creates a serialized LogisticRegression from a file
|
|
|
|
*
|
|
|
|
* Use LogisticRegression::save to serialize and store an LogisticRegression to disk.
|
|
|
|
* Load the LogisticRegression from this file again, by calling this function with the path to the file.
|
|
|
|
* Optionally specify the node for the file containing the classifier
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized LogisticRegression
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<LogisticRegression> load(const String& filepath , const String& nodeName = String());
|
2013-08-05 21:42:07 +08:00
|
|
|
};
|
|
|
|
|
2016-02-03 20:31:05 +08:00
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* Stochastic Gradient Descent SVM Classifier *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief Stochastic Gradient Descent SVM classifier
|
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach,
|
|
|
|
as presented in @cite bottou2010large.
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-25 20:31:07 +08:00
|
|
|
The classifier has following parameters:
|
2016-02-09 23:42:23 +08:00
|
|
|
- model type,
|
|
|
|
- margin type,
|
2016-02-24 18:33:43 +08:00
|
|
|
- margin regularization (\f$\lambda\f$),
|
|
|
|
- initial step size (\f$\gamma_0\f$),
|
|
|
|
- step decreasing power (\f$c\f$),
|
2016-02-09 23:42:23 +08:00
|
|
|
- and termination criteria.
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
The model type may have one of the following values: \ref SGD and \ref ASGD.
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
- \ref SGD is the classic version of SVMSGD classifier: every next step is calculated by the formula
|
|
|
|
\f[w_{t+1} = w_t - \gamma(t) \frac{dQ_i}{dw} |_{w = w_t}\f]
|
|
|
|
where
|
|
|
|
- \f$w_t\f$ is the weights vector for decision function at step \f$t\f$,
|
|
|
|
- \f$\gamma(t)\f$ is the step size of model parameters at the iteration \f$t\f$, it is decreased on each step by the formula
|
|
|
|
\f$\gamma(t) = \gamma_0 (1 + \lambda \gamma_0 t) ^ {-c}\f$
|
|
|
|
- \f$Q_i\f$ is the target functional from SVM task for sample with number \f$i\f$, this sample is chosen stochastically on each step of the algorithm.
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
- \ref ASGD is Average Stochastic Gradient Descent SVM Classifier. ASGD classifier averages weights vector on each step of algorithm by the formula
|
|
|
|
\f$\widehat{w}_{t+1} = \frac{t}{1+t}\widehat{w}_{t} + \frac{1}{1+t}w_{t+1}\f$
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
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.
|
2016-02-15 19:35:36 +08:00
|
|
|
- In the general case (if you know nothing about linear separability of your sets), use SOFT_MARGIN.
|
2016-02-09 23:42:23 +08:00
|
|
|
|
|
|
|
The other parameters may be described as follows:
|
2016-02-24 18:33:43 +08:00
|
|
|
- Margin regularization parameter is responsible for weights decreasing at each step and for the strength of restrictions on outliers
|
2016-02-09 23:42:23 +08:00
|
|
|
(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.
|
|
|
|
|
2016-02-24 18:33:43 +08:00
|
|
|
- Initial step size parameter is the initial value for the step size \f$\gamma(t)\f$.
|
2016-02-26 00:12:54 +08:00
|
|
|
You will have to find the best initial step for your problem.
|
2016-02-09 23:42:23 +08:00
|
|
|
|
2016-02-24 18:33:43 +08:00
|
|
|
- Step decreasing power is the power parameter for \f$\gamma(t)\f$ decreasing by the formula, mentioned above.
|
2016-02-09 23:42:23 +08:00
|
|
|
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.
|
|
|
|
|
2016-02-24 18:33:43 +08:00
|
|
|
Note that the parameters margin regularization, initial step size, and step decreasing power should be positive.
|
2016-02-09 23:42:23 +08:00
|
|
|
|
|
|
|
To use SVMSGD algorithm do as follows:
|
|
|
|
|
2016-02-26 19:40:23 +08:00
|
|
|
- first, create the SVMSGD object. The algoorithm will set optimal parameters by default, but you can set your own parameters via functions setSvmsgdType(),
|
|
|
|
setMarginType(), setMarginRegularization(), setInitialStepSize(), and setStepDecreasingPower().
|
2016-02-09 23:42:23 +08:00
|
|
|
|
|
|
|
- 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().
|
2016-02-03 20:31:05 +08:00
|
|
|
|
|
|
|
@code
|
2016-02-09 23:42:23 +08:00
|
|
|
// Create empty object
|
|
|
|
cv::Ptr<SVMSGD> svmsgd = SVMSGD::create();
|
|
|
|
|
2016-02-03 20:31:05 +08:00
|
|
|
// Train the Stochastic Gradient Descent SVM
|
2016-02-25 20:31:07 +08:00
|
|
|
svmsgd->train(trainData);
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
// Predict labels for the new samples
|
|
|
|
svmsgd->predict(samples, responses);
|
2016-02-03 20:31:05 +08:00
|
|
|
@endcode
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CV_EXPORTS_W SVMSGD : public cv::ml::StatModel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** SVMSGD type.
|
|
|
|
ASGD is often the preferable choice. */
|
|
|
|
enum SvmsgdType
|
|
|
|
{
|
2016-02-09 23:42:23 +08:00
|
|
|
SGD, //!< Stochastic Gradient Descent
|
|
|
|
ASGD //!< Average Stochastic Gradient Descent
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Margin type.*/
|
|
|
|
enum MarginType
|
|
|
|
{
|
|
|
|
SOFT_MARGIN, //!< General case, suits to the case of non-linearly separable sets, allows outliers.
|
|
|
|
HARD_MARGIN //!< More accurate for the case of linearly separable sets.
|
2016-02-03 20:31:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the weights of the trained model (decision function f(x) = weights * x + shift).
|
|
|
|
*/
|
|
|
|
CV_WRAP virtual Mat getWeights() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the shift of the trained model (decision function f(x) = weights * x + shift).
|
|
|
|
*/
|
|
|
|
CV_WRAP virtual float getShift() = 0;
|
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
/** @brief Creates empty model.
|
2016-02-26 00:12:54 +08:00
|
|
|
* 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.
|
2016-02-03 20:31:05 +08:00
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<SVMSGD> create();
|
|
|
|
|
2017-01-29 21:21:55 +08:00
|
|
|
/** @brief 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
|
|
|
|
*
|
|
|
|
* @param filepath path to serialized SVMSGD
|
|
|
|
* @param nodeName name of node containing the classifier
|
|
|
|
*/
|
|
|
|
CV_WRAP static Ptr<SVMSGD> load(const String& filepath , const String& nodeName = String());
|
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
/** @brief Function sets optimal parameters values for chosen SVM SGD model.
|
2016-02-26 00:12:54 +08:00
|
|
|
* @param svmsgdType is the type of SVMSGD classifier.
|
|
|
|
* @param marginType is the type of margin constraint.
|
2016-02-03 20:31:05 +08:00
|
|
|
*/
|
2016-02-10 23:21:45 +08:00
|
|
|
CV_WRAP virtual void setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN) = 0;
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-09 23:42:23 +08:00
|
|
|
/** @brief %Algorithm type, one of SVMSGD::SvmsgdType. */
|
2016-02-11 00:46:24 +08:00
|
|
|
/** @see setSvmsgdType */
|
2016-02-09 23:42:23 +08:00
|
|
|
CV_WRAP virtual int getSvmsgdType() const = 0;
|
2016-02-11 00:46:24 +08:00
|
|
|
/** @copybrief getSvmsgdType @see getSvmsgdType */
|
2016-02-09 23:42:23 +08:00
|
|
|
CV_WRAP virtual void setSvmsgdType(int svmsgdType) = 0;
|
|
|
|
|
|
|
|
/** @brief %Margin type, one of SVMSGD::MarginType. */
|
|
|
|
/** @see setMarginType */
|
|
|
|
CV_WRAP virtual int getMarginType() const = 0;
|
|
|
|
/** @copybrief getMarginType @see getMarginType */
|
|
|
|
CV_WRAP virtual void setMarginType(int marginType) = 0;
|
|
|
|
|
2016-02-26 00:12:54 +08:00
|
|
|
/** @brief Parameter marginRegularization of a %SVMSGD optimization problem. */
|
2016-02-24 18:22:07 +08:00
|
|
|
/** @see setMarginRegularization */
|
|
|
|
CV_WRAP virtual float getMarginRegularization() const = 0;
|
|
|
|
/** @copybrief getMarginRegularization @see getMarginRegularization */
|
|
|
|
CV_WRAP virtual void setMarginRegularization(float marginRegularization) = 0;
|
2016-02-03 20:31:05 +08:00
|
|
|
|
2016-02-26 00:12:54 +08:00
|
|
|
/** @brief Parameter initialStepSize of a %SVMSGD optimization problem. */
|
2016-02-24 18:22:07 +08:00
|
|
|
/** @see setInitialStepSize */
|
|
|
|
CV_WRAP virtual float getInitialStepSize() const = 0;
|
|
|
|
/** @copybrief getInitialStepSize @see getInitialStepSize */
|
|
|
|
CV_WRAP virtual void setInitialStepSize(float InitialStepSize) = 0;
|
|
|
|
|
2016-02-26 00:12:54 +08:00
|
|
|
/** @brief Parameter stepDecreasingPower of a %SVMSGD optimization problem. */
|
2016-02-24 18:22:07 +08:00
|
|
|
/** @see setStepDecreasingPower */
|
|
|
|
CV_WRAP virtual float getStepDecreasingPower() const = 0;
|
|
|
|
/** @copybrief getStepDecreasingPower @see getStepDecreasingPower */
|
|
|
|
CV_WRAP virtual void setStepDecreasingPower(float stepDecreasingPower) = 0;
|
2016-02-03 20:31:05 +08:00
|
|
|
|
|
|
|
/** @brief 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 */
|
|
|
|
CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
|
|
|
|
/** @copybrief getTermCriteria @see getTermCriteria */
|
|
|
|
CV_WRAP virtual void setTermCriteria(const cv::TermCriteria &val) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/****************************************************************************************\
|
2018-02-12 23:15:36 +08:00
|
|
|
* Auxiliary functions declarations *
|
2010-05-12 01:44:00 +08:00
|
|
|
\****************************************************************************************/
|
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Generates _sample_ from multivariate normal distribution
|
|
|
|
|
|
|
|
@param mean an average row vector
|
|
|
|
@param cov symmetric covariation matrix
|
|
|
|
@param nsamples returned samples count
|
|
|
|
@param samples returned samples array
|
|
|
|
*/
|
2015-03-19 20:11:49 +08:00
|
|
|
CV_EXPORTS void randMVNormal( InputArray mean, InputArray cov, int nsamples, OutputArray samples);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-31 19:18:50 +08:00
|
|
|
/** @brief Creates test set */
|
2015-03-19 20:11:49 +08:00
|
|
|
CV_EXPORTS void createConcentricSpheresTestSet( int nsamples, int nfeatures, int nclasses,
|
2014-07-30 03:54:23 +08:00
|
|
|
OutputArray samples, OutputArray responses);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @brief Artificial Neural Networks - Multi-Layer Perceptrons.
|
|
|
|
|
|
|
|
@sa @ref ml_intro_ann
|
|
|
|
*/
|
|
|
|
class CV_EXPORTS_W ANN_MLP_ANNEAL : public ANN_MLP
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** @see setAnnealInitialT */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual double getAnnealInitialT() const = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual void setAnnealInitialT(double val) = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
|
|
|
|
/** ANNEAL: Update final temperature.
|
|
|
|
It must be \>=0 and less than initialT. Default value is 0.1.*/
|
|
|
|
/** @see setAnnealFinalT */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual double getAnnealFinalT() const = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual void setAnnealFinalT(double val) = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
|
|
|
|
/** ANNEAL: Update cooling ratio.
|
|
|
|
It must be \>0 and less than 1. Default value is 0.95.*/
|
|
|
|
/** @see setAnnealCoolingRatio */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual double getAnnealCoolingRatio() const = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual void setAnnealCoolingRatio(double val) = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
|
|
|
|
/** ANNEAL: Update iteration per step.
|
|
|
|
It must be \>0 . Default value is 10.*/
|
|
|
|
/** @see setAnnealItePerStep */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual int getAnnealItePerStep() const = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
|
2017-12-15 22:40:08 +08:00
|
|
|
CV_WRAP virtual void setAnnealItePerStep(int val) = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
|
2017-12-15 22:40:08 +08:00
|
|
|
/** @brief Set/initialize anneal RNG */
|
|
|
|
virtual void setAnnealEnergyRNG(const RNG& rng) = 0;
|
2017-12-15 18:57:39 +08:00
|
|
|
};
|
|
|
|
|
2017-12-15 22:40:08 +08:00
|
|
|
|
2017-12-15 18:57:39 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Simulated annealing solver *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2017-12-22 20:33:23 +08:00
|
|
|
#ifdef CV_DOXYGEN
|
|
|
|
/** @brief This class declares example interface for system state used in simulated annealing optimization algorithm.
|
2017-12-15 22:40:08 +08:00
|
|
|
|
2017-12-22 20:33:23 +08:00
|
|
|
@note This class is not defined in C++ code and can't be use directly - you need your own implementation with the same methods.
|
2017-12-15 22:40:08 +08:00
|
|
|
*/
|
2017-12-22 20:33:23 +08:00
|
|
|
struct SimulatedAnnealingSolverSystem
|
2017-12-15 22:40:08 +08:00
|
|
|
{
|
|
|
|
/** Give energy value for a state of system.*/
|
2017-12-22 20:33:23 +08:00
|
|
|
double energy() const;
|
2018-02-12 23:15:36 +08:00
|
|
|
/** Function which change the state of system (random perturbation).*/
|
2017-12-22 20:33:23 +08:00
|
|
|
void changeState();
|
2017-12-15 22:40:08 +08:00
|
|
|
/** Function to reverse to the previous state. Can be called once only after changeState(). */
|
2017-12-22 20:33:23 +08:00
|
|
|
void reverseState();
|
2017-12-15 22:40:08 +08:00
|
|
|
};
|
2017-12-22 20:33:23 +08:00
|
|
|
#endif // CV_DOXYGEN
|
2017-12-15 22:40:08 +08:00
|
|
|
|
2017-12-15 18:57:39 +08:00
|
|
|
/** @brief The class implements simulated annealing for optimization.
|
2017-12-15 21:09:59 +08:00
|
|
|
|
2017-12-22 20:33:23 +08:00
|
|
|
@cite Kirkpatrick83 for details
|
2017-12-15 22:40:08 +08:00
|
|
|
|
2017-12-22 20:33:23 +08:00
|
|
|
@param solverSystem optimization system (see SimulatedAnnealingSolverSystem)
|
|
|
|
@param initialTemperature initial temperature
|
|
|
|
@param finalTemperature final temperature
|
|
|
|
@param coolingRatio temperature step multiplies
|
|
|
|
@param iterationsPerStep number of iterations per temperature changing step
|
|
|
|
@param lastTemperature optional output for last used temperature
|
|
|
|
@param rngEnergy specify custom random numbers generator (cv::theRNG() by default)
|
|
|
|
*/
|
|
|
|
template<class SimulatedAnnealingSolverSystem>
|
|
|
|
int simulatedAnnealingSolver(SimulatedAnnealingSolverSystem& solverSystem,
|
|
|
|
double initialTemperature, double finalTemperature, double coolingRatio,
|
|
|
|
size_t iterationsPerStep,
|
|
|
|
CV_OUT double* lastTemperature = NULL,
|
|
|
|
cv::RNG& rngEnergy = cv::theRNG()
|
|
|
|
);
|
2017-12-15 22:40:08 +08:00
|
|
|
|
2014-11-19 22:55:13 +08:00
|
|
|
//! @} ml
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2017-12-22 20:33:23 +08:00
|
|
|
#include <opencv2/ml/ml.inl.hpp>
|
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
#endif // __cplusplus
|
2016-09-30 17:46:29 +08:00
|
|
|
#endif // OPENCV_ML_HPP
|
2012-04-14 05:50:59 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* End of file. */
|