opencv/modules/ml/doc/expectation_maximization.rst

217 lines
8.2 KiB
ReStructuredText
Raw Normal View History

2011-05-15 21:15:36 +02:00
Expectation Maximization
========================
2011-05-15 21:15:36 +02:00
The EM (Expectation Maximization) algorithm estimates the parameters of the multivariate probability density function in the form of a Gaussian mixture distribution with a specified number of mixtures.
2011-06-15 15:16:57 +02:00
Consider the set of the N feature vectors
{ :math:`x_1, x_2,...,x_{N}` } from a d-dimensional Euclidean space drawn from a Gaussian mixture:
.. math::
2011-02-26 12:05:10 +01:00
p(x;a_k,S_k, \pi _k) = \sum _{k=1}^{m} \pi _kp_k(x), \quad \pi _k \geq 0, \quad \sum _{k=1}^{m} \pi _k=1,
.. math::
2011-02-26 12:05:10 +01:00
p_k(x)= \varphi (x;a_k,S_k)= \frac{1}{(2\pi)^{d/2}\mid{S_k}\mid^{1/2}} exp \left \{ - \frac{1}{2} (x-a_k)^TS_k^{-1}(x-a_k) \right \} ,
2011-02-26 12:05:10 +01:00
where
:math:`m` is the number of mixtures,
:math:`p_k` is the normal distribution
density with the mean
:math:`a_k` and covariance matrix
2011-05-15 21:15:36 +02:00
:math:`S_k`,
:math:`\pi_k` is the weight of the k-th mixture. Given the number of mixtures
2011-02-26 12:05:10 +01:00
:math:`M` and the samples
2011-05-15 21:15:36 +02:00
:math:`x_i`,
:math:`i=1..N` the algorithm finds the
maximum-likelihood estimates (MLE) of all the mixture parameters,
that is,
:math:`a_k`,
:math:`S_k` and
2011-02-26 12:05:10 +01:00
:math:`\pi_k` :
.. math::
2011-02-26 12:05:10 +01:00
L(x, \theta )=logp(x, \theta )= \sum _{i=1}^{N}log \left ( \sum _{k=1}^{m} \pi _kp_k(x) \right ) \to \max _{ \theta \in \Theta },
.. math::
2011-02-26 12:05:10 +01:00
\Theta = \left \{ (a_k,S_k, \pi _k): a_k \in \mathbbm{R} ^d,S_k=S_k^T>0,S_k \in \mathbbm{R} ^{d \times d}, \pi _k \geq 0, \sum _{k=1}^{m} \pi _k=1 \right \} .
2011-05-15 21:15:36 +02:00
The EM algorithm is an iterative procedure. Each iteration includes
two steps. At the first step (Expectation step or E-step), you find a
2011-02-26 12:05:10 +01:00
probability
:math:`p_{i,k}` (denoted
:math:`\alpha_{i,k}` in the formula below) of
sample ``i`` to belong to mixture ``k`` using the currently
available mixture parameter estimates:
.. math::
2011-02-26 12:05:10 +01:00
\alpha _{ki} = \frac{\pi_k\varphi(x;a_k,S_k)}{\sum\limits_{j=1}^{m}\pi_j\varphi(x;a_j,S_j)} .
2011-05-15 21:15:36 +02:00
At the second step (Maximization step or M-step), the mixture parameter estimates are refined using the computed probabilities:
.. math::
2011-05-15 21:15:36 +02:00
\pi _k= \frac{1}{N} \sum _{i=1}^{N} \alpha _{ki}, \quad a_k= \frac{\sum\limits_{i=1}^{N}\alpha_{ki}x_i}{\sum\limits_{i=1}^{N}\alpha_{ki}} , \quad S_k= \frac{\sum\limits_{i=1}^{N}\alpha_{ki}(x_i-a_k)(x_i-a_k)^T}{\sum\limits_{i=1}^{N}\alpha_{ki}}
2011-02-26 12:05:10 +01:00
Alternatively, the algorithm may start with the M-step when the initial values for
:math:`p_{i,k}` can be provided. Another alternative when
2011-05-15 21:15:36 +02:00
:math:`p_{i,k}` are unknown is to use a simpler clustering algorithm to pre-cluster the input samples and thus obtain initial
:math:`p_{i,k}` . Often (including ML) the
2011-03-08 23:22:24 +01:00
:ref:`kmeans` algorithm is used for that purpose.
2011-06-15 15:16:57 +02:00
One of the main problems of the EM algorithm is a large number
2011-05-15 21:15:36 +02:00
of parameters to estimate. The majority of the parameters reside in
2011-02-26 12:05:10 +01:00
covariance matrices, which are
:math:`d \times d` elements each
2011-05-15 21:15:36 +02:00
where
:math:`d` is the feature space dimensionality. However, in
many practical problems, the covariance matrices are close to diagonal
2011-02-26 12:05:10 +01:00
or even to
:math:`\mu_k*I` , where
2011-05-15 21:15:36 +02:00
:math:`I` is an identity matrix and
:math:`\mu_k` is a mixture-dependent "scale" parameter. So, a robust computation
scheme could start with harder constraints on the covariance
matrices and then use the estimated parameters as an input for a less
constrained optimization problem (often a diagonal covariance matrix is
already a good enough approximation).
**References:**
*
2011-05-15 21:15:36 +02:00
Bilmes98 J. A. Bilmes. *A Gentle Tutorial of the EM Algorithm and its Application to Parameter Estimation for Gaussian Mixture and Hidden Markov Models*. Technical Report TR-97-021, International Computer Science Institute and Computer Science Division, University of California at Berkeley, April 1998.
.. index:: CvEMParams
.. _CvEMParams:
CvEMParams
----------
.. c:type:: CvEMParams
2011-05-15 21:15:36 +02:00
Parameters of the EM algorithm ::
struct CvEMParams
{
CvEMParams() : nclusters(10), cov_mat_type(CvEM::COV_MAT_DIAGONAL),
2011-02-26 12:05:10 +01:00
start_step(CvEM::START_AUTO_STEP), probs(0), weights(0), means(0),
covs(0)
{
2011-02-26 12:05:10 +01:00
term_crit=cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
100, FLT_EPSILON );
}
2011-02-26 12:05:10 +01:00
CvEMParams( int _nclusters, int _cov_mat_type=1/*CvEM::COV_MAT_DIAGONAL*/,
int _start_step=0/*CvEM::START_AUTO_STEP*/,
CvTermCriteria _term_crit=cvTermCriteria(
2011-02-26 12:05:10 +01:00
CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
100, FLT_EPSILON),
const CvMat* _probs=0, const CvMat* _weights=0,
const CvMat* _means=0, const CvMat** _covs=0 ) :
2011-02-26 12:05:10 +01:00
nclusters(_nclusters), cov_mat_type(_cov_mat_type),
start_step(_start_step),
2011-02-26 12:05:10 +01:00
probs(_probs), weights(_weights), means(_means), covs(_covs),
term_crit(_term_crit)
{}
2011-02-26 12:05:10 +01:00
int nclusters;
int cov_mat_type;
int start_step;
const CvMat* probs;
const CvMat* weights;
const CvMat* means;
const CvMat** covs;
CvTermCriteria term_crit;
};
2011-03-03 08:29:55 +01:00
2011-05-15 21:15:36 +02:00
The structure has two constructors. The default one represents a rough rule-of-the-thumb. With another one it is possible to override a variety of parameters from a single number of mixtures (the only essential problem-dependent parameter) to initial values for the mixture parameters.
.. index:: CvEM
.. _CvEM:
CvEM
----
.. c:type:: CvEM
2011-05-15 21:15:36 +02:00
EM model ::
class CV_EXPORTS CvEM : public CvStatModel
{
public:
// Type of covariance matrices
enum { COV_MAT_SPHERICAL=0, COV_MAT_DIAGONAL=1, COV_MAT_GENERIC=2 };
2011-02-26 12:05:10 +01:00
2011-05-15 21:15:36 +02:00
// Initial step
enum { START_E_STEP=1, START_M_STEP=2, START_AUTO_STEP=0 };
2011-02-26 12:05:10 +01:00
CvEM();
CvEM( const Mat& samples, const Mat& sample_idx=Mat(),
CvEMParams params=CvEMParams(), Mat* labels=0 );
virtual ~CvEM();
2011-02-26 12:05:10 +01:00
virtual bool train( const Mat& samples, const Mat& sample_idx=Mat(),
CvEMParams params=CvEMParams(), Mat* labels=0 );
2011-02-26 12:05:10 +01:00
virtual float predict( const Mat& sample, Mat& probs ) const;
virtual void clear();
2011-02-26 12:05:10 +01:00
int get_nclusters() const { return params.nclusters; }
const Mat& get_means() const { return means; }
const Mat&* get_covs() const { return covs; }
const Mat& get_weights() const { return weights; }
const Mat& get_probs() const { return probs; }
2011-02-26 12:05:10 +01:00
protected:
2011-02-26 12:05:10 +01:00
virtual void set_params( const CvEMParams& params,
const CvVectors& train_data );
virtual void init_em( const CvVectors& train_data );
virtual double run_em( const CvVectors& train_data );
virtual void init_auto( const CvVectors& samples );
virtual void kmeans( const CvVectors& train_data, int nclusters,
Mat& labels, CvTermCriteria criteria,
const Mat& means );
CvEMParams params;
double log_likelihood;
2011-02-26 12:05:10 +01:00
Mat& means;
Mat&* covs;
Mat& weights;
Mat& probs;
2011-02-26 12:05:10 +01:00
Mat& log_weight_div_det;
Mat& inv_eigen_values;
Mat&* cov_rotate_mats;
};
2011-03-03 08:29:55 +01:00
.. index:: CvEM::train
.. _CvEM::train:
CvEM::train
-----------
.. cpp:function:: void CvEM::train( const Mat& samples, const Mat& sample_idx=Mat(), CvEMParams params=CvEMParams(), Mat* labels=0 )
2011-05-15 21:15:36 +02:00
Estimates the Gaussian mixture parameters from a sample set.
2011-05-15 21:15:36 +02:00
Unlike many of the ML models, EM is an unsupervised learning algorithm and it does not take responses (class labels or function values) as input. Instead, it computes the
*Maximum Likelihood Estimate* of the Gaussian mixture parameters from an input sample set, stores all the parameters inside the structure:
:math:`p_{i,k}` in ``probs``,
:math:`a_k` in ``means`` ,
:math:`S_k` in ``covs[k]``,
:math:`\pi_k` in ``weights`` , and optionally computes the output "class label" for each sample:
:math:`\texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N` (indices of the most probable mixture for each sample).
2011-05-15 21:15:36 +02:00
The trained model can be used further for prediction, just like any other classifier. The trained model is similar to the
2011-03-08 23:22:24 +01:00
:ref:`Bayes classifier`.
For example of clustering random samples of multi-Gaussian distribution using EM see em.cpp sample in OpenCV distribution.
2011-03-03 08:29:55 +01:00