test_BoSClassification.cpp

00001 
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <string>
00011 #include <iterator>
00012 #include<fstream>
00013 #include<math.h>
00014 #include <ctime>
00015 
00016 #include "opencv/cv.h"
00017 #include "opencv/cxcore.h"
00018 #include "opencv/highgui.h"
00019 
00020 #include "options.h"
00021 #include "utils/libutils.h"
00022 #include "stats/libstats.h"
00023 #include "video/libvideo.h"
00024 #include "dytex/libdytex.h"
00025 #include "platInd/platInd.hpp"
00026 #include "bufferer/Bufferer.hpp"
00027 
00028 using namespace cv;
00029 using namespace std;
00030 
00031 
00037 vector<string> getNextLineAndSplitIntoTokens(ifstream& str)
00038 {
00039     std::vector<string>   result;
00040     std::string                line;
00041     std::getline(str,line);
00042 
00043     std::stringstream          lineStream(line);
00044     std::string                cell;
00045 
00046     while(getline(lineStream,cell,','))
00047     {
00048         result.push_back(cell);
00049     }
00050     return result;
00051 }
00052 
00058 vector<vector<string> > readGtruthFile(string fpath)
00059 {
00060         vector<vector<string> > result;
00061         ifstream vlistf(fpath.c_str(), std::ifstream::in);
00062         while(vlistf.good())
00063         {
00064                 vector<string> line=getNextLineAndSplitIntoTokens(vlistf);
00065                 result.push_back(line);
00066         }
00067 
00068         return result;
00069 }
00070 
00076 void learnBaseDTM()
00077 {
00078         PlatInd::makedir("../../testdata/BoS/ucla9c8/basedtm/");
00079         //video list    
00080         vector<vector<string> > vlist=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth1.txt");
00081 
00082         //Setting DTM learning Options  
00083         int winxy=5;
00084         int winz=75;
00085         int stepxy=2;
00086         int stepz=75;
00087         PatchOptions::norm_type ntype=PatchOptions::NORM_NONE;
00088         double bkvar=-1;
00089         int K=4;
00090         int n=10;
00091         double reg=0.5;
00092         
00093         for(int i=0;i<vlist.size();i++)
00094         {
00095                 string ipath="../../testdata/BoS/ucla9c8/videos/" + vlist[i][3] + "/" +  vlist[i][4] + "/" + vlist[i][5];                       
00096                 string opath="../../testdata/BoS/ucla9c8/basedtm/" + vlist[i][3] + "_" +  vlist[i][4] + "_" + vlist[i][5] + ".dtm";                                     
00097                 
00098                 if(!PlatInd::fexist(opath))
00099                 {
00100                         cout<<"Generating BaseDTM for: "<<ipath<<endl;
00101                         //learn the model               
00102                         VidSegm vs(winxy,winz,stepxy,stepz,ntype,bkvar,K,n,reg);                
00103                         vs.learnDTM(ipath,false);       
00104 
00105                         //save the DTM          
00106                         Bufferer buf(opath,fstream::out | fstream::binary);
00107                         buf.write(vs.dtm);      
00108                         buf.close();
00109                 }
00110                 else
00111                 {
00112                         cout<<"Base DTM exists for: "<<ipath<<endl;
00113                 }
00114         }
00115 }
00121 void generateBaseCodebook()
00122 {
00123         PlatInd::makedir("../../testdata/BoS/ucla9c8/codebook/");
00124 
00125         for(int trial=1;trial<=20;trial++)
00126         {
00127                 std::string trstr;
00128                 std::stringstream out;
00129                 out << trial;
00130                 trstr = out.str();
00131 
00132                 PlatInd::makedir("../../testdata/BoS/ucla9c8/codebook/trial"+trstr);
00133                 string opath="../../testdata/BoS/ucla9c8/codebook/trial"+trstr+"/184.dtm";
00134                 if(!PlatInd::fexist(opath))
00135                 {
00136                         cout<<"Generating BaseCodebook: "<<opath<<endl;
00137                         DytexMix rDTM;
00138 
00139                         //read gtruth
00140                         vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");
00141 
00142         
00143                         //look in all the baseDTs
00144                         for(int i=0;i<gtruth.size();i++)
00145                         {
00146                                 if(gtruth[i][2].compare("1")==0)
00147                                 {
00148                                         string bdtmpt="../../testdata/BoS/ucla9c8/basedtm/" + gtruth[i][3] + "_" +  gtruth[i][4] + "_" + gtruth[i][5] + ".dtm";                                                                                                 
00149                                         //load DTM
00150                                         DytexMix dtm;                   
00151                                         Bufferer buf(bdtmpt,fstream::in | fstream::binary);
00152                                         buf.read(dtm);                  
00153                                         buf.close();
00154                                         //put in the rDTM
00155                                         for(int j=0;j<dtm.alpha.size();j++)
00156                                         {
00157                                                 rDTM.alpha.push_back(dtm.alpha[j]);
00158                                                 rDTM.dt.push_back(dtm.dt[j]);
00159                                         }
00160                                 }               
00161                         }
00162         
00163                         //normalize alpha
00164                         double alphaSum=0.0;
00165                         for(int i=0;i<rDTM.alpha.size();i++)
00166                         {
00167                                 alphaSum=alphaSum+rDTM.alpha[i];
00168                         }
00169                         for(int i=0;i<rDTM.alpha.size();i++)
00170                         {
00171                                 rDTM.alpha[i]=rDTM.alpha[i]/alphaSum;
00172                         }
00173 
00174                         
00175 
00176                         rDTM.opt=rDTM.dt[0].dtopt;
00177 
00178                         //save base codebook DT                         
00179                         std::string tdtstr;
00180                         std::stringstream out1;
00181                         out1 << rDTM.alpha.size();;
00182                         tdtstr = out1.str();
00183                         string nopath="../../testdata/BoS/ucla9c8/codebook/trial"+trstr+"/" + tdtstr + ".dtm";
00184                         Bufferer buf(nopath,fstream::out | fstream::binary);
00185                         buf.write(rDTM);        
00186                         buf.close();                    
00187                 }
00188                 else
00189                 {
00190                         cout<<"Base Codebbok Already Exists: "<<opath<<endl;
00191                 }
00192                 
00193         }
00194 }
00195 
00201 void reduceCodebook()
00202 {       
00203         int K=16;
00204         for(int trial=1;trial<=20;trial++)
00205         {
00206                 std::string trstr;
00207                 std::stringstream out;
00208                 out << trial;
00209                 trstr = out.str();
00210 
00211                 
00212                 string opath="../../testdata/BoS/ucla9c8/codebook/trial"+trstr+"/184-16.dtm";
00213                 if(!PlatInd::fexist(opath))
00214                 {
00215                         cout<<"Generating Reduce Codebook: "<<opath<<endl;
00216                         //read gtruth
00217                         vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");
00218                         //load base codebook
00219                         string bcbpt="../../testdata/BoS/ucla9c8/codebook/trial"+trstr+"/184.dtm";                      
00220                         DytexMix bDTM;                          
00221                         Bufferer buf(bcbpt,fstream::in | fstream::binary);
00222                         buf.read(bDTM);                 
00223                         buf.close();
00224 
00225                         //Reduce 
00226                         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);
00227                         HEMOptions hopt(K,ropt,0.0001,DytexOptions::NONZERO_YMEAN,HEMOptions::COMPACT);
00228                         for(int i=1;i<=K;i=i*2)
00229                         hopt.splitOpt.sched.push_back(i);
00230         
00231                         timerOn();
00232                         DytexMix rDTM=bDTM.reduceWithSplitting(hopt);
00233                         int traintime = timerOff();
00234                         cout<<"Total Time Taken :"<<(double)traintime/(double)1000<<endl;
00235 
00236                         //save reduce codebook DT                               
00237                         Bufferer bufr(opath,fstream::out | fstream::binary);
00238                         bufr.write(rDTM);       
00239                         bufr.close();                   
00240                 }
00241                 else
00242                 {
00243                         cout<<"Reduced Codebbok Already Exists: "<<opath<<endl;
00244                 }
00245                 
00246         }
00247 }
00248 
00254 void generateHistogramns()
00255 {       
00256         //patch options
00257         int tau=75;
00258         int winxy=5;
00259         int winz=75;
00260         int stepxy=2;
00261         int stepz=75;
00262         PatchOptions::norm_type ntype=PatchOptions::NORM_NONE;
00263         double bkvar=-1;
00264         
00265 
00266         for(int trial=1;trial<=20;trial++)
00267         {
00268                 //trial string
00269                 std::string trstr;
00270                 std::stringstream out;
00271                 out << trial;
00272                 trstr = out.str();
00273 
00274                 //hist directory
00275                 PlatInd::makedir("../../testdata/BoS/ucla9c8/histograms");
00276                 PlatInd::makedir("../../testdata/BoS/ucla9c8/histograms/trial"+trstr+"/");
00277                 
00278 
00279                 //loading codebook
00280                 string cbpath="../../testdata/BoS/ucla9c8/codebook/trial"+trstr+"/184-16.dtm";
00281                 DytexMix cb;                            
00282                 Bufferer buf(cbpath,fstream::in | fstream::binary);
00283                 buf.read(cb);                   
00284                 buf.close();
00285                 cb.setupKFB(tau);
00286 
00287                 //reading gtruth for trial
00288                 vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");
00289 
00290                 //computing histograms for all videos
00291                 for(int vid=0;vid<gtruth.size();vid++)
00292                 {
00293                         string ipath="../../testdata/BoS/ucla9c8/videos/" + gtruth[vid][3] + "/" +  gtruth[vid][4] + "/" + gtruth[vid][5];                      
00294                         string opath="../../testdata/BoS/ucla9c8/histograms/trial"+trstr+"/" + gtruth[vid][3] + "_" +  gtruth[vid][4] + "_" + gtruth[vid][5] + ".hist";                                 
00295                         
00296 
00297                         if(!PlatInd::fexist(opath))
00298                         {
00299                                 cout<<"computing Histogram for: "<<opath<<endl;
00300                                 std::vector<int> Nk(cb.dt.size(),0);
00301                                 std::vector<int> patdt;
00302                                 int likecount=0;
00303 
00304                                 //Get patches for current video
00305                                 Mat img=MatVid::loaddat(ipath,"t");     
00306                                 PatchOptions popt(winxy,winz,stepxy,stepz,ntype,bkvar);
00307                                 PatchBatchExtractor pbe(img, popt);
00308                                 cout<<"No of patches: "<<pbe.patches.size()<<endl;
00309 
00310                                 //Get likelihood of patches
00311                                 Mat LLm, tempM;                                                         
00312                                 cb.getClasses(pbe.patches,LLm);
00313                                 likecount=likecount+(LLm.rows*LLm.cols); //update # like count
00314                                 sortIdx(LLm, tempM, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);                                    
00315                                 Mat patclass=(tempM.colRange(0,1)).clone();
00316 
00317                                 //Get Nk 
00318                                 for (int j=0;j<pbe.patches.size();j++)
00319                                 {
00320                                         int patdtloc=patclass.at<int>(j,0);
00321                                         Nk[patdtloc]=Nk[patdtloc]+1;
00322                                         patdt.push_back(patdtloc);
00323                                 }               
00324 
00325                                 
00326                                 //saving histogrmas
00327                                 Bufferer bufout(opath,fstream::out | fstream::binary);
00328                                 bufout.write("likecount",likecount);
00329                                 bufout.write("Nk",Nk);  
00330                                 bufout.write("patdt",patdt);
00331                                 bufout.close();
00332 
00333                         }
00334                         else
00335                         {
00336                                 cout<<"Histogram Already Exists: "<<opath<<endl;
00337                         }
00338                 }               
00339                 
00340         }
00341 }
00342 
00348 void generateTFIDF()
00349 {       
00350         
00351         for(int trial=1;trial<=20;trial++)
00352         {       
00353 
00354                 //trial string
00355                 std::string trstr;
00356                 std::stringstream out;
00357                 out << trial;
00358                 trstr = out.str();
00359 
00360                 //tfidf directory
00361                 PlatInd::makedir("../../testdata/BoS/ucla9c8/TFIDF");
00362                 PlatInd::makedir("../../testdata/BoS/ucla9c8/TFIDF/trial"+trstr+"/");
00363                 
00364                                                 
00365                 //reading gtruth for trial
00366                 vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");
00367 
00368                 int lentrain=0;
00369                 int lenk=16;
00370                 //computing VTFIDF
00371                 //how many training videos have codeword i
00372                 std::vector<double> VTFIDF(16,0);
00373                 for(int v=0;v<gtruth.size();v++)
00374                 {
00375                         if(gtruth[v][2].compare("1")==0) //training video
00376                         {
00377                                 lentrain++;
00378                                 string histpath="../../testdata/BoS/ucla9c8/histograms/trial"+trstr+"/" + gtruth[v][3] + "_" +  gtruth[v][4] + "_" + gtruth[v][5] + ".hist";                                                    
00379                                 
00380                                 //loading histogrmas
00381                                 Bufferer histbuf(histpath,fstream::in | fstream::binary);
00382                                 int likecount;
00383                                 std::vector<int> Nk;
00384                                 histbuf.read("likecount",likecount);
00385                                 histbuf.read("Nk",Nk);                          
00386                                 histbuf.close();
00387 
00388                                 //update VTFIDF
00389                                 for (int cw=0;cw<16;cw++)
00390                                 {
00391                                         if( (Nk[cw]>0))
00392                                                 VTFIDF[cw]=VTFIDF[cw]+1;
00393                                 }
00394                         }
00395                 }
00396                 for(int cw=0;cw<lenk;cw++)
00397                 {
00398                         if(VTFIDF[cw]!=0)
00399                         {
00400                                 VTFIDF[cw]=log( ((double)lentrain/(double)VTFIDF[cw]) );
00401                         }
00402                 }
00403 
00404 
00405 
00406                 
00407                 //computing TFIDF for all videos
00408                 for(int vid=0;vid<gtruth.size();vid++)
00409                 {
00410                         string histpath="../../testdata/BoS/ucla9c8/histograms/trial"+trstr+"/" + gtruth[vid][3] + "_" +  gtruth[vid][4] + "_" + gtruth[vid][5] + ".hist";                                                      
00411                         string opath="../../testdata/BoS/ucla9c8/TFIDF/trial"+trstr+"/" + gtruth[vid][3] + "_" +  gtruth[vid][4] + "_" + gtruth[vid][5] + ".tfidf";                                     
00412                         
00413 
00414                         if(!PlatInd::fexist(opath))
00415                         {
00416                                 cout<<"computing TF-IDF for: "<<opath<<endl;
00417 
00418                                 //loading histogrmas
00419                                 Bufferer histbuf(histpath,fstream::in | fstream::binary);
00420                                 int likecount;
00421                                 std::vector<int> Nk;
00422                                 histbuf.read("likecount",likecount);
00423                                 histbuf.read("Nk",Nk);                          
00424                                 histbuf.close();
00425                                 
00426                                 int sumNk=0;
00427                                 for(int i=0;i<Nk.size();i++)
00428                                 {
00429                                         sumNk=sumNk+Nk[i];
00430                                 }
00431 
00432                                 //computing TF & IDF
00433                                 std::vector<double> TF;
00434                                 std::vector<double> IDF;
00435                                 for(int i=0;i<Nk.size();i++)
00436                                 {
00437                                         double tmp=(double)((double)Nk[i]/(double)sumNk);
00438                                         double tmp2=tmp*VTFIDF[i];
00439 
00440                                         TF.push_back(tmp);
00441                                         IDF.push_back(tmp2);                                    
00442                                 }
00443                                 //L1 norm for IDF & TF
00444                                 double sumTF=0;
00445                                 double sumIDF=0;
00446                                 for(int i=0;i<Nk.size();i++)
00447                                 {
00448                                         sumTF=TF[i]+sumTF;
00449                                         sumIDF=IDF[i]+sumIDF;
00450                                 }
00451                                 for(int i=0;i<Nk.size();i++)
00452                                 {                               
00453                                         TF[i]=TF[i]/sumTF;
00454                                         IDF[i]=IDF[i]/sumIDF;                                   
00455                                 }
00456 
00457                                 //saving TF-IDF
00458                                 Bufferer bufout(opath,fstream::out | fstream::binary);
00459                                 bufout.write("TF",TF);
00460                                 bufout.write("IDF",IDF);                                        
00461                                 bufout.close();
00462 
00463 
00464                         }
00465                         else
00466                         {
00467                                 cout<<"TF-IDF Already Exists: "<<opath<<endl;
00468                         }
00469                 }                               
00470         }
00471 }
00472 
00478 double chiSquareDist(std::vector<double> &vec1,std::vector<double> &vec2)
00479 {
00480         double dist=0;
00481 
00482         for(int i=0;i<vec1.size();i++)
00483         {
00484                 if( (vec1[i]+vec2[i]) !=0 )
00485                 {
00486                         dist=dist+((abs(vec1[i]-vec2[i]))/(vec1[i]+vec2[i]));
00487                 }
00488         }
00489         dist=dist/2;
00490 
00491         return dist;
00492 }
00493 
00499 double squareRootDist(std::vector<double> &vec1,std::vector<double> &vec2)
00500 {
00501         double dist=0;
00502         for(int i=0;i<vec1.size();i++)
00503         {
00504                 dist=dist+ sqrt(vec1[i]*vec2[i]);
00505         }       
00506         dist=acos(dist);
00507         return dist;
00508 }
00509 
00510 
00516 int getClassNN(std::vector<double> &testVec,std::vector< std::vector<double> > &alltrainVec,int type,int nn,std::vector<string> &trainClassIds)
00517 {
00518         
00519 
00520         vector<double> alldist;
00521         
00522         //distance of test video to all training videos
00523         for(int i=0;i<alltrainVec.size();i++)
00524         {
00525                 double dist;
00526                 if(type==1)
00527                 {
00528                         dist=chiSquareDist(testVec,alltrainVec[i]);
00529                         
00530                 }
00531                 else
00532                 {
00533                         dist=squareRootDist(testVec,alltrainVec[i]);
00534                 }
00535                 alldist.push_back(dist);
00536         }
00537 
00538         //need to check this code updated for linux
00539         std::vector<int> tempM(alldist.size(),0);
00540         Mat tempM2;
00541         Mat tempM3(alldist);
00542         sortIdx(tempM3.t(), tempM2, CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);     
00543         tempM2.copyTo(tempM);
00544 
00545         std::vector<int> topNNMat(tempM.begin(),tempM.begin()+nn);
00546         std::vector<int> topNNMatids;
00547         for(int i=0;i<topNNMat.size();i++)
00548         {
00549                 topNNMatids.push_back(atoi(trainClassIds[topNNMat[i]].c_str()));
00550         }
00551                 
00552         //Getiing mode
00553         std::vector<int> histogram(9,0); //9 classes in UCLA9
00554         for( int i=0; i<topNNMatids.size(); i++ )
00555         {
00556           histogram[topNNMatids[i]-1]++;
00557         }
00558         return (std::max_element( histogram.begin(), histogram.end() ) - histogram.begin())+1;  
00559         
00560 }
00561 
00567 void doNNClassification()
00568 {
00569         PlatInd::makedir("../../testdata/BoS/ucla9c8/NNClassification");
00570         
00571         for(int trial=1;trial<=20;trial++)
00572         {
00573                 
00574                 
00575                 std::string trstr;
00576                 std::stringstream out;
00577                 out << trial;
00578                 trstr = out.str();
00579 
00580                 PlatInd::makedir("../../testdata/BoS/ucla9c8/NNClassification/trial"+trstr+"/");                
00581                 string opath="../../testdata/BoS/ucla9c8/NNClassification/trial"+trstr+"/nnclassrslt.bin";
00582                 if(!PlatInd::fexist(opath))
00583                 {
00584                         cout<<"Doing NN Classification for: "<<opath<<endl;
00585                         //read gtruth
00586                         vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");
00587 
00588                         //load TF-IDF for all videos
00589                         std::vector< std::vector<double> > testTF;
00590                         std::vector< std::vector<double> > trainTF;
00591                         std::vector< std::vector<double> > testIDF;
00592                         std::vector< std::vector<double> > trainIDF;
00593                         std::vector<string> trainClassIds;
00594                         for(int vid=0;vid<gtruth.size();vid++)
00595                         {                                               
00596                                 std::vector<double> TF;
00597                                 std::vector<double> IDF;
00598                                 string tfidfpt="../../testdata/BoS/ucla9c8/TFIDF/trial"+trstr+"/" + gtruth[vid][3] + "_" +  gtruth[vid][4] + "_" + gtruth[vid][5] + ".tfidf";                                                                                           
00599                                 Bufferer buf(tfidfpt,fstream::in | fstream::binary);
00600                                 buf.read("TF",TF);                      
00601                                 buf.read("IDF",IDF);
00602                                 buf.close();
00603 
00604                                 if(gtruth[vid][2].compare("1")==0)
00605                                 {
00606                                         trainTF.push_back(TF);
00607                                         trainIDF.push_back(IDF);
00608                                         trainClassIds.push_back(gtruth[vid][0]);
00609                                 }
00610                                 else
00611                                 {
00612                                         testTF.push_back(TF);
00613                                         testIDF.push_back(IDF);
00614                                 }
00615                         }
00616 
00617                         //classify test videos
00618                         std::vector< std::vector<int> > rslt;
00619                         for( int i=0;i<testTF.size();i++)
00620                         {
00621                                 std::vector<int> vidrslt;
00622                                 int tmp;
00623                                 
00624                                 tmp=getClassNN(testTF[i],trainTF,1,1,trainClassIds);  //TF-CS-1
00625                                 vidrslt.push_back(tmp);
00626 
00627                                 tmp=getClassNN(testTF[i],trainTF,1,3,trainClassIds);  //TF-CS-3
00628                                 vidrslt.push_back(tmp);
00629 
00630 
00631                                 tmp=getClassNN(testTF[i],trainTF,2,1,trainClassIds);  //TF-SR-1
00632                                 vidrslt.push_back(tmp);
00633 
00634                                 tmp=getClassNN(testTF[i],trainTF,2,3,trainClassIds);  //TF-SR-3
00635                                 vidrslt.push_back(tmp);
00636 
00637                                 tmp=getClassNN(testIDF[i],trainIDF,1,1,trainClassIds);  //IDF-CS-1
00638                                 vidrslt.push_back(tmp);
00639 
00640                                 tmp=getClassNN(testIDF[i],trainIDF,1,3,trainClassIds);  //IDF-CS-3
00641                                 vidrslt.push_back(tmp);
00642 
00643 
00644                                 tmp=getClassNN(testIDF[i],trainIDF,2,1,trainClassIds);  //IDF-SR-1
00645                                 vidrslt.push_back(tmp);
00646 
00647                                 tmp=getClassNN(testIDF[i],trainIDF,2,3,trainClassIds);  //IDF-SR-3
00648                                 vidrslt.push_back(tmp);
00649 
00650                                 rslt.push_back(vidrslt);
00651 
00652                         }
00653 
00654 
00655                         //save reduce codebook DT                               
00656                         Bufferer bufr(opath,fstream::out | fstream::binary);
00657                         bufr.write("results",rslt);
00658                         bufr.close();                   
00659                         
00660 
00661                 }
00662                 else
00663                 {
00664                         cout<<"NN Classification rslt Already Exists: "<<opath<<endl;
00665                 }
00666                 
00667         }
00668 }
00669 
00675 void calculateAccuracy()
00676 {
00677         PlatInd::makedir("../../testdata/BoS/ucla9c8/Accuracy");
00678 
00679         //accuracy for each trial
00680         for(int trial=1;trial<=20;trial++)
00681         {
00682                 std::string trstr;
00683                 std::stringstream out;
00684                 out << trial;
00685                 trstr = out.str();
00686 
00687                 PlatInd::makedir("../../testdata/BoS/ucla9c8/Accuracy/trial"+trstr+"/");                                
00688                 string opath="../../testdata/BoS/ucla9c8/Accuracy/trial"+trstr+"/accuracy.bin";
00689                 if(!PlatInd::fexist(opath))
00690                 {
00691                         cout<<"Computing Accuracy: "<<opath<<endl;
00692                         
00693                         //read gtruth
00694                         vector<vector<string> > gtruth=readGtruthFile("../../testdata/BoS/ucla9c8/gtruth/gtruth"+trstr+".txt");                 
00695                         std::vector<int> testClassIds;
00696                         for(int vid=0;vid<gtruth.size();vid++)
00697                         {                                                               
00698                                 if(gtruth[vid][2].compare("2")==0)
00699                                 {                                       
00700                                         testClassIds.push_back(atoi(gtruth[vid][0].c_str()));
00701                                 }                               
00702                         }
00703                         //load classification results
00704                         std::vector< std::vector<int> > rslt;
00705                         string rsltpath="../../testdata/BoS/ucla9c8/NNClassification/trial"+trstr+"/nnclassrslt.bin";
00706                         Bufferer bufr(rsltpath,fstream::in | fstream::binary);
00707                         bufr.read("results",rslt);
00708                         bufr.close();   
00709 
00710                         //correct count for all NN methods
00711                         std::vector<int> correctCount(8,0); //all NN methods
00712                         for(int i=0;i<testClassIds.size();i++)
00713                         {
00714                                 for(int j=0;j<8;j++)
00715                                 {
00716                                         if(testClassIds[i]==rslt[i][j])
00717                                         {
00718                                                 correctCount[j]++;
00719                                         }
00720                                 }
00721                                 
00722                         }
00723 
00724                         std::vector<double> accuracy;
00725                         for(int j=0;j<8;j++)
00726                         {
00727                                 accuracy.push_back(((double)correctCount[j]/(double)testClassIds.size())*100);
00728                         }
00729                         
00730                         //save reduce codebook DT                               
00731                         Bufferer buf(opath,fstream::out | fstream::binary);
00732                         buf.write("accuracy",accuracy); 
00733                         buf.close();            
00734                 }
00735                 else
00736                 {
00737                         cout<<"Accuracy file Already Exists: "<<opath<<endl;
00738                 }
00739                 
00740         }
00741 
00742 }
00743 
00749 void printAvgAccuracy()
00750 {
00751         std::vector<double> avgAcc(8,0);  //all methods
00752 
00753         //accuracy for each trial
00754         for(int trial=1;trial<=20;trial++)
00755         {
00756                 std::string trstr;
00757                 std::stringstream out;
00758                 out << trial;
00759                 trstr = out.str();              
00760                 string acpath="../../testdata/BoS/ucla9c8/Accuracy/trial"+trstr+"/accuracy.bin";
00761 
00762                 std::vector<double> accuracy;
00763                 Bufferer buf(acpath,fstream::in | fstream::binary);
00764                 buf.read("accuracy",accuracy);  
00765                 buf.close();
00766 
00767                 for(int i=0;i<8;i++)
00768                 {
00769                         avgAcc[i]=avgAcc[i]+accuracy[i];
00770                 }
00771         }
00772 
00773         for(int i=0;i<8;i++)
00774         {
00775                 avgAcc[i]=avgAcc[i]/20;
00776         }
00777         
00778         string opath="../../testdata/BoS/ucla9c8/Accuracy/avgaccuracy.txt";
00779         ofstream fs(opath.c_str());
00780 
00781         cout<<"Average Results for different Methods:"<<endl;
00782         cout<<"TF -CS-1: "<<fixed<<setprecision(2)<<avgAcc[0]<<"%"<<endl;
00783         cout<<"TF -CS-3: "<<fixed<<setprecision(2)<<avgAcc[1]<<"%"<<endl;
00784         cout<<"TF -SR-1: "<<fixed<<setprecision(2)<<avgAcc[2]<<"%"<<endl;
00785         cout<<"TF -SR-3: "<<fixed<<setprecision(2)<<avgAcc[3]<<"%"<<endl;
00786         cout<<"IDF-CS-1: "<<fixed<<setprecision(2)<<avgAcc[4]<<"%"<<endl;
00787         cout<<"IDF-CS-3: "<<fixed<<setprecision(2)<<avgAcc[5]<<"%"<<endl;
00788         cout<<"IDF-SR-1: "<<fixed<<setprecision(2)<<avgAcc[6]<<"%"<<endl;
00789         cout<<"IDF-SR-3: "<<fixed<<setprecision(2)<<avgAcc[7]<<"%"<<endl;
00790 
00791 
00792         fs<<"Average Results for different Methods:"<<endl;
00793         fs<<"TF -CS-1: "<<fixed<<setprecision(2)<<avgAcc[0]<<"%"<<endl;
00794         fs<<"TF -CS-3: "<<fixed<<setprecision(2)<<avgAcc[1]<<"%"<<endl;
00795         fs<<"TF -SR-1: "<<fixed<<setprecision(2)<<avgAcc[2]<<"%"<<endl;
00796         fs<<"TF -SR-3: "<<fixed<<setprecision(2)<<avgAcc[3]<<"%"<<endl;
00797         fs<<"IDF-CS-1: "<<fixed<<setprecision(2)<<avgAcc[4]<<"%"<<endl;
00798         fs<<"IDF-CS-3: "<<fixed<<setprecision(2)<<avgAcc[5]<<"%"<<endl;
00799         fs<<"IDF-SR-1: "<<fixed<<setprecision(2)<<avgAcc[6]<<"%"<<endl;
00800         fs<<"IDF-SR-3: "<<fixed<<setprecision(2)<<avgAcc[7]<<"%"<<endl;
00801         fs.close();     
00802 
00803 }
00804 
00810 string getTime()
00811 {
00812         std::time_t rawtime;
00813     std::tm* timeinfo;
00814     char buffer [80];
00815 
00816     std::time(&rawtime);
00817     timeinfo = std::localtime(&rawtime);
00818 
00819     std::strftime(buffer,80,"%Y-%m-%d-%H-%M-%S",timeinfo);
00820     std::puts(buffer);
00821 
00822         string tmpstr(buffer);
00823 
00824         return tmpstr;
00825 }
00826 
00832 int main(int argc, char** argv)
00833 {
00834         string opath="../../testdata/BoS/ucla9c8/log.txt";
00835         ofstream fs(opath.c_str());
00836 
00837         fs<<"Experiment Started: "<<getTime()<<endl;
00838         fs<<"learning base DTs: "<<getTime()<<endl;
00839         learnBaseDTM();
00840         fs<<"Generating Base Codebook: "<<getTime()<<endl;
00841         generateBaseCodebook();
00842         fs<<"Reducing Codebook: "<<getTime()<<endl;
00843         reduceCodebook();
00844         fs<<"Generating Histograms: "<<getTime()<<endl;
00845         generateHistogramns();
00846         fs<<"Generating TFIDF: "<<getTime()<<endl;
00847     generateTFIDF();
00848         fs<<"NN classification: "<<getTime()<<endl;
00849         doNNClassification();
00850         fs<<"Calculating accuracy: "<<getTime()<<endl;
00851         calculateAccuracy();    
00852         printAvgAccuracy();
00853 
00854         fs<<"Experiment Finish: "<<getTime()<<endl;
00855         fs.close();
00856 
00857         
00858 }

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