Trouble with OpenCV Implementation of EM Algorithm using Spherical GMM

asked 2016-03-26 06:42:07 -0600

ornuthewolf gravatar image

updated 2016-03-26 07:14:54 -0600

Hi

I've been trying to use the OpenCV EM code and have had a problem. By setting the maxiter parameter to 1 and using the trainE function I allow only the first expectation step to be completed. I then check the loglikelihood output.

Now my problem lies in the fact that if I change the model from GENERIC to SPHERICAL, I get different values for loglikelihood for the same initialization. However the equations for the expectation step remain the same and the loglikelihoods in the first E-Step should be the same values for both models.

So I am unable to figure out where the problem lies.

Here is my code

#include <opencv2/opencv.hpp>

#define NumObs 100
#define Dim 2
#define numClusters 2
#define maxiter 1

int main(int argc, char** argv)
{   


    cv::Mat X = cv::Mat(NumObs, Dim, CV_64F);
    cv::Mat mean = cv::Mat(numClusters,Dim,CV_64F);
    std::vector<cv::Mat> covar;
    cv::Mat mixfrac = cv::Mat(numClusters, 1, CV_64F);
    cv::Mat logLikelihoods = cv::Mat(NumObs, 1, CV_64F);
    cv::Mat labels = cv::Mat(NumObs, 1, CV_32SC1);
    cv::Mat probs = cv::Mat(NumObs, numClusters, CV_64F);

    int i;
    mean = (cv::Mat_<double>(numClusters, Dim) << 2, 3, 4, 5);

    mixfrac = (cv::Mat_<double>(numClusters, 1) << 0.5, 0.5);

    cv::Mat temp(2, 2, CV_64F);
    temp = (cv::Mat_<double>(Dim, Dim) << 2, 0, 0, 2);
    covar.push_back(temp);
    temp = (cv::Mat_<double>(Dim, Dim) << 3, 0, 0, 3);
    covar.push_back(temp);

    X = (cv::Mat_<double>(NumObs, Dim) << 1.497295712046560, 2.389842806384741
        , -1.152388866819642, 2.324051537951996
        , 0.173091890138541, 3.534436744900519
        , 4.013635589903881, 2.635470732249428
        , 0.343404593485856, 2.710797002553179
        , 1.921946750771822, 2.230097059251492
        , 2.923105482852674, 1.968687377364361
        , 0.833763816549866, 1.621119740290876
        , 1.000974973520235, 0.501003642054062
        , 2.722581031828046, 2.179151938477484
        , 2.061657164408961, 1.870114601526011
        , 2.020337185029648, 1.822954976448330
        , 1.076363394410326, 2.640385082400831
        , 1.543478612011669, 2.241597508998520
        , 1.485372235285237, 2.893724906106137
        , -0.715927044291090, 1.369537907446789
        , 1.864588596758692, 1.688875700397673
        , -0.335577615523311, 2.183975485869516
        , -1.456483320753825, 1.493965172927608
        , -0.712160818106508, 1.912240385882839
        , 0.233365021273562, 1.954626227148290
        , 1.998344873833923, 0.343780671699337
        , 0.880159284239810, 0.926396318575558
        , 0.800239863676473, 2.841303191042556
        , 2.509835594907550, 0.767610261936824
        , -0.806015998003402, 2.440467562347095
        , 2.672846813458981, 1.899891020249070
        , 2.396696676145479, 3.621937374575406
        , 0.151374345266610, 2.569943677660735
        , 2.775466044119043, 1.781063533049695
        , 1.191366838548084, 1.702684062103669
        , -0.232621783623176, 1.631979353155736
        , -1.174537262824991, 1.738099976739696
        , -2.550748638571660, 1.816963733157464
        , 0.039014544632652, 1.808195242782888
        , -2.078943086646864, 2.084038835619226
        , 1.081089945552743, 2.576115695603576
        , -0.916439364072319, 0.708922705869511
        , 0.903297610096863, 2.791378047921652
        , -0.561947988394385, 2.656510856158910
        , 1.216211768410659, 1.219031885881044
        , 0.693337555617687, 1.359035671153648
        , 1.417702588339615, 2.313005179476850
        , 0.430424593609415, 1.132064410966174
        , -0.261023655491497, 1.981218019474763
        , 2.268819458983812, 0.391842019202139
        , 0.806386480042758, 3.199864590240754
        , -0.985989695126394, 0.893059534741550
        , -2.442654072043812, 1.689554199357084
        , 1.210751074224775, 1.499372753495019
        , -0.672567018407857, 2.970910199573346
        , -0.507381574772955, 1.967926272242394
        , 0.247969979069518, 0.947965104732303
        , 2.891713920836473, 0.861177994177589
        , 3.354602636199656, 1.942017768571913
        , -1.693061842976859, 1.760474923494942
        , 3.997953758133590, 1.297952400955861
        , 1.512000798331720, 0.853595479991537
        , 1.423345018469304, 3.082589664236521
        , -0.666695083234841, 1.125720158829322
        , 3.464220247867981, 1.038778673164228
        , 2.933373891043690, 1.628066445794965
        , 2.438787742568557, 1.882463318993010
        , 1.156530661730840 ...
(more)
edit retag flag offensive close merge delete

Comments

1

please put your code here, not on SO.

berak gravatar imageberak ( 2016-03-26 06:53:38 -0600 )edit

thank you !

berak gravatar imageberak ( 2016-03-26 08:04:51 -0600 )edit