Ask Your Question

How to modify EM to show loglikelihood every iteration

asked 2017-01-08 20:02:11 -0600

juborg gravatar image

updated 2017-01-08 23:25:42 -0600


i'm having troubles changing the source code for em algorithm. The em algorithm in opencv gives loglikelihood values for every data point. I would like to get the sum of the loglikelihood values for every iteration of the em algorithm. I have tried to modify "opencv\sources\modules\ml\src\em.cpp" by adding :

Mat loglike;
void setloglike(double f) {
    if (loglike.empty())
        loglike = Mat(1, 1, CV_64F);
Mat getloglike() const {
    return loglike;

After rebuilding i couldnt use this function, it was not "there", so i added to "opencv\sources\modules\ml\include\opencv2\ml.hpp" my lines :

CV_WRAP virtual Mat getloglike() const = 0;
CV_WRAP virtual void setloglike(double f) = 0;

but the result is crashing when i sue that function. I'm calling the function in my own program through this code:

Mat loglikelihood, _label, _probs;
Ptr<EM> em = EM::create();
em->setTermCriteria(TermCriteria(TermCriteria::TermCriteria::COUNT, 50, 0));
em->trainEM(DATA, loglikelihood, _label, _probs);
Mat Log = em->getloglike();

thank you for your help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-08 21:11:55 -0600

Tetragramm gravatar image

You modified the class EMImpl, right? Look at the declaration. It inherits from the public interface EM, which is what you use in your code.

class CV_EXPORTS EMImpl : public EM

Add the methods to the class EM, and you should be able to use them.

edit flag offensive delete link more


i was able to "access" my code now, but it is crashing, so i guess my code i wrote in the 1st comment isnt working :/

juborg gravatar imagejuborg ( 2017-01-08 21:50:03 -0600 )edit

These are actually part of the EMImpl class, right? That's a member variable and such? Also, where are you calling setloglike and getloglike from?

Tetragramm gravatar imageTetragramm ( 2017-01-08 22:04:12 -0600 )edit

yeah they are part of that class, i call them from my own programm where i included opencv.

juborg gravatar imagejuborg ( 2017-01-08 22:19:23 -0600 )edit

I think you need to include the code that does the calling and creation of the EM in your code. You can comment out or remove anything not calling the EM class.

Just edit the question so we can see.

Tetragramm gravatar imageTetragramm ( 2017-01-08 22:29:18 -0600 )edit

Ok, you're still missing the place where the setloglike function is called. You need to post the modifications you made to use that code, which is in the em.cpp.

Tetragramm gravatar imageTetragramm ( 2017-01-09 17:55:42 -0600 )edit

Question Tools

1 follower


Asked: 2017-01-08 20:02:11 -0600

Seen: 170 times

Last updated: Jan 08 '17