test_HEMDTM.cpp

00001 
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <string>
00011 #include <iterator>
00012 #include<fstream>
00013 #include<math.h>
00014 
00015 #include "opencv/cv.h"
00016 #include "opencv/cxcore.h"
00017 #include "opencv/highgui.h"
00018 
00019 #include "options.h"
00020 #include "utils/libutils.h"
00021 #include "stats/libstats.h"
00022 #include "video/libvideo.h"
00023 #include "dytex/libdytex.h"
00024 #include "platInd/platInd.hpp"
00025 #include "bufferer/Bufferer.hpp"
00026 
00027 
00028 using namespace cv;
00029 using namespace std;
00030 
00031 
00037 template <typename T>
00038 std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
00039 {
00040     assert(a.size() == b.size());
00041 
00042     std::vector<T> result;
00043     result.reserve(a.size());
00044 
00045     std::transform(a.begin(), a.end(), b.begin(), 
00046                    std::back_inserter(result), std::plus<T>());
00047     return result;
00048 }
00049 
00055 std::string trim(std::string& str)
00056 {
00057         str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
00058         str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
00059         return str;
00060 }
00061 
00067 void readGtruthFile(string fpath,vector<string> &vlist, vector<vector<string> > &annlist,vector<string> &taglist)
00068 {       
00069         ifstream gthf(fpath.c_str(), std::ifstream::in);
00070 
00071         //parsing gtruth file
00072         std::string                line;
00073     std::getline(gthf,line);
00074         std::getline(gthf,line);
00075         line=trim(line);
00076         line=line.substr(1,line.size()-2);
00077         
00078         //Getting tag list
00079         std::stringstream          lineStream(line);
00080     std::string                cell;
00081         while(getline(lineStream,cell,','))
00082     {
00083         taglist.push_back(cell);
00084     }
00085 
00086         std::getline(gthf,line);        
00087 
00088         while(gthf.good())
00089         {
00090                 std::getline(gthf,line);
00091                 if(line.empty())
00092                         break;
00093                 
00094                 std::stringstream  lineStream2(line);
00095                 std::string                cell2;
00096                 getline(lineStream2,cell2,' ');
00097                 getline(lineStream2,cell2,':');
00098                 
00099 
00100                 //video name
00101                 vlist.push_back(cell2);
00102 
00103                 getline(lineStream2,cell2,'}');
00104                 
00105                 cell2=trim(cell2);
00106 
00107                 cell2=cell2.substr(1,cell2.size()-1);
00108 
00109                 //Getting ann list
00110                 std::vector<string> vidann;
00111                 std::stringstream          lineStream3(cell2);
00112                 std::string                cell3;
00113                 while(getline(lineStream3,cell3,','))
00114                 {
00115                         vidann.push_back(cell3);
00116                 }
00117                 annlist.push_back(vidann);              
00118         }       
00119 }
00120 
00126 void readTrialInfo(string fpath,vector<vector<int> > &trainVidIds, vector<vector<int> > &testVidIds)
00127 {       
00128         ifstream trf(fpath.c_str(), std::ifstream::in);
00129 
00130         //parsing gtruth file
00131         std::string                line;
00132         for(int i=1;i<=5;i++)
00133         {
00134                 while(trf.good())
00135                 {                       
00136                         std::getline(trf,line);
00137                         line=trim(line);
00138 
00139                         std::string trstr;
00140                         std::stringstream out;
00141                         out << i;
00142                         trstr = out.str();      
00143 
00144                         if(line.compare("Trial-"+trstr+":")==0)
00145                                 break;
00146                 }
00147 
00148                 //train Ids
00149                 std::getline(trf,line);
00150                 std::stringstream  lineStream2(line);
00151                 std::string                cell2;
00152                 getline(lineStream2,cell2,'{');
00153                 getline(lineStream2,cell2,'}');
00154                 
00155                 std::vector<int> ids;
00156                 std::stringstream  lineStream3(cell2);
00157                 std::string                cell3;
00158                 while(getline(lineStream3,cell3,','))
00159                 {
00160                         ids.push_back(atoi(cell3.c_str())-1);
00161                 }
00162                 trainVidIds.push_back(ids);
00163 
00164 
00165                 //test Ids
00166                 std::getline(trf,line);
00167                 std::stringstream  lineStream4(line);
00168                 std::string                cell4;
00169                 getline(lineStream4,cell4,'{');
00170                 getline(lineStream4,cell4,'}');
00171                 
00172                 std::vector<int> ids2;
00173                 std::stringstream  lineStream5(cell4);
00174                 std::string                cell5;
00175                 while(getline(lineStream5,cell5,','))
00176                 {
00177                         ids2.push_back(atoi(cell5.c_str())-1);
00178                 }
00179                 testVidIds.push_back(ids2);
00180         }
00181 }
00182 
00188 std::vector<int> getTagVids(std::vector<int> &trainVidIds, string tag, std::vector<std::vector<string> > &annlist)
00189 {
00190         std::vector<int> curTagIds;
00191 
00192         for(int i=0;i<trainVidIds.size();i++)
00193         {
00194                 int vidid=trainVidIds[i]; //next video in training list
00195 
00196                 bool lab=false;
00197                 for(int j=0;j<annlist[vidid].size();j++) //look for anns of this video
00198                 {
00199                         if(annlist[vidid][j].compare(tag)==0) //check this video is labeled with tag or not
00200                         {
00201                                 lab=true;
00202                                 curTagIds.push_back(vidid);
00203                                 break;
00204                         }
00205                 }
00206         }
00207         return curTagIds;
00208 }
00209 
00215 void learnTagDTM()
00216 {
00217         PlatInd::makedir("../../testdata/HEMDTM/dyntex337/tagdtm/");
00218         
00219         //reading ground truth
00220         vector<string> vlist;
00221         vector<vector<string> > annlist;
00222         vector<string> taglist;
00223         readGtruthFile("../../testdata/HEMDTM/dyntex337/videos/dyntexgtruth.txt",vlist,annlist,taglist);
00224 
00225         //reading trial info
00226         vector<vector<int> > trainVidIds;
00227         vector<vector<int> > testVidIds;
00228         readTrialInfo("../../testdata/HEMDTM/dyntex337/videos/dyntexExperimentalSetup.txt",trainVidIds,testVidIds);
00229 
00230         //tag models for each trial
00231         for( int trial=0;trial<5;trial++)
00232         {
00233                 std::string trstr;
00234                 std::stringstream out;
00235                 out << (trial+1);
00236                 trstr = out.str();
00237 
00238                 PlatInd::makedir("../../testdata/HEMDTM/dyntex337/tagdtm/trial" + trstr);
00239 
00240                 for( int tagid=0;tagid<35;tagid++)
00241                 {
00242                         string opath="../../testdata/HEMDTM/dyntex337/tagdtm/trial" + trstr + "/" + taglist[tagid] + ".dtm";
00243 
00244                         if(!PlatInd::fexist(opath)) //learn tag model
00245                         {
00246                                 cout<<"Learning Tag model: "<<opath<<endl; 
00247                                 std::vector<int> curTagIds=getTagVids(trainVidIds[trial],taglist[tagid],annlist);
00248 
00249                                 //temp code
00250                                 std::vector< std::vector<string> > tmp;
00251                                 for (int x=0;x<curTagIds.size();x++)
00252                                 {
00253                                         tmp.push_back(annlist[curTagIds[x]]);
00254                                 }
00255 
00256                                 //aggregate Dts of all training videos labeled with this tag
00257                                 DytexMix bDTM;
00258                                 for(int i=0;i<curTagIds.size();i++)
00259                                 {
00260                                         /*if( (curTagIds[i]==259) || (curTagIds[i]==332) ) //skip problem dtm 649i110 vid No 259
00261                                                 continue;  */
00262 
00263 
00264                                         string dtmpt="../../testdata/HEMDTM/dyntex337/basedtm/"+ vlist[curTagIds[i]] + ".dtm";
00265                                         //save the DTM          
00266                                         DytexMix vdtm;
00267                                         Bufferer buf(dtmpt,fstream::in | fstream::binary);
00268                                         buf.read(vdtm); 
00269                                         buf.close();
00270 
00271                                         //put in the tag DTM
00272                                         for(int j=0;j<vdtm.alpha.size();j++)
00273                                         {
00274                                                 bDTM.alpha.push_back(vdtm.alpha[j]);
00275                                                 bDTM.dt.push_back(vdtm.dt[j]);
00276                                         }
00277                                 }
00278                                 //normalize alpha
00279                                 double alphaSum=0.0;
00280                                 for(int i=0;i<bDTM.alpha.size();i++)
00281                                 {
00282                                         alphaSum=alphaSum+bDTM.alpha[i];
00283                                 }
00284                                 for(int i=0;i<bDTM.alpha.size();i++)
00285                                 {
00286                                         bDTM.alpha[i]=bDTM.alpha[i]/alphaSum;
00287                                 }
00288                             bDTM.opt=bDTM.dt[0].dtopt;
00289 
00290                                 
00291 
00292                                 //reduce aggregated DT
00293                                 int K=8;
00294                                 DytexRegOptions ropt(CovMatrix::COV_REG_MIN,0.01,CovMatrix::COV_REG_MIN,0.01,CovMatrix::COV_REG_MIN,0.01,CovMatrix::COV_REG_ADD,0.999);
00295                                 HEMOptions hopt(K,ropt,0.0001,DytexOptions::NONZERO_YMEAN,HEMOptions::COMPACT);
00296                                 for(int i=1;i<=K;i=i*2)
00297                                 hopt.splitOpt.sched.push_back(i);
00298         
00299                                 timerOn();
00300                                 DytexMix emout=bDTM.reduceWithSplitting(hopt);
00301                                 int traintime = timerOff();
00302                                 cout<<"Total Time Taken :"<<(double)traintime/(double)1000<<endl;
00303 
00304                                 //save tag model
00305                                 Bufferer buf2(opath,fstream::out | fstream::binary);
00306                                 buf2.write(emout);      
00307                                 buf2.close();
00308 
00309 
00310                         }
00311                         else
00312                         {
00313                                 cout<<"Tag model Already exists: "<<opath<<endl;
00314                         }
00315                                 
00316                 }
00317                 cout<<endl;
00318         }
00319 }
00325 void learnBaseDTM()
00326 {
00327         PlatInd::makedir("../../testdata/HEMDTM/dyntex337/basedtm/");
00328         //video list    
00329         std::vector<string> vlist;
00330         Bufferer bufX("../../testdata/HEMDTM/dyntex337/videos/list.vec",fstream::in | fstream::binary);
00331         bufX.read("list",vlist);        
00332         bufX.close();
00333         
00334 
00335         //Setting DTM learning Options  
00336         int winxy=7;
00337         int winz=20;
00338         int stepxy=4;
00339         int stepz=10;
00340         PatchOptions::norm_type ntype=PatchOptions::NORM_NONE;
00341         double bkvar=-5;
00342         int K=16;
00343         int n=10;
00344         double reg=0.5;
00345         
00346         for(int i=0;i<vlist.size();i++)
00347         {
00348                 string ipath="../../testdata/HEMDTM/dyntex337/videos/" + vlist[i];                      
00349                 string opath="../../testdata/HEMDTM/dyntex337/basedtm/" + vlist[i] + ".dtm";                                    
00350                 
00351                 if(!PlatInd::fexist(opath))
00352                 {
00353                         cout<<"Generating BaseDTM for: "<<ipath<<endl;
00354                         //learn the model               
00355                         VidSegm vs(winxy,winz,stepxy,stepz,ntype,bkvar,K,n,reg);                
00356                         vs.learnDTM(ipath,false);       
00357 
00358                         //save the DTM          
00359                         Bufferer buf(opath,fstream::out | fstream::binary);
00360                         buf.write(vs.dtm);      
00361                         buf.close();
00362                 }
00363                 else
00364                 {
00365                         cout<<"Base DTM exists for: "<<ipath<<endl;
00366                 }
00367         }
00368 }
00369 
00375 void annotateVideos()
00376 {
00377         int winxy=7;
00378         int winz=20;
00379         int stepxy=4;
00380         int stepz=10;
00381         PatchOptions::norm_type ntype=PatchOptions::NORM_NONE;
00382         double bkvar=-5;
00383         int tau=20;
00384         int numlabels=35;
00385 
00386         PlatInd::makedir("../../testdata/HEMDTM/dyntex337/ann/");
00387         
00388         //reading ground truth
00389         vector<string> vlist;
00390         vector<vector<string> > annlist;
00391         vector<string> taglist;
00392         readGtruthFile("../../testdata/HEMDTM/dyntex337/videos/dyntexgtruth.txt",vlist,annlist,taglist);
00393 
00394         //reading trial info
00395         vector<vector<int> > trainVidIds;
00396         vector<vector<int> > testVidIds;
00397         readTrialInfo("../../testdata/HEMDTM/dyntex337/videos/dyntexExperimentalSetup.txt",trainVidIds,testVidIds);
00398 
00399         //tag models for each trial
00400         for( int trial=0;trial<5;trial++)
00401         {
00402                 std::string trstr;
00403                 std::stringstream out;
00404                 out << (trial+1);
00405                 trstr = out.str();
00406 
00407                 PlatInd::makedir("../../testdata/HEMDTM/dyntex337/ann/trial" + trstr);
00408 
00409                 //load all tag models for the trial
00410                 vector<DytexMix> alltagdtm;
00411                 for( int tagid=0;tagid<numlabels;tagid++)
00412                 {
00413                         string tagpath="../../testdata/HEMDTM/dyntex337/tagdtm/trial" + trstr + "/" + taglist[tagid] + ".dtm";
00414                         DytexMix tmp;
00415                         Bufferer buf(tagpath,fstream::in | fstream::binary);
00416                         buf.read(tmp);  
00417                         buf.close();
00418                         for(int p=0;p<tmp.dt.size();p++)
00419                         {
00420                                 tmp.dt[p].vrows=winxy;
00421                                 tmp.dt[p].vcols=winxy;
00422                         }                       
00423                         alltagdtm.push_back(tmp);
00424                 }
00425 
00426                 for( int tagid=0;tagid<numlabels;tagid++)
00427                 {
00428                         alltagdtm[tagid].setupKFB(tau);
00429                 }
00430 
00431 
00432 
00433                 for( int vidid=0;vidid<vlist.size();vidid++)
00434                 {
00435                         string opath1="../../testdata/HEMDTM/dyntex337/ann/trial" + trstr + "/" + vlist[vidid] + ".txt";
00436                         string opath2="../../testdata/HEMDTM/dyntex337/ann/trial" + trstr + "/" + vlist[vidid] + ".bin";
00437                         string ipath="../../testdata/HEMDTM/dyntex337/videos/" + vlist[vidid];
00438 
00439                         if(!PlatInd::fexist(opath1)) //learn tag model
00440                         {
00441                                 cout<<"annotating: "<<opath1<<endl; 
00442                                 
00443                                 //Getting patches
00444                                 Mat img=MatVid::loaddat(ipath,"t");     
00445                                 PatchOptions popt(winxy,winz,stepxy,stepz,ntype,bkvar);
00446                                 PatchBatchExtractor pbe(img, popt);
00447                                 cout<<"No of patches: "<<pbe.patches.size()<<endl;
00448                                 
00449                                 //vector<Mat> tmppat(pbe.patches.begin(),pbe.patches.begin()+40);
00450                                 //pbe.patches=tmppat;
00451 
00452                                  //computing likelihood for each patch for each label
00453                                 Mat LL(pbe.patches.size(),numlabels,OPT_MAT_TYPE);
00454                                 LL.setTo(0);
00455                                 for(int j=0;j<numlabels;j++)
00456                                 {
00457                                         cout<<"computing like with tag "<<j<<endl;
00458                                         Mat LLj;
00459                                         alltagdtm[j].getClasses(pbe.patches,LLj);
00460                                         Mat tmp;
00461                                         if(LLj.cols>1)
00462                                         {
00463                                                 Mat abc1=LLj.t();
00464                                                 tmp=DytexMix::logtrick(abc1);
00465                                         }
00466                                         else
00467                                         {
00468                                                 tmp=LLj;                                                
00469                                         }
00470                                         Mat tmp2=tmp.t();
00471                                         Mat a1=LL.col(j);
00472                                         tmp2.copyTo(a1);
00473                                 }                               
00474                                 
00475                                 
00476                                 //compute Z, etc.
00477                                 Mat logZ(pbe.patches.size(),numlabels,OPT_MAT_TYPE);
00478                                 logZ.setTo(0);
00479                                 Mat tmp3 = DytexMix::logtrick(LL.t()).t();                               
00480                                 for(int j=0;j<numlabels;j++)
00481                                 {
00482                                         Mat tmp4=LL.col(j) - tmp3;
00483                                         Mat b1=logZ.col(j);
00484                                         tmp4.copyTo(b1);                                
00485                                 }                               
00486                                 Mat Z;
00487                                 exp(logZ,Z);
00488     
00489         
00490                                 //average for each tag, and over time
00491                                 Mat LLm;
00492                                 reduce(LL, LLm, 0, CV_REDUCE_AVG);
00493                                 LLm=LLm/winz;                           
00494     
00495                                 // compute semantic multinomial
00496                                 Mat tmp5=DytexMix::logtrick(LLm.t());
00497                                 Mat smn;
00498                                 exp(LLm - tmp5.at<double>(1,1),smn);
00499 
00500                                 
00501                                 Mat smnlabmat,smnmat;
00502                                 sortIdx(smn, smnlabmat, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);
00503                                 cv::sort(smn, smnmat, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);
00504                                 
00505                                 std::vector<int> smnlab(35,0);
00506                                 std::vector<double> smnval(35,0);                               
00507                                 smnlabmat.row(0).copyTo(smnlab);
00508                                 smnmat.row(0).copyTo(smnval);
00509                                 
00510                                 
00511                                 //save results
00512                                 Bufferer buf2(opath2,fstream::out | fstream::binary);
00513                                 buf2.write("smnlab",smnlab);    
00514                                 buf2.write("smnval",smnval);    
00515                                 buf2.close();
00516 
00517                                 //write results in text file
00518                                 ofstream of(opath1.c_str());                            
00519                                 of<<vlist[vidid]<<endl;
00520                                 
00521                                 //writing Gtruth
00522                                 of<<"Gtruth: {";
00523                                 for(int m=0;m<annlist[vidid].size();m++)
00524                                 {
00525                                         if( m<(annlist[vidid].size()-1) )
00526                                         {
00527                                                 of<<annlist[vidid][m]<<", ";
00528                                         }
00529                                         else
00530                                         {
00531                                                 of<<annlist[vidid][m]<<"}"<<endl;
00532                                         }
00533                                 }
00534 
00535                                 //writing Results
00536                                 of<<"DTM-HEM: {";
00537                                 for(int m=0;m<35;m++)
00538                                 {
00539                                         if( m<34)
00540                                         {
00541                                                 of<<taglist[smnlab[m]]<<", ";
00542                                         }
00543                                         else
00544                                         {
00545                                                 of<<taglist[smnlab[m]]<<"}"<<endl;
00546                                         }
00547                                 }
00548 
00549                                 //check test or train video
00550                                 bool flag=false;
00551                                 for(int xx=0;xx<trainVidIds[trial].size();xx++)
00552                                 {
00553                                         if(vidid==trainVidIds[trial][xx])
00554                                         {
00555                                                 flag=true;
00556                                                 break;
00557                                         }
00558                                 }
00559                                 
00560                                 if(flag==true)
00561                                 {
00562                                         of<<"Train Video"<<endl;
00563                                 }
00564                                 else
00565                                 {
00566                                         of<<"Test Video"<<endl;
00567                                 }
00568                                 
00569 
00570 
00571                                 of.close();
00572                                 
00573                         }
00574                         else
00575                         {
00576                                 cout<<"annotation Already exists: "<<opath1<<endl;
00577                         }
00578                                 
00579                 }               
00580         }
00581 
00582 }
00583 
00589 void computePrecisionRecall()
00590 {
00591         PlatInd::makedir("../../testdata/HEMDTM/dyntex337/results/");
00592 
00593         //reading ground truth
00594         vector<string> vlist;
00595         vector<vector<string> > annlist;
00596         vector<string> taglist;
00597         readGtruthFile("../../testdata/HEMDTM/dyntex337/videos/dyntexgtruth.txt",vlist,annlist,taglist);
00598 
00599         //reading trial info
00600         vector<vector<int> > trainVidIds;
00601         vector<vector<int> > testVidIds;
00602         readTrialInfo("../../testdata/HEMDTM/dyntex337/videos/dyntexExperimentalSetup.txt",trainVidIds,testVidIds);
00603 
00604         int numlab=35;
00605 
00606         //PR for each trial
00607         for( int trial=0;trial<5;trial++)
00608         {
00609                 std::string trstr;
00610                 std::stringstream out;
00611                 out << (trial+1);
00612                 trstr = out.str();
00613 
00614                 PlatInd::makedir("../../testdata/HEMDTM/dyntex337/results/trial" + trstr);
00615                 
00616                 string opath="../../testdata/HEMDTM/dyntex337/results/trial" + trstr + "/rslt.bin";
00617                 
00618                         if(!PlatInd::fexist(opath)) //learn tag model
00619                         {
00620                                 cout<<"Generating PR for trial: "<<opath<<endl; 
00621 
00622                                 //loading HEM-DTM ann for alll videos
00623                                 std::vector<vector<int> > smnlaball;
00624                                 for( int vidid=0;vidid<vlist.size();vidid++)
00625                                 {                                       
00626                                         string ipath="../../testdata/HEMDTM/dyntex337/ann/trial" + trstr + "/" + vlist[vidid] + ".bin";                                 
00627                                         std::vector<int> smnlab;
00628                                         std::vector<double> smnval;     
00629                                         Bufferer buf(ipath,fstream::in | fstream::binary);
00630                                         buf.read("smnlab",smnlab);      
00631                                         smnlaball.push_back(smnlab);
00632                                         buf.close();
00633                                 }
00634 
00635                                 //how many test videos are labeled with each tag cCount
00636                                 std::vector<int> cCount(numlab,0);
00637                                 for( int tag=0;tag<numlab;tag++)
00638                                 {
00639                                         for( int vidid=0;vidid<testVidIds[trial].size();vidid++) //explore all test videos
00640                                         {
00641                                                 int j=testVidIds[trial][vidid]; //next test video
00642 
00643                                                 for(int numann=0;numann<annlist[j].size();numann++) //look at each lab
00644                                                 {
00645                                                         if(annlist[j][numann].compare(taglist[tag])==0)
00646                                                                 cCount[tag]++;
00647                                                 }
00648                                         }
00649                                 }
00650 
00651 
00652                                 //precison, recall at each level
00653 
00654                                 std::vector<vector<int> >  allaCount;
00655                                 std::vector<vector<int> >  allbCount;
00656 
00657                                 for(int lev=0;lev<numlab;lev++)
00658                                 {
00659                                         std::vector<int> aCount(numlab,0);
00660                                         std::vector<int> bCount(numlab,0);
00661 
00662                                         //precison, recall at each tag
00663                                         for( int tag=0;tag<numlab;tag++)
00664                                         {                                       
00665 
00666                                                 for( int vidid=0;vidid<testVidIds[trial].size();vidid++) //explore all test videos
00667                                                 {
00668                                                         int j=testVidIds[trial][vidid]; //next test video
00669 
00670                                                         for(int numann=0;numann<=lev;numann++) //look at each lab
00671                                                         {       
00672                                                                 string nextlab=taglist[smnlaball[j][numann]];
00673                                                                 
00674                                                                 if(nextlab.compare(taglist[tag])==0)
00675                                                                 {
00676                                                                         aCount[tag]++;
00677                                                                         //check if this video is labeled with this tag in gtruth
00678                                                                         for(int numann2=0;numann2<annlist[j].size();numann2++) //look at each lab
00679                                                                         {
00680                                                                                 if(annlist[j][numann2].compare(taglist[tag])==0)
00681                                                                                 bCount[tag]++;
00682                                                                         }
00683                                                                 }
00684 
00685                                                         }
00686                                                 }                                               
00687 
00688                                         }
00689                                         allaCount.push_back(aCount);
00690                                         allbCount.push_back(bCount);
00691                                 }
00692 
00693                                 //precision, recall for all tags at each level                          
00694 
00695                                 std::vector<vector<double> >  precision;
00696                                 std::vector<vector<double> >  recall;
00697                                 std::vector<vector<double> >  fmeasure;
00698                                 
00699 
00700                                 for(int lev=0;lev<numlab;lev++)
00701                                 {                                       
00702                                         vector<double> precisiontag(35,0);
00703                                         vector<double> recalltag(35,0);
00704                                         vector<double> fmeasuretag(35,0);
00705 
00706                                         //precison, recall at each tag
00707                                         for( int tag=0;tag<numlab;tag++)
00708                                         {                               
00709 
00710                                                 recalltag[tag]=(double)(allbCount[lev][tag])/(double)(cCount[tag]);
00711 
00712                                                 if(allaCount[lev][tag]!=0)
00713                                                 {
00714                                                         precisiontag[tag]=(double)(allbCount[lev][tag])/(double)(allaCount[lev][tag]);
00715                                                         
00716                                                 }
00717                                                 else
00718                                                 {
00719                                                         precisiontag[tag]=0;
00720                                                         
00721                                                 }
00722 
00723                                                 if( (precisiontag[tag]>0) && (recalltag[tag] >0) )
00724                                                 {
00725                                                         fmeasuretag[tag]= (2 * precisiontag[tag] * recalltag[tag])/(precisiontag[tag]+recalltag[tag]);
00726                                                 }
00727                                                 else
00728                                                 {
00729                                                         fmeasuretag[tag]=0;
00730                                                 }
00731 
00732                                                 
00733                                                 
00734                                                 
00735                                         }
00736 
00737                                         precision.push_back(precisiontag);
00738                                         recall.push_back(recalltag);
00739                                         fmeasure.push_back(fmeasuretag);
00740                                 }
00741 
00742                                 Bufferer bufo(opath,fstream::out | fstream::binary);
00743                                 bufo.write("precision",precision);                                      
00744                                 bufo.write("recall",recall);
00745                                 bufo.write("fmeasure",fmeasure);
00746                                 bufo.close();
00747 
00748                         }
00749                         else
00750                         {
00751                                 cout<<"PR Already Exist: "<<opath<<endl; 
00752                         }
00753         }
00754 
00755 
00756 
00757 }
00758 
00764 void printResults()
00765 {
00766         //reading ground truth
00767         vector<string> vlist;
00768         vector<vector<string> > annlist;
00769         vector<string> taglist;
00770         readGtruthFile("../../testdata/HEMDTM/dyntex337/videos/dyntexgtruth.txt",vlist,annlist,taglist);
00771 
00772         int printorder[35]={23, 22, 14, 5, 9, 3, 1, 8, 18, 15, 26, 12, 21, 10, 17, 24, 7, 13, 0, 27, 6, 11, 19, 4, 25, 2, 16, 20, 29, 32, 34, 31, 33, 30, 28};
00773 
00774         ofstream ofs("../../testdata/HEMDTM/dyntex337/results/results.txt");
00775 
00776         //PR for each trial
00777         std::vector<vector<double> >  avgprecision;
00778         std::vector<vector<double> >  avgrecall;
00779         std::vector<vector<double> >  avgfmeasure;
00780 
00781 
00782         //avgf(level)=(2*avgprec(level)*avgreca(level))/(avgprec(level)+avgreca(level));
00783 
00784         //average across trials
00785         for( int trial=0;trial<5;trial++)
00786         {
00787                 std::string trstr;
00788                 std::stringstream out;
00789                 out << (trial+1);
00790                 trstr = out.str();              
00791                 string ipath="../../testdata/HEMDTM/dyntex337/results/trial" + trstr + "/rslt.bin";
00792 
00793                 std::vector<vector<double> >  precision;
00794                 std::vector<vector<double> >  recall;           
00795                 std::vector<vector<double> >  fmeasure; 
00796 
00797                 Bufferer buf(ipath,fstream::in | fstream::binary);
00798                 buf.read("precision",precision);                                        
00799                 buf.read("recall",recall);
00800                 buf.read("fmeasure",fmeasure);
00801                 buf.close();
00802 
00803                 if(trial==0)
00804                 {
00805                         avgprecision=precision;
00806                         avgrecall= recall;
00807                         avgfmeasure= fmeasure;
00808                 }
00809                 else
00810                 {
00811                         for (int i=0;i<35;i++)  //levels
00812                         {
00813                                 avgprecision[i]=avgprecision[i]+precision[i];
00814                                 avgrecall[i]=avgrecall[i]+recall[i];
00815                                 avgfmeasure[i]=avgfmeasure[i]+fmeasure[i];
00816                                 
00817                         }
00818                 }
00819         }
00820 
00821         for (int i=0;i<35;i++)  //levels
00822         {
00823                 for (int j=0;j<35;j++)
00824                 {
00825                         avgprecision[i][j]=avgprecision[i][j]/5;
00826                         avgrecall[i][j]=avgrecall[i][j]/5;
00827                         avgfmeasure[i][j]=avgfmeasure[i][j]/5;
00828                 }
00829         }
00830 
00831         std::vector<double> alltagavgprecision(35,0);
00832         std::vector<double> alltagavgrecall(35,0);
00833         std::vector<double> alltagavgfmeasure(35,0);
00834 
00835         for (int i=0;i<35;i++)  //levels
00836         {
00837                 double sum=0;
00838                 double sum2=0;
00839                 double sum3=0;
00840                 for (int j=0;j<35;j++)
00841                 {
00842                         sum=sum+avgprecision[i][j];
00843                         sum2=sum2+avgrecall[i][j];
00844                         sum3=sum3+avgfmeasure[i][j];
00845                         
00846                 }
00847                 sum=sum/35;
00848                 sum2=sum2/35;
00849                 sum3=sum3/35;
00850 
00851                 alltagavgprecision[i]=sum;
00852                 alltagavgrecall[i]=sum2;
00853                 alltagavgfmeasure[i]=sum3;
00854         }
00855 
00856         //printing precision recall at level 3 for all tags
00857         cout<<"Precison, Recall and Fmeasure at level 3 for all tags:"<<endl;
00858         ofs<<"Precison, Recall and Fmeasure at level 3 for all tags:"<<endl;
00859         int fw=20;
00860         int levlp=2;
00861         cout<<left<<setw(fw)<<"Tag Name "<<setw(fw)<<"Precision "<<setw(fw)<<"Recall "<<setw(fw)<<"F-Measure "<<endl;
00862         ofs<<left<<setw(fw)<<"Tag Name "<<setw(fw)<<"Precision "<<setw(fw)<<"Recall "<<setw(fw)<<"F-Measure "<<endl;
00863 
00864         double avgprocessp=0;
00865         double avgprocessr=0;
00866         double avgprocessf=0;
00867 
00868         double avgstrutp=0;
00869         double avgstrutr=0;
00870         double avgstrutf=0;
00871 
00872         for (int i=0;i<35;i ++)
00873         {
00874                 
00875                 int j=printorder[i];
00876 
00877                 if(i<28)
00878                 {
00879                         avgprocessp=avgprocessp+avgprecision[levlp][j];
00880                         avgprocessr=avgprocessr+avgrecall[levlp][j];
00881                         avgprocessf=avgprocessf+avgfmeasure[levlp][j];
00882                 }
00883                 else
00884                 {
00885                         avgstrutp=avgstrutp+avgprecision[levlp][j];
00886                         avgstrutr=avgstrutr+avgrecall[levlp][j];
00887                         avgstrutf=avgstrutf+avgfmeasure[levlp][j];
00888                 }
00889 
00890                 cout<<left<<setw(fw)<<fixed<<setprecision(3)<<taglist[j]<<setw(fw)<<avgprecision[levlp][j]<<setw(fw)<<avgrecall[levlp][j]<<setw(fw)<<avgfmeasure[levlp][j]<<endl;       
00891                 ofs<<left<<setw(fw)<<fixed<<setprecision(3)<<taglist[j]<<setw(fw)<<avgprecision[levlp][j]<<setw(fw)<<avgrecall[levlp][j]<<setw(fw)<<avgfmeasure[levlp][j]<<endl;        
00892         }
00893 
00894         avgprocessp=avgprocessp/28;
00895         avgprocessr=avgprocessr/28;
00896         avgprocessf=avgprocessf/28;
00897         avgstrutp=avgstrutp/7;
00898         avgstrutr=avgstrutr/7;
00899         avgstrutf=avgstrutf/7;
00900 
00901         cout<<left<<setw(fw)<<fixed<<setprecision(3)<<"Process"<<setw(fw)<<avgprocessp<<setw(fw)<<avgprocessr<<setw(fw)<<avgprocessf<<endl;     
00902         ofs<<left<<setw(fw)<<fixed<<setprecision(3)<<"Process"<<setw(fw)<<avgprocessp<<setw(fw)<<avgprocessr<<setw(fw)<<avgprocessf<<endl;
00903         cout<<left<<setw(fw)<<fixed<<setprecision(3)<<"Structural"<<setw(fw)<<avgstrutp<<setw(fw)<<avgstrutr<<setw(fw)<<avgstrutf<<endl;        
00904         ofs<<left<<setw(fw)<<fixed<<setprecision(3)<<"Structural"<<setw(fw)<<avgstrutp<<setw(fw)<<avgstrutr<<setw(fw)<<avgstrutf<<endl;
00905         
00906 
00907         //printing Average precison, recall and F-measure at level 3
00908 
00909         cout<<"Average Precison, Recall and Fmeasure at level 3:"<<endl;
00910         ofs<<"Average Precison, Recall and Fmeasure at level 3:"<<endl;
00911 
00912         cout<<left<<"Average Precision "<<setw(fw)<<"Average Recall "<<setw(fw)<<"Average F-Measure "<<endl;
00913         ofs<<left<<"Average Precision "<<setw(fw)<<"Average Recall "<<setw(fw)<<"Average F-Measure "<<endl;
00914         cout<<setw(fw)<<left<<setw(fw)<<fixed<<setprecision(3)<<alltagavgprecision[levlp]<<setw(fw)<<alltagavgrecall[levlp]<<setw(fw)<<alltagavgfmeasure[levlp]<<endl;
00915         ofs<<setw(fw)<<left<<setw(fw)<<fixed<<setprecision(3)<<alltagavgprecision[levlp]<<setw(fw)<<alltagavgrecall[levlp]<<setw(fw)<<alltagavgfmeasure[levlp]<<endl;
00916 
00917 
00918         cout<<"Average Precison, Recall and Fmeasure at all levels "<<endl;
00919         ofs<<"Average Precison, Recall and Fmeasure at all levels "<<endl;
00920 
00921         cout<<left<<setw(fw)<<"Level: "<<setw(fw)<<"Average Precision "<<setw(fw)<<"Average Recall "<<setw(fw)<<"Average F-Measure "<<endl;
00922         ofs<<left<<setw(fw)<<"Level: "<<setw(fw)<<"Average Precision "<<setw(fw)<<"Average Recall "<<setw(fw)<<"Average F-Measure "<<endl;
00923 
00924         for (int i=0;i<35;i++)
00925         {
00926                 cout<<left<<setw(fw)<<fixed<<setprecision(3)<<setw(fw)<<i+1<<fixed<<setprecision(3)<<setw(fw)<<alltagavgprecision[i]<<setw(fw)<<alltagavgrecall[i]<<setw(fw)<<alltagavgfmeasure[i]<<endl;
00927                 ofs<<left<<setw(fw)<<fixed<<setprecision(3)<<setw(fw)<<i+1<<fixed<<setprecision(3)<<setw(fw)<<alltagavgprecision[i]<<setw(fw)<<alltagavgrecall[i]<<setw(fw)<<alltagavgfmeasure[i]<<endl;
00928                 
00929                 
00930         }
00931 
00932         ofs.close();
00933 
00934 }
00935 
00941 void tagbreakdown()
00942 {
00943         int printorder[35]={23, 22, 14, 5, 9, 3, 1, 8, 18, 15, 26, 12, 21, 10, 17, 24, 7, 13, 0, 27, 6, 11, 19, 4, 25, 2, 16, 20, 29, 32, 34, 31, 33, 30, 28};
00944 
00945         std::vector<vector<int> > tagdts;
00946         vector<int>  avgtagdts(35,0);
00947         
00948         
00949         //reading ground truth
00950         vector<string> vlist;
00951         vector<vector<string> > annlist;
00952         vector<string> taglist;
00953         readGtruthFile("../../testdata/HEMDTM/dyntex337/videos/dyntexgtruth.txt",vlist,annlist,taglist);
00954 
00955         //reading trial info
00956         vector<vector<int> > trainVidIds;
00957         vector<vector<int> > testVidIds;
00958         readTrialInfo("../../testdata/HEMDTM/dyntex337/videos/dyntexExperimentalSetup.txt",trainVidIds,testVidIds);
00959 
00960         //tag models for each trial
00961         for( int trial=0;trial<5;trial++)
00962         {               
00963                 vector<int> tagdtstr;
00964                 for( int tagid=0;tagid<35;tagid++)
00965                 {
00966                 
00967                                 std::vector<int> curTagIds=getTagVids(trainVidIds[trial],taglist[tagid],annlist);
00968                                 tagdtstr.push_back(curTagIds.size());
00969                                 
00970                 }               
00971                 tagdts.push_back(tagdtstr);
00972                 
00973         }
00974 
00975         for(int i=0;i<5;i++)
00976         {
00977                 avgtagdts=tagdts[i]+avgtagdts;
00978         }
00979 
00980         for(int i=0;i<35;i++)
00981         {
00982                 int j=printorder[i];
00983                 cout<<left<<setw(20)<<taglist[j]<<setw(20)<<fixed<<setprecision(1)<<(double)(avgtagdts[j])/5<<endl<<endl;
00984         }
00985         
00986 }
00987 
00996 string getTime()
00997 {
00998         std::time_t rawtime;
00999     std::tm* timeinfo;
01000     char buffer [80];
01001 
01002     std::time(&rawtime);
01003     timeinfo = std::localtime(&rawtime);
01004 
01005     std::strftime(buffer,80,"%Y-%m-%d-%H-%M-%S",timeinfo);
01006     std::puts(buffer);
01007 
01008         string tmpstr(buffer);
01009 
01010         return tmpstr;
01011 }
01012 
01018 int main(int argc, char** argv)
01019 {
01020         string opath="../../testdata/HEMDTM/dyntex337/log.txt";
01021         ofstream fs(opath.c_str());
01022         fs<<"Experiment Started: "<<getTime()<<endl;
01023         fs<<"learning base DTs: "<<getTime()<<endl;
01024         learnBaseDTM();
01025         fs<<"learning Tag DTs: "<<getTime()<<endl;
01026         learnTagDTM();
01027         fs<<"annotating videos: "<<getTime()<<endl;
01028         annotateVideos();
01029         fs<<"Computing precisionr recall: "<<getTime()<<endl;
01030         computePrecisionRecall();
01031         printResults();
01032         fs<<"Experiment Finish: "<<getTime()<<endl;
01033         fs.close();
01034 }

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