DytexMix.cpp

00001 
00008 #include <iostream>
00009 #include <string>
00010 #include <iterator>
00011 #include<fstream>
00012 
00013 #include "opencv/cv.h"
00014 #include "opencv/cxcore.h"
00015 #include "opencv/highgui.h"
00016 
00017 
00018 #include "options.h"
00019 #include "utils/libutils.h"
00020 #include "video/libvideo.h"
00021 #include "stats/libstats.h"
00022 #include "dytex/libdytex.h"
00023 
00024 
00025 using namespace std;
00026 using namespace cv;
00027 
00028 
00029 
00030 //initialize a mixture
00040 DytexMix::DytexMix()
00041 {
00042 }
00043 
00054 DytexMix::DytexMix(DytexOptions op):opt(op)
00055 {
00056 }
00057 
00077 void DytexMix::learnWithSplitting(std::vector<Mat> &Yin,EMOptions emopt)
00078 {
00079         double pert;    
00080         pert=emopt.splitOpt.pert;
00081         //initialize splitting sequence
00082         if(emopt.splitOpt.sched.empty())
00083         {
00084                 for(int i=1;i<=emopt.K;i++)
00085                         emopt.splitOpt.sched.push_back(i);
00086         }
00087         //check for valid splitting sched
00088         if(emopt.splitOpt.sched[0]!=1)
00089         {
00090                 CV_Error(-1, "schedule must start with 1!");
00091         }
00092         std::vector<int> tmp;
00093         vector<int>::iterator it;
00094         for(int i=1;i<emopt.splitOpt.sched.size();i++)
00095                 tmp.push_back(emopt.splitOpt.sched[i]/emopt.splitOpt.sched[i-1]);
00096         
00097         if(find_if (tmp.begin(), tmp.end(), IsGT2)!=tmp.end())
00098                 CV_Error(-1, "cannot grow K more than 2 times previous");
00099 
00100         cout<<"Growing schedule: ";
00101         copy(emopt.splitOpt.sched.begin(), emopt.splitOpt.sched.end(),ostream_iterator<int>(cout," "));
00102         cout<<endl;
00103         cout<<"Ks: "<<emopt.K<<endl;    
00104 
00105         int Kiter=1;
00106 
00107         while(dt.size()<emopt.K)
00108         {
00109                 //do first cluster
00110                 if(Kiter==1)
00111                 {                        
00112                         cout<<"*** EM: K="<<dt.size()+1<<"******************"<<endl;
00113                 }
00114                 else
00115                 {
00116                         std::vector<int> mysplits;
00117                         
00118                         //splitting current mixture
00119                         while(dt.size()<emopt.splitOpt.sched[Kiter-1])
00120                         {
00121                                 DytexSplitParams splitopt;
00122                                 splitopt.crit=emopt.splitOpt.crit;                              
00123                                 splitopt.ignore=mysplits;
00124                                 splitopt.target=-1;
00125                                 splitopt.pert=emopt.splitOpt.pert;
00126                                 splitopt.mode=emopt.splitOpt.mode;
00127                                 splitopt.vars=emopt.splitOpt.vars;
00128                                 int c1,c2;
00129                                 dytex_mix_split(splitopt,c2,c1);
00130                                 mysplits.push_back(c1);
00131                                 mysplits.push_back(c2);
00132                         }
00133                         cout<<"*** EM: K="<<dt.size()<<"******************"<<endl;
00134                 }
00135                 runEM(Yin,emopt);  //RUN EM uisng current Mixture               
00136                 Kiter++;
00137         }
00138 
00139         //RUN EM again on once on final solution
00140         emopt.termvalue=emopt.termvalBest;
00141         runEM(Yin,emopt);
00142         //initialize kalman caches
00143         setupKFB(Yin[0].size[0]);
00144 }
00145 
00164 DytexMix DytexMix::reduceWithSplitting(HEMOptions hopt)
00165 {
00166         //reduced mixture
00167         DytexMix hembest(this->opt);    
00168 
00169         //OPTIONS
00170         double pert=hopt.splitOpt.pert;
00171         int Ks=hopt.K;
00172         //initialize splitting sequence
00173         if(hopt.splitOpt.sched.empty())
00174         {
00175                 for(int i=1;i<=hopt.K;i++)
00176                         hopt.splitOpt.sched.push_back(i);
00177         }
00178         
00179         //%%% preprocess %%%
00180         cout<<"preprocessing DT..."<<endl;      
00181         for(int i=0;i<dt.size();i++)
00182         {
00183                 SVD svd(dt[i].C);
00184                 Mat Cu,Cs,Cv;
00185                 svd.u.copyTo(Cu);
00186                 svd.vt.copyTo(Cv);      
00187                 Cv=Cv.t();
00188                 Cs=Mat::zeros(svd.w.rows,svd.w.rows,svd.w.type());                              
00189                 Mat tmpM=Cs.diag();
00190                 svd.w.copyTo(tmpM);
00191                 tmpM=Cv*Cs;             
00192                 tmpM.copyTo(dt[i].Cvs);
00193                 dt[i].isCvs=true;
00194         }
00195 
00196         //check for valid splitting sched
00197         if(hopt.splitOpt.sched[0]!=1)
00198         {
00199                 CV_Error(-1, "schedule must start with 1!");
00200         }
00201         std::vector<int> tmp;
00202         vector<int>::iterator it;
00203         for(int i=1;i<hopt.splitOpt.sched.size();i++)
00204                 tmp.push_back(hopt.splitOpt.sched[i]/hopt.splitOpt.sched[i-1]);
00205 
00206         if(find_if (tmp.begin(), tmp.end(), IsGT2)!=tmp.end())
00207                 CV_Error(-1, "cannot grow K more than 2 times previous");
00208 
00209         cout<<"Growing schedule: ";
00210         copy(hopt.splitOpt.sched.begin(), hopt.splitOpt.sched.end(),ostream_iterator<int>(cout," "));
00211         cout<<endl;
00212         cout<<"Ks: "<<Ks<<endl;
00213 
00214         //HEM splitting loop
00215         int Kiter=1;
00216         while(hembest.dt.size()<hopt.K)
00217         {
00218                 if(Kiter==1)
00219                 {
00220                         cout<<"*** EM: K="<<hembest.dt.size()+1<<" ***********************"<<endl;
00221                 }
00222                 else
00223                 {
00224                         std::vector<int> mysplits;
00225                         //split here
00226                         while(hembest.dt.size()<hopt.splitOpt.sched[Kiter-1])
00227                         {
00228                                 DytexSplitParams splitopt;
00229                                 splitopt.crit=hopt.splitOpt.crit;                               
00230                                 splitopt.ignore=mysplits;
00231                                 splitopt.target=-1;
00232                                 splitopt.pert=hopt.splitOpt.pert;
00233                                 splitopt.mode=hopt.splitOpt.mode;
00234                                 splitopt.vars=hopt.splitOpt.vars;
00235                                 int c1,c2;
00236                                 hembest.dytex_mix_split(splitopt,c2,c1);
00237                                 mysplits.push_back(c1);
00238                                 mysplits.push_back(c2);
00239                         }
00240                         //remove pre-cache (since it is invalid after splitting)
00241                         for(int ii=0;ii<hembest.dt.size();ii++)
00242                         {
00243                                 hembest.dt[ii].isCvs=false;
00244                         }
00245                         cout<<"*** EM: K="<<hembest.dt.size()<<"******************"<<endl;
00246                 }
00247                 std::vector<int> classes;
00248                 //runs HEM algorithm for current mixture
00249                 runHEM(hembest,hopt,classes);
00250                 Kiter++;
00251         }
00252 
00253         //RUN HEM again on once on final solution                       
00254         hopt.termvalue=hopt.termvalBest;        
00255         hopt.maxiter=50;  //Can be adjusted to run more iterations
00256         runHEM(hembest,hopt,hembest.classes);
00257         return hembest;
00258 }
00259 
00260 
00280 void DytexMix::getClasses(std::vector<Mat> Y,std::vector<int> & classes)
00281 {
00282         //check for valid input
00283         int N=Y.size();
00284         if(N==0)
00285                 return;
00286         int K=dt.size();        
00287         
00288         Mat LL(N,K,OPT_MAT_TYPE);
00289         Mat logZ(N,K,OPT_MAT_TYPE);
00290         //compute logliklihood under each component
00291         for(int i=0;i<K;i++)
00292         {               
00293                 Mat tmpL;
00294                 kfb[i].loglike(Y,tmpL);
00295                 Mat tempM=tmpL.t()+log(alpha[i]);
00296                 Mat tmpM;
00297                 tmpM=LL.col(i);
00298                 tempM.copyTo(tmpM);
00299         }
00300         //calculate log(sum(A)) using only log(A)
00301         Mat tmp=logtrick(LL.t());
00302         tmp=tmp.t();
00303 
00304         //computing Z(posterior probability for each component) and logZ (log of Z)
00305         for(int i=0;i<K;i++)
00306         {
00307                 Mat tmpM=LL.col(i)-tmp;
00308                 Mat tmpM2;
00309                 tmpM2=logZ.col(i);
00310                 tmpM.copyTo(tmpM2);
00311         }
00312         Mat Z;
00313         exp(logZ,Z);
00314 
00315         //get classes from max probability      
00316         for(int i=0;i<Z.rows;i++)
00317         {
00318                 double minV,maxV;
00319                 Point minL,maxL;
00320                 minMaxLoc(Z.row(i),&minV,&maxV,&minL,&maxL);
00321                 classes.push_back(maxL.x+1);
00322         }
00323 }
00324 
00343 void DytexMix::getClasses(std::vector<Mat> Y, Mat &LLm)
00344 {
00345         int N=Y.size();
00346         int K=dt.size();        
00347         Mat LL(N,K,OPT_MAT_TYPE);       
00348         //compute logliklihood under each component
00349         for(int i=0;i<K;i++)
00350         {               
00351                 Mat tmpL;
00352                 kfb[i].loglike(Y,tmpL);
00353                 Mat tempM=tmpL.t()+log(alpha[i]);
00354                 Mat tmpM;
00355                 tmpM=LL.col(i);
00356                 tempM.copyTo(tmpM);
00357         }
00358         //LLm=LL;
00359         LL.copyTo(LLm);
00360 }
00361 
00362 
00373 void DytexMix::setupKFB(int tau)
00374 {
00375         //Setup the kbf for each dt
00376         kfb.clear();
00377         for(int i=0;i<dt.size();i++)
00378         {
00379                 DytexKalmanFilter kf(dt[i],tau);
00380                 kfb.push_back(kf);
00381         }
00382 }
00383 
00385 
00386 
00387 
00404 void DytexMix::runEM(const std::vector<Mat> &Yin,const EMOptions &emopt)
00405 {
00406         clock_t elapsedtime;    
00407         //used to display info in change in classes during EM loop
00408         int numlastclasses=5;                   
00409         //erase old class info
00410         classes.clear();  
00411         
00412         //options
00413         DytexOptions::Ymean_type FlagYmean   =   opt.Yopt;
00414         EMOptions::Verbose_mode FlagVerbose = emopt.verbose;    
00415         int n=opt.n;
00416         int N=Yin.size();
00417         //min total probability for blank cluster
00418         double MINPROB =(((double)1.0)/(((double)2.0)*(double)N));  
00419         
00420         /******************PREPROCESSING*********************/
00421         if(FlagVerbose)
00422                 cout<<"Converting images..."<<endl;
00423 
00424         //convert movie into a data matrix Y(:,i,t)
00425         //this makes running the Kalman filter a lot faster
00426         Mat foo;
00427         int mysx,mysy,mysc,mycnorm;
00428         int tau,dy;
00429         Mat Y;
00430         std::vector<Mat> Ymean;
00431         for(int yi=0;yi<N;yi++)
00432         {
00433                 foo=convertmovie(Yin[yi],mysx,mysy,mysc,mycnorm);
00434                 tau=foo.cols;
00435                 dy=foo.rows;
00436                 if(yi==0)
00437                 {
00438                         int sz[]={tau,dy,N};                            
00439                         Y=MatVid::create(tau,dy,N,foo.type()); 
00440                 }
00441                 else
00442                 {
00443                         if(tau!=Y.size[0])
00444                                 CV_Error(-1,"Y has videos with different lengths!");
00445                 }
00446 
00447                 if(FlagYmean) 
00448                 {
00449                         Mat tmpM;
00450                         reduce(foo,tmpM,2,CV_REDUCE_AVG);
00451                         Ymean.push_back(tmpM);
00452                 }
00453                 
00454                 for(int j=0;j<dy;j++)
00455                         for(int k=0;k<tau;k++)
00456                                 Y.at<OPT_F_TYPE>(k,j,yi)=foo.at<OPT_F_TYPE>(j,k);       
00457         }
00458         /*************************************************************************/     
00459 
00460         /***********INITIALIZATION ****************************************/
00461         //initialization of first DT in the blank mixture using Sub-optimal methhod
00462         if(dt.size()==0)  
00463         {
00464                 std::vector<int> randind(N,0);
00465                 for(int i=0;i<N;i++)
00466                         randind[i]=i;
00467 
00468                 if(FlagVerbose)
00469                         cout<<"Initializing First DT with Sub-optimal: "<<endl;
00470                 
00471                 std::vector<Mat> tmpY = extractY(Y, randind);
00472                 Dytex tmpC= initcluster_doretto(tmpY, opt);
00473                 dt.push_back(tmpC);
00474                 alpha.push_back(1.0);
00475         }
00476         int K=dt.size(); //current mixture size
00477         //Regularize the initializations
00478         for(int i=0;i<K;i++)
00479         {               
00480                 dt[i].setRegularizer(emopt.regopt);
00481                 dt[i].regularize();                                                     
00482         }
00483         /**************************************************************************/
00484                 
00485         /*****************RUN EM loooop*************************/
00486         
00487         //initialize convergence measures       
00488         std::vector<double> datalikelihood(emopt.maxiter+1,0);
00489         std::vector<double> ddtall(emopt.maxiter,0);
00490         std::vector<double> pdtall(emopt.maxiter,0);
00491         std::vector<std::vector<int> > lastclasses(numlastclasses);
00492         int lastclassesind = 0;
00493         Mat Z;
00494         Mat logZ;
00495         for(int i=0;i<numlastclasses;i++)
00496                 lastclasses[i]=std::vector<int>(N,0);
00497 
00498         //initializa blanks
00499         std::vector<double> blank(K,0);
00500         for(int j=0;j<K;j++)
00501         {
00502                 if(dt[j].isempty)
00503                         blank[j]=1;
00504         }
00505 
00506         //initialize loop
00507         clock_t starttime=clock();      
00508         int iter=0;
00509         double ddLL,dpLL;
00510         //loop EM********************
00511         while(1)
00512         {
00513                 if(FlagVerbose>=2)
00514                 {
00515                         cout<<"***** iter "<<iter<<" *****"<<endl;
00516                         cout<<"E-Step: Running Kalman filter: ";
00517                 }
00518                 
00519                 //state expectation
00520                 std::vector<std::vector<Mat> > xthat;
00521                 //state covariance
00522                 std::vector<Mat> Vthat,Vtt1hat;
00523                 //log-likelihoods of each Y
00524                 Mat LL(N,K,OPT_MAT_TYPE);
00525 
00526                 string di3opt="";
00527                 //%%%% E-Step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00528                 for(int j=0;j<K;j++)
00529                 {
00530                         if (FlagVerbose >= 2)
00531                                 cout<<j<<" ";
00532 
00533                         //option to subtract Ymean
00534                         if (FlagYmean)
00535                                 di3opt = "y";
00536                         else
00537                                 di3opt = "";
00538                         
00539                         if (blank[j]==0)
00540                         {
00541                                 //compute likelihood of each video for this cluster
00542                                 std::vector<Mat> xt;
00543                                 Mat Vt,Vtt1;
00544                                 Mat tmpL;
00545                                 Dytex test1=dt[j];
00546                                 //conditional state inference using Kalman smoothing filter
00547                                 DytexKalmanFilter::dytex_kalman(Y, dt[j], di3opt,xt,Vt,Vtt1,tmpL);      
00548                                 xthat.push_back(xt);
00549                                 Vthat.push_back(Vt);
00550                                 Vtt1hat.push_back(Vtt1);
00551                                 LL.col(j) = tmpL.t() + log(alpha[j]);
00552                         }
00553                         else
00554                         {
00555                                 LL.col(j) = (double)(-1e300); // a very small number...
00556                         }                               
00557                 }
00558                                 
00559                 //% soft assignment and data likelihood (no constraints)
00560                 Mat tmp = (logtrick(LL.t())).t();                               
00561                 logZ.create(tmp.rows,K,OPT_MAT_TYPE);
00562                 for(int j=0;j<K;j++)
00563                 {
00564                         logZ.col(j) = LL.col(j) - tmp;
00565                 }
00566                 Scalar stmp=sum(tmp);
00567                 datalikelihood[iter] = stmp[0];
00568                 exp(logZ,Z);            
00569 
00570                 if (FlagVerbose >= 2)
00571                         cout<<endl;
00572 
00573                 //hard assignment
00574                 classes.clear();
00575                 for(int i=0;i<Z.rows;i++)
00576                 {
00577                         double min,max;
00578                         Point minL,maxL;
00579                         minMaxLoc(Z.row(i),&min,&max,&minL,&maxL);                              
00580                         classes.push_back(maxL.x+1);
00581                 }
00582 
00583                 //Check Convergence
00584                 if(iter>0)
00585                 {
00586                         //compute change in log-likelihood
00587                         ddLL=datalikelihood[iter]-datalikelihood[iter-1];
00588                         dpLL=abs(ddLL/datalikelihood[iter-1]);
00589                 }
00590                 else
00591                 {
00592                         ddLL = INF;
00593                         dpLL = INF;                     
00594                         
00595                 }
00596                 //class assignment info
00597                 lastclasses[lastclassesind]=classes;
00598 
00599                 //count the number of class changes
00600                 std::vector<int> dclass;
00601                 for(int ii=0;ii<numlastclasses;ii++)
00602                 {
00603                         int sum=0;
00604                         for(int i=0;i<lastclasses[0].size();i++)
00605                         {
00606                                 if(lastclasses[ii][i]!=lastclasses[lastclassesind][i])
00607                                         sum++;
00608                         }
00609                         dclass.push_back(sum);
00610                 }
00611 
00612                 string dclassstr="";                    
00613                 for(int i=lastclassesind+1;i<numlastclasses;i++)
00614                 {
00615                         stringstream ss;
00616                         ss<<dclass[i];
00617                         dclassstr=dclassstr+ss.str()+" ";
00618                 }
00619                 for(int i=0;i<lastclassesind;i++)
00620                 {
00621                         stringstream ss;
00622                         ss<<dclass[i];
00623                         dclassstr=dclassstr+ss.str()+" ";
00624                 }
00625 
00626                 //% lastclassind points to the oldest classes
00627                 lastclassesind = lastclassesind+1;
00628                 if (lastclassesind>=numlastclasses)
00629                         lastclassesind = 0;
00630 
00631 
00632                 //output strings
00633                 stringstream ss2;
00634                 ss2<<"dclass = "<<dclassstr;
00635                 string outstr2=ss2.str();
00636                 string outstr1s;
00637                 string outstr3;         
00638                 stringstream ss3;                                       
00639                 ss3<<"L= "<<datalikelihood[iter]<<" (pL= "<<dpLL<<")";
00640                 outstr1s=ss3.str();             
00641                 if(FlagVerbose==1)
00642                 {
00643                         stringstream ss3;
00644                         ss3<<"iter= "<<iter+1<<"; "<<outstr1s<<"; "<<outstr2<<";  ";
00645                         outstr3=ss3.str();
00646                         cout<<outstr3<<endl;
00647                 }
00648                 else if(FlagVerbose>=2)
00649                 {                       
00650                         cout<<outstr2<<endl;
00651                 }
00652                 // check if negative change in log-likelihood!
00653                 if (ddLL<0)
00654                 {
00655                         cout<<"WARNING -- change in log likelihood is negative???"<<endl;       
00656                 }       
00657 
00658 
00659                 //%%% check convergence conditions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00660                 int breakout = 0;
00661                 string termstr;
00662                 if (iter >= emopt.maxiter)
00663                 {
00664                         termstr = "***** done -- max iter reached\n";
00665                         breakout = 1;
00666                 }
00667 
00668                 //only this convergence condition
00669                 if ( (ddLL >= 0) && (dpLL < emopt.termvalue) )
00670                 {
00671                         termstr = "***** done -- small percent change in data likelihood\n";
00672                         breakout = 1;
00673                 }
00674                         
00675                 //%%% convergence condition was reached... %%%%%%%%%%%%%%%%%%%%%%%%%%%%
00676                 if (breakout)
00677                 {
00678                         if (FlagVerbose >= 1)
00679                         {
00680                                 if (FlagVerbose == 1)
00681                                 {
00682                                         cout<<endl;                             
00683                                 }
00684                                 cout<<termstr<<endl;
00685                         }       
00686                         break;
00687                 }
00688 
00689                 //%%% M-Step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
00690 
00691                 //1) update prior probabilities
00692                 //total soft assignments per cluster
00693                 Mat Nhat;
00694                 reduce(Z,Nhat,0,CV_REDUCE_SUM);
00695                 tmp=Nhat/((double)N);           
00696                 for(int i=0;i<alpha.size();i++)
00697                         alpha[i]=tmp.at<OPT_F_TYPE>(0,i);
00698 
00699                 
00700                 //%%% loop through each cluster and compute aggregate statistics%%%
00701                 for(int j=0;j<K;j++)
00702                 {               
00703                         //output of Kalman filters
00704                         std::vector<Mat> jxthat;
00705                         Mat jYmean;
00706                         Mat jz;
00707                         Mat PhiAll,Phi,sphi,Psi,Lambda,Gamma,Eta,Xi;
00708                         //check if this is cluster is blank
00709                         if(alpha[j]<=MINPROB)
00710                         {
00711                                 blank[j]=1;
00712                                 dt[j].isempty=true;
00713                                 if (FlagVerbose >= 1)
00714                                         cout<<"blank";
00715                         }                       
00716                         else // % --- standard M-step: learn the parameters -------------------------
00717                         {
00718                                 //get output of Kalman filters
00719                                 jxthat=xthat[j];
00720                                 Mat jVthat=Vthat[j];
00721                                 Mat jVtt1hat=Vtt1hat[j];
00722                                 jz=Z.col(j);
00723                                 double jN=Nhat.at<OPT_F_TYPE>(0,j);
00724 
00725                                 if(FlagYmean)
00726                                         jYmean=dt[j].Ymean;
00727 
00728                                 //compute aggregated statistics %%%
00729                                 //% Initialize Phi and phi
00730                                 PhiAll=Mat::zeros(n,n,OPT_MAT_TYPE);
00731                                 Phi=Mat::zeros(n,n,OPT_MAT_TYPE); // varphi
00732                                 sphi=Mat::zeros(n,n,OPT_MAT_TYPE); //lowercase phi
00733 
00734                                 Mat tmp1;
00735                                 MatVid::reduce(jVthat,tmp1,CV_REDUCE_SUM);
00736                                 Mat tmp1a = tmp1 - MatVid::frame(jVthat,0);
00737                                 Mat tmp1b = tmp1 - MatVid::frame(jVthat,jVthat.size[0]-1);
00738 
00739                                 //Initialize Psi
00740                                 Psi=Mat::zeros(n,n,OPT_MAT_TYPE);
00741                                 Mat tmp3;
00742                                 MatVid::reduce(jVtt1hat,tmp3,CV_REDUCE_SUM);
00743 
00744                                 //Initialize Gamma and Lambda
00745                                 Gamma = Mat::zeros(dy,n,OPT_MAT_TYPE);
00746 
00747                                 switch(opt.Ropt)
00748                                 {
00749                                 case CovMatrix::COV_IID:
00750                                         Lambda.create(1,1,OPT_MAT_TYPE);
00751                                         Lambda=Scalar(0);
00752                                         break;
00753                                 default:
00754                                         CV_Error(-1,"TO DO");
00755                                 }
00756 
00757                                 //Initialize Xi
00758                                 Xi = Mat::zeros(n,1,OPT_MAT_TYPE);
00759                                 //Initialize Eta
00760                                 Eta = Mat::zeros(n,n,OPT_MAT_TYPE);
00761 
00762                                 // aggregate over all samples     
00763                                 for(int i=0;i<N;i++)
00764                                 {
00765                                         //current X and Y and Z
00766                                         Mat myx=jxthat[i];
00767                                         Mat myy;
00768                                         double myz;
00769                                         Mat tmpM(dy,tau,OPT_MAT_TYPE);
00770                                         for(int p=0;p<Y.size[0];p++)
00771                                                 for(int q=0;q<Y.size[1];q++)
00772                                                         tmpM.at<OPT_F_TYPE>(q,p)=Y.at<OPT_F_TYPE>(p,q,i);
00773 
00774                                         if (FlagYmean)
00775                                         {                                                       
00776                                                 Mat tmpM2;
00777                                                 repeat(jYmean,1,tau,tmpM2);
00778                                                 myy=tmpM-tmpM2;
00779                                         }
00780                                         else
00781                                         {
00782                                                 myy=tmpM;
00783                                         }
00784 
00785                                         myz=jz.at<OPT_F_TYPE>(i,0);
00786 
00787                                         // update Phi and phi
00788                                         Mat tmp2 = myx*myx.t();
00789                                         Mat tmp2a = tmp2 - myx.col(0)*(myx.col(0)).t();
00790                                         Mat tmp2b = tmp2 - myx.col(myx.cols-1)*(myx.col(myx.cols-1)).t();
00791 
00792                                         Phi    = Phi    + myz*(tmp1a+tmp2a);
00793                                         sphi   = sphi   + myz*(tmp1b+tmp2b);
00794                                         PhiAll = PhiAll + myz*(tmp1+tmp2);
00795 
00796                                         // update Psi                   
00797                                         Mat tmp4 = myx.colRange(Range(1,myx.cols))*((myx.colRange(Range(0,myx.cols-1))).t());
00798                                         Psi = Psi + myz*(tmp3+tmp4);
00799 
00800                                         //Gamma and Lambda
00801                                         Gamma = Gamma + myz*(myy*myx.t());
00802                                         Scalar scatmp;
00803 
00804                                         switch(opt.Ropt)
00805                                         {
00806                                         case CovMatrix::COV_IID:
00807 
00808                                                 multiply(myy,myy,tmpM);
00809                                                 scatmp=sum(tmpM);
00810                                                 Lambda=Lambda+myz*scatmp[0];
00811                                                 break;
00812                                         default:
00813                                                 CV_Error(-1,"TO DO");
00814                                         }
00815 
00816                                         // Xi and Eta                                   
00817                                         // using only initial state samples
00818                                         Xi  = Xi  + myz*myx.col(0);
00819                                         Eta = Eta + myz*(MatVid::frame(jVthat,0) + myx.col(0)*(myx.col(0).t()));
00820                                 }
00821 
00822                                 //Compute New parameters
00823 
00824                                 //2) C parameter
00825                                 Mat iPhiAll=PhiAll.inv();
00826                                 Mat newC=Gamma*iPhiAll;
00827                                 dt[j].C=newC;
00828 
00829                                 //3) R parameter
00830                                 Mat rnew;
00831                                 switch(opt.Ropt)
00832                                 {
00833                                 case CovMatrix::COV_IID:                        
00834                                         rnew = (Lambda - trace(iPhiAll*(Gamma.t()*Gamma))) / (dy*tau*jN);
00835                                         dt[j].R.mtx=rnew;
00836                                         dt[j].R.covopt=CovMatrix::COV_IID;                                              
00837                                         break;
00838                                 default:
00839                                         CV_Error(-1,"TO DO");
00840                                 }
00841 
00842                                 //4) A parameter
00843                                 Mat newA = Psi*sphi.inv();
00844                                 dt[j].A = newA;
00845 
00846                                 // 5) Q parameter
00847                                 Mat Q = (Phi-newA*Psi.t())/( (tau-1)*jN);
00848                                 dt[j].Q.mtx= Q;
00849 
00850                                 // 6) mu parameter
00851                                 Mat newmu = Xi / jN;
00852                                 dt[j].mu0 = newmu;
00853 
00854                                 //7) S parameter 
00855                                 Mat newS = Eta/jN - newmu*newmu.t();
00856 
00857                                 switch(opt.Sopt)
00858                                 {
00859                                 case CovMatrix::COV_DIAG:                       
00860                                         dt[j].S0.mtx=newS.diag();
00861                                         dt[j].S0.covopt=CovMatrix::COV_DIAG;
00862                                         break;
00863                                 default:
00864                                         CV_Error(-1,"TO DO");
00865                                 }
00866 
00867                                 // 8) Ymean parameter
00868                                 Mat newYmean = Mat::zeros(dy,1,OPT_MAT_TYPE);
00869                                 if (FlagYmean)
00870                                 {
00871                                         for(int i=0;i<N;i++)
00872                                         {
00873                                                 Mat tmpM;
00874                                                 reduce(jxthat[i],tmpM,1,CV_REDUCE_AVG);                                                 
00875                                                 newYmean = newYmean + (jz.at<OPT_F_TYPE>(i,0))*(Ymean[i] - newC*tmpM);
00876                                         }                                       
00877                                         newYmean = newYmean / jN;
00878                                 }
00879 
00880                                 dt[j].Ymean = newYmean;
00881 
00882                                 // regularize the new parameters
00883                                 dt[j].setRegularizer(emopt.regopt);
00884                                 dt[j].regularize();                     
00885 
00886                         }       
00887                 }
00888                 if (FlagVerbose >= 2)
00889                         cout<<endl;
00890 
00891                 //%%% handle empty clusters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
00892                 //find the largest cluster and split it to fill the blank cluster
00893                 for(int j=0;j<K;j++)
00894                 {
00895                         if(blank[j])
00896                         {
00897                                 if(FlagVerbose)
00898                                         cout<<"Cluster "<<j<<"Is blank"<<endl;                  
00899 
00900                                 DytexSplitParams splitopt;                              
00901                                 splitopt.crit=emopt.emptySplitOpt.crit;
00902                                 splitopt.pert=emopt.emptySplitOpt.pert;
00903                                 splitopt.mode=emopt.emptySplitOpt.mode;
00904                                 splitopt.vars=emopt.emptySplitOpt.vars;
00905                                 splitopt.ignore.clear();
00906                                 splitopt.target=j+1;
00907                                 int c1,c2;
00908                                 dytex_mix_split(splitopt,c1,c2);
00909 
00910                                 blank[j]=0;                                             
00911                         }
00912                 }       
00913 
00914                 elapsedtime=clock()-starttime;
00915                 if(FlagVerbose>=2)
00916                 {
00917                         cout<<"Elapsed Time: "<<elapsedtime<<endl;
00918                 }
00919 
00920                 iter=iter+1;  //finish current EM iteration
00921         }
00922         
00923         //End of EM loop
00924         if(FlagVerbose>=1)
00925         {
00926                 cout<<"alpha= ";
00927                 for(int i=0;i<alpha.size();i++)
00928                         cout<<alpha[i]<<"  ";
00929 
00930                 cout<<endl;
00931         }
00932 }
00933 
00934 
00954 void DytexMix::runHEM(DytexMix &hembest, HEMOptions &hopt,std::vector<int> &classes)
00955 {
00956         //used to display info in change in classes during EM loop
00957         clock_t elapsedtime;
00958         int numlastclasses=5;
00959         bool FlagYmean=hopt.Ymean;
00960         int FlagVerbose=hopt.COMPACT;
00961 
00962         int Kb=this->dt.size();
00963         if (FlagVerbose)
00964                 cout<<"Preprocessing "<<Kb<<" base components...";
00965 
00966         for(int i=0;i<Kb;i++)
00967         {               
00968                 if(this->dt[i].dtopt.Yopt!=hopt.Ymean)
00969                 {
00970                         cout<<"** Warning: hemopt.Ymean does not match "<<this->dt[i].dtopt.Yopt<<endl;
00971                 }
00972                 //Preprocessing already done
00973         }
00974 
00975         //HEM parameters
00976         int n=this->dt[0].dtopt.n;
00977         int m=this->dt[0].dtopt.m;
00978         if (FlagVerbose)
00979                 cout<<"n="<<n<<"; m="<<m<<"; Ymean="<<this->dt[0].dtopt.Yopt<<endl;
00980         
00981         int Nvs=hopt.N;
00982         int tau=hopt.tau;
00983         //min total probability for blank cluster
00984         double MINPROB =(((double)1.0)/(((double)2.0)*(double)Kb));  
00985 
00986         //initializations
00987         if(hembest.dt.size()==0)
00988         {
00989                 if(FlagVerbose)
00990                         cout<<"Initializing First DT with Sub-optimal: "<<endl;
00991                 
00992                 //average of all DTs
00993                 Dytex tmpC= init_multiple_dt();
00994                 hembest.dt.push_back(tmpC);
00995                 hembest.alpha.push_back(1.0);
00996         }
00997         
00998         //current mixture size
00999         int Kr=hembest.dt.size(); 
01000 
01001         //Regularize the initializations
01002         for(int i=0;i<Kr;i++)
01003         {               
01004                 hembest.dt[i].setRegularizer(hopt.regopt);
01005                 hembest.dt[i].regularize(true);                 
01006 
01007                 if(hembest.dt[i].isCvs==false)
01008                 {
01009                         SVD svd(hembest.dt[i].C);
01010                         Mat Cu,Cs,Cv;
01011                         svd.u.copyTo(Cu);
01012                         svd.vt.copyTo(Cv);      
01013                         Cv=Cv.t();
01014                         Cs=Mat::zeros(svd.w.rows,svd.w.rows,svd.w.type());                              
01015                         Mat tmpM=Cs.diag();
01016                         svd.w.copyTo(tmpM);
01017                         tmpM=Cv*Cs;                     
01018                         tmpM.copyTo(hembest.dt[i].Cvs);
01019                         hembest.dt[i].isCvs=true;
01020                 }
01021         }
01022 
01023 
01024         //%%% RUN HEM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01025 
01026         //initialize convergence measures       
01027         std::vector<double> datalikelihood(hopt.maxiter+1,0);
01028         std::vector<double> ddtall(hopt.maxiter,0);
01029         std::vector<double> pdtall(hopt.maxiter,0);
01030         std::vector<std::vector<int> > lastclasses(numlastclasses);
01031         int lastclassesind = 0;
01032 
01033         for(int i=0;i<numlastclasses;i++)
01034                 lastclasses[i]=std::vector<int>(Kb,0);
01035 
01036         //initializa blanks
01037         std::vector<double> blank(Kr,0);
01038         for(int j=0;j<Kr;j++)
01039         {
01040                 if(hembest.dt[j].isempty)
01041                         blank[j]=1;
01042         }
01043 
01044         //initialize loop
01045         clock_t starttime=clock();      
01046         int iter=0;
01047         //hem loop
01048         while(1)
01049         {
01050                 //compute statistics between 2 DT for HEM E-step
01051                 Estats Estat(dt,hembest.dt,tau,FlagYmean);
01052                 Estat.computeEll();
01053                 Mat ell=Estat.Ell.clone();
01054                 Mat tmpM=Mat(alpha).clone();
01055                 tmpM=tmpM*Nvs;
01056                 Mat tmpM2;
01057                 repeat(tmpM,1,Kr,tmpM2);
01058 
01059                 Mat tmpM3(hembest.alpha);
01060                 Mat tmpM4;
01061                 log(tmpM3.t(),tmpM4);
01062                 Mat tmpM5;
01063                 repeat(tmpM4,Kb,1,tmpM5);
01064                 multiply(ell,tmpM2,tmpM);
01065 
01066                 //aggregated statistics for dti and dtj
01067                 ell = tmpM + tmpM5;
01068 
01069                 // soft assignment and data likelihood
01070                 Mat logZ   = Mat::zeros(Kb,Kr,OPT_MAT_TYPE);  
01071                 Mat tmp = (logtrick(ell.t())).t();
01072                 for(int j=0;j<Kr;j++)
01073                 {
01074                         logZ.col(j) = ell.col(j) - tmp;
01075                 }
01076 
01077                 datalikelihood[iter]=sum(tmp)[0];
01078                 Mat Z;
01079                 exp(logZ,Z);
01080 
01081                 if(FlagVerbose>=2)
01082                         cout<<endl;
01083 
01084                 //hard assignment
01085                 classes.clear();                
01086                 for(int i=0;i<Z.rows;i++)
01087                 {
01088                         double min,max;
01089                         Point minL,maxL;
01090                         minMaxLoc(Z.row(i),&min,&max,&minL,&maxL);                              
01091                         classes.push_back(maxL.x+1);
01092                 }
01093 
01094                 //Check Convergence
01095                 double ddLL=0;
01096                 double dpLL=0;
01097                 
01098                 if(iter>0)
01099                 {
01100                         //compute change in log-likelihood
01101                         ddLL=datalikelihood[iter]-datalikelihood[iter-1];
01102                         dpLL=abs(ddLL/datalikelihood[iter-1]);
01103                 }
01104                 else
01105                 {
01106                         ddLL = INF;                     
01107                         dpLL = INF;
01108                 }
01109                 //class assignment info
01110                 lastclasses[lastclassesind]=classes;
01111 
01112                 //count the number of class changes
01113                 std::vector<int> dclass;
01114                 for(int ii=0;ii<numlastclasses;ii++)
01115                 {
01116                         int sum=0;
01117                         for(int i=0;i<lastclasses[0].size();i++)
01118                         {
01119                                 if(lastclasses[ii][i]!=lastclasses[lastclassesind][i])
01120                                         sum++;
01121                         }
01122                         dclass.push_back(sum);
01123                 }
01124 
01125                 string dclassstr="";                    
01126                 for(int i=lastclassesind+1;i<numlastclasses;i++)
01127                 {
01128                         stringstream ss;
01129                         ss<<dclass[i];
01130                         dclassstr=dclassstr+ss.str()+" ";
01131                 }
01132                 for(int i=0;i<lastclassesind;i++)
01133                 {
01134                         stringstream ss;
01135                         ss<<dclass[i];
01136                         dclassstr=dclassstr+ss.str()+" ";
01137                 }
01138 
01139                 //% lastclassind points to the oldest classes
01140                 lastclassesind = lastclassesind+1;
01141                 if (lastclassesind>=numlastclasses)
01142                         lastclassesind = 0;
01143 
01144                 //output strings
01145                 stringstream ss2;
01146                 ss2<<"dclass = "<<dclassstr;
01147                 string outstr2=ss2.str();
01148                 string outstr1s;
01149                 string outstr3;
01150                 stringstream ss3;
01151 
01152                 ss3<<"L= "<<datalikelihood[iter]<<" (pL= "<<dpLL<<")";
01153                 outstr1s=ss3.str();
01154                 
01155                 if(FlagVerbose==1)
01156                 {
01157                         stringstream ss3;
01158                         ss3<<"iter= "<<iter+1<<"; "<<outstr1s<<"; "<<outstr2<<";  ";
01159                         outstr3=ss3.str();
01160                         cout<<outstr3<<endl;
01161                 }
01162                 else if(FlagVerbose>=2)
01163                 {                       
01164                         cout<<outstr2<<endl;
01165                 }
01166 
01167                 // check if negative change in log-likelihood!
01168                 if (ddLL<0)
01169                 {
01170                         cout<<"WARNING -- change in log likelihood is negative???"<<endl;
01171                 }       
01172                 //%%% check convergence conditions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01173                 int breakout = 0;
01174                 string termstr;
01175                 if (iter >= hopt.maxiter)
01176                 {
01177                         termstr = "***** done -- max iter reached\n";
01178                         breakout = 1;
01179                 }
01180                 //only this convergence condition
01181                 if ( (ddLL >= 0) && (dpLL < hopt.termvalue) )
01182                 {
01183                         termstr = "***** done -- small percent change in data likelihood\n";
01184                         breakout = 1;
01185                 }
01186 
01187                 //%%% convergence condition was reached... %%%%%%%%%%%%%%%%%%%%%%%%%%%%
01188                 if (breakout)
01189                 {
01190                         if (FlagVerbose >= 1)
01191                         {
01192                                 if (FlagVerbose == 1)
01193                                 {
01194                                         cout<<endl;                             
01195                                 }
01196                                 cout<<termstr<<endl;
01197                         }       
01198                         break;
01199                 }
01200 
01201                 //%%% M-Step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
01202 
01203                 //compute Nhat - total soft assignments per cluster
01204                 //1) update prior probabilities
01205                 Mat Nhat;
01206                 reduce(Z,Nhat,0,CV_REDUCE_SUM);
01207                 tmp=Nhat/((double)Kb);
01208                 for(int i=0;i<hembest.alpha.size();i++)
01209                         hembest.alpha[i]=tmp.at<OPT_F_TYPE>(0,i);
01210                                 
01211 
01212                 // compute weights: Zij * alpha(i)
01213                 repeat(Mat(alpha), 1, Kr,tmpM);
01214                 Mat W;
01215                 multiply(Z,tmpM,W);             
01216                 // normalize weights
01217                 reduce(W,tmpM,0,CV_REDUCE_SUM);
01218                 repeat(tmpM, Kb, 1,tmpM2);
01219                 divide(W,tmpM2,tmpM3);
01220                 W=tmpM3.clone();
01221                 Estat.computeAggregateStats(W);
01222 
01223                 //%%% loop through each cluster %%%
01224                 for(int j=0;j<Kr;j++)
01225                 {                       
01226                         //check if this is cluster is blank
01227                         if(hembest.alpha[j]<=MINPROB)
01228                         {
01229                                 blank[j]=1;
01230                                 hembest.dt[j].isempty=true;
01231                                 if (FlagVerbose >= 1)
01232                                         cout<<"blank";
01233                         }                       
01234                         else // % --- standard M-step: learn the parameters -------------------------
01235                         {                       
01236 
01237                                 Mat xij      = Estat.xij[j];
01238                                 Mat etaj     = Estat.etaj[j];
01239                                 Mat gammaj   = Estat.gammaj[j];
01240                                 Mat Phij     = Estat.Phij[j];
01241                                 Mat varphij  = Estat.varphij[j];
01242                                 Mat phij     = Estat.phij[j];
01243                                 Mat betaj    = Estat.betaj[j];
01244                                 Mat Psij     = Estat.Psij[j];
01245                                 double Lambdaj  = Estat.Lambdaj[j];
01246                                 Mat Gammaj   = Estat.Gammaj[j];
01247                                 
01248                                 //%%% compute new parameters %%%
01249 
01250                                 //C parameter
01251                                 Mat iPhij = Phij.inv();
01252                                 Mat newC = Gammaj*iPhij;
01253                                 hembest.dt[j].C=newC;
01254                                 //update preprocessing step
01255                                 SVD svd(newC);
01256                                 Mat test=Mat::zeros(svd.w.rows,svd.w.rows,OPT_MAT_TYPE);                                
01257                                 Mat tmpM;
01258                                 tmpM=test.diag();
01259                                 svd.w.copyTo(tmpM);
01260                                 hembest.dt[j].Cvs=svd.vt.t()*test;
01261                                 hembest.dt[j].isCvs=true;
01262 
01263                                 // R parameter
01264                                 hembest.dt[j].R.mtx=(Lambdaj - trace(iPhij*(Gammaj.t()*Gammaj))[0]) / (m);
01265 
01266                                 // A parameter
01267                                 Mat newA = Psij*(phij.inv());
01268                                 hembest.dt[j].A = newA;
01269 
01270                                 // Q parameter
01271                                 hembest.dt[j].Q.mtx = (varphij-newA*Psij.t());
01272 
01273                                 //mu parameter
01274                                 Mat newmu=xij.clone();
01275                                 hembest.dt[j].mu0=newmu;
01276 
01277 
01278                                 // S parameter 
01279                                 Mat newS = etaj - newmu*newmu.t();
01280                                 switch(opt.Sopt)
01281                                 {
01282                                 case CovMatrix::COV_DIAG:                       
01283                                         hembest.dt[j].S0.mtx=newS.diag().clone();
01284                                         hembest.dt[j].S0.covopt=CovMatrix::COV_DIAG;
01285                                         break;
01286                                 default:
01287                                         CV_Error(-1,"TO DO");
01288                                 }
01289 
01290                                 // Ymean parameter
01291                                 Mat newYmean;
01292                                 if (FlagYmean)
01293                                 {
01294                                         newYmean = (gammaj - newC*betaj);
01295                                 }
01296                                 else
01297                                 {
01298                                         newYmean = Mat::zeros(m,1,OPT_MAT_TYPE);
01299                                 }                               
01300                                 hembest.dt[j].Ymean = newYmean;
01301 
01302                                 // regularize the new parameters
01303                                 hembest.dt[j].setRegularizer(hopt.regopt);
01304                                 hembest.dt[j].regularize(true);
01305                         }
01306                 }
01307                 if (FlagVerbose >= 2)
01308                 {
01309                         cout<<endl;
01310                 }
01311 
01312                 //%%% handle empty clusters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01314                 for(int j=0;j<Kr;j++)
01315                 {
01316                         if(blank[j])
01317                         {
01318                                 if(FlagVerbose)
01319                                         cout<<"Cluster "<<j<<"Is blank"<<endl;                  
01320 
01321                                 DytexSplitParams splitopt;                              
01322                                 splitopt.crit=hopt.emptySplitOpt.crit;
01323                                 splitopt.pert=hopt.emptySplitOpt.pert;
01324                                 splitopt.mode=hopt.emptySplitOpt.mode;
01325                                 splitopt.vars=hopt.emptySplitOpt.vars;
01326                                 //                              splitopt.len=tau;
01327                                 splitopt.ignore.clear();
01328                                 splitopt.target=j+1;
01329                                 int c1,c2;
01330                                 hembest.dytex_mix_split(splitopt,c1,c2);
01331 
01332                                 blank[j]=0;                                             
01333                         }
01334                 }
01335 
01336                 elapsedtime=clock()-starttime;
01337                 if(FlagVerbose>=2)
01338                 {
01339                         cout<<"Elapsed Time: "<<elapsedtime<<endl;
01340                 }
01341 
01342                 iter=iter+1; //End of HEM iteration
01343 
01344         } 
01345 }
01346 
01347 
01378 Mat DytexMix::convertmovie(Mat Y,int &sx,int &sy,int &sc,int &cnorm)
01379 {
01380         Mat Yout;
01381         if(Y.dims==3)
01382         {
01383                 sy=Y.size[1];
01384                 sx=Y.size[2];
01385                 sc=1;
01386                 Yout.create(sy*sx,Y.size[0],Y.type());                  
01387                 //copy data column wise
01388                 int index=0;            
01389                 int index2=0;                           
01390                 for(int k=0;k<Y.size[2];k++)
01391                 {
01392                         for(int j=0;j<Y.size[1];j++)
01393                         {
01394                                 index=0;
01395                                 for(int i=0;i<Y.size[0];i++)
01396                                 {
01397                                         Yout.at<OPT_F_TYPE>(index2,index)=Y.at<OPT_F_TYPE>(i,j,k);
01398                                         index++;
01399                                 }
01400                                 index2++;
01401                         }
01402                 }                       
01403         }
01404         double minV,maxV;
01405         minMaxLoc(Yout,&minV,&maxV);
01406         if ((sc==3) && (maxV<=1.0))
01407         {
01408                 cnorm=1;
01409         }
01410         else
01411         {
01412                 cnorm=0;
01413         }
01414         return Yout;
01415 }
01416 
01433 Dytex DytexMix::initcluster_doretto(const std::vector<Mat> &Y,const DytexOptions &param)
01434 {
01435         Dytex mydt=Dytex(param);
01436         mydt.learnLeastSquares(Y);      
01437         return mydt;
01438 }
01439 
01440 
01457 std::vector<Mat> DytexMix::extractY(const Mat &Yin, std::vector<int> inds)
01458 {       
01459         std::vector<Mat> Yout;          
01460         int dy=Yin.size[1];
01461         int tau=Yin.size[0];
01462 
01463         for(int i=0;i<inds.size();i++)
01464         {
01465                 Mat tmpM(dy,tau,Yin.type());
01466                 //copy data
01467                 for(int j=0;j<dy;j++)
01468                 {
01469                         for(int k=0;k<tau;k++)
01470                         {
01471                                 tmpM.at<OPT_F_TYPE>(j,k)=Yin.at<OPT_F_TYPE>(k,j,inds[i]);
01472                         }
01473                 }
01474                 Yout.push_back(tmpM);
01475         }
01476 
01477         return Yout;
01478 }
01479 
01496 void DytexMix::dytex_mix_split(DytexSplitParams splitopt,int& ctg,int& csp )
01497 {
01498         int K=alpha.size();
01499         int c1,c2,newK;
01500         if(K==1)
01501         {
01502                 c1=1;
01503         }
01504         else
01505         {
01506                 std::vector<Dytex> olddt=dt;
01507                 std::vector<double> olda1=alpha;
01508                 std::vector<double> tmpal;
01509                 cout<<"*** split criteria ***\n";
01510 
01511                 std::vector<Mat> myQ(K);
01512                 std::vector<double> myQe(K);
01513                 Mat F;
01514                 bool flag=true;
01515                 vector<OPT_F_TYPE>::iterator result;
01516                 switch(splitopt.crit)
01517                 {
01518                         //%%% split component with largest Q variance %%%
01519                         case DytexSplitParams::SPLITQ:
01520                                 for(int jjj=0;jjj<K;jjj++)
01521                                 {
01522                                         if(find(splitopt.ignore.begin(), splitopt.ignore.end(), jjj)!=splitopt.ignore.end())
01523                                         {
01524                                                 myQe[jjj]=-1;
01525                                         }
01526                                         else
01527                                         {
01528                                                 //check empty
01529                                                 if(olddt[jjj].isempty)
01530                                                 {
01531                                                         myQe[jjj]=-1;
01532                                                 }
01533                                                 else
01534                                                 {
01535                                                         //normalize Q by forcing C to be orthonormal
01536                                                         SVD svd(olddt[jjj].C);
01537                                                         Mat test(svd.w.rows,svd.w.rows,OPT_MAT_TYPE);
01538                                                         test=Scalar(0);
01539                                                         Mat tmpM;
01540                                                         tmpM=test.diag();
01541                                                         svd.w.copyTo(tmpM);
01542                                                         F=test*svd.vt;
01543                                                         myQ[jjj]=F*olddt[jjj].Q.mtx*F.t();
01544                                                         Mat eigM;
01545                                                         eigen(myQ[jjj],eigM);
01546                                                         double minVal,maxVal;
01547                                                         minMaxLoc(eigM,&minVal,&maxVal);
01548                                                         myQe[jjj]=maxVal;                                                               
01549                                                 }
01550                                         }
01551                                 }
01552 
01553                                 for(int i=0;i<myQe.size();i++)
01554                                 {
01555                                         if(myQe[i]!=-1)
01556                                         {
01557                                                 flag=false;
01558                                                 break;
01559                                         }
01560                                 }
01561                                 if(flag)
01562                                 {
01563                                         c1=0;
01564                                         cout<<"nothing to split"<<endl;
01565                                 }
01566                                 else
01567                                 {
01568                                         vector<OPT_F_TYPE>::iterator result = max_element(myQe.begin(),myQe.end()); 
01569                                         c1=distance(myQe.begin(), result)+1;
01570                                 }
01571                         break;
01572 
01573                         //split component with largest prior
01574                         case DytexSplitParams::SPLITP:
01575                                 tmpal=olda1;
01576                                 for(int i=0;i<splitopt.ignore.size();i++)
01577                                 {
01578                                         tmpal[splitopt.ignore[i]]=-1;
01579                                 }
01580                                 result = max_element(tmpal.begin(),tmpal.end()); 
01581                                 c1=distance(tmpal.begin(), result)+1;
01582 
01583                         break;
01584 
01585                         default:
01586                                 CV_Error(-1,"TO DO");
01587 
01588                 }
01589         }
01590 
01591         // initialize with previous     
01592         //adding a new one
01593         if(splitopt.target==-1)
01594         {
01595                 c2=K+1;
01596                 newK=K+1;                       
01597                 dt.push_back(Dytex()); //add one mroe blank DT in the list
01598                 alpha.push_back(0.0);
01599                 
01600         }
01601         //updating existing
01602         else
01603         {
01604                 c2=splitopt.target;
01605                 newK=K;
01606         }
01607 
01608         cout<<"Spliting Cluster "<<c1<<" : new cluster "<<c2<<endl;
01609         
01610         //check if there is anything to split
01611         if (c1 == 0)
01612         {
01613                 dt[c2-1].isempty=true;          
01614                 alpha[c2-1]=0;
01615         }
01616         else
01617         {
01618                 //duplicate cluster %%% all parameters c
01619                 dt[c1-1].A.copyTo(dt[c2-1].A);
01620                 dt[c1-1].C.copyTo(dt[c2-1].C);
01621                 dt[c1-1].Ymean.copyTo(dt[c2-1].Ymean);  
01622                 dt[c1-1].mu0.copyTo(dt[c2-1].mu0);
01623                 dt[c1-1].Q.mtx.copyTo(dt[c2-1].Q.mtx);                  
01624                 dt[c2-1].Q.covopt=dt[c1-1].Q.covopt;
01625                 dt[c2-1].Q.n=dt[c1-1].Q.n;
01626                 dt[c2-1].Q.regopt=dt[c1-1].Q.regopt;
01627                 dt[c2-1].Q.regval=dt[c1-1].Q.regval;
01628                 dt[c1-1].R.mtx.copyTo(dt[c2-1].R.mtx);                  
01629                 dt[c2-1].R.covopt=dt[c1-1].R.covopt;
01630                 dt[c2-1].R.n=dt[c1-1].R.n;
01631                 dt[c2-1].R.regopt=dt[c1-1].R.regopt;
01632                 dt[c2-1].R.regval=dt[c1-1].R.regval;
01633                 dt[c1-1].S0.mtx.copyTo(dt[c2-1].S0.mtx);                        
01634                 dt[c2-1].S0.covopt=dt[c1-1].S0.covopt;
01635                 dt[c2-1].S0.n=dt[c1-1].S0.n;
01636                 dt[c2-1].S0.regopt=dt[c1-1].S0.regopt;
01637                 dt[c2-1].S0.regval=dt[c1-1].S0.regval;                                  
01638                 dt[c2-1].isempty=dt[c1-1].isempty;
01639                 dt[c2-1].vrows=dt[c1-1].vrows;
01640                 dt[c2-1].vcols=dt[c1-1].vcols;
01641                 dt[c2-1].dtopt=dt[c1-1].dtopt;
01642                 dt[c1-1].Cvs.copyTo(dt[c2-1].Cvs);
01643                 dt[c2-1].isCvs=dt[c1-1].isCvs;
01644                 double tmp     = alpha[c1-1]/((double)2.0);
01645                 alpha[c1-1] = tmp;
01646                 alpha[c2-1] = tmp;
01647                 
01648                 //perturb new cluster
01649                 dytex_perturb(dt[c2-1], splitopt.pert, splitopt.mode, splitopt.vars);
01650 
01651                 // also perturb old cluster (if principal axis split on C, A, x)
01652                 if(splitopt.mode==DytexSplitParams::MODEP)
01653                 {
01654                         if( splitopt.vars==DytexSplitParams::VARC)
01655                         {
01656                                 dytex_perturb(dt[c1-1], -splitopt.pert, splitopt.mode, splitopt.vars);
01657                         }
01658                 }
01659 
01660         }
01661         ctg=c2-1;
01662         csp=c1-1;
01663 }
01664 
01665 
01687 void DytexMix::dytex_perturb(Dytex &dtp, double pert, DytexSplitParams::Split_mode mode, DytexSplitParams::Split_vars vars)
01688 {
01689         cout<<"perturbing C by "<<pert<<endl;
01690 
01691         switch(mode)
01692         {
01693                 //scale up principal axis
01694                 case DytexSplitParams::MODEP:
01695                         switch(vars)
01696                         {
01697                                 //perturb C
01698                                 case DytexSplitParams::VARC:
01699                                         ppertC(dtp.C,dtp.S0.mtx,pert);  
01700                                         break;
01701                                 default:
01702                                         CV_Error(-1,"TO DO");
01703                         }
01704                         break;
01705                 default:
01706                         CV_Error(-1,"TO DO");
01707         }
01708 }
01709 
01726 void DytexMix::ppertC(Mat &C,Mat S0,double pert)
01727 {       
01728 
01729         int m=C.rows;
01730         int n=C.cols;
01731 
01732         Mat cc;
01733         sqrt((C.t()*C).diag(),cc);
01734 
01735         Mat tmpM,tmpM2;
01736         multiply(cc,cc,tmpM);
01737         multiply(S0,tmpM,tmpM2);
01738 
01739         double minVal,maxVal;
01740         Point minLoc,maxLoc;
01741         minMaxLoc(tmpM2,&minVal,&maxVal,&minLoc,&maxLoc);
01742 
01743         C.col(maxLoc.y)=(((double)1.0)+pert)*C.col(maxLoc.y);
01744 
01745 
01746 }
01747 
01758 Dytex DytexMix::init_multiple_dt()
01759 {
01760         //copy template
01761         Dytex odt(dt[0].dtopt);
01762 
01763         if(odt.dtopt.Yopt==DytexOptions::ZERO_YMEAN)
01764         {
01765                 odt.Ymean=Mat::zeros(odt.dtopt.m,1,OPT_MAT_TYPE);
01766         }
01767         dt[0].Cvs.copyTo(odt.Cvs);
01768         odt.isCvs=false;
01769         //extract Cs
01770         Mat Call(odt.dtopt.m,odt.dtopt.n*dt.size(),OPT_MAT_TYPE);
01771         Call=Scalar(0);
01772         for(int i=0;i<dt.size();i++)
01773         {
01774                 int cstart=i*odt.dtopt.n;
01775                 Mat tmpM=Call.colRange(cstart,cstart+odt.dtopt.n);
01776                 dt[i].C.copyTo(tmpM);
01777         }
01778         if(Call.rows>Call.cols)
01779         {
01780                 SVD svd(Call);          
01781                 svd.u.colRange(0,odt.dtopt.n).copyTo(odt.C);            
01782         }
01783         else
01784         {
01785                 Mat CC = Call*Call.t();
01786                 SVD svd(CC);            
01787                 svd.u.colRange(0,odt.dtopt.n).copyTo(odt.C);
01788         }
01789         //initialize accumulators
01790         odt.mu0=Scalar(0);
01791         odt.S0.mtx=Scalar(0);
01792         odt.Ymean=Scalar(0);
01793         odt.A=Scalar(0);
01794         odt.Q.mtx=Scalar(0);
01795         odt.R.mtx=Scalar(0);
01796 
01797         //compute other parameters by averaging
01798         for(int i=0;i<dt.size();i++)
01799         {
01800                 //compute transformation
01801                 Mat F=odt.C.t()*dt[i].C;
01802 
01803                 //accumulate
01804                 odt.mu0=odt.mu0+F*dt[i].mu0;            
01805 
01806                 Mat tmpM(dt[i].S0.mtx.rows,dt[i].S0.mtx.rows,OPT_MAT_TYPE);
01807                 tmpM=Scalar(0);
01808                 Mat tmpM2=tmpM.diag();
01809                 dt[i].S0.mtx.copyTo(tmpM2);
01810 
01811                 odt.S0.mtx=odt.S0.mtx+ (F*tmpM*(F.t())).diag();
01812                 odt.Ymean=odt.Ymean+dt[i].Ymean;
01813                 odt.A=odt.A+F*dt[i].A*(F.inv());
01814                 odt.Q.mtx=odt.Q.mtx+ F*dt[i].Q.mtx*(F.t());
01815                 odt.R.mtx=odt.R.mtx+dt[i].R.mtx;
01816         }
01817 
01818         odt.mu0    = odt.mu0/dt.size(); 
01819         odt.S0.mtx     = odt.S0.mtx/dt.size();
01820         odt.Ymean  = odt.Ymean/dt.size();
01821         odt.A      = odt.A/dt.size();
01822         odt.Q.mtx  = odt.Q.mtx/dt.size();
01823         odt.R.mtx  = odt.R.mtx/dt.size();
01824         
01825         return odt;
01826 }
01827 
01828 
01842 Mat DytexMix::logtrick(Mat lA)
01843 {
01844         Mat s;
01845         Mat mv;
01846         reduce(lA,mv,0,CV_REDUCE_MAX);
01847         Mat tmpM;
01848         repeat(mv,lA.rows,1,tmpM);
01849         Mat temp=lA-tmpM;
01850 
01851         exp(temp,tmpM);
01852         Mat cterm;
01853         reduce(tmpM,cterm,0,CV_REDUCE_SUM);
01854         log(cterm,tmpM);
01855         s=mv+tmpM;
01856 
01857         return s;
01858 }
01859 
01870 bool DytexMix::IsGT2 (int i) 
01871 {
01872         return ((i>2)==1);
01873 }

Generated on 18 Dec 2014 for libdt-OpenCV by  doxygen 1.6.1