openCV library for Renesas RZ/A

Dependents:   RZ_A2M_Mbed_samples

Committer:
RyoheiHagimoto
Date:
Fri Jan 29 04:53:38 2021 +0000
Revision:
0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:0e0631af0305 1 /*M///////////////////////////////////////////////////////////////////////////////////////
RyoheiHagimoto 0:0e0631af0305 2 //
RyoheiHagimoto 0:0e0631af0305 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
RyoheiHagimoto 0:0e0631af0305 4 //
RyoheiHagimoto 0:0e0631af0305 5 // By downloading, copying, installing or using the software you agree to this license.
RyoheiHagimoto 0:0e0631af0305 6 // If you do not agree to this license, do not download, install,
RyoheiHagimoto 0:0e0631af0305 7 // copy or use the software.
RyoheiHagimoto 0:0e0631af0305 8 //
RyoheiHagimoto 0:0e0631af0305 9 //
RyoheiHagimoto 0:0e0631af0305 10 // License Agreement
RyoheiHagimoto 0:0e0631af0305 11 // For Open Source Computer Vision Library
RyoheiHagimoto 0:0e0631af0305 12 //
RyoheiHagimoto 0:0e0631af0305 13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
RyoheiHagimoto 0:0e0631af0305 14 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
RyoheiHagimoto 0:0e0631af0305 15 // Copyright (C) 2014, Itseez Inc, all rights reserved.
RyoheiHagimoto 0:0e0631af0305 16 // Third party copyrights are property of their respective owners.
RyoheiHagimoto 0:0e0631af0305 17 //
RyoheiHagimoto 0:0e0631af0305 18 // Redistribution and use in source and binary forms, with or without modification,
RyoheiHagimoto 0:0e0631af0305 19 // are permitted provided that the following conditions are met:
RyoheiHagimoto 0:0e0631af0305 20 //
RyoheiHagimoto 0:0e0631af0305 21 // * Redistribution's of source code must retain the above copyright notice,
RyoheiHagimoto 0:0e0631af0305 22 // this list of conditions and the following disclaimer.
RyoheiHagimoto 0:0e0631af0305 23 //
RyoheiHagimoto 0:0e0631af0305 24 // * Redistribution's in binary form must reproduce the above copyright notice,
RyoheiHagimoto 0:0e0631af0305 25 // this list of conditions and the following disclaimer in the documentation
RyoheiHagimoto 0:0e0631af0305 26 // and/or other materials provided with the distribution.
RyoheiHagimoto 0:0e0631af0305 27 //
RyoheiHagimoto 0:0e0631af0305 28 // * The name of the copyright holders may not be used to endorse or promote products
RyoheiHagimoto 0:0e0631af0305 29 // derived from this software without specific prior written permission.
RyoheiHagimoto 0:0e0631af0305 30 //
RyoheiHagimoto 0:0e0631af0305 31 // This software is provided by the copyright holders and contributors "as is" and
RyoheiHagimoto 0:0e0631af0305 32 // any express or implied warranties, including, but not limited to, the implied
RyoheiHagimoto 0:0e0631af0305 33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
RyoheiHagimoto 0:0e0631af0305 34 // In no event shall the Intel Corporation or contributors be liable for any direct,
RyoheiHagimoto 0:0e0631af0305 35 // indirect, incidental, special, exemplary, or consequential damages
RyoheiHagimoto 0:0e0631af0305 36 // (including, but not limited to, procurement of substitute goods or services;
RyoheiHagimoto 0:0e0631af0305 37 // loss of use, data, or profits; or business interruption) however caused
RyoheiHagimoto 0:0e0631af0305 38 // and on any theory of liability, whether in contract, strict liability,
RyoheiHagimoto 0:0e0631af0305 39 // or tort (including negligence or otherwise) arising in any way out of
RyoheiHagimoto 0:0e0631af0305 40 // the use of this software, even if advised of the possibility of such damage.
RyoheiHagimoto 0:0e0631af0305 41 //
RyoheiHagimoto 0:0e0631af0305 42 //M*/
RyoheiHagimoto 0:0e0631af0305 43
RyoheiHagimoto 0:0e0631af0305 44 #ifndef OPENCV_ML_HPP
RyoheiHagimoto 0:0e0631af0305 45 #define OPENCV_ML_HPP
RyoheiHagimoto 0:0e0631af0305 46
RyoheiHagimoto 0:0e0631af0305 47 #ifdef __cplusplus
RyoheiHagimoto 0:0e0631af0305 48 # include "opencv2/core.hpp"
RyoheiHagimoto 0:0e0631af0305 49 #endif
RyoheiHagimoto 0:0e0631af0305 50
RyoheiHagimoto 0:0e0631af0305 51 #ifdef __cplusplus
RyoheiHagimoto 0:0e0631af0305 52
RyoheiHagimoto 0:0e0631af0305 53 #include <float.h>
RyoheiHagimoto 0:0e0631af0305 54 #include <map>
RyoheiHagimoto 0:0e0631af0305 55 #include <iostream>
RyoheiHagimoto 0:0e0631af0305 56
RyoheiHagimoto 0:0e0631af0305 57 /**
RyoheiHagimoto 0:0e0631af0305 58 @defgroup ml Machine Learning
RyoheiHagimoto 0:0e0631af0305 59
RyoheiHagimoto 0:0e0631af0305 60 The Machine Learning Library (MLL) is a set of classes and functions for statistical
RyoheiHagimoto 0:0e0631af0305 61 classification, regression, and clustering of data.
RyoheiHagimoto 0:0e0631af0305 62
RyoheiHagimoto 0:0e0631af0305 63 Most of the classification and regression algorithms are implemented as C++ classes. As the
RyoheiHagimoto 0:0e0631af0305 64 algorithms have different sets of features (like an ability to handle missing measurements or
RyoheiHagimoto 0:0e0631af0305 65 categorical input variables), there is a little common ground between the classes. This common
RyoheiHagimoto 0:0e0631af0305 66 ground is defined by the class cv::ml::StatModel that all the other ML classes are derived from.
RyoheiHagimoto 0:0e0631af0305 67
RyoheiHagimoto 0:0e0631af0305 68 See detailed overview here: @ref ml_intro.
RyoheiHagimoto 0:0e0631af0305 69 */
RyoheiHagimoto 0:0e0631af0305 70
RyoheiHagimoto 0:0e0631af0305 71 namespace cv
RyoheiHagimoto 0:0e0631af0305 72 {
RyoheiHagimoto 0:0e0631af0305 73
RyoheiHagimoto 0:0e0631af0305 74 namespace ml
RyoheiHagimoto 0:0e0631af0305 75 {
RyoheiHagimoto 0:0e0631af0305 76
RyoheiHagimoto 0:0e0631af0305 77 //! @addtogroup ml
RyoheiHagimoto 0:0e0631af0305 78 //! @{
RyoheiHagimoto 0:0e0631af0305 79
RyoheiHagimoto 0:0e0631af0305 80 /** @brief Variable types */
RyoheiHagimoto 0:0e0631af0305 81 enum VariableTypes
RyoheiHagimoto 0:0e0631af0305 82 {
RyoheiHagimoto 0:0e0631af0305 83 VAR_NUMERICAL =0, //!< same as VAR_ORDERED
RyoheiHagimoto 0:0e0631af0305 84 VAR_ORDERED =0, //!< ordered variables
RyoheiHagimoto 0:0e0631af0305 85 VAR_CATEGORICAL =1 //!< categorical variables
RyoheiHagimoto 0:0e0631af0305 86 };
RyoheiHagimoto 0:0e0631af0305 87
RyoheiHagimoto 0:0e0631af0305 88 /** @brief %Error types */
RyoheiHagimoto 0:0e0631af0305 89 enum ErrorTypes
RyoheiHagimoto 0:0e0631af0305 90 {
RyoheiHagimoto 0:0e0631af0305 91 TEST_ERROR = 0,
RyoheiHagimoto 0:0e0631af0305 92 TRAIN_ERROR = 1
RyoheiHagimoto 0:0e0631af0305 93 };
RyoheiHagimoto 0:0e0631af0305 94
RyoheiHagimoto 0:0e0631af0305 95 /** @brief Sample types */
RyoheiHagimoto 0:0e0631af0305 96 enum SampleTypes
RyoheiHagimoto 0:0e0631af0305 97 {
RyoheiHagimoto 0:0e0631af0305 98 ROW_SAMPLE = 0, //!< each training sample is a row of samples
RyoheiHagimoto 0:0e0631af0305 99 COL_SAMPLE = 1 //!< each training sample occupies a column of samples
RyoheiHagimoto 0:0e0631af0305 100 };
RyoheiHagimoto 0:0e0631af0305 101
RyoheiHagimoto 0:0e0631af0305 102 /** @brief The structure represents the logarithmic grid range of statmodel parameters.
RyoheiHagimoto 0:0e0631af0305 103
RyoheiHagimoto 0:0e0631af0305 104 It is used for optimizing statmodel accuracy by varying model parameters, the accuracy estimate
RyoheiHagimoto 0:0e0631af0305 105 being computed by cross-validation.
RyoheiHagimoto 0:0e0631af0305 106 */
RyoheiHagimoto 0:0e0631af0305 107 class CV_EXPORTS ParamGrid
RyoheiHagimoto 0:0e0631af0305 108 {
RyoheiHagimoto 0:0e0631af0305 109 public:
RyoheiHagimoto 0:0e0631af0305 110 /** @brief Default constructor */
RyoheiHagimoto 0:0e0631af0305 111 ParamGrid();
RyoheiHagimoto 0:0e0631af0305 112 /** @brief Constructor with parameters */
RyoheiHagimoto 0:0e0631af0305 113 ParamGrid(double _minVal, double _maxVal, double _logStep);
RyoheiHagimoto 0:0e0631af0305 114
RyoheiHagimoto 0:0e0631af0305 115 double minVal; //!< Minimum value of the statmodel parameter. Default value is 0.
RyoheiHagimoto 0:0e0631af0305 116 double maxVal; //!< Maximum value of the statmodel parameter. Default value is 0.
RyoheiHagimoto 0:0e0631af0305 117 /** @brief Logarithmic step for iterating the statmodel parameter.
RyoheiHagimoto 0:0e0631af0305 118
RyoheiHagimoto 0:0e0631af0305 119 The grid determines the following iteration sequence of the statmodel parameter values:
RyoheiHagimoto 0:0e0631af0305 120 \f[(minVal, minVal*step, minVal*{step}^2, \dots, minVal*{logStep}^n),\f]
RyoheiHagimoto 0:0e0631af0305 121 where \f$n\f$ is the maximal index satisfying
RyoheiHagimoto 0:0e0631af0305 122 \f[\texttt{minVal} * \texttt{logStep} ^n < \texttt{maxVal}\f]
RyoheiHagimoto 0:0e0631af0305 123 The grid is logarithmic, so logStep must always be greater then 1. Default value is 1.
RyoheiHagimoto 0:0e0631af0305 124 */
RyoheiHagimoto 0:0e0631af0305 125 double logStep;
RyoheiHagimoto 0:0e0631af0305 126 };
RyoheiHagimoto 0:0e0631af0305 127
RyoheiHagimoto 0:0e0631af0305 128 /** @brief Class encapsulating training data.
RyoheiHagimoto 0:0e0631af0305 129
RyoheiHagimoto 0:0e0631af0305 130 Please note that the class only specifies the interface of training data, but not implementation.
RyoheiHagimoto 0:0e0631af0305 131 All the statistical model classes in _ml_ module accepts Ptr\<TrainData\> as parameter. In other
RyoheiHagimoto 0:0e0631af0305 132 words, you can create your own class derived from TrainData and pass smart pointer to the instance
RyoheiHagimoto 0:0e0631af0305 133 of this class into StatModel::train.
RyoheiHagimoto 0:0e0631af0305 134
RyoheiHagimoto 0:0e0631af0305 135 @sa @ref ml_intro_data
RyoheiHagimoto 0:0e0631af0305 136 */
RyoheiHagimoto 0:0e0631af0305 137 class CV_EXPORTS_W TrainData
RyoheiHagimoto 0:0e0631af0305 138 {
RyoheiHagimoto 0:0e0631af0305 139 public:
RyoheiHagimoto 0:0e0631af0305 140 static inline float missingValue() { return FLT_MAX; }
RyoheiHagimoto 0:0e0631af0305 141 virtual ~TrainData();
RyoheiHagimoto 0:0e0631af0305 142
RyoheiHagimoto 0:0e0631af0305 143 CV_WRAP virtual int getLayout() const = 0;
RyoheiHagimoto 0:0e0631af0305 144 CV_WRAP virtual int getNTrainSamples() const = 0;
RyoheiHagimoto 0:0e0631af0305 145 CV_WRAP virtual int getNTestSamples() const = 0;
RyoheiHagimoto 0:0e0631af0305 146 CV_WRAP virtual int getNSamples() const = 0;
RyoheiHagimoto 0:0e0631af0305 147 CV_WRAP virtual int getNVars() const = 0;
RyoheiHagimoto 0:0e0631af0305 148 CV_WRAP virtual int getNAllVars() const = 0;
RyoheiHagimoto 0:0e0631af0305 149
RyoheiHagimoto 0:0e0631af0305 150 CV_WRAP virtual void getSample(InputArray varIdx, int sidx, float* buf) const = 0;
RyoheiHagimoto 0:0e0631af0305 151 CV_WRAP virtual Mat getSamples() const = 0;
RyoheiHagimoto 0:0e0631af0305 152 CV_WRAP virtual Mat getMissing() const = 0;
RyoheiHagimoto 0:0e0631af0305 153
RyoheiHagimoto 0:0e0631af0305 154 /** @brief Returns matrix of train samples
RyoheiHagimoto 0:0e0631af0305 155
RyoheiHagimoto 0:0e0631af0305 156 @param layout The requested layout. If it's different from the initial one, the matrix is
RyoheiHagimoto 0:0e0631af0305 157 transposed. See ml::SampleTypes.
RyoheiHagimoto 0:0e0631af0305 158 @param compressSamples if true, the function returns only the training samples (specified by
RyoheiHagimoto 0:0e0631af0305 159 sampleIdx)
RyoheiHagimoto 0:0e0631af0305 160 @param compressVars if true, the function returns the shorter training samples, containing only
RyoheiHagimoto 0:0e0631af0305 161 the active variables.
RyoheiHagimoto 0:0e0631af0305 162
RyoheiHagimoto 0:0e0631af0305 163 In current implementation the function tries to avoid physical data copying and returns the
RyoheiHagimoto 0:0e0631af0305 164 matrix stored inside TrainData (unless the transposition or compression is needed).
RyoheiHagimoto 0:0e0631af0305 165 */
RyoheiHagimoto 0:0e0631af0305 166 CV_WRAP virtual Mat getTrainSamples(int layout=ROW_SAMPLE,
RyoheiHagimoto 0:0e0631af0305 167 bool compressSamples=true,
RyoheiHagimoto 0:0e0631af0305 168 bool compressVars=true) const = 0;
RyoheiHagimoto 0:0e0631af0305 169
RyoheiHagimoto 0:0e0631af0305 170 /** @brief Returns the vector of responses
RyoheiHagimoto 0:0e0631af0305 171
RyoheiHagimoto 0:0e0631af0305 172 The function returns ordered or the original categorical responses. Usually it's used in
RyoheiHagimoto 0:0e0631af0305 173 regression algorithms.
RyoheiHagimoto 0:0e0631af0305 174 */
RyoheiHagimoto 0:0e0631af0305 175 CV_WRAP virtual Mat getTrainResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 176
RyoheiHagimoto 0:0e0631af0305 177 /** @brief Returns the vector of normalized categorical responses
RyoheiHagimoto 0:0e0631af0305 178
RyoheiHagimoto 0:0e0631af0305 179 The function returns vector of responses. Each response is integer from `0` to `<number of
RyoheiHagimoto 0:0e0631af0305 180 classes>-1`. The actual label value can be retrieved then from the class label vector, see
RyoheiHagimoto 0:0e0631af0305 181 TrainData::getClassLabels.
RyoheiHagimoto 0:0e0631af0305 182 */
RyoheiHagimoto 0:0e0631af0305 183 CV_WRAP virtual Mat getTrainNormCatResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 184 CV_WRAP virtual Mat getTestResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 185 CV_WRAP virtual Mat getTestNormCatResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 186 CV_WRAP virtual Mat getResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 187 CV_WRAP virtual Mat getNormCatResponses() const = 0;
RyoheiHagimoto 0:0e0631af0305 188 CV_WRAP virtual Mat getSampleWeights() const = 0;
RyoheiHagimoto 0:0e0631af0305 189 CV_WRAP virtual Mat getTrainSampleWeights() const = 0;
RyoheiHagimoto 0:0e0631af0305 190 CV_WRAP virtual Mat getTestSampleWeights() const = 0;
RyoheiHagimoto 0:0e0631af0305 191 CV_WRAP virtual Mat getVarIdx() const = 0;
RyoheiHagimoto 0:0e0631af0305 192 CV_WRAP virtual Mat getVarType() const = 0;
RyoheiHagimoto 0:0e0631af0305 193 CV_WRAP Mat getVarSymbolFlags() const;
RyoheiHagimoto 0:0e0631af0305 194 CV_WRAP virtual int getResponseType() const = 0;
RyoheiHagimoto 0:0e0631af0305 195 CV_WRAP virtual Mat getTrainSampleIdx() const = 0;
RyoheiHagimoto 0:0e0631af0305 196 CV_WRAP virtual Mat getTestSampleIdx() const = 0;
RyoheiHagimoto 0:0e0631af0305 197 CV_WRAP virtual void getValues(int vi, InputArray sidx, float* values) const = 0;
RyoheiHagimoto 0:0e0631af0305 198 virtual void getNormCatValues(int vi, InputArray sidx, int* values) const = 0;
RyoheiHagimoto 0:0e0631af0305 199 CV_WRAP virtual Mat getDefaultSubstValues() const = 0;
RyoheiHagimoto 0:0e0631af0305 200
RyoheiHagimoto 0:0e0631af0305 201 CV_WRAP virtual int getCatCount(int vi) const = 0;
RyoheiHagimoto 0:0e0631af0305 202
RyoheiHagimoto 0:0e0631af0305 203 /** @brief Returns the vector of class labels
RyoheiHagimoto 0:0e0631af0305 204
RyoheiHagimoto 0:0e0631af0305 205 The function returns vector of unique labels occurred in the responses.
RyoheiHagimoto 0:0e0631af0305 206 */
RyoheiHagimoto 0:0e0631af0305 207 CV_WRAP virtual Mat getClassLabels() const = 0;
RyoheiHagimoto 0:0e0631af0305 208
RyoheiHagimoto 0:0e0631af0305 209 CV_WRAP virtual Mat getCatOfs() const = 0;
RyoheiHagimoto 0:0e0631af0305 210 CV_WRAP virtual Mat getCatMap() const = 0;
RyoheiHagimoto 0:0e0631af0305 211
RyoheiHagimoto 0:0e0631af0305 212 /** @brief Splits the training data into the training and test parts
RyoheiHagimoto 0:0e0631af0305 213 @sa TrainData::setTrainTestSplitRatio
RyoheiHagimoto 0:0e0631af0305 214 */
RyoheiHagimoto 0:0e0631af0305 215 CV_WRAP virtual void setTrainTestSplit(int count, bool shuffle=true) = 0;
RyoheiHagimoto 0:0e0631af0305 216
RyoheiHagimoto 0:0e0631af0305 217 /** @brief Splits the training data into the training and test parts
RyoheiHagimoto 0:0e0631af0305 218
RyoheiHagimoto 0:0e0631af0305 219 The function selects a subset of specified relative size and then returns it as the training
RyoheiHagimoto 0:0e0631af0305 220 set. If the function is not called, all the data is used for training. Please, note that for
RyoheiHagimoto 0:0e0631af0305 221 each of TrainData::getTrain\* there is corresponding TrainData::getTest\*, so that the test
RyoheiHagimoto 0:0e0631af0305 222 subset can be retrieved and processed as well.
RyoheiHagimoto 0:0e0631af0305 223 @sa TrainData::setTrainTestSplit
RyoheiHagimoto 0:0e0631af0305 224 */
RyoheiHagimoto 0:0e0631af0305 225 CV_WRAP virtual void setTrainTestSplitRatio(double ratio, bool shuffle=true) = 0;
RyoheiHagimoto 0:0e0631af0305 226 CV_WRAP virtual void shuffleTrainTest() = 0;
RyoheiHagimoto 0:0e0631af0305 227
RyoheiHagimoto 0:0e0631af0305 228 /** @brief Returns matrix of test samples */
RyoheiHagimoto 0:0e0631af0305 229 CV_WRAP Mat getTestSamples() const;
RyoheiHagimoto 0:0e0631af0305 230
RyoheiHagimoto 0:0e0631af0305 231 /** @brief Returns vector of symbolic names captured in loadFromCSV() */
RyoheiHagimoto 0:0e0631af0305 232 CV_WRAP void getNames(std::vector<String>& names) const;
RyoheiHagimoto 0:0e0631af0305 233
RyoheiHagimoto 0:0e0631af0305 234 CV_WRAP static Mat getSubVector(const Mat& vec, const Mat& idx);
RyoheiHagimoto 0:0e0631af0305 235
RyoheiHagimoto 0:0e0631af0305 236 /** @brief Reads the dataset from a .csv file and returns the ready-to-use training data.
RyoheiHagimoto 0:0e0631af0305 237
RyoheiHagimoto 0:0e0631af0305 238 @param filename The input file name
RyoheiHagimoto 0:0e0631af0305 239 @param headerLineCount The number of lines in the beginning to skip; besides the header, the
RyoheiHagimoto 0:0e0631af0305 240 function also skips empty lines and lines staring with `#`
RyoheiHagimoto 0:0e0631af0305 241 @param responseStartIdx Index of the first output variable. If -1, the function considers the
RyoheiHagimoto 0:0e0631af0305 242 last variable as the response
RyoheiHagimoto 0:0e0631af0305 243 @param responseEndIdx Index of the last output variable + 1. If -1, then there is single
RyoheiHagimoto 0:0e0631af0305 244 response variable at responseStartIdx.
RyoheiHagimoto 0:0e0631af0305 245 @param varTypeSpec The optional text string that specifies the variables' types. It has the
RyoheiHagimoto 0:0e0631af0305 246 format `ord[n1-n2,n3,n4-n5,...]cat[n6,n7-n8,...]`. That is, variables from `n1 to n2`
RyoheiHagimoto 0:0e0631af0305 247 (inclusive range), `n3`, `n4 to n5` ... are considered ordered and `n6`, `n7 to n8` ... are
RyoheiHagimoto 0:0e0631af0305 248 considered as categorical. The range `[n1..n2] + [n3] + [n4..n5] + ... + [n6] + [n7..n8]`
RyoheiHagimoto 0:0e0631af0305 249 should cover all the variables. If varTypeSpec is not specified, then algorithm uses the
RyoheiHagimoto 0:0e0631af0305 250 following rules:
RyoheiHagimoto 0:0e0631af0305 251 - all input variables are considered ordered by default. If some column contains has non-
RyoheiHagimoto 0:0e0631af0305 252 numerical values, e.g. 'apple', 'pear', 'apple', 'apple', 'mango', the corresponding
RyoheiHagimoto 0:0e0631af0305 253 variable is considered categorical.
RyoheiHagimoto 0:0e0631af0305 254 - if there are several output variables, they are all considered as ordered. Error is
RyoheiHagimoto 0:0e0631af0305 255 reported when non-numerical values are used.
RyoheiHagimoto 0:0e0631af0305 256 - if there is a single output variable, then if its values are non-numerical or are all
RyoheiHagimoto 0:0e0631af0305 257 integers, then it's considered categorical. Otherwise, it's considered ordered.
RyoheiHagimoto 0:0e0631af0305 258 @param delimiter The character used to separate values in each line.
RyoheiHagimoto 0:0e0631af0305 259 @param missch The character used to specify missing measurements. It should not be a digit.
RyoheiHagimoto 0:0e0631af0305 260 Although it's a non-numerical value, it surely does not affect the decision of whether the
RyoheiHagimoto 0:0e0631af0305 261 variable ordered or categorical.
RyoheiHagimoto 0:0e0631af0305 262 @note If the dataset only contains input variables and no responses, use responseStartIdx = -2
RyoheiHagimoto 0:0e0631af0305 263 and responseEndIdx = 0. The output variables vector will just contain zeros.
RyoheiHagimoto 0:0e0631af0305 264 */
RyoheiHagimoto 0:0e0631af0305 265 static Ptr<TrainData> loadFromCSV(const String& filename,
RyoheiHagimoto 0:0e0631af0305 266 int headerLineCount,
RyoheiHagimoto 0:0e0631af0305 267 int responseStartIdx=-1,
RyoheiHagimoto 0:0e0631af0305 268 int responseEndIdx=-1,
RyoheiHagimoto 0:0e0631af0305 269 const String& varTypeSpec=String(),
RyoheiHagimoto 0:0e0631af0305 270 char delimiter=',',
RyoheiHagimoto 0:0e0631af0305 271 char missch='?');
RyoheiHagimoto 0:0e0631af0305 272
RyoheiHagimoto 0:0e0631af0305 273 /** @brief Creates training data from in-memory arrays.
RyoheiHagimoto 0:0e0631af0305 274
RyoheiHagimoto 0:0e0631af0305 275 @param samples matrix of samples. It should have CV_32F type.
RyoheiHagimoto 0:0e0631af0305 276 @param layout see ml::SampleTypes.
RyoheiHagimoto 0:0e0631af0305 277 @param responses matrix of responses. If the responses are scalar, they should be stored as a
RyoheiHagimoto 0:0e0631af0305 278 single row or as a single column. The matrix should have type CV_32F or CV_32S (in the
RyoheiHagimoto 0:0e0631af0305 279 former case the responses are considered as ordered by default; in the latter case - as
RyoheiHagimoto 0:0e0631af0305 280 categorical)
RyoheiHagimoto 0:0e0631af0305 281 @param varIdx vector specifying which variables to use for training. It can be an integer vector
RyoheiHagimoto 0:0e0631af0305 282 (CV_32S) containing 0-based variable indices or byte vector (CV_8U) containing a mask of
RyoheiHagimoto 0:0e0631af0305 283 active variables.
RyoheiHagimoto 0:0e0631af0305 284 @param sampleIdx vector specifying which samples to use for training. It can be an integer
RyoheiHagimoto 0:0e0631af0305 285 vector (CV_32S) containing 0-based sample indices or byte vector (CV_8U) containing a mask
RyoheiHagimoto 0:0e0631af0305 286 of training samples.
RyoheiHagimoto 0:0e0631af0305 287 @param sampleWeights optional vector with weights for each sample. It should have CV_32F type.
RyoheiHagimoto 0:0e0631af0305 288 @param varType optional vector of type CV_8U and size `<number_of_variables_in_samples> +
RyoheiHagimoto 0:0e0631af0305 289 <number_of_variables_in_responses>`, containing types of each input and output variable. See
RyoheiHagimoto 0:0e0631af0305 290 ml::VariableTypes.
RyoheiHagimoto 0:0e0631af0305 291 */
RyoheiHagimoto 0:0e0631af0305 292 CV_WRAP static Ptr<TrainData> create(InputArray samples, int layout, InputArray responses,
RyoheiHagimoto 0:0e0631af0305 293 InputArray varIdx=noArray(), InputArray sampleIdx=noArray(),
RyoheiHagimoto 0:0e0631af0305 294 InputArray sampleWeights=noArray(), InputArray varType=noArray());
RyoheiHagimoto 0:0e0631af0305 295 };
RyoheiHagimoto 0:0e0631af0305 296
RyoheiHagimoto 0:0e0631af0305 297 /** @brief Base class for statistical models in OpenCV ML.
RyoheiHagimoto 0:0e0631af0305 298 */
RyoheiHagimoto 0:0e0631af0305 299 class CV_EXPORTS_W StatModel : public Algorithm
RyoheiHagimoto 0:0e0631af0305 300 {
RyoheiHagimoto 0:0e0631af0305 301 public:
RyoheiHagimoto 0:0e0631af0305 302 /** Predict options */
RyoheiHagimoto 0:0e0631af0305 303 enum Flags {
RyoheiHagimoto 0:0e0631af0305 304 UPDATE_MODEL = 1,
RyoheiHagimoto 0:0e0631af0305 305 RAW_OUTPUT=1, //!< makes the method return the raw results (the sum), not the class label
RyoheiHagimoto 0:0e0631af0305 306 COMPRESSED_INPUT=2,
RyoheiHagimoto 0:0e0631af0305 307 PREPROCESSED_INPUT=4
RyoheiHagimoto 0:0e0631af0305 308 };
RyoheiHagimoto 0:0e0631af0305 309
RyoheiHagimoto 0:0e0631af0305 310 /** @brief Returns the number of variables in training samples */
RyoheiHagimoto 0:0e0631af0305 311 CV_WRAP virtual int getVarCount() const = 0;
RyoheiHagimoto 0:0e0631af0305 312
RyoheiHagimoto 0:0e0631af0305 313 CV_WRAP virtual bool empty() const;
RyoheiHagimoto 0:0e0631af0305 314
RyoheiHagimoto 0:0e0631af0305 315 /** @brief Returns true if the model is trained */
RyoheiHagimoto 0:0e0631af0305 316 CV_WRAP virtual bool isTrained() const = 0;
RyoheiHagimoto 0:0e0631af0305 317 /** @brief Returns true if the model is classifier */
RyoheiHagimoto 0:0e0631af0305 318 CV_WRAP virtual bool isClassifier() const = 0;
RyoheiHagimoto 0:0e0631af0305 319
RyoheiHagimoto 0:0e0631af0305 320 /** @brief Trains the statistical model
RyoheiHagimoto 0:0e0631af0305 321
RyoheiHagimoto 0:0e0631af0305 322 @param trainData training data that can be loaded from file using TrainData::loadFromCSV or
RyoheiHagimoto 0:0e0631af0305 323 created with TrainData::create.
RyoheiHagimoto 0:0e0631af0305 324 @param flags optional flags, depending on the model. Some of the models can be updated with the
RyoheiHagimoto 0:0e0631af0305 325 new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
RyoheiHagimoto 0:0e0631af0305 326 */
RyoheiHagimoto 0:0e0631af0305 327 CV_WRAP virtual bool train( const Ptr<TrainData>& trainData, int flags=0 );
RyoheiHagimoto 0:0e0631af0305 328
RyoheiHagimoto 0:0e0631af0305 329 /** @brief Trains the statistical model
RyoheiHagimoto 0:0e0631af0305 330
RyoheiHagimoto 0:0e0631af0305 331 @param samples training samples
RyoheiHagimoto 0:0e0631af0305 332 @param layout See ml::SampleTypes.
RyoheiHagimoto 0:0e0631af0305 333 @param responses vector of responses associated with the training samples.
RyoheiHagimoto 0:0e0631af0305 334 */
RyoheiHagimoto 0:0e0631af0305 335 CV_WRAP virtual bool train( InputArray samples, int layout, InputArray responses );
RyoheiHagimoto 0:0e0631af0305 336
RyoheiHagimoto 0:0e0631af0305 337 /** @brief Computes error on the training or test dataset
RyoheiHagimoto 0:0e0631af0305 338
RyoheiHagimoto 0:0e0631af0305 339 @param data the training data
RyoheiHagimoto 0:0e0631af0305 340 @param test if true, the error is computed over the test subset of the data, otherwise it's
RyoheiHagimoto 0:0e0631af0305 341 computed over the training subset of the data. Please note that if you loaded a completely
RyoheiHagimoto 0:0e0631af0305 342 different dataset to evaluate already trained classifier, you will probably want not to set
RyoheiHagimoto 0:0e0631af0305 343 the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so
RyoheiHagimoto 0:0e0631af0305 344 that the error is computed for the whole new set. Yes, this sounds a bit confusing.
RyoheiHagimoto 0:0e0631af0305 345 @param resp the optional output responses.
RyoheiHagimoto 0:0e0631af0305 346
RyoheiHagimoto 0:0e0631af0305 347 The method uses StatModel::predict to compute the error. For regression models the error is
RyoheiHagimoto 0:0e0631af0305 348 computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).
RyoheiHagimoto 0:0e0631af0305 349 */
RyoheiHagimoto 0:0e0631af0305 350 CV_WRAP virtual float calcError( const Ptr<TrainData>& data, bool test, OutputArray resp ) const;
RyoheiHagimoto 0:0e0631af0305 351
RyoheiHagimoto 0:0e0631af0305 352 /** @brief Predicts response(s) for the provided sample(s)
RyoheiHagimoto 0:0e0631af0305 353
RyoheiHagimoto 0:0e0631af0305 354 @param samples The input samples, floating-point matrix
RyoheiHagimoto 0:0e0631af0305 355 @param results The optional output matrix of results.
RyoheiHagimoto 0:0e0631af0305 356 @param flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
RyoheiHagimoto 0:0e0631af0305 357 */
RyoheiHagimoto 0:0e0631af0305 358 CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
RyoheiHagimoto 0:0e0631af0305 359
RyoheiHagimoto 0:0e0631af0305 360 /** @brief Create and train model with default parameters
RyoheiHagimoto 0:0e0631af0305 361
RyoheiHagimoto 0:0e0631af0305 362 The class must implement static `create()` method with no parameters or with all default parameter values
RyoheiHagimoto 0:0e0631af0305 363 */
RyoheiHagimoto 0:0e0631af0305 364 template<typename _Tp> static Ptr<_Tp> train(const Ptr<TrainData>& data, int flags=0)
RyoheiHagimoto 0:0e0631af0305 365 {
RyoheiHagimoto 0:0e0631af0305 366 Ptr<_Tp> model = _Tp::create();
RyoheiHagimoto 0:0e0631af0305 367 return !model.empty() && model->train(data, flags) ? model : Ptr<_Tp>();
RyoheiHagimoto 0:0e0631af0305 368 }
RyoheiHagimoto 0:0e0631af0305 369 };
RyoheiHagimoto 0:0e0631af0305 370
RyoheiHagimoto 0:0e0631af0305 371 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 372 * Normal Bayes Classifier *
RyoheiHagimoto 0:0e0631af0305 373 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 374
RyoheiHagimoto 0:0e0631af0305 375 /** @brief Bayes classifier for normally distributed data.
RyoheiHagimoto 0:0e0631af0305 376
RyoheiHagimoto 0:0e0631af0305 377 @sa @ref ml_intro_bayes
RyoheiHagimoto 0:0e0631af0305 378 */
RyoheiHagimoto 0:0e0631af0305 379 class CV_EXPORTS_W NormalBayesClassifier : public StatModel
RyoheiHagimoto 0:0e0631af0305 380 {
RyoheiHagimoto 0:0e0631af0305 381 public:
RyoheiHagimoto 0:0e0631af0305 382 /** @brief Predicts the response for sample(s).
RyoheiHagimoto 0:0e0631af0305 383
RyoheiHagimoto 0:0e0631af0305 384 The method estimates the most probable classes for input vectors. Input vectors (one or more)
RyoheiHagimoto 0:0e0631af0305 385 are stored as rows of the matrix inputs. In case of multiple input vectors, there should be one
RyoheiHagimoto 0:0e0631af0305 386 output vector outputs. The predicted class for a single input vector is returned by the method.
RyoheiHagimoto 0:0e0631af0305 387 The vector outputProbs contains the output probabilities corresponding to each element of
RyoheiHagimoto 0:0e0631af0305 388 result.
RyoheiHagimoto 0:0e0631af0305 389 */
RyoheiHagimoto 0:0e0631af0305 390 CV_WRAP virtual float predictProb( InputArray inputs, OutputArray outputs,
RyoheiHagimoto 0:0e0631af0305 391 OutputArray outputProbs, int flags=0 ) const = 0;
RyoheiHagimoto 0:0e0631af0305 392
RyoheiHagimoto 0:0e0631af0305 393 /** Creates empty model
RyoheiHagimoto 0:0e0631af0305 394 Use StatModel::train to train the model after creation. */
RyoheiHagimoto 0:0e0631af0305 395 CV_WRAP static Ptr<NormalBayesClassifier> create();
RyoheiHagimoto 0:0e0631af0305 396 };
RyoheiHagimoto 0:0e0631af0305 397
RyoheiHagimoto 0:0e0631af0305 398 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 399 * K-Nearest Neighbour Classifier *
RyoheiHagimoto 0:0e0631af0305 400 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 401
RyoheiHagimoto 0:0e0631af0305 402 /** @brief The class implements K-Nearest Neighbors model
RyoheiHagimoto 0:0e0631af0305 403
RyoheiHagimoto 0:0e0631af0305 404 @sa @ref ml_intro_knn
RyoheiHagimoto 0:0e0631af0305 405 */
RyoheiHagimoto 0:0e0631af0305 406 class CV_EXPORTS_W KNearest : public StatModel
RyoheiHagimoto 0:0e0631af0305 407 {
RyoheiHagimoto 0:0e0631af0305 408 public:
RyoheiHagimoto 0:0e0631af0305 409
RyoheiHagimoto 0:0e0631af0305 410 /** Default number of neighbors to use in predict method. */
RyoheiHagimoto 0:0e0631af0305 411 /** @see setDefaultK */
RyoheiHagimoto 0:0e0631af0305 412 CV_WRAP virtual int getDefaultK() const = 0;
RyoheiHagimoto 0:0e0631af0305 413 /** @copybrief getDefaultK @see getDefaultK */
RyoheiHagimoto 0:0e0631af0305 414 CV_WRAP virtual void setDefaultK(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 415
RyoheiHagimoto 0:0e0631af0305 416 /** Whether classification or regression model should be trained. */
RyoheiHagimoto 0:0e0631af0305 417 /** @see setIsClassifier */
RyoheiHagimoto 0:0e0631af0305 418 CV_WRAP virtual bool getIsClassifier() const = 0;
RyoheiHagimoto 0:0e0631af0305 419 /** @copybrief getIsClassifier @see getIsClassifier */
RyoheiHagimoto 0:0e0631af0305 420 CV_WRAP virtual void setIsClassifier(bool val) = 0;
RyoheiHagimoto 0:0e0631af0305 421
RyoheiHagimoto 0:0e0631af0305 422 /** Parameter for KDTree implementation. */
RyoheiHagimoto 0:0e0631af0305 423 /** @see setEmax */
RyoheiHagimoto 0:0e0631af0305 424 CV_WRAP virtual int getEmax() const = 0;
RyoheiHagimoto 0:0e0631af0305 425 /** @copybrief getEmax @see getEmax */
RyoheiHagimoto 0:0e0631af0305 426 CV_WRAP virtual void setEmax(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 427
RyoheiHagimoto 0:0e0631af0305 428 /** %Algorithm type, one of KNearest::Types. */
RyoheiHagimoto 0:0e0631af0305 429 /** @see setAlgorithmType */
RyoheiHagimoto 0:0e0631af0305 430 CV_WRAP virtual int getAlgorithmType() const = 0;
RyoheiHagimoto 0:0e0631af0305 431 /** @copybrief getAlgorithmType @see getAlgorithmType */
RyoheiHagimoto 0:0e0631af0305 432 CV_WRAP virtual void setAlgorithmType(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 433
RyoheiHagimoto 0:0e0631af0305 434 /** @brief Finds the neighbors and predicts responses for input vectors.
RyoheiHagimoto 0:0e0631af0305 435
RyoheiHagimoto 0:0e0631af0305 436 @param samples Input samples stored by rows. It is a single-precision floating-point matrix of
RyoheiHagimoto 0:0e0631af0305 437 `<number_of_samples> * k` size.
RyoheiHagimoto 0:0e0631af0305 438 @param k Number of used nearest neighbors. Should be greater than 1.
RyoheiHagimoto 0:0e0631af0305 439 @param results Vector with results of prediction (regression or classification) for each input
RyoheiHagimoto 0:0e0631af0305 440 sample. It is a single-precision floating-point vector with `<number_of_samples>` elements.
RyoheiHagimoto 0:0e0631af0305 441 @param neighborResponses Optional output values for corresponding neighbors. It is a single-
RyoheiHagimoto 0:0e0631af0305 442 precision floating-point matrix of `<number_of_samples> * k` size.
RyoheiHagimoto 0:0e0631af0305 443 @param dist Optional output distances from the input vectors to the corresponding neighbors. It
RyoheiHagimoto 0:0e0631af0305 444 is a single-precision floating-point matrix of `<number_of_samples> * k` size.
RyoheiHagimoto 0:0e0631af0305 445
RyoheiHagimoto 0:0e0631af0305 446 For each input vector (a row of the matrix samples), the method finds the k nearest neighbors.
RyoheiHagimoto 0:0e0631af0305 447 In case of regression, the predicted result is a mean value of the particular vector's neighbor
RyoheiHagimoto 0:0e0631af0305 448 responses. In case of classification, the class is determined by voting.
RyoheiHagimoto 0:0e0631af0305 449
RyoheiHagimoto 0:0e0631af0305 450 For each input vector, the neighbors are sorted by their distances to the vector.
RyoheiHagimoto 0:0e0631af0305 451
RyoheiHagimoto 0:0e0631af0305 452 In case of C++ interface you can use output pointers to empty matrices and the function will
RyoheiHagimoto 0:0e0631af0305 453 allocate memory itself.
RyoheiHagimoto 0:0e0631af0305 454
RyoheiHagimoto 0:0e0631af0305 455 If only a single input vector is passed, all output matrices are optional and the predicted
RyoheiHagimoto 0:0e0631af0305 456 value is returned by the method.
RyoheiHagimoto 0:0e0631af0305 457
RyoheiHagimoto 0:0e0631af0305 458 The function is parallelized with the TBB library.
RyoheiHagimoto 0:0e0631af0305 459 */
RyoheiHagimoto 0:0e0631af0305 460 CV_WRAP virtual float findNearest( InputArray samples, int k,
RyoheiHagimoto 0:0e0631af0305 461 OutputArray results,
RyoheiHagimoto 0:0e0631af0305 462 OutputArray neighborResponses=noArray(),
RyoheiHagimoto 0:0e0631af0305 463 OutputArray dist=noArray() ) const = 0;
RyoheiHagimoto 0:0e0631af0305 464
RyoheiHagimoto 0:0e0631af0305 465 /** @brief Implementations of KNearest algorithm
RyoheiHagimoto 0:0e0631af0305 466 */
RyoheiHagimoto 0:0e0631af0305 467 enum Types
RyoheiHagimoto 0:0e0631af0305 468 {
RyoheiHagimoto 0:0e0631af0305 469 BRUTE_FORCE=1,
RyoheiHagimoto 0:0e0631af0305 470 KDTREE=2
RyoheiHagimoto 0:0e0631af0305 471 };
RyoheiHagimoto 0:0e0631af0305 472
RyoheiHagimoto 0:0e0631af0305 473 /** @brief Creates the empty model
RyoheiHagimoto 0:0e0631af0305 474
RyoheiHagimoto 0:0e0631af0305 475 The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
RyoheiHagimoto 0:0e0631af0305 476 */
RyoheiHagimoto 0:0e0631af0305 477 CV_WRAP static Ptr<KNearest> create();
RyoheiHagimoto 0:0e0631af0305 478 };
RyoheiHagimoto 0:0e0631af0305 479
RyoheiHagimoto 0:0e0631af0305 480 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 481 * Support Vector Machines *
RyoheiHagimoto 0:0e0631af0305 482 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 483
RyoheiHagimoto 0:0e0631af0305 484 /** @brief Support Vector Machines.
RyoheiHagimoto 0:0e0631af0305 485
RyoheiHagimoto 0:0e0631af0305 486 @sa @ref ml_intro_svm
RyoheiHagimoto 0:0e0631af0305 487 */
RyoheiHagimoto 0:0e0631af0305 488 class CV_EXPORTS_W SVM : public StatModel
RyoheiHagimoto 0:0e0631af0305 489 {
RyoheiHagimoto 0:0e0631af0305 490 public:
RyoheiHagimoto 0:0e0631af0305 491
RyoheiHagimoto 0:0e0631af0305 492 class CV_EXPORTS Kernel : public Algorithm
RyoheiHagimoto 0:0e0631af0305 493 {
RyoheiHagimoto 0:0e0631af0305 494 public:
RyoheiHagimoto 0:0e0631af0305 495 virtual int getType() const = 0;
RyoheiHagimoto 0:0e0631af0305 496 virtual void calc( int vcount, int n, const float* vecs, const float* another, float* results ) = 0;
RyoheiHagimoto 0:0e0631af0305 497 };
RyoheiHagimoto 0:0e0631af0305 498
RyoheiHagimoto 0:0e0631af0305 499 /** Type of a %SVM formulation.
RyoheiHagimoto 0:0e0631af0305 500 See SVM::Types. Default value is SVM::C_SVC. */
RyoheiHagimoto 0:0e0631af0305 501 /** @see setType */
RyoheiHagimoto 0:0e0631af0305 502 CV_WRAP virtual int getType() const = 0;
RyoheiHagimoto 0:0e0631af0305 503 /** @copybrief getType @see getType */
RyoheiHagimoto 0:0e0631af0305 504 CV_WRAP virtual void setType(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 505
RyoheiHagimoto 0:0e0631af0305 506 /** Parameter \f$\gamma\f$ of a kernel function.
RyoheiHagimoto 0:0e0631af0305 507 For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1. */
RyoheiHagimoto 0:0e0631af0305 508 /** @see setGamma */
RyoheiHagimoto 0:0e0631af0305 509 CV_WRAP virtual double getGamma() const = 0;
RyoheiHagimoto 0:0e0631af0305 510 /** @copybrief getGamma @see getGamma */
RyoheiHagimoto 0:0e0631af0305 511 CV_WRAP virtual void setGamma(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 512
RyoheiHagimoto 0:0e0631af0305 513 /** Parameter _coef0_ of a kernel function.
RyoheiHagimoto 0:0e0631af0305 514 For SVM::POLY or SVM::SIGMOID. Default value is 0.*/
RyoheiHagimoto 0:0e0631af0305 515 /** @see setCoef0 */
RyoheiHagimoto 0:0e0631af0305 516 CV_WRAP virtual double getCoef0() const = 0;
RyoheiHagimoto 0:0e0631af0305 517 /** @copybrief getCoef0 @see getCoef0 */
RyoheiHagimoto 0:0e0631af0305 518 CV_WRAP virtual void setCoef0(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 519
RyoheiHagimoto 0:0e0631af0305 520 /** Parameter _degree_ of a kernel function.
RyoheiHagimoto 0:0e0631af0305 521 For SVM::POLY. Default value is 0. */
RyoheiHagimoto 0:0e0631af0305 522 /** @see setDegree */
RyoheiHagimoto 0:0e0631af0305 523 CV_WRAP virtual double getDegree() const = 0;
RyoheiHagimoto 0:0e0631af0305 524 /** @copybrief getDegree @see getDegree */
RyoheiHagimoto 0:0e0631af0305 525 CV_WRAP virtual void setDegree(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 526
RyoheiHagimoto 0:0e0631af0305 527 /** Parameter _C_ of a %SVM optimization problem.
RyoheiHagimoto 0:0e0631af0305 528 For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0. */
RyoheiHagimoto 0:0e0631af0305 529 /** @see setC */
RyoheiHagimoto 0:0e0631af0305 530 CV_WRAP virtual double getC() const = 0;
RyoheiHagimoto 0:0e0631af0305 531 /** @copybrief getC @see getC */
RyoheiHagimoto 0:0e0631af0305 532 CV_WRAP virtual void setC(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 533
RyoheiHagimoto 0:0e0631af0305 534 /** Parameter \f$\nu\f$ of a %SVM optimization problem.
RyoheiHagimoto 0:0e0631af0305 535 For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0. */
RyoheiHagimoto 0:0e0631af0305 536 /** @see setNu */
RyoheiHagimoto 0:0e0631af0305 537 CV_WRAP virtual double getNu() const = 0;
RyoheiHagimoto 0:0e0631af0305 538 /** @copybrief getNu @see getNu */
RyoheiHagimoto 0:0e0631af0305 539 CV_WRAP virtual void setNu(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 540
RyoheiHagimoto 0:0e0631af0305 541 /** Parameter \f$\epsilon\f$ of a %SVM optimization problem.
RyoheiHagimoto 0:0e0631af0305 542 For SVM::EPS_SVR. Default value is 0. */
RyoheiHagimoto 0:0e0631af0305 543 /** @see setP */
RyoheiHagimoto 0:0e0631af0305 544 CV_WRAP virtual double getP() const = 0;
RyoheiHagimoto 0:0e0631af0305 545 /** @copybrief getP @see getP */
RyoheiHagimoto 0:0e0631af0305 546 CV_WRAP virtual void setP(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 547
RyoheiHagimoto 0:0e0631af0305 548 /** Optional weights in the SVM::C_SVC problem, assigned to particular classes.
RyoheiHagimoto 0:0e0631af0305 549 They are multiplied by _C_ so the parameter _C_ of class _i_ becomes `classWeights(i) * C`. Thus
RyoheiHagimoto 0:0e0631af0305 550 these weights affect the misclassification penalty for different classes. The larger weight,
RyoheiHagimoto 0:0e0631af0305 551 the larger penalty on misclassification of data from the corresponding class. Default value is
RyoheiHagimoto 0:0e0631af0305 552 empty Mat. */
RyoheiHagimoto 0:0e0631af0305 553 /** @see setClassWeights */
RyoheiHagimoto 0:0e0631af0305 554 CV_WRAP virtual cv::Mat getClassWeights() const = 0;
RyoheiHagimoto 0:0e0631af0305 555 /** @copybrief getClassWeights @see getClassWeights */
RyoheiHagimoto 0:0e0631af0305 556 CV_WRAP virtual void setClassWeights(const cv::Mat &val) = 0;
RyoheiHagimoto 0:0e0631af0305 557
RyoheiHagimoto 0:0e0631af0305 558 /** Termination criteria of the iterative %SVM training procedure which solves a partial
RyoheiHagimoto 0:0e0631af0305 559 case of constrained quadratic optimization problem.
RyoheiHagimoto 0:0e0631af0305 560 You can specify tolerance and/or the maximum number of iterations. Default value is
RyoheiHagimoto 0:0e0631af0305 561 `TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON )`; */
RyoheiHagimoto 0:0e0631af0305 562 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 563 CV_WRAP virtual cv::TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 564 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 565 CV_WRAP virtual void setTermCriteria(const cv::TermCriteria &val) = 0;
RyoheiHagimoto 0:0e0631af0305 566
RyoheiHagimoto 0:0e0631af0305 567 /** Type of a %SVM kernel.
RyoheiHagimoto 0:0e0631af0305 568 See SVM::KernelTypes. Default value is SVM::RBF. */
RyoheiHagimoto 0:0e0631af0305 569 CV_WRAP virtual int getKernelType() const = 0;
RyoheiHagimoto 0:0e0631af0305 570
RyoheiHagimoto 0:0e0631af0305 571 /** Initialize with one of predefined kernels.
RyoheiHagimoto 0:0e0631af0305 572 See SVM::KernelTypes. */
RyoheiHagimoto 0:0e0631af0305 573 CV_WRAP virtual void setKernel(int kernelType) = 0;
RyoheiHagimoto 0:0e0631af0305 574
RyoheiHagimoto 0:0e0631af0305 575 /** Initialize with custom kernel.
RyoheiHagimoto 0:0e0631af0305 576 See SVM::Kernel class for implementation details */
RyoheiHagimoto 0:0e0631af0305 577 virtual void setCustomKernel(const Ptr<Kernel> &_kernel) = 0;
RyoheiHagimoto 0:0e0631af0305 578
RyoheiHagimoto 0:0e0631af0305 579 //! %SVM type
RyoheiHagimoto 0:0e0631af0305 580 enum Types {
RyoheiHagimoto 0:0e0631af0305 581 /** C-Support Vector Classification. n-class classification (n \f$\geq\f$ 2), allows
RyoheiHagimoto 0:0e0631af0305 582 imperfect separation of classes with penalty multiplier C for outliers. */
RyoheiHagimoto 0:0e0631af0305 583 C_SVC=100,
RyoheiHagimoto 0:0e0631af0305 584 /** \f$\nu\f$-Support Vector Classification. n-class classification with possible
RyoheiHagimoto 0:0e0631af0305 585 imperfect separation. Parameter \f$\nu\f$ (in the range 0..1, the larger the value, the smoother
RyoheiHagimoto 0:0e0631af0305 586 the decision boundary) is used instead of C. */
RyoheiHagimoto 0:0e0631af0305 587 NU_SVC=101,
RyoheiHagimoto 0:0e0631af0305 588 /** Distribution Estimation (One-class %SVM). All the training data are from
RyoheiHagimoto 0:0e0631af0305 589 the same class, %SVM builds a boundary that separates the class from the rest of the feature
RyoheiHagimoto 0:0e0631af0305 590 space. */
RyoheiHagimoto 0:0e0631af0305 591 ONE_CLASS=102,
RyoheiHagimoto 0:0e0631af0305 592 /** \f$\epsilon\f$-Support Vector Regression. The distance between feature vectors
RyoheiHagimoto 0:0e0631af0305 593 from the training set and the fitting hyper-plane must be less than p. For outliers the
RyoheiHagimoto 0:0e0631af0305 594 penalty multiplier C is used. */
RyoheiHagimoto 0:0e0631af0305 595 EPS_SVR=103,
RyoheiHagimoto 0:0e0631af0305 596 /** \f$\nu\f$-Support Vector Regression. \f$\nu\f$ is used instead of p.
RyoheiHagimoto 0:0e0631af0305 597 See @cite LibSVM for details. */
RyoheiHagimoto 0:0e0631af0305 598 NU_SVR=104
RyoheiHagimoto 0:0e0631af0305 599 };
RyoheiHagimoto 0:0e0631af0305 600
RyoheiHagimoto 0:0e0631af0305 601 /** @brief %SVM kernel type
RyoheiHagimoto 0:0e0631af0305 602
RyoheiHagimoto 0:0e0631af0305 603 A comparison of different kernels on the following 2D test case with four classes. Four
RyoheiHagimoto 0:0e0631af0305 604 SVM::C_SVC SVMs have been trained (one against rest) with auto_train. Evaluation on three
RyoheiHagimoto 0:0e0631af0305 605 different kernels (SVM::CHI2, SVM::INTER, SVM::RBF). The color depicts the class with max score.
RyoheiHagimoto 0:0e0631af0305 606 Bright means max-score \> 0, dark means max-score \< 0.
RyoheiHagimoto 0:0e0631af0305 607 ![image](pics/SVM_Comparison.png)
RyoheiHagimoto 0:0e0631af0305 608 */
RyoheiHagimoto 0:0e0631af0305 609 enum KernelTypes {
RyoheiHagimoto 0:0e0631af0305 610 /** Returned by SVM::getKernelType in case when custom kernel has been set */
RyoheiHagimoto 0:0e0631af0305 611 CUSTOM=-1,
RyoheiHagimoto 0:0e0631af0305 612 /** Linear kernel. No mapping is done, linear discrimination (or regression) is
RyoheiHagimoto 0:0e0631af0305 613 done in the original feature space. It is the fastest option. \f$K(x_i, x_j) = x_i^T x_j\f$. */
RyoheiHagimoto 0:0e0631af0305 614 LINEAR=0,
RyoheiHagimoto 0:0e0631af0305 615 /** Polynomial kernel:
RyoheiHagimoto 0:0e0631af0305 616 \f$K(x_i, x_j) = (\gamma x_i^T x_j + coef0)^{degree}, \gamma > 0\f$. */
RyoheiHagimoto 0:0e0631af0305 617 POLY=1,
RyoheiHagimoto 0:0e0631af0305 618 /** Radial basis function (RBF), a good choice in most cases.
RyoheiHagimoto 0:0e0631af0305 619 \f$K(x_i, x_j) = e^{-\gamma ||x_i - x_j||^2}, \gamma > 0\f$. */
RyoheiHagimoto 0:0e0631af0305 620 RBF=2,
RyoheiHagimoto 0:0e0631af0305 621 /** Sigmoid kernel: \f$K(x_i, x_j) = \tanh(\gamma x_i^T x_j + coef0)\f$. */
RyoheiHagimoto 0:0e0631af0305 622 SIGMOID=3,
RyoheiHagimoto 0:0e0631af0305 623 /** Exponential Chi2 kernel, similar to the RBF kernel:
RyoheiHagimoto 0:0e0631af0305 624 \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$. */
RyoheiHagimoto 0:0e0631af0305 625 CHI2=4,
RyoheiHagimoto 0:0e0631af0305 626 /** Histogram intersection kernel. A fast kernel. \f$K(x_i, x_j) = min(x_i,x_j)\f$. */
RyoheiHagimoto 0:0e0631af0305 627 INTER=5
RyoheiHagimoto 0:0e0631af0305 628 };
RyoheiHagimoto 0:0e0631af0305 629
RyoheiHagimoto 0:0e0631af0305 630 //! %SVM params type
RyoheiHagimoto 0:0e0631af0305 631 enum ParamTypes {
RyoheiHagimoto 0:0e0631af0305 632 C=0,
RyoheiHagimoto 0:0e0631af0305 633 GAMMA=1,
RyoheiHagimoto 0:0e0631af0305 634 P=2,
RyoheiHagimoto 0:0e0631af0305 635 NU=3,
RyoheiHagimoto 0:0e0631af0305 636 COEF=4,
RyoheiHagimoto 0:0e0631af0305 637 DEGREE=5
RyoheiHagimoto 0:0e0631af0305 638 };
RyoheiHagimoto 0:0e0631af0305 639
RyoheiHagimoto 0:0e0631af0305 640 /** @brief Trains an %SVM with optimal parameters.
RyoheiHagimoto 0:0e0631af0305 641
RyoheiHagimoto 0:0e0631af0305 642 @param data the training data that can be constructed using TrainData::create or
RyoheiHagimoto 0:0e0631af0305 643 TrainData::loadFromCSV.
RyoheiHagimoto 0:0e0631af0305 644 @param kFold Cross-validation parameter. The training set is divided into kFold subsets. One
RyoheiHagimoto 0:0e0631af0305 645 subset is used to test the model, the others form the train set. So, the %SVM algorithm is
RyoheiHagimoto 0:0e0631af0305 646 executed kFold times.
RyoheiHagimoto 0:0e0631af0305 647 @param Cgrid grid for C
RyoheiHagimoto 0:0e0631af0305 648 @param gammaGrid grid for gamma
RyoheiHagimoto 0:0e0631af0305 649 @param pGrid grid for p
RyoheiHagimoto 0:0e0631af0305 650 @param nuGrid grid for nu
RyoheiHagimoto 0:0e0631af0305 651 @param coeffGrid grid for coeff
RyoheiHagimoto 0:0e0631af0305 652 @param degreeGrid grid for degree
RyoheiHagimoto 0:0e0631af0305 653 @param balanced If true and the problem is 2-class classification then the method creates more
RyoheiHagimoto 0:0e0631af0305 654 balanced cross-validation subsets that is proportions between classes in subsets are close
RyoheiHagimoto 0:0e0631af0305 655 to such proportion in the whole train dataset.
RyoheiHagimoto 0:0e0631af0305 656
RyoheiHagimoto 0:0e0631af0305 657 The method trains the %SVM model automatically by choosing the optimal parameters C, gamma, p,
RyoheiHagimoto 0:0e0631af0305 658 nu, coef0, degree. Parameters are considered optimal when the cross-validation
RyoheiHagimoto 0:0e0631af0305 659 estimate of the test set error is minimal.
RyoheiHagimoto 0:0e0631af0305 660
RyoheiHagimoto 0:0e0631af0305 661 If there is no need to optimize a parameter, the corresponding grid step should be set to any
RyoheiHagimoto 0:0e0631af0305 662 value less than or equal to 1. For example, to avoid optimization in gamma, set `gammaGrid.step
RyoheiHagimoto 0:0e0631af0305 663 = 0`, `gammaGrid.minVal`, `gamma_grid.maxVal` as arbitrary numbers. In this case, the value
RyoheiHagimoto 0:0e0631af0305 664 `Gamma` is taken for gamma.
RyoheiHagimoto 0:0e0631af0305 665
RyoheiHagimoto 0:0e0631af0305 666 And, finally, if the optimization in a parameter is required but the corresponding grid is
RyoheiHagimoto 0:0e0631af0305 667 unknown, you may call the function SVM::getDefaultGrid. To generate a grid, for example, for
RyoheiHagimoto 0:0e0631af0305 668 gamma, call `SVM::getDefaultGrid(SVM::GAMMA)`.
RyoheiHagimoto 0:0e0631af0305 669
RyoheiHagimoto 0:0e0631af0305 670 This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the
RyoheiHagimoto 0:0e0631af0305 671 regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and
RyoheiHagimoto 0:0e0631af0305 672 the usual %SVM with parameters specified in params is executed.
RyoheiHagimoto 0:0e0631af0305 673 */
RyoheiHagimoto 0:0e0631af0305 674 virtual bool trainAuto( const Ptr<TrainData>& data, int kFold = 10,
RyoheiHagimoto 0:0e0631af0305 675 ParamGrid Cgrid = SVM::getDefaultGrid(SVM::C),
RyoheiHagimoto 0:0e0631af0305 676 ParamGrid gammaGrid = SVM::getDefaultGrid(SVM::GAMMA),
RyoheiHagimoto 0:0e0631af0305 677 ParamGrid pGrid = SVM::getDefaultGrid(SVM::P),
RyoheiHagimoto 0:0e0631af0305 678 ParamGrid nuGrid = SVM::getDefaultGrid(SVM::NU),
RyoheiHagimoto 0:0e0631af0305 679 ParamGrid coeffGrid = SVM::getDefaultGrid(SVM::COEF),
RyoheiHagimoto 0:0e0631af0305 680 ParamGrid degreeGrid = SVM::getDefaultGrid(SVM::DEGREE),
RyoheiHagimoto 0:0e0631af0305 681 bool balanced=false) = 0;
RyoheiHagimoto 0:0e0631af0305 682
RyoheiHagimoto 0:0e0631af0305 683 /** @brief Retrieves all the support vectors
RyoheiHagimoto 0:0e0631af0305 684
RyoheiHagimoto 0:0e0631af0305 685 The method returns all the support vectors as a floating-point matrix, where support vectors are
RyoheiHagimoto 0:0e0631af0305 686 stored as matrix rows.
RyoheiHagimoto 0:0e0631af0305 687 */
RyoheiHagimoto 0:0e0631af0305 688 CV_WRAP virtual Mat getSupportVectors() const = 0;
RyoheiHagimoto 0:0e0631af0305 689
RyoheiHagimoto 0:0e0631af0305 690 /** @brief Retrieves all the uncompressed support vectors of a linear %SVM
RyoheiHagimoto 0:0e0631af0305 691
RyoheiHagimoto 0:0e0631af0305 692 The method returns all the uncompressed support vectors of a linear %SVM that the compressed
RyoheiHagimoto 0:0e0631af0305 693 support vector, used for prediction, was derived from. They are returned in a floating-point
RyoheiHagimoto 0:0e0631af0305 694 matrix, where the support vectors are stored as matrix rows.
RyoheiHagimoto 0:0e0631af0305 695 */
RyoheiHagimoto 0:0e0631af0305 696 CV_WRAP Mat getUncompressedSupportVectors() const;
RyoheiHagimoto 0:0e0631af0305 697
RyoheiHagimoto 0:0e0631af0305 698 /** @brief Retrieves the decision function
RyoheiHagimoto 0:0e0631af0305 699
RyoheiHagimoto 0:0e0631af0305 700 @param i the index of the decision function. If the problem solved is regression, 1-class or
RyoheiHagimoto 0:0e0631af0305 701 2-class classification, then there will be just one decision function and the index should
RyoheiHagimoto 0:0e0631af0305 702 always be 0. Otherwise, in the case of N-class classification, there will be \f$N(N-1)/2\f$
RyoheiHagimoto 0:0e0631af0305 703 decision functions.
RyoheiHagimoto 0:0e0631af0305 704 @param alpha the optional output vector for weights, corresponding to different support vectors.
RyoheiHagimoto 0:0e0631af0305 705 In the case of linear %SVM all the alpha's will be 1's.
RyoheiHagimoto 0:0e0631af0305 706 @param svidx the optional output vector of indices of support vectors within the matrix of
RyoheiHagimoto 0:0e0631af0305 707 support vectors (which can be retrieved by SVM::getSupportVectors). In the case of linear
RyoheiHagimoto 0:0e0631af0305 708 %SVM each decision function consists of a single "compressed" support vector.
RyoheiHagimoto 0:0e0631af0305 709
RyoheiHagimoto 0:0e0631af0305 710 The method returns rho parameter of the decision function, a scalar subtracted from the weighted
RyoheiHagimoto 0:0e0631af0305 711 sum of kernel responses.
RyoheiHagimoto 0:0e0631af0305 712 */
RyoheiHagimoto 0:0e0631af0305 713 CV_WRAP virtual double getDecisionFunction(int i, OutputArray alpha, OutputArray svidx) const = 0;
RyoheiHagimoto 0:0e0631af0305 714
RyoheiHagimoto 0:0e0631af0305 715 /** @brief Generates a grid for %SVM parameters.
RyoheiHagimoto 0:0e0631af0305 716
RyoheiHagimoto 0:0e0631af0305 717 @param param_id %SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is
RyoheiHagimoto 0:0e0631af0305 718 generated for the parameter with this ID.
RyoheiHagimoto 0:0e0631af0305 719
RyoheiHagimoto 0:0e0631af0305 720 The function generates a grid for the specified parameter of the %SVM algorithm. The grid may be
RyoheiHagimoto 0:0e0631af0305 721 passed to the function SVM::trainAuto.
RyoheiHagimoto 0:0e0631af0305 722 */
RyoheiHagimoto 0:0e0631af0305 723 static ParamGrid getDefaultGrid( int param_id );
RyoheiHagimoto 0:0e0631af0305 724
RyoheiHagimoto 0:0e0631af0305 725 /** Creates empty model.
RyoheiHagimoto 0:0e0631af0305 726 Use StatModel::train to train the model. Since %SVM has several parameters, you may want to
RyoheiHagimoto 0:0e0631af0305 727 find the best parameters for your problem, it can be done with SVM::trainAuto. */
RyoheiHagimoto 0:0e0631af0305 728 CV_WRAP static Ptr<SVM> create();
RyoheiHagimoto 0:0e0631af0305 729
RyoheiHagimoto 0:0e0631af0305 730 /** @brief Loads and creates a serialized svm from a file
RyoheiHagimoto 0:0e0631af0305 731 *
RyoheiHagimoto 0:0e0631af0305 732 * Use SVM::save to serialize and store an SVM to disk.
RyoheiHagimoto 0:0e0631af0305 733 * Load the SVM from this file again, by calling this function with the path to the file.
RyoheiHagimoto 0:0e0631af0305 734 *
RyoheiHagimoto 0:0e0631af0305 735 * @param filepath path to serialized svm
RyoheiHagimoto 0:0e0631af0305 736 */
RyoheiHagimoto 0:0e0631af0305 737 CV_WRAP static Ptr<SVM> load(const String& filepath);
RyoheiHagimoto 0:0e0631af0305 738 };
RyoheiHagimoto 0:0e0631af0305 739
RyoheiHagimoto 0:0e0631af0305 740 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 741 * Expectation - Maximization *
RyoheiHagimoto 0:0e0631af0305 742 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 743
RyoheiHagimoto 0:0e0631af0305 744 /** @brief The class implements the Expectation Maximization algorithm.
RyoheiHagimoto 0:0e0631af0305 745
RyoheiHagimoto 0:0e0631af0305 746 @sa @ref ml_intro_em
RyoheiHagimoto 0:0e0631af0305 747 */
RyoheiHagimoto 0:0e0631af0305 748 class CV_EXPORTS_W EM : public StatModel
RyoheiHagimoto 0:0e0631af0305 749 {
RyoheiHagimoto 0:0e0631af0305 750 public:
RyoheiHagimoto 0:0e0631af0305 751 //! Type of covariation matrices
RyoheiHagimoto 0:0e0631af0305 752 enum Types {
RyoheiHagimoto 0:0e0631af0305 753 /** A scaled identity matrix \f$\mu_k * I\f$. There is the only
RyoheiHagimoto 0:0e0631af0305 754 parameter \f$\mu_k\f$ to be estimated for each matrix. The option may be used in special cases,
RyoheiHagimoto 0:0e0631af0305 755 when the constraint is relevant, or as a first step in the optimization (for example in case
RyoheiHagimoto 0:0e0631af0305 756 when the data is preprocessed with PCA). The results of such preliminary estimation may be
RyoheiHagimoto 0:0e0631af0305 757 passed again to the optimization procedure, this time with
RyoheiHagimoto 0:0e0631af0305 758 covMatType=EM::COV_MAT_DIAGONAL. */
RyoheiHagimoto 0:0e0631af0305 759 COV_MAT_SPHERICAL=0,
RyoheiHagimoto 0:0e0631af0305 760 /** A diagonal matrix with positive diagonal elements. The number of
RyoheiHagimoto 0:0e0631af0305 761 free parameters is d for each matrix. This is most commonly used option yielding good
RyoheiHagimoto 0:0e0631af0305 762 estimation results. */
RyoheiHagimoto 0:0e0631af0305 763 COV_MAT_DIAGONAL=1,
RyoheiHagimoto 0:0e0631af0305 764 /** A symmetric positively defined matrix. The number of free
RyoheiHagimoto 0:0e0631af0305 765 parameters in each matrix is about \f$d^2/2\f$. It is not recommended to use this option, unless
RyoheiHagimoto 0:0e0631af0305 766 there is pretty accurate initial estimation of the parameters and/or a huge number of
RyoheiHagimoto 0:0e0631af0305 767 training samples. */
RyoheiHagimoto 0:0e0631af0305 768 COV_MAT_GENERIC=2,
RyoheiHagimoto 0:0e0631af0305 769 COV_MAT_DEFAULT=COV_MAT_DIAGONAL
RyoheiHagimoto 0:0e0631af0305 770 };
RyoheiHagimoto 0:0e0631af0305 771
RyoheiHagimoto 0:0e0631af0305 772 //! Default parameters
RyoheiHagimoto 0:0e0631af0305 773 enum {DEFAULT_NCLUSTERS=5, DEFAULT_MAX_ITERS=100};
RyoheiHagimoto 0:0e0631af0305 774
RyoheiHagimoto 0:0e0631af0305 775 //! The initial step
RyoheiHagimoto 0:0e0631af0305 776 enum {START_E_STEP=1, START_M_STEP=2, START_AUTO_STEP=0};
RyoheiHagimoto 0:0e0631af0305 777
RyoheiHagimoto 0:0e0631af0305 778 /** The number of mixture components in the Gaussian mixture model.
RyoheiHagimoto 0:0e0631af0305 779 Default value of the parameter is EM::DEFAULT_NCLUSTERS=5. Some of %EM implementation could
RyoheiHagimoto 0:0e0631af0305 780 determine the optimal number of mixtures within a specified value range, but that is not the
RyoheiHagimoto 0:0e0631af0305 781 case in ML yet. */
RyoheiHagimoto 0:0e0631af0305 782 /** @see setClustersNumber */
RyoheiHagimoto 0:0e0631af0305 783 CV_WRAP virtual int getClustersNumber() const = 0;
RyoheiHagimoto 0:0e0631af0305 784 /** @copybrief getClustersNumber @see getClustersNumber */
RyoheiHagimoto 0:0e0631af0305 785 CV_WRAP virtual void setClustersNumber(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 786
RyoheiHagimoto 0:0e0631af0305 787 /** Constraint on covariance matrices which defines type of matrices.
RyoheiHagimoto 0:0e0631af0305 788 See EM::Types. */
RyoheiHagimoto 0:0e0631af0305 789 /** @see setCovarianceMatrixType */
RyoheiHagimoto 0:0e0631af0305 790 CV_WRAP virtual int getCovarianceMatrixType() const = 0;
RyoheiHagimoto 0:0e0631af0305 791 /** @copybrief getCovarianceMatrixType @see getCovarianceMatrixType */
RyoheiHagimoto 0:0e0631af0305 792 CV_WRAP virtual void setCovarianceMatrixType(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 793
RyoheiHagimoto 0:0e0631af0305 794 /** The termination criteria of the %EM algorithm.
RyoheiHagimoto 0:0e0631af0305 795 The %EM algorithm can be terminated by the number of iterations termCrit.maxCount (number of
RyoheiHagimoto 0:0e0631af0305 796 M-steps) or when relative change of likelihood logarithm is less than termCrit.epsilon. Default
RyoheiHagimoto 0:0e0631af0305 797 maximum number of iterations is EM::DEFAULT_MAX_ITERS=100. */
RyoheiHagimoto 0:0e0631af0305 798 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 799 CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 800 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 801 CV_WRAP virtual void setTermCriteria(const TermCriteria &val) = 0;
RyoheiHagimoto 0:0e0631af0305 802
RyoheiHagimoto 0:0e0631af0305 803 /** @brief Returns weights of the mixtures
RyoheiHagimoto 0:0e0631af0305 804
RyoheiHagimoto 0:0e0631af0305 805 Returns vector with the number of elements equal to the number of mixtures.
RyoheiHagimoto 0:0e0631af0305 806 */
RyoheiHagimoto 0:0e0631af0305 807 CV_WRAP virtual Mat getWeights() const = 0;
RyoheiHagimoto 0:0e0631af0305 808 /** @brief Returns the cluster centers (means of the Gaussian mixture)
RyoheiHagimoto 0:0e0631af0305 809
RyoheiHagimoto 0:0e0631af0305 810 Returns matrix with the number of rows equal to the number of mixtures and number of columns
RyoheiHagimoto 0:0e0631af0305 811 equal to the space dimensionality.
RyoheiHagimoto 0:0e0631af0305 812 */
RyoheiHagimoto 0:0e0631af0305 813 CV_WRAP virtual Mat getMeans() const = 0;
RyoheiHagimoto 0:0e0631af0305 814 /** @brief Returns covariation matrices
RyoheiHagimoto 0:0e0631af0305 815
RyoheiHagimoto 0:0e0631af0305 816 Returns vector of covariation matrices. Number of matrices is the number of gaussian mixtures,
RyoheiHagimoto 0:0e0631af0305 817 each matrix is a square floating-point matrix NxN, where N is the space dimensionality.
RyoheiHagimoto 0:0e0631af0305 818 */
RyoheiHagimoto 0:0e0631af0305 819 CV_WRAP virtual void getCovs(CV_OUT std::vector<Mat>& covs) const = 0;
RyoheiHagimoto 0:0e0631af0305 820
RyoheiHagimoto 0:0e0631af0305 821 /** @brief Returns a likelihood logarithm value and an index of the most probable mixture component
RyoheiHagimoto 0:0e0631af0305 822 for the given sample.
RyoheiHagimoto 0:0e0631af0305 823
RyoheiHagimoto 0:0e0631af0305 824 @param sample A sample for classification. It should be a one-channel matrix of
RyoheiHagimoto 0:0e0631af0305 825 \f$1 \times dims\f$ or \f$dims \times 1\f$ size.
RyoheiHagimoto 0:0e0631af0305 826 @param probs Optional output matrix that contains posterior probabilities of each component
RyoheiHagimoto 0:0e0631af0305 827 given the sample. It has \f$1 \times nclusters\f$ size and CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 828
RyoheiHagimoto 0:0e0631af0305 829 The method returns a two-element double vector. Zero element is a likelihood logarithm value for
RyoheiHagimoto 0:0e0631af0305 830 the sample. First element is an index of the most probable mixture component for the given
RyoheiHagimoto 0:0e0631af0305 831 sample.
RyoheiHagimoto 0:0e0631af0305 832 */
RyoheiHagimoto 0:0e0631af0305 833 CV_WRAP virtual Vec2d predict2(InputArray sample, OutputArray probs) const = 0;
RyoheiHagimoto 0:0e0631af0305 834
RyoheiHagimoto 0:0e0631af0305 835 /** @brief Estimate the Gaussian mixture parameters from a samples set.
RyoheiHagimoto 0:0e0631af0305 836
RyoheiHagimoto 0:0e0631af0305 837 This variation starts with Expectation step. Initial values of the model parameters will be
RyoheiHagimoto 0:0e0631af0305 838 estimated by the k-means algorithm.
RyoheiHagimoto 0:0e0631af0305 839
RyoheiHagimoto 0:0e0631af0305 840 Unlike many of the ML models, %EM is an unsupervised learning algorithm and it does not take
RyoheiHagimoto 0:0e0631af0305 841 responses (class labels or function values) as input. Instead, it computes the *Maximum
RyoheiHagimoto 0:0e0631af0305 842 Likelihood Estimate* of the Gaussian mixture parameters from an input sample set, stores all the
RyoheiHagimoto 0:0e0631af0305 843 parameters inside the structure: \f$p_{i,k}\f$ in probs, \f$a_k\f$ in means , \f$S_k\f$ in
RyoheiHagimoto 0:0e0631af0305 844 covs[k], \f$\pi_k\f$ in weights , and optionally computes the output "class label" for each
RyoheiHagimoto 0:0e0631af0305 845 sample: \f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most
RyoheiHagimoto 0:0e0631af0305 846 probable mixture component for each sample).
RyoheiHagimoto 0:0e0631af0305 847
RyoheiHagimoto 0:0e0631af0305 848 The trained model can be used further for prediction, just like any other classifier. The
RyoheiHagimoto 0:0e0631af0305 849 trained model is similar to the NormalBayesClassifier.
RyoheiHagimoto 0:0e0631af0305 850
RyoheiHagimoto 0:0e0631af0305 851 @param samples Samples from which the Gaussian mixture model will be estimated. It should be a
RyoheiHagimoto 0:0e0631af0305 852 one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
RyoheiHagimoto 0:0e0631af0305 853 it will be converted to the inner matrix of such type for the further computing.
RyoheiHagimoto 0:0e0631af0305 854 @param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
RyoheiHagimoto 0:0e0631af0305 855 each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 856 @param labels The optional output "class label" for each sample:
RyoheiHagimoto 0:0e0631af0305 857 \f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
RyoheiHagimoto 0:0e0631af0305 858 mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
RyoheiHagimoto 0:0e0631af0305 859 @param probs The optional output matrix that contains posterior probabilities of each Gaussian
RyoheiHagimoto 0:0e0631af0305 860 mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
RyoheiHagimoto 0:0e0631af0305 861 CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 862 */
RyoheiHagimoto 0:0e0631af0305 863 CV_WRAP virtual bool trainEM(InputArray samples,
RyoheiHagimoto 0:0e0631af0305 864 OutputArray logLikelihoods=noArray(),
RyoheiHagimoto 0:0e0631af0305 865 OutputArray labels=noArray(),
RyoheiHagimoto 0:0e0631af0305 866 OutputArray probs=noArray()) = 0;
RyoheiHagimoto 0:0e0631af0305 867
RyoheiHagimoto 0:0e0631af0305 868 /** @brief Estimate the Gaussian mixture parameters from a samples set.
RyoheiHagimoto 0:0e0631af0305 869
RyoheiHagimoto 0:0e0631af0305 870 This variation starts with Expectation step. You need to provide initial means \f$a_k\f$ of
RyoheiHagimoto 0:0e0631af0305 871 mixture components. Optionally you can pass initial weights \f$\pi_k\f$ and covariance matrices
RyoheiHagimoto 0:0e0631af0305 872 \f$S_k\f$ of mixture components.
RyoheiHagimoto 0:0e0631af0305 873
RyoheiHagimoto 0:0e0631af0305 874 @param samples Samples from which the Gaussian mixture model will be estimated. It should be a
RyoheiHagimoto 0:0e0631af0305 875 one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
RyoheiHagimoto 0:0e0631af0305 876 it will be converted to the inner matrix of such type for the further computing.
RyoheiHagimoto 0:0e0631af0305 877 @param means0 Initial means \f$a_k\f$ of mixture components. It is a one-channel matrix of
RyoheiHagimoto 0:0e0631af0305 878 \f$nclusters \times dims\f$ size. If the matrix does not have CV_64F type it will be
RyoheiHagimoto 0:0e0631af0305 879 converted to the inner matrix of such type for the further computing.
RyoheiHagimoto 0:0e0631af0305 880 @param covs0 The vector of initial covariance matrices \f$S_k\f$ of mixture components. Each of
RyoheiHagimoto 0:0e0631af0305 881 covariance matrices is a one-channel matrix of \f$dims \times dims\f$ size. If the matrices
RyoheiHagimoto 0:0e0631af0305 882 do not have CV_64F type they will be converted to the inner matrices of such type for the
RyoheiHagimoto 0:0e0631af0305 883 further computing.
RyoheiHagimoto 0:0e0631af0305 884 @param weights0 Initial weights \f$\pi_k\f$ of mixture components. It should be a one-channel
RyoheiHagimoto 0:0e0631af0305 885 floating-point matrix with \f$1 \times nclusters\f$ or \f$nclusters \times 1\f$ size.
RyoheiHagimoto 0:0e0631af0305 886 @param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
RyoheiHagimoto 0:0e0631af0305 887 each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 888 @param labels The optional output "class label" for each sample:
RyoheiHagimoto 0:0e0631af0305 889 \f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
RyoheiHagimoto 0:0e0631af0305 890 mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
RyoheiHagimoto 0:0e0631af0305 891 @param probs The optional output matrix that contains posterior probabilities of each Gaussian
RyoheiHagimoto 0:0e0631af0305 892 mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
RyoheiHagimoto 0:0e0631af0305 893 CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 894 */
RyoheiHagimoto 0:0e0631af0305 895 CV_WRAP virtual bool trainE(InputArray samples, InputArray means0,
RyoheiHagimoto 0:0e0631af0305 896 InputArray covs0=noArray(),
RyoheiHagimoto 0:0e0631af0305 897 InputArray weights0=noArray(),
RyoheiHagimoto 0:0e0631af0305 898 OutputArray logLikelihoods=noArray(),
RyoheiHagimoto 0:0e0631af0305 899 OutputArray labels=noArray(),
RyoheiHagimoto 0:0e0631af0305 900 OutputArray probs=noArray()) = 0;
RyoheiHagimoto 0:0e0631af0305 901
RyoheiHagimoto 0:0e0631af0305 902 /** @brief Estimate the Gaussian mixture parameters from a samples set.
RyoheiHagimoto 0:0e0631af0305 903
RyoheiHagimoto 0:0e0631af0305 904 This variation starts with Maximization step. You need to provide initial probabilities
RyoheiHagimoto 0:0e0631af0305 905 \f$p_{i,k}\f$ to use this option.
RyoheiHagimoto 0:0e0631af0305 906
RyoheiHagimoto 0:0e0631af0305 907 @param samples Samples from which the Gaussian mixture model will be estimated. It should be a
RyoheiHagimoto 0:0e0631af0305 908 one-channel matrix, each row of which is a sample. If the matrix does not have CV_64F type
RyoheiHagimoto 0:0e0631af0305 909 it will be converted to the inner matrix of such type for the further computing.
RyoheiHagimoto 0:0e0631af0305 910 @param probs0
RyoheiHagimoto 0:0e0631af0305 911 @param logLikelihoods The optional output matrix that contains a likelihood logarithm value for
RyoheiHagimoto 0:0e0631af0305 912 each sample. It has \f$nsamples \times 1\f$ size and CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 913 @param labels The optional output "class label" for each sample:
RyoheiHagimoto 0:0e0631af0305 914 \f$\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N\f$ (indices of the most probable
RyoheiHagimoto 0:0e0631af0305 915 mixture component for each sample). It has \f$nsamples \times 1\f$ size and CV_32SC1 type.
RyoheiHagimoto 0:0e0631af0305 916 @param probs The optional output matrix that contains posterior probabilities of each Gaussian
RyoheiHagimoto 0:0e0631af0305 917 mixture component given the each sample. It has \f$nsamples \times nclusters\f$ size and
RyoheiHagimoto 0:0e0631af0305 918 CV_64FC1 type.
RyoheiHagimoto 0:0e0631af0305 919 */
RyoheiHagimoto 0:0e0631af0305 920 CV_WRAP virtual bool trainM(InputArray samples, InputArray probs0,
RyoheiHagimoto 0:0e0631af0305 921 OutputArray logLikelihoods=noArray(),
RyoheiHagimoto 0:0e0631af0305 922 OutputArray labels=noArray(),
RyoheiHagimoto 0:0e0631af0305 923 OutputArray probs=noArray()) = 0;
RyoheiHagimoto 0:0e0631af0305 924
RyoheiHagimoto 0:0e0631af0305 925 /** Creates empty %EM model.
RyoheiHagimoto 0:0e0631af0305 926 The model should be trained then using StatModel::train(traindata, flags) method. Alternatively, you
RyoheiHagimoto 0:0e0631af0305 927 can use one of the EM::train\* methods or load it from file using Algorithm::load\<EM\>(filename).
RyoheiHagimoto 0:0e0631af0305 928 */
RyoheiHagimoto 0:0e0631af0305 929 CV_WRAP static Ptr<EM> create();
RyoheiHagimoto 0:0e0631af0305 930 };
RyoheiHagimoto 0:0e0631af0305 931
RyoheiHagimoto 0:0e0631af0305 932 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 933 * Decision Tree *
RyoheiHagimoto 0:0e0631af0305 934 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 935
RyoheiHagimoto 0:0e0631af0305 936 /** @brief The class represents a single decision tree or a collection of decision trees.
RyoheiHagimoto 0:0e0631af0305 937
RyoheiHagimoto 0:0e0631af0305 938 The current public interface of the class allows user to train only a single decision tree, however
RyoheiHagimoto 0:0e0631af0305 939 the class is capable of storing multiple decision trees and using them for prediction (by summing
RyoheiHagimoto 0:0e0631af0305 940 responses or using a voting schemes), and the derived from DTrees classes (such as RTrees and Boost)
RyoheiHagimoto 0:0e0631af0305 941 use this capability to implement decision tree ensembles.
RyoheiHagimoto 0:0e0631af0305 942
RyoheiHagimoto 0:0e0631af0305 943 @sa @ref ml_intro_trees
RyoheiHagimoto 0:0e0631af0305 944 */
RyoheiHagimoto 0:0e0631af0305 945 class CV_EXPORTS_W DTrees : public StatModel
RyoheiHagimoto 0:0e0631af0305 946 {
RyoheiHagimoto 0:0e0631af0305 947 public:
RyoheiHagimoto 0:0e0631af0305 948 /** Predict options */
RyoheiHagimoto 0:0e0631af0305 949 enum Flags { PREDICT_AUTO=0, PREDICT_SUM=(1<<8), PREDICT_MAX_VOTE=(2<<8), PREDICT_MASK=(3<<8) };
RyoheiHagimoto 0:0e0631af0305 950
RyoheiHagimoto 0:0e0631af0305 951 /** Cluster possible values of a categorical variable into K\<=maxCategories clusters to
RyoheiHagimoto 0:0e0631af0305 952 find a suboptimal split.
RyoheiHagimoto 0:0e0631af0305 953 If a discrete variable, on which the training procedure tries to make a split, takes more than
RyoheiHagimoto 0:0e0631af0305 954 maxCategories values, the precise best subset estimation may take a very long time because the
RyoheiHagimoto 0:0e0631af0305 955 algorithm is exponential. Instead, many decision trees engines (including our implementation)
RyoheiHagimoto 0:0e0631af0305 956 try to find sub-optimal split in this case by clustering all the samples into maxCategories
RyoheiHagimoto 0:0e0631af0305 957 clusters that is some categories are merged together. The clustering is applied only in n \>
RyoheiHagimoto 0:0e0631af0305 958 2-class classification problems for categorical variables with N \> max_categories possible
RyoheiHagimoto 0:0e0631af0305 959 values. In case of regression and 2-class classification the optimal split can be found
RyoheiHagimoto 0:0e0631af0305 960 efficiently without employing clustering, thus the parameter is not used in these cases.
RyoheiHagimoto 0:0e0631af0305 961 Default value is 10.*/
RyoheiHagimoto 0:0e0631af0305 962 /** @see setMaxCategories */
RyoheiHagimoto 0:0e0631af0305 963 CV_WRAP virtual int getMaxCategories() const = 0;
RyoheiHagimoto 0:0e0631af0305 964 /** @copybrief getMaxCategories @see getMaxCategories */
RyoheiHagimoto 0:0e0631af0305 965 CV_WRAP virtual void setMaxCategories(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 966
RyoheiHagimoto 0:0e0631af0305 967 /** The maximum possible depth of the tree.
RyoheiHagimoto 0:0e0631af0305 968 That is the training algorithms attempts to split a node while its depth is less than maxDepth.
RyoheiHagimoto 0:0e0631af0305 969 The root node has zero depth. The actual depth may be smaller if the other termination criteria
RyoheiHagimoto 0:0e0631af0305 970 are met (see the outline of the training procedure @ref ml_intro_trees "here"), and/or if the
RyoheiHagimoto 0:0e0631af0305 971 tree is pruned. Default value is INT_MAX.*/
RyoheiHagimoto 0:0e0631af0305 972 /** @see setMaxDepth */
RyoheiHagimoto 0:0e0631af0305 973 CV_WRAP virtual int getMaxDepth() const = 0;
RyoheiHagimoto 0:0e0631af0305 974 /** @copybrief getMaxDepth @see getMaxDepth */
RyoheiHagimoto 0:0e0631af0305 975 CV_WRAP virtual void setMaxDepth(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 976
RyoheiHagimoto 0:0e0631af0305 977 /** If the number of samples in a node is less than this parameter then the node will not be split.
RyoheiHagimoto 0:0e0631af0305 978
RyoheiHagimoto 0:0e0631af0305 979 Default value is 10.*/
RyoheiHagimoto 0:0e0631af0305 980 /** @see setMinSampleCount */
RyoheiHagimoto 0:0e0631af0305 981 CV_WRAP virtual int getMinSampleCount() const = 0;
RyoheiHagimoto 0:0e0631af0305 982 /** @copybrief getMinSampleCount @see getMinSampleCount */
RyoheiHagimoto 0:0e0631af0305 983 CV_WRAP virtual void setMinSampleCount(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 984
RyoheiHagimoto 0:0e0631af0305 985 /** If CVFolds \> 1 then algorithms prunes the built decision tree using K-fold
RyoheiHagimoto 0:0e0631af0305 986 cross-validation procedure where K is equal to CVFolds.
RyoheiHagimoto 0:0e0631af0305 987 Default value is 10.*/
RyoheiHagimoto 0:0e0631af0305 988 /** @see setCVFolds */
RyoheiHagimoto 0:0e0631af0305 989 CV_WRAP virtual int getCVFolds() const = 0;
RyoheiHagimoto 0:0e0631af0305 990 /** @copybrief getCVFolds @see getCVFolds */
RyoheiHagimoto 0:0e0631af0305 991 CV_WRAP virtual void setCVFolds(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 992
RyoheiHagimoto 0:0e0631af0305 993 /** If true then surrogate splits will be built.
RyoheiHagimoto 0:0e0631af0305 994 These splits allow to work with missing data and compute variable importance correctly.
RyoheiHagimoto 0:0e0631af0305 995 Default value is false.
RyoheiHagimoto 0:0e0631af0305 996 @note currently it's not implemented.*/
RyoheiHagimoto 0:0e0631af0305 997 /** @see setUseSurrogates */
RyoheiHagimoto 0:0e0631af0305 998 CV_WRAP virtual bool getUseSurrogates() const = 0;
RyoheiHagimoto 0:0e0631af0305 999 /** @copybrief getUseSurrogates @see getUseSurrogates */
RyoheiHagimoto 0:0e0631af0305 1000 CV_WRAP virtual void setUseSurrogates(bool val) = 0;
RyoheiHagimoto 0:0e0631af0305 1001
RyoheiHagimoto 0:0e0631af0305 1002 /** If true then a pruning will be harsher.
RyoheiHagimoto 0:0e0631af0305 1003 This will make a tree more compact and more resistant to the training data noise but a bit less
RyoheiHagimoto 0:0e0631af0305 1004 accurate. Default value is true.*/
RyoheiHagimoto 0:0e0631af0305 1005 /** @see setUse1SERule */
RyoheiHagimoto 0:0e0631af0305 1006 CV_WRAP virtual bool getUse1SERule() const = 0;
RyoheiHagimoto 0:0e0631af0305 1007 /** @copybrief getUse1SERule @see getUse1SERule */
RyoheiHagimoto 0:0e0631af0305 1008 CV_WRAP virtual void setUse1SERule(bool val) = 0;
RyoheiHagimoto 0:0e0631af0305 1009
RyoheiHagimoto 0:0e0631af0305 1010 /** If true then pruned branches are physically removed from the tree.
RyoheiHagimoto 0:0e0631af0305 1011 Otherwise they are retained and it is possible to get results from the original unpruned (or
RyoheiHagimoto 0:0e0631af0305 1012 pruned less aggressively) tree. Default value is true.*/
RyoheiHagimoto 0:0e0631af0305 1013 /** @see setTruncatePrunedTree */
RyoheiHagimoto 0:0e0631af0305 1014 CV_WRAP virtual bool getTruncatePrunedTree() const = 0;
RyoheiHagimoto 0:0e0631af0305 1015 /** @copybrief getTruncatePrunedTree @see getTruncatePrunedTree */
RyoheiHagimoto 0:0e0631af0305 1016 CV_WRAP virtual void setTruncatePrunedTree(bool val) = 0;
RyoheiHagimoto 0:0e0631af0305 1017
RyoheiHagimoto 0:0e0631af0305 1018 /** Termination criteria for regression trees.
RyoheiHagimoto 0:0e0631af0305 1019 If all absolute differences between an estimated value in a node and values of train samples
RyoheiHagimoto 0:0e0631af0305 1020 in this node are less than this parameter then the node will not be split further. Default
RyoheiHagimoto 0:0e0631af0305 1021 value is 0.01f*/
RyoheiHagimoto 0:0e0631af0305 1022 /** @see setRegressionAccuracy */
RyoheiHagimoto 0:0e0631af0305 1023 CV_WRAP virtual float getRegressionAccuracy() const = 0;
RyoheiHagimoto 0:0e0631af0305 1024 /** @copybrief getRegressionAccuracy @see getRegressionAccuracy */
RyoheiHagimoto 0:0e0631af0305 1025 CV_WRAP virtual void setRegressionAccuracy(float val) = 0;
RyoheiHagimoto 0:0e0631af0305 1026
RyoheiHagimoto 0:0e0631af0305 1027 /** @brief The array of a priori class probabilities, sorted by the class label value.
RyoheiHagimoto 0:0e0631af0305 1028
RyoheiHagimoto 0:0e0631af0305 1029 The parameter can be used to tune the decision tree preferences toward a certain class. For
RyoheiHagimoto 0:0e0631af0305 1030 example, if you want to detect some rare anomaly occurrence, the training base will likely
RyoheiHagimoto 0:0e0631af0305 1031 contain much more normal cases than anomalies, so a very good classification performance
RyoheiHagimoto 0:0e0631af0305 1032 will be achieved just by considering every case as normal. To avoid this, the priors can be
RyoheiHagimoto 0:0e0631af0305 1033 specified, where the anomaly probability is artificially increased (up to 0.5 or even
RyoheiHagimoto 0:0e0631af0305 1034 greater), so the weight of the misclassified anomalies becomes much bigger, and the tree is
RyoheiHagimoto 0:0e0631af0305 1035 adjusted properly.
RyoheiHagimoto 0:0e0631af0305 1036
RyoheiHagimoto 0:0e0631af0305 1037 You can also think about this parameter as weights of prediction categories which determine
RyoheiHagimoto 0:0e0631af0305 1038 relative weights that you give to misclassification. That is, if the weight of the first
RyoheiHagimoto 0:0e0631af0305 1039 category is 1 and the weight of the second category is 10, then each mistake in predicting
RyoheiHagimoto 0:0e0631af0305 1040 the second category is equivalent to making 10 mistakes in predicting the first category.
RyoheiHagimoto 0:0e0631af0305 1041 Default value is empty Mat.*/
RyoheiHagimoto 0:0e0631af0305 1042 /** @see setPriors */
RyoheiHagimoto 0:0e0631af0305 1043 CV_WRAP virtual cv::Mat getPriors() const = 0;
RyoheiHagimoto 0:0e0631af0305 1044 /** @copybrief getPriors @see getPriors */
RyoheiHagimoto 0:0e0631af0305 1045 CV_WRAP virtual void setPriors(const cv::Mat &val) = 0;
RyoheiHagimoto 0:0e0631af0305 1046
RyoheiHagimoto 0:0e0631af0305 1047 /** @brief The class represents a decision tree node.
RyoheiHagimoto 0:0e0631af0305 1048 */
RyoheiHagimoto 0:0e0631af0305 1049 class CV_EXPORTS Node
RyoheiHagimoto 0:0e0631af0305 1050 {
RyoheiHagimoto 0:0e0631af0305 1051 public:
RyoheiHagimoto 0:0e0631af0305 1052 Node();
RyoheiHagimoto 0:0e0631af0305 1053 double value; //!< Value at the node: a class label in case of classification or estimated
RyoheiHagimoto 0:0e0631af0305 1054 //!< function value in case of regression.
RyoheiHagimoto 0:0e0631af0305 1055 int classIdx; //!< Class index normalized to 0..class_count-1 range and assigned to the
RyoheiHagimoto 0:0e0631af0305 1056 //!< node. It is used internally in classification trees and tree ensembles.
RyoheiHagimoto 0:0e0631af0305 1057 int parent; //!< Index of the parent node
RyoheiHagimoto 0:0e0631af0305 1058 int left; //!< Index of the left child node
RyoheiHagimoto 0:0e0631af0305 1059 int right; //!< Index of right child node
RyoheiHagimoto 0:0e0631af0305 1060 int defaultDir; //!< Default direction where to go (-1: left or +1: right). It helps in the
RyoheiHagimoto 0:0e0631af0305 1061 //!< case of missing values.
RyoheiHagimoto 0:0e0631af0305 1062 int split; //!< Index of the first split
RyoheiHagimoto 0:0e0631af0305 1063 };
RyoheiHagimoto 0:0e0631af0305 1064
RyoheiHagimoto 0:0e0631af0305 1065 /** @brief The class represents split in a decision tree.
RyoheiHagimoto 0:0e0631af0305 1066 */
RyoheiHagimoto 0:0e0631af0305 1067 class CV_EXPORTS Split
RyoheiHagimoto 0:0e0631af0305 1068 {
RyoheiHagimoto 0:0e0631af0305 1069 public:
RyoheiHagimoto 0:0e0631af0305 1070 Split();
RyoheiHagimoto 0:0e0631af0305 1071 int varIdx; //!< Index of variable on which the split is created.
RyoheiHagimoto 0:0e0631af0305 1072 bool inversed; //!< If true, then the inverse split rule is used (i.e. left and right
RyoheiHagimoto 0:0e0631af0305 1073 //!< branches are exchanged in the rule expressions below).
RyoheiHagimoto 0:0e0631af0305 1074 float quality; //!< The split quality, a positive number. It is used to choose the best split.
RyoheiHagimoto 0:0e0631af0305 1075 int next; //!< Index of the next split in the list of splits for the node
RyoheiHagimoto 0:0e0631af0305 1076 float c; /**< The threshold value in case of split on an ordered variable.
RyoheiHagimoto 0:0e0631af0305 1077 The rule is:
RyoheiHagimoto 0:0e0631af0305 1078 @code{.none}
RyoheiHagimoto 0:0e0631af0305 1079 if var_value < c
RyoheiHagimoto 0:0e0631af0305 1080 then next_node <- left
RyoheiHagimoto 0:0e0631af0305 1081 else next_node <- right
RyoheiHagimoto 0:0e0631af0305 1082 @endcode */
RyoheiHagimoto 0:0e0631af0305 1083 int subsetOfs; /**< Offset of the bitset used by the split on a categorical variable.
RyoheiHagimoto 0:0e0631af0305 1084 The rule is:
RyoheiHagimoto 0:0e0631af0305 1085 @code{.none}
RyoheiHagimoto 0:0e0631af0305 1086 if bitset[var_value] == 1
RyoheiHagimoto 0:0e0631af0305 1087 then next_node <- left
RyoheiHagimoto 0:0e0631af0305 1088 else next_node <- right
RyoheiHagimoto 0:0e0631af0305 1089 @endcode */
RyoheiHagimoto 0:0e0631af0305 1090 };
RyoheiHagimoto 0:0e0631af0305 1091
RyoheiHagimoto 0:0e0631af0305 1092 /** @brief Returns indices of root nodes
RyoheiHagimoto 0:0e0631af0305 1093 */
RyoheiHagimoto 0:0e0631af0305 1094 virtual const std::vector<int>& getRoots() const = 0;
RyoheiHagimoto 0:0e0631af0305 1095 /** @brief Returns all the nodes
RyoheiHagimoto 0:0e0631af0305 1096
RyoheiHagimoto 0:0e0631af0305 1097 all the node indices are indices in the returned vector
RyoheiHagimoto 0:0e0631af0305 1098 */
RyoheiHagimoto 0:0e0631af0305 1099 virtual const std::vector<Node>& getNodes() const = 0;
RyoheiHagimoto 0:0e0631af0305 1100 /** @brief Returns all the splits
RyoheiHagimoto 0:0e0631af0305 1101
RyoheiHagimoto 0:0e0631af0305 1102 all the split indices are indices in the returned vector
RyoheiHagimoto 0:0e0631af0305 1103 */
RyoheiHagimoto 0:0e0631af0305 1104 virtual const std::vector<Split>& getSplits() const = 0;
RyoheiHagimoto 0:0e0631af0305 1105 /** @brief Returns all the bitsets for categorical splits
RyoheiHagimoto 0:0e0631af0305 1106
RyoheiHagimoto 0:0e0631af0305 1107 Split::subsetOfs is an offset in the returned vector
RyoheiHagimoto 0:0e0631af0305 1108 */
RyoheiHagimoto 0:0e0631af0305 1109 virtual const std::vector<int>& getSubsets() const = 0;
RyoheiHagimoto 0:0e0631af0305 1110
RyoheiHagimoto 0:0e0631af0305 1111 /** @brief Creates the empty model
RyoheiHagimoto 0:0e0631af0305 1112
RyoheiHagimoto 0:0e0631af0305 1113 The static method creates empty decision tree with the specified parameters. It should be then
RyoheiHagimoto 0:0e0631af0305 1114 trained using train method (see StatModel::train). Alternatively, you can load the model from
RyoheiHagimoto 0:0e0631af0305 1115 file using Algorithm::load\<DTrees\>(filename).
RyoheiHagimoto 0:0e0631af0305 1116 */
RyoheiHagimoto 0:0e0631af0305 1117 CV_WRAP static Ptr<DTrees> create();
RyoheiHagimoto 0:0e0631af0305 1118 };
RyoheiHagimoto 0:0e0631af0305 1119
RyoheiHagimoto 0:0e0631af0305 1120 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1121 * Random Trees Classifier *
RyoheiHagimoto 0:0e0631af0305 1122 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1123
RyoheiHagimoto 0:0e0631af0305 1124 /** @brief The class implements the random forest predictor.
RyoheiHagimoto 0:0e0631af0305 1125
RyoheiHagimoto 0:0e0631af0305 1126 @sa @ref ml_intro_rtrees
RyoheiHagimoto 0:0e0631af0305 1127 */
RyoheiHagimoto 0:0e0631af0305 1128 class CV_EXPORTS_W RTrees : public DTrees
RyoheiHagimoto 0:0e0631af0305 1129 {
RyoheiHagimoto 0:0e0631af0305 1130 public:
RyoheiHagimoto 0:0e0631af0305 1131
RyoheiHagimoto 0:0e0631af0305 1132 /** If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.
RyoheiHagimoto 0:0e0631af0305 1133 Default value is false.*/
RyoheiHagimoto 0:0e0631af0305 1134 /** @see setCalculateVarImportance */
RyoheiHagimoto 0:0e0631af0305 1135 CV_WRAP virtual bool getCalculateVarImportance() const = 0;
RyoheiHagimoto 0:0e0631af0305 1136 /** @copybrief getCalculateVarImportance @see getCalculateVarImportance */
RyoheiHagimoto 0:0e0631af0305 1137 CV_WRAP virtual void setCalculateVarImportance(bool val) = 0;
RyoheiHagimoto 0:0e0631af0305 1138
RyoheiHagimoto 0:0e0631af0305 1139 /** The size of the randomly selected subset of features at each tree node and that are used
RyoheiHagimoto 0:0e0631af0305 1140 to find the best split(s).
RyoheiHagimoto 0:0e0631af0305 1141 If you set it to 0 then the size will be set to the square root of the total number of
RyoheiHagimoto 0:0e0631af0305 1142 features. Default value is 0.*/
RyoheiHagimoto 0:0e0631af0305 1143 /** @see setActiveVarCount */
RyoheiHagimoto 0:0e0631af0305 1144 CV_WRAP virtual int getActiveVarCount() const = 0;
RyoheiHagimoto 0:0e0631af0305 1145 /** @copybrief getActiveVarCount @see getActiveVarCount */
RyoheiHagimoto 0:0e0631af0305 1146 CV_WRAP virtual void setActiveVarCount(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1147
RyoheiHagimoto 0:0e0631af0305 1148 /** The termination criteria that specifies when the training algorithm stops.
RyoheiHagimoto 0:0e0631af0305 1149 Either when the specified number of trees is trained and added to the ensemble or when
RyoheiHagimoto 0:0e0631af0305 1150 sufficient accuracy (measured as OOB error) is achieved. Typically the more trees you have the
RyoheiHagimoto 0:0e0631af0305 1151 better the accuracy. However, the improvement in accuracy generally diminishes and asymptotes
RyoheiHagimoto 0:0e0631af0305 1152 pass a certain number of trees. Also to keep in mind, the number of tree increases the
RyoheiHagimoto 0:0e0631af0305 1153 prediction time linearly. Default value is TermCriteria(TermCriteria::MAX_ITERS +
RyoheiHagimoto 0:0e0631af0305 1154 TermCriteria::EPS, 50, 0.1)*/
RyoheiHagimoto 0:0e0631af0305 1155 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1156 CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 1157 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1158 CV_WRAP virtual void setTermCriteria(const TermCriteria &val) = 0;
RyoheiHagimoto 0:0e0631af0305 1159
RyoheiHagimoto 0:0e0631af0305 1160 /** Returns the variable importance array.
RyoheiHagimoto 0:0e0631af0305 1161 The method returns the variable importance vector, computed at the training stage when
RyoheiHagimoto 0:0e0631af0305 1162 CalculateVarImportance is set to true. If this flag was set to false, the empty matrix is
RyoheiHagimoto 0:0e0631af0305 1163 returned.
RyoheiHagimoto 0:0e0631af0305 1164 */
RyoheiHagimoto 0:0e0631af0305 1165 CV_WRAP virtual Mat getVarImportance() const = 0;
RyoheiHagimoto 0:0e0631af0305 1166
RyoheiHagimoto 0:0e0631af0305 1167 /** Creates the empty model.
RyoheiHagimoto 0:0e0631af0305 1168 Use StatModel::train to train the model, StatModel::train to create and train the model,
RyoheiHagimoto 0:0e0631af0305 1169 Algorithm::load to load the pre-trained model.
RyoheiHagimoto 0:0e0631af0305 1170 */
RyoheiHagimoto 0:0e0631af0305 1171 CV_WRAP static Ptr<RTrees> create();
RyoheiHagimoto 0:0e0631af0305 1172 };
RyoheiHagimoto 0:0e0631af0305 1173
RyoheiHagimoto 0:0e0631af0305 1174 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1175 * Boosted tree classifier *
RyoheiHagimoto 0:0e0631af0305 1176 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1177
RyoheiHagimoto 0:0e0631af0305 1178 /** @brief Boosted tree classifier derived from DTrees
RyoheiHagimoto 0:0e0631af0305 1179
RyoheiHagimoto 0:0e0631af0305 1180 @sa @ref ml_intro_boost
RyoheiHagimoto 0:0e0631af0305 1181 */
RyoheiHagimoto 0:0e0631af0305 1182 class CV_EXPORTS_W Boost : public DTrees
RyoheiHagimoto 0:0e0631af0305 1183 {
RyoheiHagimoto 0:0e0631af0305 1184 public:
RyoheiHagimoto 0:0e0631af0305 1185 /** Type of the boosting algorithm.
RyoheiHagimoto 0:0e0631af0305 1186 See Boost::Types. Default value is Boost::REAL. */
RyoheiHagimoto 0:0e0631af0305 1187 /** @see setBoostType */
RyoheiHagimoto 0:0e0631af0305 1188 CV_WRAP virtual int getBoostType() const = 0;
RyoheiHagimoto 0:0e0631af0305 1189 /** @copybrief getBoostType @see getBoostType */
RyoheiHagimoto 0:0e0631af0305 1190 CV_WRAP virtual void setBoostType(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1191
RyoheiHagimoto 0:0e0631af0305 1192 /** The number of weak classifiers.
RyoheiHagimoto 0:0e0631af0305 1193 Default value is 100. */
RyoheiHagimoto 0:0e0631af0305 1194 /** @see setWeakCount */
RyoheiHagimoto 0:0e0631af0305 1195 CV_WRAP virtual int getWeakCount() const = 0;
RyoheiHagimoto 0:0e0631af0305 1196 /** @copybrief getWeakCount @see getWeakCount */
RyoheiHagimoto 0:0e0631af0305 1197 CV_WRAP virtual void setWeakCount(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1198
RyoheiHagimoto 0:0e0631af0305 1199 /** A threshold between 0 and 1 used to save computational time.
RyoheiHagimoto 0:0e0631af0305 1200 Samples with summary weight \f$\leq 1 - weight_trim_rate\f$ do not participate in the *next*
RyoheiHagimoto 0:0e0631af0305 1201 iteration of training. Set this parameter to 0 to turn off this functionality. Default value is 0.95.*/
RyoheiHagimoto 0:0e0631af0305 1202 /** @see setWeightTrimRate */
RyoheiHagimoto 0:0e0631af0305 1203 CV_WRAP virtual double getWeightTrimRate() const = 0;
RyoheiHagimoto 0:0e0631af0305 1204 /** @copybrief getWeightTrimRate @see getWeightTrimRate */
RyoheiHagimoto 0:0e0631af0305 1205 CV_WRAP virtual void setWeightTrimRate(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1206
RyoheiHagimoto 0:0e0631af0305 1207 /** Boosting type.
RyoheiHagimoto 0:0e0631af0305 1208 Gentle AdaBoost and Real AdaBoost are often the preferable choices. */
RyoheiHagimoto 0:0e0631af0305 1209 enum Types {
RyoheiHagimoto 0:0e0631af0305 1210 DISCRETE=0, //!< Discrete AdaBoost.
RyoheiHagimoto 0:0e0631af0305 1211 REAL=1, //!< Real AdaBoost. It is a technique that utilizes confidence-rated predictions
RyoheiHagimoto 0:0e0631af0305 1212 //!< and works well with categorical data.
RyoheiHagimoto 0:0e0631af0305 1213 LOGIT=2, //!< LogitBoost. It can produce good regression fits.
RyoheiHagimoto 0:0e0631af0305 1214 GENTLE=3 //!< Gentle AdaBoost. It puts less weight on outlier data points and for that
RyoheiHagimoto 0:0e0631af0305 1215 //!<reason is often good with regression data.
RyoheiHagimoto 0:0e0631af0305 1216 };
RyoheiHagimoto 0:0e0631af0305 1217
RyoheiHagimoto 0:0e0631af0305 1218 /** Creates the empty model.
RyoheiHagimoto 0:0e0631af0305 1219 Use StatModel::train to train the model, Algorithm::load\<Boost\>(filename) to load the pre-trained model. */
RyoheiHagimoto 0:0e0631af0305 1220 CV_WRAP static Ptr<Boost> create();
RyoheiHagimoto 0:0e0631af0305 1221 };
RyoheiHagimoto 0:0e0631af0305 1222
RyoheiHagimoto 0:0e0631af0305 1223 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1224 * Gradient Boosted Trees *
RyoheiHagimoto 0:0e0631af0305 1225 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1226
RyoheiHagimoto 0:0e0631af0305 1227 /*class CV_EXPORTS_W GBTrees : public DTrees
RyoheiHagimoto 0:0e0631af0305 1228 {
RyoheiHagimoto 0:0e0631af0305 1229 public:
RyoheiHagimoto 0:0e0631af0305 1230 struct CV_EXPORTS_W_MAP Params : public DTrees::Params
RyoheiHagimoto 0:0e0631af0305 1231 {
RyoheiHagimoto 0:0e0631af0305 1232 CV_PROP_RW int weakCount;
RyoheiHagimoto 0:0e0631af0305 1233 CV_PROP_RW int lossFunctionType;
RyoheiHagimoto 0:0e0631af0305 1234 CV_PROP_RW float subsamplePortion;
RyoheiHagimoto 0:0e0631af0305 1235 CV_PROP_RW float shrinkage;
RyoheiHagimoto 0:0e0631af0305 1236
RyoheiHagimoto 0:0e0631af0305 1237 Params();
RyoheiHagimoto 0:0e0631af0305 1238 Params( int lossFunctionType, int weakCount, float shrinkage,
RyoheiHagimoto 0:0e0631af0305 1239 float subsamplePortion, int maxDepth, bool useSurrogates );
RyoheiHagimoto 0:0e0631af0305 1240 };
RyoheiHagimoto 0:0e0631af0305 1241
RyoheiHagimoto 0:0e0631af0305 1242 enum {SQUARED_LOSS=0, ABSOLUTE_LOSS, HUBER_LOSS=3, DEVIANCE_LOSS};
RyoheiHagimoto 0:0e0631af0305 1243
RyoheiHagimoto 0:0e0631af0305 1244 virtual void setK(int k) = 0;
RyoheiHagimoto 0:0e0631af0305 1245
RyoheiHagimoto 0:0e0631af0305 1246 virtual float predictSerial( InputArray samples,
RyoheiHagimoto 0:0e0631af0305 1247 OutputArray weakResponses, int flags) const = 0;
RyoheiHagimoto 0:0e0631af0305 1248
RyoheiHagimoto 0:0e0631af0305 1249 static Ptr<GBTrees> create(const Params& p);
RyoheiHagimoto 0:0e0631af0305 1250 };*/
RyoheiHagimoto 0:0e0631af0305 1251
RyoheiHagimoto 0:0e0631af0305 1252 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1253 * Artificial Neural Networks (ANN) *
RyoheiHagimoto 0:0e0631af0305 1254 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1255
RyoheiHagimoto 0:0e0631af0305 1256 /////////////////////////////////// Multi-Layer Perceptrons //////////////////////////////
RyoheiHagimoto 0:0e0631af0305 1257
RyoheiHagimoto 0:0e0631af0305 1258 /** @brief Artificial Neural Networks - Multi-Layer Perceptrons.
RyoheiHagimoto 0:0e0631af0305 1259
RyoheiHagimoto 0:0e0631af0305 1260 Unlike many other models in ML that are constructed and trained at once, in the MLP model these
RyoheiHagimoto 0:0e0631af0305 1261 steps are separated. First, a network with the specified topology is created using the non-default
RyoheiHagimoto 0:0e0631af0305 1262 constructor or the method ANN_MLP::create. All the weights are set to zeros. Then, the network is
RyoheiHagimoto 0:0e0631af0305 1263 trained using a set of input and output vectors. The training procedure can be repeated more than
RyoheiHagimoto 0:0e0631af0305 1264 once, that is, the weights can be adjusted based on the new training data.
RyoheiHagimoto 0:0e0631af0305 1265
RyoheiHagimoto 0:0e0631af0305 1266 Additional flags for StatModel::train are available: ANN_MLP::TrainFlags.
RyoheiHagimoto 0:0e0631af0305 1267
RyoheiHagimoto 0:0e0631af0305 1268 @sa @ref ml_intro_ann
RyoheiHagimoto 0:0e0631af0305 1269 */
RyoheiHagimoto 0:0e0631af0305 1270 class CV_EXPORTS_W ANN_MLP : public StatModel
RyoheiHagimoto 0:0e0631af0305 1271 {
RyoheiHagimoto 0:0e0631af0305 1272 public:
RyoheiHagimoto 0:0e0631af0305 1273 /** Available training methods */
RyoheiHagimoto 0:0e0631af0305 1274 enum TrainingMethods {
RyoheiHagimoto 0:0e0631af0305 1275 BACKPROP=0, //!< The back-propagation algorithm.
RyoheiHagimoto 0:0e0631af0305 1276 RPROP=1 //!< The RPROP algorithm. See @cite RPROP93 for details.
RyoheiHagimoto 0:0e0631af0305 1277 };
RyoheiHagimoto 0:0e0631af0305 1278
RyoheiHagimoto 0:0e0631af0305 1279 /** Sets training method and common parameters.
RyoheiHagimoto 0:0e0631af0305 1280 @param method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
RyoheiHagimoto 0:0e0631af0305 1281 @param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP
RyoheiHagimoto 0:0e0631af0305 1282 @param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP.
RyoheiHagimoto 0:0e0631af0305 1283 */
RyoheiHagimoto 0:0e0631af0305 1284 CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;
RyoheiHagimoto 0:0e0631af0305 1285
RyoheiHagimoto 0:0e0631af0305 1286 /** Returns current training method */
RyoheiHagimoto 0:0e0631af0305 1287 CV_WRAP virtual int getTrainMethod() const = 0;
RyoheiHagimoto 0:0e0631af0305 1288
RyoheiHagimoto 0:0e0631af0305 1289 /** Initialize the activation function for each neuron.
RyoheiHagimoto 0:0e0631af0305 1290 Currently the default and the only fully supported activation function is ANN_MLP::SIGMOID_SYM.
RyoheiHagimoto 0:0e0631af0305 1291 @param type The type of activation function. See ANN_MLP::ActivationFunctions.
RyoheiHagimoto 0:0e0631af0305 1292 @param param1 The first parameter of the activation function, \f$\alpha\f$. Default value is 0.
RyoheiHagimoto 0:0e0631af0305 1293 @param param2 The second parameter of the activation function, \f$\beta\f$. Default value is 0.
RyoheiHagimoto 0:0e0631af0305 1294 */
RyoheiHagimoto 0:0e0631af0305 1295 CV_WRAP virtual void setActivationFunction(int type, double param1 = 0, double param2 = 0) = 0;
RyoheiHagimoto 0:0e0631af0305 1296
RyoheiHagimoto 0:0e0631af0305 1297 /** Integer vector specifying the number of neurons in each layer including the input and output layers.
RyoheiHagimoto 0:0e0631af0305 1298 The very first element specifies the number of elements in the input layer.
RyoheiHagimoto 0:0e0631af0305 1299 The last element - number of elements in the output layer. Default value is empty Mat.
RyoheiHagimoto 0:0e0631af0305 1300 @sa getLayerSizes */
RyoheiHagimoto 0:0e0631af0305 1301 CV_WRAP virtual void setLayerSizes(InputArray _layer_sizes) = 0;
RyoheiHagimoto 0:0e0631af0305 1302
RyoheiHagimoto 0:0e0631af0305 1303 /** Integer vector specifying the number of neurons in each layer including the input and output layers.
RyoheiHagimoto 0:0e0631af0305 1304 The very first element specifies the number of elements in the input layer.
RyoheiHagimoto 0:0e0631af0305 1305 The last element - number of elements in the output layer.
RyoheiHagimoto 0:0e0631af0305 1306 @sa setLayerSizes */
RyoheiHagimoto 0:0e0631af0305 1307 CV_WRAP virtual cv::Mat getLayerSizes() const = 0;
RyoheiHagimoto 0:0e0631af0305 1308
RyoheiHagimoto 0:0e0631af0305 1309 /** Termination criteria of the training algorithm.
RyoheiHagimoto 0:0e0631af0305 1310 You can specify the maximum number of iterations (maxCount) and/or how much the error could
RyoheiHagimoto 0:0e0631af0305 1311 change between the iterations to make the algorithm continue (epsilon). Default value is
RyoheiHagimoto 0:0e0631af0305 1312 TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, 0.01).*/
RyoheiHagimoto 0:0e0631af0305 1313 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1314 CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 1315 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1316 CV_WRAP virtual void setTermCriteria(TermCriteria val) = 0;
RyoheiHagimoto 0:0e0631af0305 1317
RyoheiHagimoto 0:0e0631af0305 1318 /** BPROP: Strength of the weight gradient term.
RyoheiHagimoto 0:0e0631af0305 1319 The recommended value is about 0.1. Default value is 0.1.*/
RyoheiHagimoto 0:0e0631af0305 1320 /** @see setBackpropWeightScale */
RyoheiHagimoto 0:0e0631af0305 1321 CV_WRAP virtual double getBackpropWeightScale() const = 0;
RyoheiHagimoto 0:0e0631af0305 1322 /** @copybrief getBackpropWeightScale @see getBackpropWeightScale */
RyoheiHagimoto 0:0e0631af0305 1323 CV_WRAP virtual void setBackpropWeightScale(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1324
RyoheiHagimoto 0:0e0631af0305 1325 /** BPROP: Strength of the momentum term (the difference between weights on the 2 previous iterations).
RyoheiHagimoto 0:0e0631af0305 1326 This parameter provides some inertia to smooth the random fluctuations of the weights. It can
RyoheiHagimoto 0:0e0631af0305 1327 vary from 0 (the feature is disabled) to 1 and beyond. The value 0.1 or so is good enough.
RyoheiHagimoto 0:0e0631af0305 1328 Default value is 0.1.*/
RyoheiHagimoto 0:0e0631af0305 1329 /** @see setBackpropMomentumScale */
RyoheiHagimoto 0:0e0631af0305 1330 CV_WRAP virtual double getBackpropMomentumScale() const = 0;
RyoheiHagimoto 0:0e0631af0305 1331 /** @copybrief getBackpropMomentumScale @see getBackpropMomentumScale */
RyoheiHagimoto 0:0e0631af0305 1332 CV_WRAP virtual void setBackpropMomentumScale(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1333
RyoheiHagimoto 0:0e0631af0305 1334 /** RPROP: Initial value \f$\Delta_0\f$ of update-values \f$\Delta_{ij}\f$.
RyoheiHagimoto 0:0e0631af0305 1335 Default value is 0.1.*/
RyoheiHagimoto 0:0e0631af0305 1336 /** @see setRpropDW0 */
RyoheiHagimoto 0:0e0631af0305 1337 CV_WRAP virtual double getRpropDW0() const = 0;
RyoheiHagimoto 0:0e0631af0305 1338 /** @copybrief getRpropDW0 @see getRpropDW0 */
RyoheiHagimoto 0:0e0631af0305 1339 CV_WRAP virtual void setRpropDW0(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1340
RyoheiHagimoto 0:0e0631af0305 1341 /** RPROP: Increase factor \f$\eta^+\f$.
RyoheiHagimoto 0:0e0631af0305 1342 It must be \>1. Default value is 1.2.*/
RyoheiHagimoto 0:0e0631af0305 1343 /** @see setRpropDWPlus */
RyoheiHagimoto 0:0e0631af0305 1344 CV_WRAP virtual double getRpropDWPlus() const = 0;
RyoheiHagimoto 0:0e0631af0305 1345 /** @copybrief getRpropDWPlus @see getRpropDWPlus */
RyoheiHagimoto 0:0e0631af0305 1346 CV_WRAP virtual void setRpropDWPlus(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1347
RyoheiHagimoto 0:0e0631af0305 1348 /** RPROP: Decrease factor \f$\eta^-\f$.
RyoheiHagimoto 0:0e0631af0305 1349 It must be \<1. Default value is 0.5.*/
RyoheiHagimoto 0:0e0631af0305 1350 /** @see setRpropDWMinus */
RyoheiHagimoto 0:0e0631af0305 1351 CV_WRAP virtual double getRpropDWMinus() const = 0;
RyoheiHagimoto 0:0e0631af0305 1352 /** @copybrief getRpropDWMinus @see getRpropDWMinus */
RyoheiHagimoto 0:0e0631af0305 1353 CV_WRAP virtual void setRpropDWMinus(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1354
RyoheiHagimoto 0:0e0631af0305 1355 /** RPROP: Update-values lower limit \f$\Delta_{min}\f$.
RyoheiHagimoto 0:0e0631af0305 1356 It must be positive. Default value is FLT_EPSILON.*/
RyoheiHagimoto 0:0e0631af0305 1357 /** @see setRpropDWMin */
RyoheiHagimoto 0:0e0631af0305 1358 CV_WRAP virtual double getRpropDWMin() const = 0;
RyoheiHagimoto 0:0e0631af0305 1359 /** @copybrief getRpropDWMin @see getRpropDWMin */
RyoheiHagimoto 0:0e0631af0305 1360 CV_WRAP virtual void setRpropDWMin(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1361
RyoheiHagimoto 0:0e0631af0305 1362 /** RPROP: Update-values upper limit \f$\Delta_{max}\f$.
RyoheiHagimoto 0:0e0631af0305 1363 It must be \>1. Default value is 50.*/
RyoheiHagimoto 0:0e0631af0305 1364 /** @see setRpropDWMax */
RyoheiHagimoto 0:0e0631af0305 1365 CV_WRAP virtual double getRpropDWMax() const = 0;
RyoheiHagimoto 0:0e0631af0305 1366 /** @copybrief getRpropDWMax @see getRpropDWMax */
RyoheiHagimoto 0:0e0631af0305 1367 CV_WRAP virtual void setRpropDWMax(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1368
RyoheiHagimoto 0:0e0631af0305 1369 /** possible activation functions */
RyoheiHagimoto 0:0e0631af0305 1370 enum ActivationFunctions {
RyoheiHagimoto 0:0e0631af0305 1371 /** Identity function: \f$f(x)=x\f$ */
RyoheiHagimoto 0:0e0631af0305 1372 IDENTITY = 0,
RyoheiHagimoto 0:0e0631af0305 1373 /** Symmetrical sigmoid: \f$f(x)=\beta*(1-e^{-\alpha x})/(1+e^{-\alpha x}\f$
RyoheiHagimoto 0:0e0631af0305 1374 @note
RyoheiHagimoto 0:0e0631af0305 1375 If you are using the default sigmoid activation function with the default parameter values
RyoheiHagimoto 0:0e0631af0305 1376 fparam1=0 and fparam2=0 then the function used is y = 1.7159\*tanh(2/3 \* x), so the output
RyoheiHagimoto 0:0e0631af0305 1377 will range from [-1.7159, 1.7159], instead of [0,1].*/
RyoheiHagimoto 0:0e0631af0305 1378 SIGMOID_SYM = 1,
RyoheiHagimoto 0:0e0631af0305 1379 /** Gaussian function: \f$f(x)=\beta e^{-\alpha x*x}\f$ */
RyoheiHagimoto 0:0e0631af0305 1380 GAUSSIAN = 2
RyoheiHagimoto 0:0e0631af0305 1381 };
RyoheiHagimoto 0:0e0631af0305 1382
RyoheiHagimoto 0:0e0631af0305 1383 /** Train options */
RyoheiHagimoto 0:0e0631af0305 1384 enum TrainFlags {
RyoheiHagimoto 0:0e0631af0305 1385 /** Update the network weights, rather than compute them from scratch. In the latter case
RyoheiHagimoto 0:0e0631af0305 1386 the weights are initialized using the Nguyen-Widrow algorithm. */
RyoheiHagimoto 0:0e0631af0305 1387 UPDATE_WEIGHTS = 1,
RyoheiHagimoto 0:0e0631af0305 1388 /** Do not normalize the input vectors. If this flag is not set, the training algorithm
RyoheiHagimoto 0:0e0631af0305 1389 normalizes each input feature independently, shifting its mean value to 0 and making the
RyoheiHagimoto 0:0e0631af0305 1390 standard deviation equal to 1. If the network is assumed to be updated frequently, the new
RyoheiHagimoto 0:0e0631af0305 1391 training data could be much different from original one. In this case, you should take care
RyoheiHagimoto 0:0e0631af0305 1392 of proper normalization. */
RyoheiHagimoto 0:0e0631af0305 1393 NO_INPUT_SCALE = 2,
RyoheiHagimoto 0:0e0631af0305 1394 /** Do not normalize the output vectors. If the flag is not set, the training algorithm
RyoheiHagimoto 0:0e0631af0305 1395 normalizes each output feature independently, by transforming it to the certain range
RyoheiHagimoto 0:0e0631af0305 1396 depending on the used activation function. */
RyoheiHagimoto 0:0e0631af0305 1397 NO_OUTPUT_SCALE = 4
RyoheiHagimoto 0:0e0631af0305 1398 };
RyoheiHagimoto 0:0e0631af0305 1399
RyoheiHagimoto 0:0e0631af0305 1400 CV_WRAP virtual Mat getWeights(int layerIdx) const = 0;
RyoheiHagimoto 0:0e0631af0305 1401
RyoheiHagimoto 0:0e0631af0305 1402 /** @brief Creates empty model
RyoheiHagimoto 0:0e0631af0305 1403
RyoheiHagimoto 0:0e0631af0305 1404 Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
RyoheiHagimoto 0:0e0631af0305 1405 Note that the train method has optional flags: ANN_MLP::TrainFlags.
RyoheiHagimoto 0:0e0631af0305 1406 */
RyoheiHagimoto 0:0e0631af0305 1407 CV_WRAP static Ptr<ANN_MLP> create();
RyoheiHagimoto 0:0e0631af0305 1408
RyoheiHagimoto 0:0e0631af0305 1409 /** @brief Loads and creates a serialized ANN from a file
RyoheiHagimoto 0:0e0631af0305 1410 *
RyoheiHagimoto 0:0e0631af0305 1411 * Use ANN::save to serialize and store an ANN to disk.
RyoheiHagimoto 0:0e0631af0305 1412 * Load the ANN from this file again, by calling this function with the path to the file.
RyoheiHagimoto 0:0e0631af0305 1413 *
RyoheiHagimoto 0:0e0631af0305 1414 * @param filepath path to serialized ANN
RyoheiHagimoto 0:0e0631af0305 1415 */
RyoheiHagimoto 0:0e0631af0305 1416 CV_WRAP static Ptr<ANN_MLP> load(const String& filepath);
RyoheiHagimoto 0:0e0631af0305 1417
RyoheiHagimoto 0:0e0631af0305 1418 };
RyoheiHagimoto 0:0e0631af0305 1419
RyoheiHagimoto 0:0e0631af0305 1420 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1421 * Logistic Regression *
RyoheiHagimoto 0:0e0631af0305 1422 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1423
RyoheiHagimoto 0:0e0631af0305 1424 /** @brief Implements Logistic Regression classifier.
RyoheiHagimoto 0:0e0631af0305 1425
RyoheiHagimoto 0:0e0631af0305 1426 @sa @ref ml_intro_lr
RyoheiHagimoto 0:0e0631af0305 1427 */
RyoheiHagimoto 0:0e0631af0305 1428 class CV_EXPORTS_W LogisticRegression : public StatModel
RyoheiHagimoto 0:0e0631af0305 1429 {
RyoheiHagimoto 0:0e0631af0305 1430 public:
RyoheiHagimoto 0:0e0631af0305 1431
RyoheiHagimoto 0:0e0631af0305 1432 /** Learning rate. */
RyoheiHagimoto 0:0e0631af0305 1433 /** @see setLearningRate */
RyoheiHagimoto 0:0e0631af0305 1434 CV_WRAP virtual double getLearningRate() const = 0;
RyoheiHagimoto 0:0e0631af0305 1435 /** @copybrief getLearningRate @see getLearningRate */
RyoheiHagimoto 0:0e0631af0305 1436 CV_WRAP virtual void setLearningRate(double val) = 0;
RyoheiHagimoto 0:0e0631af0305 1437
RyoheiHagimoto 0:0e0631af0305 1438 /** Number of iterations. */
RyoheiHagimoto 0:0e0631af0305 1439 /** @see setIterations */
RyoheiHagimoto 0:0e0631af0305 1440 CV_WRAP virtual int getIterations() const = 0;
RyoheiHagimoto 0:0e0631af0305 1441 /** @copybrief getIterations @see getIterations */
RyoheiHagimoto 0:0e0631af0305 1442 CV_WRAP virtual void setIterations(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1443
RyoheiHagimoto 0:0e0631af0305 1444 /** Kind of regularization to be applied. See LogisticRegression::RegKinds. */
RyoheiHagimoto 0:0e0631af0305 1445 /** @see setRegularization */
RyoheiHagimoto 0:0e0631af0305 1446 CV_WRAP virtual int getRegularization() const = 0;
RyoheiHagimoto 0:0e0631af0305 1447 /** @copybrief getRegularization @see getRegularization */
RyoheiHagimoto 0:0e0631af0305 1448 CV_WRAP virtual void setRegularization(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1449
RyoheiHagimoto 0:0e0631af0305 1450 /** Kind of training method used. See LogisticRegression::Methods. */
RyoheiHagimoto 0:0e0631af0305 1451 /** @see setTrainMethod */
RyoheiHagimoto 0:0e0631af0305 1452 CV_WRAP virtual int getTrainMethod() const = 0;
RyoheiHagimoto 0:0e0631af0305 1453 /** @copybrief getTrainMethod @see getTrainMethod */
RyoheiHagimoto 0:0e0631af0305 1454 CV_WRAP virtual void setTrainMethod(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1455
RyoheiHagimoto 0:0e0631af0305 1456 /** Specifies the number of training samples taken in each step of Mini-Batch Gradient
RyoheiHagimoto 0:0e0631af0305 1457 Descent. Will only be used if using LogisticRegression::MINI_BATCH training algorithm. It
RyoheiHagimoto 0:0e0631af0305 1458 has to take values less than the total number of training samples. */
RyoheiHagimoto 0:0e0631af0305 1459 /** @see setMiniBatchSize */
RyoheiHagimoto 0:0e0631af0305 1460 CV_WRAP virtual int getMiniBatchSize() const = 0;
RyoheiHagimoto 0:0e0631af0305 1461 /** @copybrief getMiniBatchSize @see getMiniBatchSize */
RyoheiHagimoto 0:0e0631af0305 1462 CV_WRAP virtual void setMiniBatchSize(int val) = 0;
RyoheiHagimoto 0:0e0631af0305 1463
RyoheiHagimoto 0:0e0631af0305 1464 /** Termination criteria of the algorithm. */
RyoheiHagimoto 0:0e0631af0305 1465 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1466 CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 1467 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1468 CV_WRAP virtual void setTermCriteria(TermCriteria val) = 0;
RyoheiHagimoto 0:0e0631af0305 1469
RyoheiHagimoto 0:0e0631af0305 1470 //! Regularization kinds
RyoheiHagimoto 0:0e0631af0305 1471 enum RegKinds {
RyoheiHagimoto 0:0e0631af0305 1472 REG_DISABLE = -1, //!< Regularization disabled
RyoheiHagimoto 0:0e0631af0305 1473 REG_L1 = 0, //!< %L1 norm
RyoheiHagimoto 0:0e0631af0305 1474 REG_L2 = 1 //!< %L2 norm
RyoheiHagimoto 0:0e0631af0305 1475 };
RyoheiHagimoto 0:0e0631af0305 1476
RyoheiHagimoto 0:0e0631af0305 1477 //! Training methods
RyoheiHagimoto 0:0e0631af0305 1478 enum Methods {
RyoheiHagimoto 0:0e0631af0305 1479 BATCH = 0,
RyoheiHagimoto 0:0e0631af0305 1480 MINI_BATCH = 1 //!< Set MiniBatchSize to a positive integer when using this method.
RyoheiHagimoto 0:0e0631af0305 1481 };
RyoheiHagimoto 0:0e0631af0305 1482
RyoheiHagimoto 0:0e0631af0305 1483 /** @brief Predicts responses for input samples and returns a float type.
RyoheiHagimoto 0:0e0631af0305 1484
RyoheiHagimoto 0:0e0631af0305 1485 @param samples The input data for the prediction algorithm. Matrix [m x n], where each row
RyoheiHagimoto 0:0e0631af0305 1486 contains variables (features) of one object being classified. Should have data type CV_32F.
RyoheiHagimoto 0:0e0631af0305 1487 @param results Predicted labels as a column matrix of type CV_32S.
RyoheiHagimoto 0:0e0631af0305 1488 @param flags Not used.
RyoheiHagimoto 0:0e0631af0305 1489 */
RyoheiHagimoto 0:0e0631af0305 1490 CV_WRAP virtual float predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const = 0;
RyoheiHagimoto 0:0e0631af0305 1491
RyoheiHagimoto 0:0e0631af0305 1492 /** @brief This function returns the trained paramters arranged across rows.
RyoheiHagimoto 0:0e0631af0305 1493
RyoheiHagimoto 0:0e0631af0305 1494 For a two class classifcation problem, it returns a row matrix. It returns learnt paramters of
RyoheiHagimoto 0:0e0631af0305 1495 the Logistic Regression as a matrix of type CV_32F.
RyoheiHagimoto 0:0e0631af0305 1496 */
RyoheiHagimoto 0:0e0631af0305 1497 CV_WRAP virtual Mat get_learnt_thetas() const = 0;
RyoheiHagimoto 0:0e0631af0305 1498
RyoheiHagimoto 0:0e0631af0305 1499 /** @brief Creates empty model.
RyoheiHagimoto 0:0e0631af0305 1500
RyoheiHagimoto 0:0e0631af0305 1501 Creates Logistic Regression model with parameters given.
RyoheiHagimoto 0:0e0631af0305 1502 */
RyoheiHagimoto 0:0e0631af0305 1503 CV_WRAP static Ptr<LogisticRegression> create();
RyoheiHagimoto 0:0e0631af0305 1504 };
RyoheiHagimoto 0:0e0631af0305 1505
RyoheiHagimoto 0:0e0631af0305 1506
RyoheiHagimoto 0:0e0631af0305 1507 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1508 * Stochastic Gradient Descent SVM Classifier *
RyoheiHagimoto 0:0e0631af0305 1509 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1510
RyoheiHagimoto 0:0e0631af0305 1511 /*!
RyoheiHagimoto 0:0e0631af0305 1512 @brief Stochastic Gradient Descent SVM classifier
RyoheiHagimoto 0:0e0631af0305 1513
RyoheiHagimoto 0:0e0631af0305 1514 SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach,
RyoheiHagimoto 0:0e0631af0305 1515 as presented in @cite bottou2010large.
RyoheiHagimoto 0:0e0631af0305 1516
RyoheiHagimoto 0:0e0631af0305 1517 The classifier has following parameters:
RyoheiHagimoto 0:0e0631af0305 1518 - model type,
RyoheiHagimoto 0:0e0631af0305 1519 - margin type,
RyoheiHagimoto 0:0e0631af0305 1520 - margin regularization (\f$\lambda\f$),
RyoheiHagimoto 0:0e0631af0305 1521 - initial step size (\f$\gamma_0\f$),
RyoheiHagimoto 0:0e0631af0305 1522 - step decreasing power (\f$c\f$),
RyoheiHagimoto 0:0e0631af0305 1523 - and termination criteria.
RyoheiHagimoto 0:0e0631af0305 1524
RyoheiHagimoto 0:0e0631af0305 1525 The model type may have one of the following values: \ref SGD and \ref ASGD.
RyoheiHagimoto 0:0e0631af0305 1526
RyoheiHagimoto 0:0e0631af0305 1527 - \ref SGD is the classic version of SVMSGD classifier: every next step is calculated by the formula
RyoheiHagimoto 0:0e0631af0305 1528 \f[w_{t+1} = w_t - \gamma(t) \frac{dQ_i}{dw} |_{w = w_t}\f]
RyoheiHagimoto 0:0e0631af0305 1529 where
RyoheiHagimoto 0:0e0631af0305 1530 - \f$w_t\f$ is the weights vector for decision function at step \f$t\f$,
RyoheiHagimoto 0:0e0631af0305 1531 - \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
RyoheiHagimoto 0:0e0631af0305 1532 \f$\gamma(t) = \gamma_0 (1 + \lambda \gamma_0 t) ^ {-c}\f$
RyoheiHagimoto 0:0e0631af0305 1533 - \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.
RyoheiHagimoto 0:0e0631af0305 1534
RyoheiHagimoto 0:0e0631af0305 1535 - \ref ASGD is Average Stochastic Gradient Descent SVM Classifier. ASGD classifier averages weights vector on each step of algorithm by the formula
RyoheiHagimoto 0:0e0631af0305 1536 \f$\widehat{w}_{t+1} = \frac{t}{1+t}\widehat{w}_{t} + \frac{1}{1+t}w_{t+1}\f$
RyoheiHagimoto 0:0e0631af0305 1537
RyoheiHagimoto 0:0e0631af0305 1538 The recommended model type is ASGD (following @cite bottou2010large).
RyoheiHagimoto 0:0e0631af0305 1539
RyoheiHagimoto 0:0e0631af0305 1540 The margin type may have one of the following values: \ref SOFT_MARGIN or \ref HARD_MARGIN.
RyoheiHagimoto 0:0e0631af0305 1541
RyoheiHagimoto 0:0e0631af0305 1542 - You should use \ref HARD_MARGIN type, if you have linearly separable sets.
RyoheiHagimoto 0:0e0631af0305 1543 - You should use \ref SOFT_MARGIN type, if you have non-linearly separable sets or sets with outliers.
RyoheiHagimoto 0:0e0631af0305 1544 - In the general case (if you know nothing about linear separability of your sets), use SOFT_MARGIN.
RyoheiHagimoto 0:0e0631af0305 1545
RyoheiHagimoto 0:0e0631af0305 1546 The other parameters may be described as follows:
RyoheiHagimoto 0:0e0631af0305 1547 - Margin regularization parameter is responsible for weights decreasing at each step and for the strength of restrictions on outliers
RyoheiHagimoto 0:0e0631af0305 1548 (the less the parameter, the less probability that an outlier will be ignored).
RyoheiHagimoto 0:0e0631af0305 1549 Recommended value for SGD model is 0.0001, for ASGD model is 0.00001.
RyoheiHagimoto 0:0e0631af0305 1550
RyoheiHagimoto 0:0e0631af0305 1551 - Initial step size parameter is the initial value for the step size \f$\gamma(t)\f$.
RyoheiHagimoto 0:0e0631af0305 1552 You will have to find the best initial step for your problem.
RyoheiHagimoto 0:0e0631af0305 1553
RyoheiHagimoto 0:0e0631af0305 1554 - Step decreasing power is the power parameter for \f$\gamma(t)\f$ decreasing by the formula, mentioned above.
RyoheiHagimoto 0:0e0631af0305 1555 Recommended value for SGD model is 1, for ASGD model is 0.75.
RyoheiHagimoto 0:0e0631af0305 1556
RyoheiHagimoto 0:0e0631af0305 1557 - Termination criteria can be TermCriteria::COUNT, TermCriteria::EPS or TermCriteria::COUNT + TermCriteria::EPS.
RyoheiHagimoto 0:0e0631af0305 1558 You will have to find the best termination criteria for your problem.
RyoheiHagimoto 0:0e0631af0305 1559
RyoheiHagimoto 0:0e0631af0305 1560 Note that the parameters margin regularization, initial step size, and step decreasing power should be positive.
RyoheiHagimoto 0:0e0631af0305 1561
RyoheiHagimoto 0:0e0631af0305 1562 To use SVMSGD algorithm do as follows:
RyoheiHagimoto 0:0e0631af0305 1563
RyoheiHagimoto 0:0e0631af0305 1564 - first, create the SVMSGD object. The algoorithm will set optimal parameters by default, but you can set your own parameters via functions setSvmsgdType(),
RyoheiHagimoto 0:0e0631af0305 1565 setMarginType(), setMarginRegularization(), setInitialStepSize(), and setStepDecreasingPower().
RyoheiHagimoto 0:0e0631af0305 1566
RyoheiHagimoto 0:0e0631af0305 1567 - then the SVM model can be trained using the train features and the correspondent labels by the method train().
RyoheiHagimoto 0:0e0631af0305 1568
RyoheiHagimoto 0:0e0631af0305 1569 - after that, the label of a new feature vector can be predicted using the method predict().
RyoheiHagimoto 0:0e0631af0305 1570
RyoheiHagimoto 0:0e0631af0305 1571 @code
RyoheiHagimoto 0:0e0631af0305 1572 // Create empty object
RyoheiHagimoto 0:0e0631af0305 1573 cv::Ptr<SVMSGD> svmsgd = SVMSGD::create();
RyoheiHagimoto 0:0e0631af0305 1574
RyoheiHagimoto 0:0e0631af0305 1575 // Train the Stochastic Gradient Descent SVM
RyoheiHagimoto 0:0e0631af0305 1576 svmsgd->train(trainData);
RyoheiHagimoto 0:0e0631af0305 1577
RyoheiHagimoto 0:0e0631af0305 1578 // Predict labels for the new samples
RyoheiHagimoto 0:0e0631af0305 1579 svmsgd->predict(samples, responses);
RyoheiHagimoto 0:0e0631af0305 1580 @endcode
RyoheiHagimoto 0:0e0631af0305 1581
RyoheiHagimoto 0:0e0631af0305 1582 */
RyoheiHagimoto 0:0e0631af0305 1583
RyoheiHagimoto 0:0e0631af0305 1584 class CV_EXPORTS_W SVMSGD : public cv::ml::StatModel
RyoheiHagimoto 0:0e0631af0305 1585 {
RyoheiHagimoto 0:0e0631af0305 1586 public:
RyoheiHagimoto 0:0e0631af0305 1587
RyoheiHagimoto 0:0e0631af0305 1588 /** SVMSGD type.
RyoheiHagimoto 0:0e0631af0305 1589 ASGD is often the preferable choice. */
RyoheiHagimoto 0:0e0631af0305 1590 enum SvmsgdType
RyoheiHagimoto 0:0e0631af0305 1591 {
RyoheiHagimoto 0:0e0631af0305 1592 SGD, //!< Stochastic Gradient Descent
RyoheiHagimoto 0:0e0631af0305 1593 ASGD //!< Average Stochastic Gradient Descent
RyoheiHagimoto 0:0e0631af0305 1594 };
RyoheiHagimoto 0:0e0631af0305 1595
RyoheiHagimoto 0:0e0631af0305 1596 /** Margin type.*/
RyoheiHagimoto 0:0e0631af0305 1597 enum MarginType
RyoheiHagimoto 0:0e0631af0305 1598 {
RyoheiHagimoto 0:0e0631af0305 1599 SOFT_MARGIN, //!< General case, suits to the case of non-linearly separable sets, allows outliers.
RyoheiHagimoto 0:0e0631af0305 1600 HARD_MARGIN //!< More accurate for the case of linearly separable sets.
RyoheiHagimoto 0:0e0631af0305 1601 };
RyoheiHagimoto 0:0e0631af0305 1602
RyoheiHagimoto 0:0e0631af0305 1603 /**
RyoheiHagimoto 0:0e0631af0305 1604 * @return the weights of the trained model (decision function f(x) = weights * x + shift).
RyoheiHagimoto 0:0e0631af0305 1605 */
RyoheiHagimoto 0:0e0631af0305 1606 CV_WRAP virtual Mat getWeights() = 0;
RyoheiHagimoto 0:0e0631af0305 1607
RyoheiHagimoto 0:0e0631af0305 1608 /**
RyoheiHagimoto 0:0e0631af0305 1609 * @return the shift of the trained model (decision function f(x) = weights * x + shift).
RyoheiHagimoto 0:0e0631af0305 1610 */
RyoheiHagimoto 0:0e0631af0305 1611 CV_WRAP virtual float getShift() = 0;
RyoheiHagimoto 0:0e0631af0305 1612
RyoheiHagimoto 0:0e0631af0305 1613 /** @brief Creates empty model.
RyoheiHagimoto 0:0e0631af0305 1614 * Use StatModel::train to train the model. Since %SVMSGD has several parameters, you may want to
RyoheiHagimoto 0:0e0631af0305 1615 * find the best parameters for your problem or use setOptimalParameters() to set some default parameters.
RyoheiHagimoto 0:0e0631af0305 1616 */
RyoheiHagimoto 0:0e0631af0305 1617 CV_WRAP static Ptr<SVMSGD> create();
RyoheiHagimoto 0:0e0631af0305 1618
RyoheiHagimoto 0:0e0631af0305 1619 /** @brief Function sets optimal parameters values for chosen SVM SGD model.
RyoheiHagimoto 0:0e0631af0305 1620 * @param svmsgdType is the type of SVMSGD classifier.
RyoheiHagimoto 0:0e0631af0305 1621 * @param marginType is the type of margin constraint.
RyoheiHagimoto 0:0e0631af0305 1622 */
RyoheiHagimoto 0:0e0631af0305 1623 CV_WRAP virtual void setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN) = 0;
RyoheiHagimoto 0:0e0631af0305 1624
RyoheiHagimoto 0:0e0631af0305 1625 /** @brief %Algorithm type, one of SVMSGD::SvmsgdType. */
RyoheiHagimoto 0:0e0631af0305 1626 /** @see setSvmsgdType */
RyoheiHagimoto 0:0e0631af0305 1627 CV_WRAP virtual int getSvmsgdType() const = 0;
RyoheiHagimoto 0:0e0631af0305 1628 /** @copybrief getSvmsgdType @see getSvmsgdType */
RyoheiHagimoto 0:0e0631af0305 1629 CV_WRAP virtual void setSvmsgdType(int svmsgdType) = 0;
RyoheiHagimoto 0:0e0631af0305 1630
RyoheiHagimoto 0:0e0631af0305 1631 /** @brief %Margin type, one of SVMSGD::MarginType. */
RyoheiHagimoto 0:0e0631af0305 1632 /** @see setMarginType */
RyoheiHagimoto 0:0e0631af0305 1633 CV_WRAP virtual int getMarginType() const = 0;
RyoheiHagimoto 0:0e0631af0305 1634 /** @copybrief getMarginType @see getMarginType */
RyoheiHagimoto 0:0e0631af0305 1635 CV_WRAP virtual void setMarginType(int marginType) = 0;
RyoheiHagimoto 0:0e0631af0305 1636
RyoheiHagimoto 0:0e0631af0305 1637 /** @brief Parameter marginRegularization of a %SVMSGD optimization problem. */
RyoheiHagimoto 0:0e0631af0305 1638 /** @see setMarginRegularization */
RyoheiHagimoto 0:0e0631af0305 1639 CV_WRAP virtual float getMarginRegularization() const = 0;
RyoheiHagimoto 0:0e0631af0305 1640 /** @copybrief getMarginRegularization @see getMarginRegularization */
RyoheiHagimoto 0:0e0631af0305 1641 CV_WRAP virtual void setMarginRegularization(float marginRegularization) = 0;
RyoheiHagimoto 0:0e0631af0305 1642
RyoheiHagimoto 0:0e0631af0305 1643 /** @brief Parameter initialStepSize of a %SVMSGD optimization problem. */
RyoheiHagimoto 0:0e0631af0305 1644 /** @see setInitialStepSize */
RyoheiHagimoto 0:0e0631af0305 1645 CV_WRAP virtual float getInitialStepSize() const = 0;
RyoheiHagimoto 0:0e0631af0305 1646 /** @copybrief getInitialStepSize @see getInitialStepSize */
RyoheiHagimoto 0:0e0631af0305 1647 CV_WRAP virtual void setInitialStepSize(float InitialStepSize) = 0;
RyoheiHagimoto 0:0e0631af0305 1648
RyoheiHagimoto 0:0e0631af0305 1649 /** @brief Parameter stepDecreasingPower of a %SVMSGD optimization problem. */
RyoheiHagimoto 0:0e0631af0305 1650 /** @see setStepDecreasingPower */
RyoheiHagimoto 0:0e0631af0305 1651 CV_WRAP virtual float getStepDecreasingPower() const = 0;
RyoheiHagimoto 0:0e0631af0305 1652 /** @copybrief getStepDecreasingPower @see getStepDecreasingPower */
RyoheiHagimoto 0:0e0631af0305 1653 CV_WRAP virtual void setStepDecreasingPower(float stepDecreasingPower) = 0;
RyoheiHagimoto 0:0e0631af0305 1654
RyoheiHagimoto 0:0e0631af0305 1655 /** @brief Termination criteria of the training algorithm.
RyoheiHagimoto 0:0e0631af0305 1656 You can specify the maximum number of iterations (maxCount) and/or how much the error could
RyoheiHagimoto 0:0e0631af0305 1657 change between the iterations to make the algorithm continue (epsilon).*/
RyoheiHagimoto 0:0e0631af0305 1658 /** @see setTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1659 CV_WRAP virtual TermCriteria getTermCriteria() const = 0;
RyoheiHagimoto 0:0e0631af0305 1660 /** @copybrief getTermCriteria @see getTermCriteria */
RyoheiHagimoto 0:0e0631af0305 1661 CV_WRAP virtual void setTermCriteria(const cv::TermCriteria &val) = 0;
RyoheiHagimoto 0:0e0631af0305 1662 };
RyoheiHagimoto 0:0e0631af0305 1663
RyoheiHagimoto 0:0e0631af0305 1664
RyoheiHagimoto 0:0e0631af0305 1665 /****************************************************************************************\
RyoheiHagimoto 0:0e0631af0305 1666 * Auxilary functions declarations *
RyoheiHagimoto 0:0e0631af0305 1667 \****************************************************************************************/
RyoheiHagimoto 0:0e0631af0305 1668
RyoheiHagimoto 0:0e0631af0305 1669 /** @brief Generates _sample_ from multivariate normal distribution
RyoheiHagimoto 0:0e0631af0305 1670
RyoheiHagimoto 0:0e0631af0305 1671 @param mean an average row vector
RyoheiHagimoto 0:0e0631af0305 1672 @param cov symmetric covariation matrix
RyoheiHagimoto 0:0e0631af0305 1673 @param nsamples returned samples count
RyoheiHagimoto 0:0e0631af0305 1674 @param samples returned samples array
RyoheiHagimoto 0:0e0631af0305 1675 */
RyoheiHagimoto 0:0e0631af0305 1676 CV_EXPORTS void randMVNormal( InputArray mean, InputArray cov, int nsamples, OutputArray samples);
RyoheiHagimoto 0:0e0631af0305 1677
RyoheiHagimoto 0:0e0631af0305 1678 /** @brief Creates test set */
RyoheiHagimoto 0:0e0631af0305 1679 CV_EXPORTS void createConcentricSpheresTestSet( int nsamples, int nfeatures, int nclasses,
RyoheiHagimoto 0:0e0631af0305 1680 OutputArray samples, OutputArray responses);
RyoheiHagimoto 0:0e0631af0305 1681
RyoheiHagimoto 0:0e0631af0305 1682 //! @} ml
RyoheiHagimoto 0:0e0631af0305 1683
RyoheiHagimoto 0:0e0631af0305 1684 }
RyoheiHagimoto 0:0e0631af0305 1685 }
RyoheiHagimoto 0:0e0631af0305 1686
RyoheiHagimoto 0:0e0631af0305 1687 #endif // __cplusplus
RyoheiHagimoto 0:0e0631af0305 1688 #endif // OPENCV_ML_HPP
RyoheiHagimoto 0:0e0631af0305 1689
RyoheiHagimoto 0:0e0631af0305 1690 /* End of file. */