Expectation Maximization

This section describes obsolete C interface of EM algorithm. Details of the algorithm and its C++ interface can be found in the other section Expectation Maximization.

Note

  • An example on using the Expectation Maximalization algorithm can be found at opencv_source_code/samples/cpp/em.cpp
  • (Python) An example using Expectation Maximalization for Gaussian Mixing can be found at opencv_source_code/samples/python2/gaussian_mix.py

CvEMParams

struct CvEMParams

Parameters of the EM algorithm. All parameters are public. You can initialize them by a constructor and then override some of them directly if you want.

CvEMParams::CvEMParams

The constructors

C++: CvEMParams::CvEMParams()
C++: CvEMParams::CvEMParams(int nclusters, int cov_mat_type=EM::COV_MAT_DIAGONAL, int start_step=EM::START_AUTO_STEP, CvTermCriteria term_crit=cvTermCriteria(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 )
Parameters:
  • nclusters – The number of mixture components in the Gaussian mixture model. Some of EM implementation could determine the optimal number of mixtures within a specified value range, but that is not the case in ML yet.
  • cov_mat_type

    Constraint on covariance matrices which defines type of matrices. Possible values are:

    • CvEM::COV_MAT_SPHERICAL A scaled identity matrix \mu_k * I. There is the only parameter \mu_k to be estimated for each matrix. The option may be used in special cases, when the constraint is relevant, or as a first step in the optimization (for example in case when the data is preprocessed with PCA). The results of such preliminary estimation may be passed again to the optimization procedure, this time with cov_mat_type=CvEM::COV_MAT_DIAGONAL.
    • CvEM::COV_MAT_DIAGONAL A diagonal matrix with positive diagonal elements. The number of free parameters is d for each matrix. This is most commonly used option yielding good estimation results.
    • CvEM::COV_MAT_GENERIC A symmetric positively defined matrix. The number of free parameters in each matrix is about d^2/2. It is not recommended to use this option, unless there is pretty accurate initial estimation of the parameters and/or a huge number of training samples.
  • start_step

    The start step of the EM algorithm:

    • CvEM::START_E_STEP Start with Expectation step. You need to provide means a_k of mixture components to use this option. Optionally you can pass weights \pi_k and covariance matrices S_k of mixture components.
    • CvEM::START_M_STEP Start with Maximization step. You need to provide initial probabilities p_{i,k} to use this option.
    • CvEM::START_AUTO_STEP Start with Expectation step. You need not provide any parameters because they will be estimated by the kmeans algorithm.
  • term_crit – The termination criteria of the EM algorithm. The EM algorithm can be terminated by the number of iterations term_crit.max_iter (number of M-steps) or when relative change of likelihood logarithm is less than term_crit.epsilon.
  • probs – Initial probabilities p_{i,k} of sample i to belong to mixture component k. It is a floating-point matrix of nsamples \times nclusters size. It is used and must be not NULL only when start_step=CvEM::START_M_STEP.
  • weights – Initial weights \pi_k of mixture components. It is a floating-point vector with nclusters elements. It is used (if not NULL) only when start_step=CvEM::START_E_STEP.
  • means – Initial means a_k of mixture components. It is a floating-point matrix of nclusters \times dims size. It is used used and must be not NULL only when start_step=CvEM::START_E_STEP.
  • covs – Initial covariance matrices S_k of mixture components. Each of covariance matrices is a valid square floating-point matrix of dims \times dims size. It is used (if not NULL) only when start_step=CvEM::START_E_STEP.

The default constructor represents a rough rule-of-the-thumb:

CvEMParams() : nclusters(10), cov_mat_type(1/*CvEM::COV_MAT_DIAGONAL*/),
    start_step(0/*CvEM::START_AUTO_STEP*/), probs(0), weights(0), means(0), covs(0)
{
    term_crit=cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, FLT_EPSILON );
}

With another constructor 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.

CvEM

class CvEM : public CvStatModel

The class implements the EM algorithm as described in the beginning of the section Expectation Maximization.

CvEM::train

Estimates the Gaussian mixture parameters from a sample set.

C++: bool CvEM::train(const Mat& samples, const Mat& sampleIdx=Mat(), CvEMParams params=CvEMParams(), Mat* labels=0 )
C++: bool CvEM::train(const CvMat* samples, const CvMat* sampleIdx=0, CvEMParams params=CvEMParams(), CvMat* labels=0 )
Parameters:
  • samples – Samples from which the Gaussian mixture model will be estimated.
  • sampleIdx – Mask of samples to use. All samples are used by default.
  • params – Parameters of the EM algorithm.
  • labels – The optional output “class label” for each sample: \texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N (indices of the most probable mixture component for each sample).

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: p_{i,k} in probs, a_k in means , S_k in covs[k], \pi_k in weights , and optionally computes the output “class label” for each sample: \texttt{labels}_i=\texttt{arg max}_k(p_{i,k}), i=1..N (indices of the most probable mixture component for each sample).

The trained model can be used further for prediction, just like any other classifier. The trained model is similar to the CvNormalBayesClassifier.

For an example of clustering random samples of the multi-Gaussian distribution using EM, see em.cpp sample in the OpenCV distribution.

CvEM::predict

Returns a mixture component index of a sample.

C++: float CvEM::predict(const Mat& sample, Mat* probs=0 ) const
C++: float CvEM::predict(const CvMat* sample, CvMat* probs) const
Parameters:
  • sample – A sample for classification.
  • probs – If it is not null then the method will write posterior probabilities of each component given the sample data to this parameter.

CvEM::getNClusters

Returns the number of mixture components M in the Gaussian mixture model.

C++: int CvEM::getNClusters() const
C++: int CvEM::get_nclusters() const

CvEM::getMeans

Returns mixture means a_k.

C++: Mat CvEM::getMeans() const
C++: const CvMat* CvEM::get_means() const

CvEM::getCovs

Returns mixture covariance matrices S_k.

C++: void CvEM::getCovs(std::vector<cv::Mat>& covs) const
C++: const CvMat** CvEM::get_covs() const

CvEM::getWeights

Returns mixture weights \pi_k.

C++: Mat CvEM::getWeights() const
C++: const CvMat* CvEM::get_weights() const

CvEM::getProbs

Returns vectors of probabilities for each training sample.

C++: Mat CvEM::getProbs() const
C++: const CvMat* CvEM::get_probs() const

For each training sample i (that have been passed to the constructor or to CvEM::train()) returns probabilities p_{i,k} to belong to a mixture component k.

CvEM::getLikelihood

Returns logarithm of likelihood.

C++: double CvEM::getLikelihood() const
C++: double CvEM::get_log_likelihood() const

CvEM::write

Writes the trained Gaussian mixture model to the file storage.

C++: void CvEM::write(CvFileStorage* fs, const char* name) const
Parameters:
  • fs – A file storage where the model will be written.
  • name – A name of the file node where the model data will be written.

CvEM::read

Reads the trained Gaussian mixture model from the file storage.

C++: void CvEM::read(CvFileStorage* fs, CvFileNode* node)
Parameters:
  • fs – A file storage with the trained model.
  • node – The parent map. If it is NULL, the function searches a node with parameters in all the top-level nodes (streams), starting with the first one.