mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Normalize line endings and whitespace
This commit is contained in:
committed by
Andrey Kamaev
parent
69020da607
commit
04384a71e4
@@ -1,272 +1,272 @@
|
||||
.. _Gradient Boosted Trees:
|
||||
|
||||
Gradient Boosted Trees
|
||||
======================
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
Gradient Boosted Trees (GBT) is a generalized boosting algorithm introduced by
|
||||
Jerome Friedman: http://www.salfordsystems.com/doc/GreedyFuncApproxSS.pdf .
|
||||
In contrast to the AdaBoost.M1 algorithm, GBT can deal with both multiclass
|
||||
classification and regression problems. Moreover, it can use any
|
||||
differential loss function, some popular ones are implemented.
|
||||
Decision trees (:ocv:class:`CvDTree`) usage as base learners allows to process ordered
|
||||
and categorical variables.
|
||||
|
||||
.. _Training GBT:
|
||||
|
||||
Training the GBT model
|
||||
----------------------
|
||||
|
||||
Gradient Boosted Trees model represents an ensemble of single regression trees
|
||||
built in a greedy fashion. Training procedure is an iterative process
|
||||
similar to the numerical optimization via the gradient descent method. Summary loss
|
||||
on the training set depends only on the current model predictions for the
|
||||
training samples, in other words
|
||||
:math:`\sum^N_{i=1}L(y_i, F(x_i)) \equiv \mathcal{L}(F(x_1), F(x_2), ... , F(x_N))
|
||||
\equiv \mathcal{L}(F)`. And the :math:`\mathcal{L}(F)`
|
||||
gradient can be computed as follows:
|
||||
|
||||
.. math::
|
||||
grad(\mathcal{L}(F)) = \left( \dfrac{\partial{L(y_1, F(x_1))}}{\partial{F(x_1)}},
|
||||
\dfrac{\partial{L(y_2, F(x_2))}}{\partial{F(x_2)}}, ... ,
|
||||
\dfrac{\partial{L(y_N, F(x_N))}}{\partial{F(x_N)}} \right) .
|
||||
|
||||
At every training step, a single regression tree is built to predict an
|
||||
antigradient vector components. Step length is computed corresponding to the
|
||||
loss function and separately for every region determined by the tree leaf. It
|
||||
can be eliminated by changing values of the leaves directly.
|
||||
|
||||
See below the main scheme of the training process:
|
||||
|
||||
#.
|
||||
Find the best constant model.
|
||||
#.
|
||||
For :math:`i` in :math:`[1,M]`:
|
||||
|
||||
#.
|
||||
Compute the antigradient.
|
||||
#.
|
||||
Grow a regression tree to predict antigradient components.
|
||||
#.
|
||||
Change values in the tree leaves.
|
||||
#.
|
||||
Add the tree to the model.
|
||||
|
||||
|
||||
The following loss functions are implemented for regression problems:
|
||||
|
||||
*
|
||||
Squared loss (``CvGBTrees::SQUARED_LOSS``):
|
||||
:math:`L(y,f(x))=\dfrac{1}{2}(y-f(x))^2`
|
||||
*
|
||||
Absolute loss (``CvGBTrees::ABSOLUTE_LOSS``):
|
||||
:math:`L(y,f(x))=|y-f(x)|`
|
||||
*
|
||||
Huber loss (``CvGBTrees::HUBER_LOSS``):
|
||||
:math:`L(y,f(x)) = \left\{ \begin{array}{lr}
|
||||
\delta\cdot\left(|y-f(x)|-\dfrac{\delta}{2}\right) & : |y-f(x)|>\delta\\
|
||||
\dfrac{1}{2}\cdot(y-f(x))^2 & : |y-f(x)|\leq\delta \end{array} \right.`,
|
||||
|
||||
where :math:`\delta` is the :math:`\alpha`-quantile estimation of the
|
||||
:math:`|y-f(x)|`. In the current implementation :math:`\alpha=0.2`.
|
||||
|
||||
|
||||
The following loss functions are implemented for classification problems:
|
||||
|
||||
*
|
||||
Deviance or cross-entropy loss (``CvGBTrees::DEVIANCE_LOSS``):
|
||||
:math:`K` functions are built, one function for each output class, and
|
||||
:math:`L(y,f_1(x),...,f_K(x)) = -\sum^K_{k=0}1(y=k)\ln{p_k(x)}`,
|
||||
where :math:`p_k(x)=\dfrac{\exp{f_k(x)}}{\sum^K_{i=1}\exp{f_i(x)}}`
|
||||
is the estimation of the probability of :math:`y=k`.
|
||||
|
||||
As a result, you get the following model:
|
||||
|
||||
.. math:: f(x) = f_0 + \nu\cdot\sum^M_{i=1}T_i(x) ,
|
||||
|
||||
where :math:`f_0` is the initial guess (the best constant model) and :math:`\nu`
|
||||
is a regularization parameter from the interval :math:`(0,1]`, further called
|
||||
*shrinkage*.
|
||||
|
||||
.. _Predicting with GBT:
|
||||
|
||||
Predicting with the GBT Model
|
||||
-----------------------------
|
||||
|
||||
To get the GBT model prediction, you need to compute the sum of responses of
|
||||
all the trees in the ensemble. For regression problems, it is the answer.
|
||||
For classification problems, the result is :math:`\arg\max_{i=1..K}(f_i(x))`.
|
||||
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
|
||||
CvGBTreesParams
|
||||
---------------
|
||||
.. ocv:struct:: CvGBTreesParams : public CvDTreeParams
|
||||
|
||||
GBT training parameters.
|
||||
|
||||
The structure contains parameters for each single decision tree in the ensemble,
|
||||
as well as the whole model characteristics. The structure is derived from
|
||||
:ocv:class:`CvDTreeParams` but not all of the decision tree parameters are supported:
|
||||
cross-validation, pruning, and class priorities are not used.
|
||||
|
||||
CvGBTreesParams::CvGBTreesParams
|
||||
--------------------------------
|
||||
.. ocv:function:: CvGBTreesParams::CvGBTreesParams()
|
||||
|
||||
.. ocv:function:: CvGBTreesParams::CvGBTreesParams( int loss_function_type, int weak_count, float shrinkage, float subsample_portion, int max_depth, bool use_surrogates )
|
||||
|
||||
:param loss_function_type: Type of the loss function used for training
|
||||
(see :ref:`Training GBT`). It must be one of the
|
||||
following types: ``CvGBTrees::SQUARED_LOSS``, ``CvGBTrees::ABSOLUTE_LOSS``,
|
||||
``CvGBTrees::HUBER_LOSS``, ``CvGBTrees::DEVIANCE_LOSS``. The first three
|
||||
types are used for regression problems, and the last one for
|
||||
classification.
|
||||
|
||||
:param weak_count: Count of boosting algorithm iterations. ``weak_count*K`` is the total
|
||||
count of trees in the GBT model, where ``K`` is the output classes count
|
||||
(equal to one in case of a regression).
|
||||
|
||||
:param shrinkage: Regularization parameter (see :ref:`Training GBT`).
|
||||
|
||||
:param subsample_portion: Portion of the whole training set used for each algorithm iteration.
|
||||
Subset is generated randomly. For more information see
|
||||
http://www.salfordsystems.com/doc/StochasticBoostingSS.pdf.
|
||||
|
||||
:param max_depth: Maximal depth of each decision tree in the ensemble (see :ocv:class:`CvDTree`).
|
||||
|
||||
:param use_surrogates: If ``true``, surrogate splits are built (see :ocv:class:`CvDTree`).
|
||||
|
||||
By default the following constructor is used:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
CvGBTreesParams(CvGBTrees::SQUARED_LOSS, 200, 0.8f, 0.01f, 3, false)
|
||||
: CvDTreeParams( 3, 10, 0, false, 10, 0, false, false, 0 )
|
||||
|
||||
CvGBTrees
|
||||
---------
|
||||
.. ocv:class:: CvGBTrees : public CvStatModel
|
||||
|
||||
The class implements the Gradient boosted tree model as described in the beginning of this section.
|
||||
|
||||
CvGBTrees::CvGBTrees
|
||||
--------------------
|
||||
Default and training constructors.
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees()
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees( const Mat& trainData, int tflag, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), const Mat& varType=Mat(), const Mat& missingDataMask=Mat(), CvGBTreesParams params=CvGBTreesParams() )
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees( const CvMat* trainData, int tflag, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, const CvMat* varType=0, const CvMat* missingDataMask=0, CvGBTreesParams params=CvGBTreesParams() )
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees([trainData, tflag, responses[, varIdx[, sampleIdx[, varType[, missingDataMask[, params]]]]]]) -> <GBTrees object>
|
||||
|
||||
The constructors follow conventions of :ocv:func:`CvStatModel::CvStatModel`. See :ocv:func:`CvStatModel::train` for parameters descriptions.
|
||||
|
||||
CvGBTrees::train
|
||||
----------------
|
||||
Trains a Gradient boosted tree model.
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train(const Mat& trainData, int tflag, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), const Mat& varType=Mat(), const Mat& missingDataMask=Mat(), CvGBTreesParams params=CvGBTreesParams(), bool update=false)
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train( const CvMat* trainData, int tflag, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, const CvMat* varType=0, const CvMat* missingDataMask=0, CvGBTreesParams params=CvGBTreesParams(), bool update=false )
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train(CvMLData* data, CvGBTreesParams params=CvGBTreesParams(), bool update=false)
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.train(trainData, tflag, responses[, varIdx[, sampleIdx[, varType[, missingDataMask[, params[, update]]]]]]) -> retval
|
||||
|
||||
The first train method follows the common template (see :ocv:func:`CvStatModel::train`).
|
||||
Both ``tflag`` values (``CV_ROW_SAMPLE``, ``CV_COL_SAMPLE``) are supported.
|
||||
``trainData`` must be of the ``CV_32F`` type. ``responses`` must be a matrix of type
|
||||
``CV_32S`` or ``CV_32F``. In both cases it is converted into the ``CV_32F``
|
||||
matrix inside the training procedure. ``varIdx`` and ``sampleIdx`` must be a
|
||||
list of indices (``CV_32S``) or a mask (``CV_8U`` or ``CV_8S``). ``update`` is
|
||||
a dummy parameter.
|
||||
|
||||
The second form of :ocv:func:`CvGBTrees::train` function uses :ocv:class:`CvMLData` as a
|
||||
data set container. ``update`` is still a dummy parameter.
|
||||
|
||||
All parameters specific to the GBT model are passed into the training function
|
||||
as a :ocv:class:`CvGBTreesParams` structure.
|
||||
|
||||
|
||||
CvGBTrees::predict
|
||||
------------------
|
||||
Predicts a response for an input sample.
|
||||
|
||||
.. ocv:function:: float CvGBTrees::predict(const Mat& sample, const Mat& missing=Mat(), const Range& slice = Range::all(), int k=-1) const
|
||||
|
||||
.. ocv:function:: float CvGBTrees::predict( const CvMat* sample, const CvMat* missing=0, CvMat* weakResponses=0, CvSlice slice = CV_WHOLE_SEQ, int k=-1 ) const
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.predict(sample[, missing[, slice[, k]]]) -> retval
|
||||
|
||||
:param sample: Input feature vector that has the same format as every training set
|
||||
element. If not all the variables were actually used during training,
|
||||
``sample`` contains forged values at the appropriate places.
|
||||
|
||||
:param missing: Missing values mask, which is a dimensional matrix of the same size as
|
||||
``sample`` having the ``CV_8U`` type. ``1`` corresponds to the missing value
|
||||
in the same position in the ``sample`` vector. If there are no missing values
|
||||
in the feature vector, an empty matrix can be passed instead of the missing mask.
|
||||
|
||||
:param weakResponses: Matrix used to obtain predictions of all the trees.
|
||||
The matrix has :math:`K` rows,
|
||||
where :math:`K` is the count of output classes (1 for the regression case).
|
||||
The matrix has as many columns as the ``slice`` length.
|
||||
|
||||
:param slice: Parameter defining the part of the ensemble used for prediction.
|
||||
If ``slice = Range::all()``, all trees are used. Use this parameter to
|
||||
get predictions of the GBT models with different ensemble sizes learning
|
||||
only one model.
|
||||
|
||||
:param k: Number of tree ensembles built in case of the classification problem
|
||||
(see :ref:`Training GBT`). Use this
|
||||
parameter to change the output to sum of the trees' predictions in the
|
||||
``k``-th ensemble only. To get the total GBT model prediction, ``k`` value
|
||||
must be -1. For regression problems, ``k`` is also equal to -1.
|
||||
|
||||
The method predicts the response corresponding to the given sample
|
||||
(see :ref:`Predicting with GBT`).
|
||||
The result is either the class label or the estimated function value. The
|
||||
:ocv:func:`CvGBTrees::predict` method enables using the parallel version of the GBT model
|
||||
prediction if the OpenCV is built with the TBB library. In this case, predictions
|
||||
of single trees are computed in a parallel fashion.
|
||||
|
||||
|
||||
CvGBTrees::clear
|
||||
----------------
|
||||
Clears the model.
|
||||
|
||||
.. ocv:function:: void CvGBTrees::clear()
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.clear() -> None
|
||||
|
||||
The function deletes the data set information and all the weak models and sets all internal
|
||||
variables to the initial state. The function is called in :ocv:func:`CvGBTrees::train` and in the
|
||||
destructor.
|
||||
|
||||
|
||||
CvGBTrees::calc_error
|
||||
---------------------
|
||||
Calculates a training or testing error.
|
||||
|
||||
.. ocv:function:: float CvGBTrees::calc_error( CvMLData* _data, int type, std::vector<float> *resp = 0 )
|
||||
|
||||
:param _data: Data set.
|
||||
|
||||
:param type: Parameter defining the error that should be computed: train (``CV_TRAIN_ERROR``) or test
|
||||
(``CV_TEST_ERROR``).
|
||||
|
||||
:param resp: If non-zero, a vector of predictions on the corresponding data set is
|
||||
returned.
|
||||
|
||||
If the :ocv:class:`CvMLData` data is used to store the data set, :ocv:func:`CvGBTrees::calc_error` can be
|
||||
used to get a training/testing error easily and (optionally) all predictions
|
||||
on the training/testing set. If the Intel* TBB* library is used, the error is computed in a
|
||||
parallel way, namely, predictions for different samples are computed at the same time.
|
||||
In case of a regression problem, a mean squared error is returned. For
|
||||
classifications, the result is a misclassification error in percent.
|
||||
.. _Gradient Boosted Trees:
|
||||
|
||||
Gradient Boosted Trees
|
||||
======================
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
Gradient Boosted Trees (GBT) is a generalized boosting algorithm introduced by
|
||||
Jerome Friedman: http://www.salfordsystems.com/doc/GreedyFuncApproxSS.pdf .
|
||||
In contrast to the AdaBoost.M1 algorithm, GBT can deal with both multiclass
|
||||
classification and regression problems. Moreover, it can use any
|
||||
differential loss function, some popular ones are implemented.
|
||||
Decision trees (:ocv:class:`CvDTree`) usage as base learners allows to process ordered
|
||||
and categorical variables.
|
||||
|
||||
.. _Training GBT:
|
||||
|
||||
Training the GBT model
|
||||
----------------------
|
||||
|
||||
Gradient Boosted Trees model represents an ensemble of single regression trees
|
||||
built in a greedy fashion. Training procedure is an iterative process
|
||||
similar to the numerical optimization via the gradient descent method. Summary loss
|
||||
on the training set depends only on the current model predictions for the
|
||||
training samples, in other words
|
||||
:math:`\sum^N_{i=1}L(y_i, F(x_i)) \equiv \mathcal{L}(F(x_1), F(x_2), ... , F(x_N))
|
||||
\equiv \mathcal{L}(F)`. And the :math:`\mathcal{L}(F)`
|
||||
gradient can be computed as follows:
|
||||
|
||||
.. math::
|
||||
grad(\mathcal{L}(F)) = \left( \dfrac{\partial{L(y_1, F(x_1))}}{\partial{F(x_1)}},
|
||||
\dfrac{\partial{L(y_2, F(x_2))}}{\partial{F(x_2)}}, ... ,
|
||||
\dfrac{\partial{L(y_N, F(x_N))}}{\partial{F(x_N)}} \right) .
|
||||
|
||||
At every training step, a single regression tree is built to predict an
|
||||
antigradient vector components. Step length is computed corresponding to the
|
||||
loss function and separately for every region determined by the tree leaf. It
|
||||
can be eliminated by changing values of the leaves directly.
|
||||
|
||||
See below the main scheme of the training process:
|
||||
|
||||
#.
|
||||
Find the best constant model.
|
||||
#.
|
||||
For :math:`i` in :math:`[1,M]`:
|
||||
|
||||
#.
|
||||
Compute the antigradient.
|
||||
#.
|
||||
Grow a regression tree to predict antigradient components.
|
||||
#.
|
||||
Change values in the tree leaves.
|
||||
#.
|
||||
Add the tree to the model.
|
||||
|
||||
|
||||
The following loss functions are implemented for regression problems:
|
||||
|
||||
*
|
||||
Squared loss (``CvGBTrees::SQUARED_LOSS``):
|
||||
:math:`L(y,f(x))=\dfrac{1}{2}(y-f(x))^2`
|
||||
*
|
||||
Absolute loss (``CvGBTrees::ABSOLUTE_LOSS``):
|
||||
:math:`L(y,f(x))=|y-f(x)|`
|
||||
*
|
||||
Huber loss (``CvGBTrees::HUBER_LOSS``):
|
||||
:math:`L(y,f(x)) = \left\{ \begin{array}{lr}
|
||||
\delta\cdot\left(|y-f(x)|-\dfrac{\delta}{2}\right) & : |y-f(x)|>\delta\\
|
||||
\dfrac{1}{2}\cdot(y-f(x))^2 & : |y-f(x)|\leq\delta \end{array} \right.`,
|
||||
|
||||
where :math:`\delta` is the :math:`\alpha`-quantile estimation of the
|
||||
:math:`|y-f(x)|`. In the current implementation :math:`\alpha=0.2`.
|
||||
|
||||
|
||||
The following loss functions are implemented for classification problems:
|
||||
|
||||
*
|
||||
Deviance or cross-entropy loss (``CvGBTrees::DEVIANCE_LOSS``):
|
||||
:math:`K` functions are built, one function for each output class, and
|
||||
:math:`L(y,f_1(x),...,f_K(x)) = -\sum^K_{k=0}1(y=k)\ln{p_k(x)}`,
|
||||
where :math:`p_k(x)=\dfrac{\exp{f_k(x)}}{\sum^K_{i=1}\exp{f_i(x)}}`
|
||||
is the estimation of the probability of :math:`y=k`.
|
||||
|
||||
As a result, you get the following model:
|
||||
|
||||
.. math:: f(x) = f_0 + \nu\cdot\sum^M_{i=1}T_i(x) ,
|
||||
|
||||
where :math:`f_0` is the initial guess (the best constant model) and :math:`\nu`
|
||||
is a regularization parameter from the interval :math:`(0,1]`, further called
|
||||
*shrinkage*.
|
||||
|
||||
.. _Predicting with GBT:
|
||||
|
||||
Predicting with the GBT Model
|
||||
-----------------------------
|
||||
|
||||
To get the GBT model prediction, you need to compute the sum of responses of
|
||||
all the trees in the ensemble. For regression problems, it is the answer.
|
||||
For classification problems, the result is :math:`\arg\max_{i=1..K}(f_i(x))`.
|
||||
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
|
||||
CvGBTreesParams
|
||||
---------------
|
||||
.. ocv:struct:: CvGBTreesParams : public CvDTreeParams
|
||||
|
||||
GBT training parameters.
|
||||
|
||||
The structure contains parameters for each single decision tree in the ensemble,
|
||||
as well as the whole model characteristics. The structure is derived from
|
||||
:ocv:class:`CvDTreeParams` but not all of the decision tree parameters are supported:
|
||||
cross-validation, pruning, and class priorities are not used.
|
||||
|
||||
CvGBTreesParams::CvGBTreesParams
|
||||
--------------------------------
|
||||
.. ocv:function:: CvGBTreesParams::CvGBTreesParams()
|
||||
|
||||
.. ocv:function:: CvGBTreesParams::CvGBTreesParams( int loss_function_type, int weak_count, float shrinkage, float subsample_portion, int max_depth, bool use_surrogates )
|
||||
|
||||
:param loss_function_type: Type of the loss function used for training
|
||||
(see :ref:`Training GBT`). It must be one of the
|
||||
following types: ``CvGBTrees::SQUARED_LOSS``, ``CvGBTrees::ABSOLUTE_LOSS``,
|
||||
``CvGBTrees::HUBER_LOSS``, ``CvGBTrees::DEVIANCE_LOSS``. The first three
|
||||
types are used for regression problems, and the last one for
|
||||
classification.
|
||||
|
||||
:param weak_count: Count of boosting algorithm iterations. ``weak_count*K`` is the total
|
||||
count of trees in the GBT model, where ``K`` is the output classes count
|
||||
(equal to one in case of a regression).
|
||||
|
||||
:param shrinkage: Regularization parameter (see :ref:`Training GBT`).
|
||||
|
||||
:param subsample_portion: Portion of the whole training set used for each algorithm iteration.
|
||||
Subset is generated randomly. For more information see
|
||||
http://www.salfordsystems.com/doc/StochasticBoostingSS.pdf.
|
||||
|
||||
:param max_depth: Maximal depth of each decision tree in the ensemble (see :ocv:class:`CvDTree`).
|
||||
|
||||
:param use_surrogates: If ``true``, surrogate splits are built (see :ocv:class:`CvDTree`).
|
||||
|
||||
By default the following constructor is used:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
CvGBTreesParams(CvGBTrees::SQUARED_LOSS, 200, 0.8f, 0.01f, 3, false)
|
||||
: CvDTreeParams( 3, 10, 0, false, 10, 0, false, false, 0 )
|
||||
|
||||
CvGBTrees
|
||||
---------
|
||||
.. ocv:class:: CvGBTrees : public CvStatModel
|
||||
|
||||
The class implements the Gradient boosted tree model as described in the beginning of this section.
|
||||
|
||||
CvGBTrees::CvGBTrees
|
||||
--------------------
|
||||
Default and training constructors.
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees()
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees( const Mat& trainData, int tflag, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), const Mat& varType=Mat(), const Mat& missingDataMask=Mat(), CvGBTreesParams params=CvGBTreesParams() )
|
||||
|
||||
.. ocv:function:: CvGBTrees::CvGBTrees( const CvMat* trainData, int tflag, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, const CvMat* varType=0, const CvMat* missingDataMask=0, CvGBTreesParams params=CvGBTreesParams() )
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees([trainData, tflag, responses[, varIdx[, sampleIdx[, varType[, missingDataMask[, params]]]]]]) -> <GBTrees object>
|
||||
|
||||
The constructors follow conventions of :ocv:func:`CvStatModel::CvStatModel`. See :ocv:func:`CvStatModel::train` for parameters descriptions.
|
||||
|
||||
CvGBTrees::train
|
||||
----------------
|
||||
Trains a Gradient boosted tree model.
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train(const Mat& trainData, int tflag, const Mat& responses, const Mat& varIdx=Mat(), const Mat& sampleIdx=Mat(), const Mat& varType=Mat(), const Mat& missingDataMask=Mat(), CvGBTreesParams params=CvGBTreesParams(), bool update=false)
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train( const CvMat* trainData, int tflag, const CvMat* responses, const CvMat* varIdx=0, const CvMat* sampleIdx=0, const CvMat* varType=0, const CvMat* missingDataMask=0, CvGBTreesParams params=CvGBTreesParams(), bool update=false )
|
||||
|
||||
.. ocv:function:: bool CvGBTrees::train(CvMLData* data, CvGBTreesParams params=CvGBTreesParams(), bool update=false)
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.train(trainData, tflag, responses[, varIdx[, sampleIdx[, varType[, missingDataMask[, params[, update]]]]]]) -> retval
|
||||
|
||||
The first train method follows the common template (see :ocv:func:`CvStatModel::train`).
|
||||
Both ``tflag`` values (``CV_ROW_SAMPLE``, ``CV_COL_SAMPLE``) are supported.
|
||||
``trainData`` must be of the ``CV_32F`` type. ``responses`` must be a matrix of type
|
||||
``CV_32S`` or ``CV_32F``. In both cases it is converted into the ``CV_32F``
|
||||
matrix inside the training procedure. ``varIdx`` and ``sampleIdx`` must be a
|
||||
list of indices (``CV_32S``) or a mask (``CV_8U`` or ``CV_8S``). ``update`` is
|
||||
a dummy parameter.
|
||||
|
||||
The second form of :ocv:func:`CvGBTrees::train` function uses :ocv:class:`CvMLData` as a
|
||||
data set container. ``update`` is still a dummy parameter.
|
||||
|
||||
All parameters specific to the GBT model are passed into the training function
|
||||
as a :ocv:class:`CvGBTreesParams` structure.
|
||||
|
||||
|
||||
CvGBTrees::predict
|
||||
------------------
|
||||
Predicts a response for an input sample.
|
||||
|
||||
.. ocv:function:: float CvGBTrees::predict(const Mat& sample, const Mat& missing=Mat(), const Range& slice = Range::all(), int k=-1) const
|
||||
|
||||
.. ocv:function:: float CvGBTrees::predict( const CvMat* sample, const CvMat* missing=0, CvMat* weakResponses=0, CvSlice slice = CV_WHOLE_SEQ, int k=-1 ) const
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.predict(sample[, missing[, slice[, k]]]) -> retval
|
||||
|
||||
:param sample: Input feature vector that has the same format as every training set
|
||||
element. If not all the variables were actually used during training,
|
||||
``sample`` contains forged values at the appropriate places.
|
||||
|
||||
:param missing: Missing values mask, which is a dimensional matrix of the same size as
|
||||
``sample`` having the ``CV_8U`` type. ``1`` corresponds to the missing value
|
||||
in the same position in the ``sample`` vector. If there are no missing values
|
||||
in the feature vector, an empty matrix can be passed instead of the missing mask.
|
||||
|
||||
:param weakResponses: Matrix used to obtain predictions of all the trees.
|
||||
The matrix has :math:`K` rows,
|
||||
where :math:`K` is the count of output classes (1 for the regression case).
|
||||
The matrix has as many columns as the ``slice`` length.
|
||||
|
||||
:param slice: Parameter defining the part of the ensemble used for prediction.
|
||||
If ``slice = Range::all()``, all trees are used. Use this parameter to
|
||||
get predictions of the GBT models with different ensemble sizes learning
|
||||
only one model.
|
||||
|
||||
:param k: Number of tree ensembles built in case of the classification problem
|
||||
(see :ref:`Training GBT`). Use this
|
||||
parameter to change the output to sum of the trees' predictions in the
|
||||
``k``-th ensemble only. To get the total GBT model prediction, ``k`` value
|
||||
must be -1. For regression problems, ``k`` is also equal to -1.
|
||||
|
||||
The method predicts the response corresponding to the given sample
|
||||
(see :ref:`Predicting with GBT`).
|
||||
The result is either the class label or the estimated function value. The
|
||||
:ocv:func:`CvGBTrees::predict` method enables using the parallel version of the GBT model
|
||||
prediction if the OpenCV is built with the TBB library. In this case, predictions
|
||||
of single trees are computed in a parallel fashion.
|
||||
|
||||
|
||||
CvGBTrees::clear
|
||||
----------------
|
||||
Clears the model.
|
||||
|
||||
.. ocv:function:: void CvGBTrees::clear()
|
||||
|
||||
.. ocv:pyfunction:: cv2.GBTrees.clear() -> None
|
||||
|
||||
The function deletes the data set information and all the weak models and sets all internal
|
||||
variables to the initial state. The function is called in :ocv:func:`CvGBTrees::train` and in the
|
||||
destructor.
|
||||
|
||||
|
||||
CvGBTrees::calc_error
|
||||
---------------------
|
||||
Calculates a training or testing error.
|
||||
|
||||
.. ocv:function:: float CvGBTrees::calc_error( CvMLData* _data, int type, std::vector<float> *resp = 0 )
|
||||
|
||||
:param _data: Data set.
|
||||
|
||||
:param type: Parameter defining the error that should be computed: train (``CV_TRAIN_ERROR``) or test
|
||||
(``CV_TEST_ERROR``).
|
||||
|
||||
:param resp: If non-zero, a vector of predictions on the corresponding data set is
|
||||
returned.
|
||||
|
||||
If the :ocv:class:`CvMLData` data is used to store the data set, :ocv:func:`CvGBTrees::calc_error` can be
|
||||
used to get a training/testing error easily and (optionally) all predictions
|
||||
on the training/testing set. If the Intel* TBB* library is used, the error is computed in a
|
||||
parallel way, namely, predictions for different samples are computed at the same time.
|
||||
In case of a regression problem, a mean squared error is returned. For
|
||||
classifications, the result is a misclassification error in percent.
|
||||
|
||||
@@ -1900,7 +1900,7 @@ void CvBoost::write_params( CvFileStorage* fs ) const
|
||||
else
|
||||
cvWriteInt( fs, "splitting_criteria", params.split_criteria );
|
||||
|
||||
cvWriteInt( fs, "ntrees", weak->total );
|
||||
cvWriteInt( fs, "ntrees", weak->total );
|
||||
cvWriteReal( fs, "weight_trimming_rate", params.weight_trim_rate );
|
||||
|
||||
data->write_params( fs );
|
||||
|
||||
+18
-18
@@ -263,10 +263,10 @@ static void icvTrainCNNetwork( CvCNNetwork* network,
|
||||
{
|
||||
image.data.fl = (float*)images[i];
|
||||
cvTranspose( &image, X[0] );
|
||||
|
||||
|
||||
for( k = 0, layer = first_layer; k < n_layers; k++, layer = layer->next_layer )
|
||||
CV_CALL(layer->forward( layer, X[k], X[k+1] ));
|
||||
|
||||
|
||||
cvTranspose( X[n_layers], dE_dX[n_layers] );
|
||||
cvGetRow( etalons, &etalon, *right_etal_idx );
|
||||
loss = (float)cvNorm( dE_dX[n_layers], &etalon );
|
||||
@@ -466,8 +466,8 @@ static void icvCNNModelRelease( CvStatModel** cnn_model )
|
||||
\****************************************************************************************/
|
||||
ML_IMPL CvCNNetwork* cvCreateCNNetwork( CvCNNLayer* first_layer )
|
||||
{
|
||||
CvCNNetwork* network = 0;
|
||||
|
||||
CvCNNetwork* network = 0;
|
||||
|
||||
CV_FUNCNAME( "cvCreateCNNetwork" );
|
||||
__BEGIN__;
|
||||
|
||||
@@ -669,7 +669,7 @@ ML_IMPL CvCNNLayer* cvCreateCNNConvolutionLayer(
|
||||
CvRNG rng = cvRNG( 0xFFFFFFFF );
|
||||
cvRandArr( &rng, layer->weights, CV_RAND_UNI, cvRealScalar(-1), cvRealScalar(1) );
|
||||
}
|
||||
|
||||
|
||||
if( connect_mask )
|
||||
{
|
||||
if( !ICV_IS_MAT_OF_TYPE( connect_mask, CV_8UC1 ) )
|
||||
@@ -689,7 +689,7 @@ ML_IMPL CvCNNLayer* cvCreateCNNConvolutionLayer(
|
||||
cvReleaseMat( &layer->connect_mask );
|
||||
cvFree( &layer );
|
||||
}
|
||||
|
||||
|
||||
return (CvCNNLayer*)layer;
|
||||
}
|
||||
|
||||
@@ -757,7 +757,7 @@ ML_IMPL CvCNNLayer* cvCreateCNNSubSamplingLayer(
|
||||
}
|
||||
|
||||
/****************************************************************************************/
|
||||
ML_IMPL CvCNNLayer* cvCreateCNNFullConnectLayer(
|
||||
ML_IMPL CvCNNLayer* cvCreateCNNFullConnectLayer(
|
||||
int n_inputs, int n_outputs, float a, float s,
|
||||
float init_learn_rate, int learn_rate_decrease_type, CvMat* weights )
|
||||
{
|
||||
@@ -928,7 +928,7 @@ static void icvCNNSubSamplingForward( CvCNNLayer* _layer,
|
||||
for( xx = 0; xx < Ywidth; xx++, sumX_data++ )
|
||||
*sumX_data += Xplane[((yy+ky)*Xwidth+(xx+kx))];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w = layer->weights->data.fl;
|
||||
cvGetRows( layer->sumX, &sumX_sub_col, 0, Ysize );
|
||||
@@ -1256,7 +1256,7 @@ static void icvCNNFullConnectBackward( CvCNNLayer* _layer,
|
||||
{
|
||||
CvMat* dE_dY_activ_func_der = 0;
|
||||
CvMat* dE_dW = 0;
|
||||
|
||||
|
||||
CV_FUNCNAME( "icvCNNFullConnectBackward" );
|
||||
|
||||
if( !ICV_IS_CNN_FULLCONNECT_LAYER(_layer) )
|
||||
@@ -1276,12 +1276,12 @@ static void icvCNNFullConnectBackward( CvCNNLayer* _layer,
|
||||
CV_ASSERT(X->cols == 1 && X->rows == n_inputs);
|
||||
CV_ASSERT(dE_dY->rows == 1 && dE_dY->cols == n_outputs );
|
||||
CV_ASSERT(dE_dX->rows == 1 && dE_dX->cols == n_inputs );
|
||||
|
||||
|
||||
// we violate the convetion about vector's orientation because
|
||||
// here is more convenient to make this parameter a row-vector
|
||||
// here is more convenient to make this parameter a row-vector
|
||||
CV_CALL(dE_dY_activ_func_der = cvCreateMat( 1, n_outputs, CV_32FC1 ));
|
||||
CV_CALL(dE_dW = cvCreateMat( 1, weights->rows*weights->cols, CV_32FC1 ));
|
||||
|
||||
|
||||
// 1) compute gradients dE_dX and dE_dW
|
||||
// activ_func_der == 4as*(layer->exp2ssumWX)/(layer->exp2ssumWX + 1)^2
|
||||
CV_CALL(cvReshape( layer->exp2ssumWX, &exp2ssumWXrow, 0, layer->exp2ssumWX->cols ));
|
||||
@@ -1359,7 +1359,7 @@ static void icvCNNSubSamplingRelease( CvCNNLayer** p_layer )
|
||||
__BEGIN__;
|
||||
|
||||
CvCNNSubSamplingLayer* layer = 0;
|
||||
|
||||
|
||||
if( !p_layer )
|
||||
CV_ERROR( CV_StsNullPtr, "Null double pointer" );
|
||||
|
||||
@@ -1384,7 +1384,7 @@ static void icvCNNFullConnectRelease( CvCNNLayer** p_layer )
|
||||
__BEGIN__;
|
||||
|
||||
CvCNNFullConnectLayer* layer = 0;
|
||||
|
||||
|
||||
if( !p_layer )
|
||||
CV_ERROR( CV_StsNullPtr, "Null double pointer" );
|
||||
|
||||
@@ -1467,7 +1467,7 @@ static CvCNNLayer* icvReadCNNLayer( CvFileStorage* fs, CvFileNode* node )
|
||||
if( !connect_mask )
|
||||
CV_ERROR( CV_StsParseError, "Missing <connect mask>" );
|
||||
|
||||
CV_CALL(layer = cvCreateCNNConvolutionLayer(
|
||||
CV_CALL(layer = cvCreateCNNConvolutionLayer(
|
||||
n_input_planes, input_height, input_width, n_output_planes, K,
|
||||
init_learn_rate, learn_type, connect_mask, weights ));
|
||||
}
|
||||
@@ -1619,7 +1619,7 @@ static void* icvReadCNNModel( CvFileStorage* fs, CvFileNode* root_node )
|
||||
static void
|
||||
icvWriteCNNModel( CvFileStorage* fs, const char* name,
|
||||
const void* struct_ptr, CvAttrList )
|
||||
|
||||
|
||||
{
|
||||
CV_FUNCNAME ("icvWriteCNNModel");
|
||||
__BEGIN__;
|
||||
@@ -1632,12 +1632,12 @@ icvWriteCNNModel( CvFileStorage* fs, const char* name,
|
||||
CV_ERROR( CV_StsBadArg, "Invalid pointer" );
|
||||
|
||||
n_layers = cnn->network->n_layers;
|
||||
|
||||
|
||||
CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_ML_CNN ));
|
||||
|
||||
CV_CALL(cvWrite( fs, "etalons", cnn->etalons ));
|
||||
CV_CALL(cvWrite( fs, "cls_labels", cnn->cls_labels ));
|
||||
|
||||
|
||||
CV_CALL( cvStartWriteStruct( fs, "network", CV_NODE_SEQ ));
|
||||
|
||||
layer = cnn->network->layers;
|
||||
|
||||
+53
-53
@@ -41,7 +41,7 @@
|
||||
#include "precomp.hpp"
|
||||
#include <ctype.h>
|
||||
|
||||
#define MISS_VAL FLT_MAX
|
||||
#define MISS_VAL FLT_MAX
|
||||
#define CV_VAR_MISS 0
|
||||
|
||||
CvTrainTestSplit::CvTrainTestSplit()
|
||||
@@ -57,7 +57,7 @@ CvTrainTestSplit::CvTrainTestSplit( int _train_sample_count, bool _mix )
|
||||
train_sample_part.count = _train_sample_count;
|
||||
mix = _mix;
|
||||
}
|
||||
|
||||
|
||||
CvTrainTestSplit::CvTrainTestSplit( float _train_sample_portion, bool _mix )
|
||||
{
|
||||
train_sample_part_mode = CV_PORTION;
|
||||
@@ -109,7 +109,7 @@ void CvMLData::clear()
|
||||
cvReleaseMat( &var_types_out );
|
||||
|
||||
free_train_test_idx();
|
||||
|
||||
|
||||
total_class_count = 0;
|
||||
|
||||
response_idx = -1;
|
||||
@@ -119,17 +119,17 @@ void CvMLData::clear()
|
||||
|
||||
static char *fgets_chomp(char *str, int n, FILE *stream)
|
||||
{
|
||||
char *head = fgets(str, n, stream);
|
||||
if( head )
|
||||
{
|
||||
for(char *tail = head + strlen(head) - 1; tail >= head; --tail)
|
||||
{
|
||||
if( *tail != '\r' && *tail != '\n' )
|
||||
break;
|
||||
*tail = '\0';
|
||||
}
|
||||
}
|
||||
return head;
|
||||
char *head = fgets(str, n, stream);
|
||||
if( head )
|
||||
{
|
||||
for(char *tail = head + strlen(head) - 1; tail >= head; --tail)
|
||||
{
|
||||
if( *tail != '\r' && *tail != '\n' )
|
||||
break;
|
||||
*tail = '\0';
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
|
||||
@@ -143,13 +143,13 @@ int CvMLData::read_csv(const char* filename)
|
||||
char *ptr;
|
||||
float* el_ptr;
|
||||
CvSeqReader reader;
|
||||
int cols_count = 0;
|
||||
int cols_count = 0;
|
||||
uchar *var_types_ptr = 0;
|
||||
|
||||
clear();
|
||||
|
||||
file = fopen( filename, "rt" );
|
||||
|
||||
|
||||
if( !file )
|
||||
return -1;
|
||||
|
||||
@@ -177,7 +177,7 @@ int CvMLData::read_csv(const char* filename)
|
||||
ptr++;
|
||||
}
|
||||
|
||||
cols_count++;
|
||||
cols_count++;
|
||||
|
||||
if ( cols_count == 0)
|
||||
{
|
||||
@@ -199,7 +199,7 @@ int CvMLData::read_csv(const char* filename)
|
||||
char *token = NULL;
|
||||
int type;
|
||||
token = strtok(buf, str_delimiter);
|
||||
if (!token)
|
||||
if (!token)
|
||||
break;
|
||||
for (int i = 0; i < cols_count-1; i++)
|
||||
{
|
||||
@@ -274,7 +274,7 @@ const std::map<std::string, int>& CvMLData::get_class_labels_map() const
|
||||
|
||||
void CvMLData::str_to_flt_elem( const char* token, float& flt_elem, int& type)
|
||||
{
|
||||
|
||||
|
||||
char* stopstring = NULL;
|
||||
flt_elem = (float)strtod( token, &stopstring );
|
||||
assert( stopstring );
|
||||
@@ -308,7 +308,7 @@ void CvMLData::set_delimiter(char ch)
|
||||
|
||||
if (ch == miss_ch /*|| ch == flt_separator*/)
|
||||
CV_ERROR(CV_StsBadArg, "delimited, miss_character and flt_separator must be different");
|
||||
|
||||
|
||||
delimiter = ch;
|
||||
|
||||
__END__;
|
||||
@@ -326,7 +326,7 @@ void CvMLData::set_miss_ch(char ch)
|
||||
|
||||
if (ch == delimiter/* || ch == flt_separator*/)
|
||||
CV_ERROR(CV_StsBadArg, "delimited, miss_character and flt_separator must be different");
|
||||
|
||||
|
||||
miss_ch = ch;
|
||||
|
||||
__END__;
|
||||
@@ -354,7 +354,7 @@ void CvMLData::set_response_idx( int idx )
|
||||
chahge_var_idx( idx, false );
|
||||
response_idx = idx;
|
||||
|
||||
__END__;
|
||||
__END__;
|
||||
}
|
||||
|
||||
int CvMLData::get_response_idx() const
|
||||
@@ -372,12 +372,12 @@ void CvMLData::change_var_type( int var_idx, int type )
|
||||
{
|
||||
CV_FUNCNAME( "CvMLData::change_var_type" );
|
||||
__BEGIN__;
|
||||
|
||||
|
||||
int var_count = 0;
|
||||
|
||||
if ( !values )
|
||||
CV_ERROR( CV_StsInternal, "data is empty" );
|
||||
|
||||
|
||||
var_count = values->cols;
|
||||
|
||||
if ( var_idx < 0 || var_idx >= var_count)
|
||||
@@ -386,7 +386,7 @@ void CvMLData::change_var_type( int var_idx, int type )
|
||||
if ( type != CV_VAR_ORDERED && type != CV_VAR_CATEGORICAL)
|
||||
CV_ERROR( CV_StsBadArg, "type is not correct" );
|
||||
|
||||
assert( var_types );
|
||||
assert( var_types );
|
||||
if ( var_types->data.ptr[var_idx] == CV_VAR_CATEGORICAL && type == CV_VAR_ORDERED)
|
||||
CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );
|
||||
var_types->data.ptr[var_idx] = (uchar)type;
|
||||
@@ -409,12 +409,12 @@ void CvMLData::set_var_types( const char* str )
|
||||
var_count = values->cols;
|
||||
|
||||
assert( var_types );
|
||||
|
||||
|
||||
ord = strstr( str, "ord" );
|
||||
cat = strstr( str, "cat" );
|
||||
cat = strstr( str, "cat" );
|
||||
if ( !ord && !cat )
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
|
||||
|
||||
if ( !ord && strlen(cat) == 3 ) // str == "cat"
|
||||
{
|
||||
cvSet( var_types, cvScalarAll(CV_VAR_CATEGORICAL) );
|
||||
@@ -429,10 +429,10 @@ void CvMLData::set_var_types( const char* str )
|
||||
|
||||
if ( ord ) // parse ord str
|
||||
{
|
||||
char* stopstring = NULL;
|
||||
char* stopstring = NULL;
|
||||
if ( ord[3] != '[')
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
|
||||
|
||||
ord += 4; // pass "ord["
|
||||
do
|
||||
{
|
||||
@@ -447,18 +447,18 @@ void CvMLData::set_var_types( const char* str )
|
||||
var_types->data.ptr[b1] = CV_VAR_ORDERED;
|
||||
set_var_type_count++;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
if ( stopstring[0] == '-')
|
||||
if ( stopstring[0] == '-')
|
||||
{
|
||||
int b2 = (int)strtod( ord, &stopstring);
|
||||
if ( (*stopstring == 0) || (*stopstring != ',' && *stopstring != ']') )
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
ord = stopstring + 1;
|
||||
for (int i = b1; i <= b2; i++)
|
||||
{
|
||||
if ( var_types->data.ptr[i] == CV_VAR_CATEGORICAL)
|
||||
CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );
|
||||
CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );
|
||||
var_types->data.ptr[i] = CV_VAR_ORDERED;
|
||||
}
|
||||
set_var_type_count += b2 - b1 + 1;
|
||||
@@ -472,14 +472,14 @@ void CvMLData::set_var_types( const char* str )
|
||||
|
||||
if ( stopstring[1] != '\0' && stopstring[1] != ',')
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
}
|
||||
}
|
||||
|
||||
if ( cat ) // parse cat str
|
||||
{
|
||||
char* stopstring = NULL;
|
||||
char* stopstring = NULL;
|
||||
if ( cat[3] != '[')
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
|
||||
|
||||
cat += 4; // pass "cat["
|
||||
do
|
||||
{
|
||||
@@ -492,13 +492,13 @@ void CvMLData::set_var_types( const char* str )
|
||||
var_types->data.ptr[b1] = CV_VAR_CATEGORICAL;
|
||||
set_var_type_count++;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
if ( stopstring[0] == '-')
|
||||
if ( stopstring[0] == '-')
|
||||
{
|
||||
int b2 = (int)strtod( cat, &stopstring);
|
||||
if ( (*stopstring == 0) || (*stopstring != ',' && *stopstring != ']') )
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
cat = stopstring + 1;
|
||||
for (int i = b1; i <= b2; i++)
|
||||
var_types->data.ptr[i] = CV_VAR_CATEGORICAL;
|
||||
@@ -513,7 +513,7 @@ void CvMLData::set_var_types( const char* str )
|
||||
|
||||
if ( stopstring[1] != '\0' && stopstring[1] != ',')
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
}
|
||||
}
|
||||
|
||||
if (set_var_type_count != var_count)
|
||||
CV_ERROR( CV_StsBadArg, "types string is not correct" );
|
||||
@@ -539,7 +539,7 @@ const CvMat* CvMLData::get_var_types()
|
||||
if ( avcount == values->cols || (avcount == values->cols-1 && response_idx == values->cols-1) )
|
||||
return var_types;
|
||||
|
||||
if ( !var_types_out || ( var_types_out && var_types_out->cols != vt_size ) )
|
||||
if ( !var_types_out || ( var_types_out && var_types_out->cols != vt_size ) )
|
||||
{
|
||||
cvReleaseMat( &var_types_out );
|
||||
var_types_out = cvCreateMat( 1, vt_size, CV_8UC1 );
|
||||
@@ -575,7 +575,7 @@ const CvMat* CvMLData::get_responses()
|
||||
if ( !values )
|
||||
CV_ERROR( CV_StsInternal, "data is empty" );
|
||||
var_count = values->cols;
|
||||
|
||||
|
||||
if ( response_idx < 0 || response_idx >= var_count )
|
||||
return 0;
|
||||
if ( !response_out )
|
||||
@@ -600,7 +600,7 @@ void CvMLData::set_train_test_split( const CvTrainTestSplit * spl)
|
||||
CV_ERROR( CV_StsInternal, "data is empty" );
|
||||
|
||||
sample_count = values->rows;
|
||||
|
||||
|
||||
float train_sample_portion;
|
||||
|
||||
if (spl->train_sample_part_mode == CV_COUNT)
|
||||
@@ -615,7 +615,7 @@ void CvMLData::set_train_test_split( const CvTrainTestSplit * spl)
|
||||
train_sample_portion = spl->train_sample_part.portion;
|
||||
if ( train_sample_portion > 1)
|
||||
CV_ERROR( CV_StsBadArg, "train samples count is not correct" );
|
||||
train_sample_portion = train_sample_portion <= FLT_EPSILON ||
|
||||
train_sample_portion = train_sample_portion <= FLT_EPSILON ||
|
||||
1 - train_sample_portion <= FLT_EPSILON ? 1 : train_sample_portion;
|
||||
train_sample_count = std::max(1, cvFloor( train_sample_portion * sample_count ));
|
||||
}
|
||||
@@ -642,11 +642,11 @@ void CvMLData::set_train_test_split( const CvTrainTestSplit * spl)
|
||||
test_sample_idx = cvCreateMatHeader( 1, test_sample_count, CV_32SC1 );
|
||||
*test_sample_idx = cvMat( 1, test_sample_count, CV_32SC1, &sample_idx[train_sample_count] );
|
||||
}
|
||||
|
||||
|
||||
mix = spl->mix;
|
||||
if ( mix )
|
||||
mix_train_and_test_idx();
|
||||
|
||||
|
||||
__END__;
|
||||
}
|
||||
|
||||
@@ -710,14 +710,14 @@ const CvMat* CvMLData::get_var_idx()
|
||||
CV_ERROR( CV_StsInternal, "data is empty" );
|
||||
|
||||
assert( var_idx_mask );
|
||||
|
||||
|
||||
avcount = cvFloor( cvNorm( var_idx_mask, 0, CV_L1 ) );
|
||||
int* vidx;
|
||||
|
||||
if ( avcount == values->cols )
|
||||
return 0;
|
||||
|
||||
if ( !var_idx_out || ( var_idx_out && var_idx_out->cols != avcount ) )
|
||||
|
||||
if ( !var_idx_out || ( var_idx_out && var_idx_out->cols != avcount ) )
|
||||
{
|
||||
cvReleaseMat( &var_idx_out );
|
||||
var_idx_out = cvCreateMat( 1, avcount, CV_32SC1);
|
||||
@@ -726,10 +726,10 @@ const CvMat* CvMLData::get_var_idx()
|
||||
}
|
||||
|
||||
vidx = var_idx_out->data.i;
|
||||
|
||||
|
||||
for(int i = 0; i < var_idx_mask->cols; i++)
|
||||
if ( var_idx_mask->data.ptr[i] )
|
||||
{
|
||||
{
|
||||
*vidx = i;
|
||||
vidx++;
|
||||
}
|
||||
@@ -759,7 +759,7 @@ void CvMLData::change_var_idx( int vi, bool state )
|
||||
if ( vi < 0 || vi >= var_count)
|
||||
CV_ERROR( CV_StsBadArg, "variable index is not correct" );
|
||||
|
||||
assert( var_idx_mask );
|
||||
assert( var_idx_mask );
|
||||
var_idx_mask->data.ptr[vi] = state;
|
||||
|
||||
__END__;
|
||||
|
||||
@@ -79,7 +79,7 @@ void EM::clear()
|
||||
logWeightDivDet.release();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool EM::train(InputArray samples,
|
||||
OutputArray logLikelihoods,
|
||||
OutputArray labels,
|
||||
@@ -101,7 +101,7 @@ bool EM::trainE(InputArray samples,
|
||||
Mat samplesMat = samples.getMat();
|
||||
vector<Mat> covs0;
|
||||
_covs0.getMatVector(covs0);
|
||||
|
||||
|
||||
Mat means0 = _means0.getMat(), weights0 = _weights0.getMat();
|
||||
|
||||
setTrainData(START_E_STEP, samplesMat, 0, !_means0.empty() ? &means0 : 0,
|
||||
@@ -117,12 +117,12 @@ bool EM::trainM(InputArray samples,
|
||||
{
|
||||
Mat samplesMat = samples.getMat();
|
||||
Mat probs0 = _probs0.getMat();
|
||||
|
||||
|
||||
setTrainData(START_M_STEP, samplesMat, !_probs0.empty() ? &probs0 : 0, 0, 0, 0);
|
||||
return doTrain(START_M_STEP, logLikelihoods, labels, probs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Vec2d EM::predict(InputArray _sample, OutputArray _probs) const
|
||||
{
|
||||
Mat sample = _sample.getMat();
|
||||
@@ -456,14 +456,14 @@ bool EM::doTrain(int startStep, OutputArray logLikelihoods, OutputArray labels,
|
||||
covs[clusterIndex] = Mat::diag(covsEigenValues[clusterIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(labels.needed())
|
||||
trainLabels.copyTo(labels);
|
||||
if(probs.needed())
|
||||
trainProbs.copyTo(probs);
|
||||
if(logLikelihoods.needed())
|
||||
trainLogLikelihoods.copyTo(logLikelihoods);
|
||||
|
||||
|
||||
trainSamples.release();
|
||||
trainProbs.release();
|
||||
trainLabels.release();
|
||||
|
||||
+31
-31
@@ -149,8 +149,8 @@ int cvCrossValNextStep (CvStatModel* estimateModel)
|
||||
// Do checking part of loop of cross-validations metod.
|
||||
ML_IMPL
|
||||
void cvCrossValCheckClassifier (CvStatModel* estimateModel,
|
||||
const CvStatModel* model,
|
||||
const CvMat* trainData,
|
||||
const CvStatModel* model,
|
||||
const CvMat* trainData,
|
||||
int sample_t_flag,
|
||||
const CvMat* trainClasses)
|
||||
{
|
||||
@@ -194,7 +194,7 @@ void cvCrossValCheckClassifier (CvStatModel* estimateModel,
|
||||
data = crVal->sampleIdxEval->data.i;
|
||||
|
||||
// Eval tested feature vectors.
|
||||
CV_CALL (cvStatModelMultiPredict (model, trainData, sample_t_flag,
|
||||
CV_CALL (cvStatModelMultiPredict (model, trainData, sample_t_flag,
|
||||
crVal->predict_results, NULL, crVal->sampleIdxEval));
|
||||
// Count number if correct results.
|
||||
responses_result = crVal->predict_results->data.fl;
|
||||
@@ -307,12 +307,12 @@ float cvCrossValGetResult (const CvStatModel* estimateModel,
|
||||
result = ((float)crVal->sq_error) / crVal->all_results;
|
||||
if (correlation)
|
||||
{
|
||||
te = crVal->all_results * crVal->sum_cp -
|
||||
te = crVal->all_results * crVal->sum_cp -
|
||||
crVal->sum_correct * crVal->sum_predict;
|
||||
te *= te;
|
||||
te1 = (crVal->all_results * crVal->sum_cc -
|
||||
te1 = (crVal->all_results * crVal->sum_cc -
|
||||
crVal->sum_correct * crVal->sum_correct) *
|
||||
(crVal->all_results * crVal->sum_pp -
|
||||
(crVal->all_results * crVal->sum_pp -
|
||||
crVal->sum_predict * crVal->sum_predict);
|
||||
*correlation = (float)(te / te1);
|
||||
|
||||
@@ -330,7 +330,7 @@ float cvCrossValGetResult (const CvStatModel* estimateModel,
|
||||
}
|
||||
|
||||
/****************************************************************************************/
|
||||
// Reset cross-validation EstimateModel to state the same as it was immidiatly after
|
||||
// Reset cross-validation EstimateModel to state the same as it was immidiatly after
|
||||
// its creating.
|
||||
ML_IMPL
|
||||
void cvCrossValReset (CvStatModel* estimateModel)
|
||||
@@ -368,7 +368,7 @@ void cvReleaseCrossValidationModel (CvStatModel** model)
|
||||
|
||||
CV_FUNCNAME ("cvReleaseCrossValidationModel");
|
||||
__BEGIN__
|
||||
|
||||
|
||||
if (!model)
|
||||
{
|
||||
CV_ERROR (CV_StsNullPtr, "");
|
||||
@@ -397,7 +397,7 @@ void cvReleaseCrossValidationModel (CvStatModel** model)
|
||||
|
||||
/****************************************************************************************/
|
||||
// This function create cross-validation EstimateModel.
|
||||
ML_IMPL CvStatModel*
|
||||
ML_IMPL CvStatModel*
|
||||
cvCreateCrossValidationEstimateModel(
|
||||
int samples_all,
|
||||
const CvStatModelParams* estimateParams,
|
||||
@@ -413,7 +413,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
|
||||
int i, j, k, s_len;
|
||||
int samples_selected;
|
||||
CvRNG rng;
|
||||
CvRNG rng;
|
||||
CvRNG* prng;
|
||||
int* res_s_data;
|
||||
int* te_s_data;
|
||||
@@ -435,7 +435,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
|
||||
// Alloc memory and fill standart StatModel's fields.
|
||||
CV_CALL (crVal = (CvCrossValidationModel*)cvCreateStatModel (
|
||||
CV_STAT_MODEL_MAGIC_VAL | CV_CROSSVAL_MAGIC_VAL,
|
||||
CV_STAT_MODEL_MAGIC_VAL | CV_CROSSVAL_MAGIC_VAL,
|
||||
sizeof(CvCrossValidationModel),
|
||||
cvReleaseCrossValidationModel,
|
||||
NULL, NULL));
|
||||
@@ -443,7 +443,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
crVal->folds_all = k_fold;
|
||||
if (estimateParams && ((CvCrossValidationParams*)estimateParams)->is_regression)
|
||||
crVal->is_regression = 1;
|
||||
else
|
||||
else
|
||||
crVal->is_regression = 0;
|
||||
if (estimateParams && ((CvCrossValidationParams*)estimateParams)->rng)
|
||||
prng = ((CvCrossValidationParams*)estimateParams)->rng;
|
||||
@@ -455,7 +455,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
{
|
||||
int s_step;
|
||||
int s_type = 0;
|
||||
|
||||
|
||||
if (!CV_IS_MAT (sampleIdx))
|
||||
CV_ERROR (CV_StsBadArg, "Invalid sampleIdx array");
|
||||
|
||||
@@ -463,7 +463,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
CV_ERROR (CV_StsBadSize, "sampleIdx array must be 1-dimensional");
|
||||
|
||||
s_len = sampleIdx->rows + sampleIdx->cols - 1;
|
||||
s_step = sampleIdx->rows == 1 ?
|
||||
s_step = sampleIdx->rows == 1 ?
|
||||
1 : sampleIdx->step / CV_ELEM_SIZE(sampleIdx->type);
|
||||
|
||||
s_type = CV_MAT_TYPE (sampleIdx->type);
|
||||
@@ -474,13 +474,13 @@ cvCreateCrossValidationEstimateModel(
|
||||
case CV_8SC1:
|
||||
{
|
||||
uchar* s_data = sampleIdx->data.ptr;
|
||||
|
||||
|
||||
// sampleIdx is array of 1's and 0's -
|
||||
// i.e. it is a mask of the selected samples
|
||||
if( s_len != samples_all )
|
||||
CV_ERROR (CV_StsUnmatchedSizes,
|
||||
"Sample mask should contain as many elements as the total number of samples");
|
||||
|
||||
|
||||
samples_selected = 0;
|
||||
for (i = 0; i < s_len; i++)
|
||||
samples_selected += s_data[i * s_step] != 0;
|
||||
@@ -502,7 +502,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
}
|
||||
|
||||
// Alloc additional memory for internal Idx and fill it.
|
||||
/*!!*/ CV_CALL (res_s_data = crVal->sampleIdxAll =
|
||||
/*!!*/ CV_CALL (res_s_data = crVal->sampleIdxAll =
|
||||
(int*)cvAlloc (2 * s_len * sizeof(int)));
|
||||
|
||||
if (s_type < CV_32SC1)
|
||||
@@ -529,7 +529,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
|
||||
if (out_of_order)
|
||||
qsort (res_s_data, s_len, sizeof(res_s_data[0]), icvCmpIntegers);
|
||||
|
||||
|
||||
if (res_s_data[0] < 0 ||
|
||||
res_s_data[s_len - 1] >= samples_all)
|
||||
CV_ERROR (CV_StsBadArg, "There are out-of-range sample indices");
|
||||
@@ -548,7 +548,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
*res_s_data++ = i;
|
||||
}
|
||||
res_s_data = crVal->sampleIdxAll;
|
||||
} // if (sampleIdx) ... else
|
||||
} // if (sampleIdx) ... else
|
||||
|
||||
// Resort internal Idx.
|
||||
te_s_data = res_s_data + s_len;
|
||||
@@ -560,7 +560,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
res_s_data[j] = k;
|
||||
}
|
||||
|
||||
// Duplicate resorted internal Idx.
|
||||
// Duplicate resorted internal Idx.
|
||||
// It will be used to simplify operation of getting trainIdx.
|
||||
te_s_data = res_s_data + s_len;
|
||||
for (i = 0; i < s_len; i++)
|
||||
@@ -573,7 +573,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
{
|
||||
if (k_fold > s_len)
|
||||
{
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
"Error in parameters of cross-validation ('k_fold' > #samples)!");
|
||||
}
|
||||
folds = crVal->folds = (int*) cvAlloc ((k_fold + 1) * sizeof (int));
|
||||
@@ -593,7 +593,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
crVal->max_fold_size = k;
|
||||
if (k >= s_len)
|
||||
{
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
"Error in parameters of cross-validation (-'k_fold' > #samples)!");
|
||||
}
|
||||
crVal->folds_all = k = (s_len - 1) / k + 1;
|
||||
@@ -634,7 +634,7 @@ cvCreateCrossValidationEstimateModel(
|
||||
|
||||
return model;
|
||||
} // End of cvCreateCrossValidationEstimateModel
|
||||
|
||||
|
||||
|
||||
/****************************************************************************************\
|
||||
* Extended interface with backcalls for models *
|
||||
@@ -643,13 +643,13 @@ ML_IMPL float
|
||||
cvCrossValidation (const CvMat* trueData,
|
||||
int tflag,
|
||||
const CvMat* trueClasses,
|
||||
CvStatModel* (*createClassifier) (const CvMat*,
|
||||
int,
|
||||
CvStatModel* (*createClassifier) (const CvMat*,
|
||||
int,
|
||||
const CvMat*,
|
||||
const CvClassifierTrainParams*,
|
||||
const CvMat*,
|
||||
const CvMat*,
|
||||
const CvMat*,
|
||||
const CvMat*,
|
||||
const CvMat*,
|
||||
const CvMat*,
|
||||
const CvMat*),
|
||||
const CvClassifierTrainParams* estimateParams,
|
||||
const CvClassifierTrainParams* trainParams,
|
||||
@@ -676,7 +676,7 @@ cvCrossValidation (const CvMat* trueData,
|
||||
}
|
||||
if (pCrValModel && *pCrValModel && !CV_IS_CROSSVAL(*pCrValModel))
|
||||
{
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
CV_ERROR (CV_StsBadArg,
|
||||
"<pCrValModel> point to not cross-validation model");
|
||||
}
|
||||
|
||||
@@ -698,9 +698,9 @@ cvCrossValidation (const CvMat* trueData,
|
||||
// operation loop
|
||||
for (; crVal->nextStep((CvStatModel*)crVal) != 0; )
|
||||
{
|
||||
CV_CALL (pClassifier = createClassifier (trueData, tflag, trueClasses,
|
||||
CV_CALL (pClassifier = createClassifier (trueData, tflag, trueClasses,
|
||||
trainParams, compIdx, trainDataIdx, typeMask, missedMeasurementMask));
|
||||
CV_CALL (crVal->check ((CvStatModel*)crVal, pClassifier,
|
||||
CV_CALL (crVal->check ((CvStatModel*)crVal, pClassifier,
|
||||
trueData, tflag, trueClasses));
|
||||
|
||||
pClassifier->release (&pClassifier);
|
||||
|
||||
+1399
-1399
File diff suppressed because it is too large
Load Diff
@@ -58,7 +58,7 @@ int CV_AMLTest::run_test_case( int testCaseIdx )
|
||||
{
|
||||
//#define GET_STAT
|
||||
#ifdef GET_STAT
|
||||
const char* data_name = ((CvFileNode*)cvGetSeqElem( dataSetNames, testCaseIdx ))->data.str.ptr;
|
||||
const char* data_name = ((CvFileNode*)cvGetSeqElem( dataSetNames, testCaseIdx ))->data.str.ptr;
|
||||
printf("%s, %s ", name, data_name);
|
||||
const int icount = 100;
|
||||
float res[icount];
|
||||
@@ -66,7 +66,7 @@ int CV_AMLTest::run_test_case( int testCaseIdx )
|
||||
{
|
||||
#endif
|
||||
data.mix_train_and_test_idx();
|
||||
code = train( testCaseIdx );
|
||||
code = train( testCaseIdx );
|
||||
#ifdef GET_STAT
|
||||
float case_result = get_error();
|
||||
|
||||
@@ -94,9 +94,9 @@ int CV_AMLTest::validate_test_results( int testCaseIdx )
|
||||
int iters;
|
||||
float mean, sigma;
|
||||
// read validation params
|
||||
FileNode resultNode =
|
||||
FileNode resultNode =
|
||||
validationFS.getFirstTopLevelNode()["validation"][modelName][dataSetNames[testCaseIdx]]["result"];
|
||||
resultNode["iter_count"] >> iters;
|
||||
resultNode["iter_count"] >> iters;
|
||||
if ( iters > 0)
|
||||
{
|
||||
resultNode["mean"] >> mean;
|
||||
|
||||
Reference in New Issue
Block a user