Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

Bayes.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_Bayes_h
00002 #define Impala_Core_Table_Bayes_h
00003 
00004 namespace Impala
00005 {
00006 namespace Core
00007 {
00008 namespace Table
00009 {
00010 
00015 class Bayes
00016 {
00017 public:
00018 
00019     Bayes(String annoSetName, String setName, String genreSet, String conceptSet, 
00020                                               String quid, int nStart,int nEnd)
00021     {
00022         mAnnoSetName = annoSetName;
00023         mSetName = setName;
00024         mGenreSet = genreSet;     //genre set: g_{j}
00025         mConceptSet = conceptSet; //concept set: c_{k}
00026         mQuid = quid;
00027 
00028         if (quid == "Image")
00029         {
00030             mIsImageSet = true;
00031             //mImgSet = (Core::ImageSet::ImageSet*) mSet;
00032             //mAnnoImgSet = (Core::ImageSet::ImageSet*) mAnnoSet;
00033         }
00034         else
00035         {
00036             mIsImageSet = false;
00037             //mVidSet = (Core::VideoSet::VideoSet*)mSet;
00038             //mAnnoVidSet = (Core::VideoSet::VideoSet*)mAnnoSet;
00039         }
00040 
00041         mbDumpTable = false;
00042         mStart = nStart;
00043         mEnd = nEnd;
00044 
00045         //mVerbose = false;
00046         mVerbose = true;
00047 
00048         mTableSize = 0;
00049 
00050         mMatrixFile = "Genre/" + mConceptSet + "/" + mGenreSet + "_" + "ConProb.matrix";
00051     
00052         if (mAnnoSetName != "")
00053         {
00054             if (mIsImageSet)
00055             {
00056                 mAnnoImgSet = Core::ImageSet::MakeImageSet(mAnnoSetName);
00057                 mTabGenreSet = Core::Table::AnnotationTableSet::MakeFromFile(mAnnoImgSet, mGenreSet, true,
00058                                                                          StringToQuidClass(mQuid));
00059                 mTabConceptSet = Core::Table::AnnotationTableSet::MakeFromFile(mAnnoImgSet, mConceptSet, true,
00060                                                                            StringToQuidClass(mQuid));
00061             }
00062             else
00063             {
00064                 mAnnoVidSet = Core::VideoSet::MakeVideoSet(mAnnoSetName);
00065                 mTabGenreSet = Core::Table::AnnotationTableSet::MakeFromFile(mAnnoVidSet, mGenreSet, true,
00066                                                                          StringToQuidClass(mQuid));
00067                 mTabConceptSet = Core::Table::AnnotationTableSet::MakeFromFile(mAnnoVidSet, mConceptSet, true,
00068                                                                            StringToQuidClass(mQuid));
00069             }
00070             mHasAnnotation = true;
00071 
00072                 
00073 
00074             // need to check the existence of the data set
00075             if (mTabGenreSet == NULL || mTabConceptSet == NULL)
00076             {
00077                 printf("ERROR: The Concept or Genre Set does NOT exist.\n");
00078                 return;
00079             }
00080 
00081             mGenreNum = mTabGenreSet->Size();
00082             mConceptNum = mTabConceptSet->Size();
00083             //_ASSERT(mGenreNum>0 && mConceptNum>0);
00084             if (mGenreNum<0 || mConceptNum<0)
00085             {
00086                 printf("ERROR: The number of concept or genre is Zero.\n");
00087                 return;
00088             }
00089 
00090             tabConcept = new AnnotationTable* [mConceptNum];
00091             tabGenre = new AnnotationTable* [mGenreNum];
00092 
00093             AllocateMatrix(mProb_c_g, mConceptNum, mGenreNum);
00094             ResetMatrix(mProb_c_g, mConceptNum, mGenreNum);
00095 
00096             InitializeAnnoTables();
00097             VerifyAndSetAnnoTableSize();
00098             //_ASSERT(mAnnoTableSize>0);
00099 
00100             if (mAnnoTableSize<0)
00101             {
00102                 printf("ERROR: The annotation table size is Zero.\n");
00103                 return;
00104             }
00105 
00106             //mAnnoQuidTable = mTabGenreSet->GetTable(0);
00107         }
00108         else
00109         {
00110             mAnnoVidSet = NULL;         
00111             mAnnoImgSet = NULL;   
00112             mGenreNum = 0;
00113             mConceptNum = 0;
00114             tabConcept = NULL;
00115             tabGenre = NULL;
00116 
00117             std::cout << "Note: Annotation Data Set is NOT given." << std::endl;
00118 
00119             mHasAnnotation = false;
00120         }
00121 
00122         if (mSetName != "")
00123         {
00124             if (mIsImageSet)
00125                 mImgSet = Core::ImageSet::MakeImageSet(mSetName);
00126             else
00127                 mVidSet = Core::VideoSet::MakeVideoSet(mSetName);
00128             mHandleTestSet = true;
00129         }
00130         else
00131         {
00132             mVidSet = NULL;
00133             mImgSet = NULL;
00134             mHandleTestSet = false;
00135 
00136             std::cout << "Note: Test Data Set is NOT given." << std::endl;
00137         }
00138 
00139     }
00140 
00141     virtual ~Bayes()
00142     {
00143         for (int k=0;k<mConceptNum;k++)
00144         {
00145             delete []tabConcept[k];
00146         }
00147         for (int j=0;j<mGenreNum;j++)
00148         {
00149             delete []tabGenre[j];
00150         }
00151         delete []tabConcept;
00152         delete []tabGenre;
00153      
00154         delete mTabGenreSet;
00155         delete mTabConceptSet;
00156 
00157         if (mIsImageSet)
00158         {
00159             if (NULL != mAnnoImgSet)
00160                 delete mAnnoImgSet;
00161             if (NULL != mImgSet)
00162                 delete mImgSet;
00163         }
00164         else
00165         {
00166             if (NULL != mAnnoVidSet)
00167                 delete mAnnoVidSet;
00168             if (NULL != mVidSet)
00169                 delete mVidSet;
00170         }
00171 
00172         //delete mAnnoQuidTable;
00173 
00174         ReleaseMatrix(mProb_c_g, mConceptNum);
00175 
00176     }
00177 
00178     bool
00179     HasAnnotation()
00180     {
00181         return mHasAnnotation;
00182     }
00183 
00184     int
00185     GetGenreNum() const
00186     {
00187         return mGenreNum;
00188     }
00189 
00190     int
00191     GetConceptNum() const
00192     {
00193         return mConceptNum;
00194     }
00195 
00196     int
00197     GetAnnoTableSize() const
00198     {
00199         return mAnnoTableSize;
00200     }
00201 
00202     int
00203     GetTableSize() const
00204     {
00205         return mTableSize;
00206     }
00207 
00208     void
00209     InitializeAnnoTables()
00210     {
00211         InitializeConceptAnnoTables();
00212         InitializeGenreAnnoTables();
00213     }
00214 
00215     void
00216     InitializeConceptAnnoTables()
00217     {
00218         // initialize all concept tables
00219         for (int k=0 ; k< mConceptNum ; k++)
00220         {
00221             //AnnotationTable* tabConcept = mTabConceptSet->GetTable(k);
00222             tabConcept[k] = mTabConceptSet->GetTable(k);
00223             if (mbDumpTable)
00224             {
00225                 if (mIsImageSet)
00226                     tabConcept[k]->Dump(mAnnoImgSet, mStart, mEnd);
00227                 else
00228                     tabConcept[k]->Dump(mAnnoVidSet, mStart, mEnd);
00229                 //tabConcept[k]->DumpSummary();
00230             }  
00231 
00232         }
00233 
00234     }
00235 
00236     void
00237     InitializeGenreAnnoTables()
00238     {
00239         // initialize all genre tables
00240         for (int j=0 ; j < mGenreNum ; j++)
00241         {
00242             //AnnotationTable* tabGenre = mTabGenreSet->GetTable(j);
00243             tabGenre[j] = mTabGenreSet->GetTable(j);
00244             if (mbDumpTable)
00245             {
00246                 if (mIsImageSet)
00247                     tabGenre[j]->Dump(mAnnoImgSet, mStart, mEnd);
00248                 else
00249                     tabGenre[j]->Dump(mAnnoVidSet, mStart, mEnd);
00250                 //tabGenre[j]->DumpSummary();
00251             }
00252             
00253         }
00254 
00255     }
00256 
00257     void
00258     VerifyAndSetAnnoTableSize()
00259     {
00260         // the table size should be the same as the total keyframes
00261         int nTempTabSize = 0;
00262 
00263         bool bIsConceptValid = true;
00264         bool bIsGenreValid = true;
00265 
00266         // loop for all concepts c_{k}
00267         for (int k=0 ; k< mConceptNum ; k++)
00268         {
00269             int tabSize = tabConcept[k]->Capacity();
00270 
00271             if (0 == nTempTabSize)
00272             {
00273                 nTempTabSize = tabSize;
00274 
00275             } else {
00276                 if (nTempTabSize != tabSize)
00277                 {
00278                     printf("Warnning: Concept %d table size %d is different.\n", k, tabSize);
00279                     bIsConceptValid = false;
00280                 }
00281             }
00282             
00283         }
00284 
00285         // loop for all video genres
00286         for (int j=0 ; j < mGenreNum ; j++)
00287         {
00288             int tabSize = tabGenre[j]->Capacity();
00289 
00290             if (0 == nTempTabSize)
00291             {
00292                 nTempTabSize = tabSize;
00293 
00294             } else {
00295                 if (nTempTabSize != tabSize)
00296                 {
00297                     printf("Warnning: Genre %d table size %d is different.\n", j, tabSize);
00298                     bIsGenreValid = false;
00299                 }
00300             }
00301         }
00302 
00303         if (!bIsConceptValid || !bIsGenreValid)
00304         {
00305             printf("Warnning: Attention! Some tables have different size.\n");
00306         }
00307 
00308         // set table size
00309         mAnnoTableSize = nTempTabSize;
00310         //_ASSERT(mAnnoTableSize>0);
00311 
00312         std::cout << "Anno Table Size = " << mAnnoTableSize << std::endl;
00313 
00314     }
00315 
00316     void
00317     SaveQuidTablePerGenre(bool bWrite)
00318     {
00319         if (mAnnoTableSize == 0)
00320         {
00321             std::cout << "The table size is zero: NOT acceptable" <<std::endl;
00322             return;
00323         }
00324         std::cout<<std::endl<<"Genre Prior Probability:" <<std::endl <<std::endl;
00325 
00326         // loop for all video genres
00327         for (int j=0 ; j < mGenreNum ; j++)
00328         {
00329             // we do not consider skipped samples currectly
00330             int posNum = tabGenre[j]->GetNrPositive();
00331             int negNum = tabGenre[j]->GetNrNegative();
00332             String strGenre = tabGenre[j]->GetLabel();
00333 
00334             Core::Table::QuidTable* posQuidTable = new Core::Table::QuidTable(posNum);
00335             Core::Table::QuidTable* negQuidTable = new Core::Table::QuidTable(negNum);
00336 
00337             for (int i=0 ; i<mAnnoTableSize ; i++)
00338             {
00339                 Quid q = tabGenre[j]->Get1(i);
00340                 if ( tabGenre[j]->IsPositive(i) )
00341                 {
00342                     posQuidTable->Add(q);
00343                 }
00344                 if ( tabGenre[j]->IsNegative(i))
00345                 {
00346                     negQuidTable->Add(q);
00347                 }
00348             }
00349 
00350             if (bWrite)
00351             {
00352                 String fPosName = "Annotations/Genre/" + mGenreSet + "/" + strGenre + "_pos.tab";
00353                 String fNegName = "Annotations/Genre/" + mGenreSet + "/" + strGenre + "_neg.tab";
00354                 //String fName = mAnnoVidSet->GetFilePathAnnotation(name,true,false);
00355                 //String fName = mAnnoImgSet->GetFilePathAnnotation(name,true,false);
00356                 if (!fPosName.empty())
00357                 {
00358                     if (mIsImageSet)
00359                         Core::Table::Write(posQuidTable, fPosName, mAnnoImgSet->GetDatabase(), true);
00360                     else
00361                         Core::Table::Write(posQuidTable, fPosName, mAnnoVidSet->GetDatabase(), true);
00362                     std::cout << "Saved: [" << posNum << "] " << fPosName << std::endl; 
00363                 }
00364                 if (!fNegName.empty())
00365                 {
00366                     if (mIsImageSet)
00367                         Core::Table::Write(negQuidTable, fNegName, mAnnoImgSet->GetDatabase(), true);
00368                     else
00369                         Core::Table::Write(negQuidTable, fNegName, mAnnoVidSet->GetDatabase(), true);
00370                     std::cout << "Saved: [" << negNum << "] " << fNegName << std::endl; 
00371                 }
00372             }
00373 
00374             delete posQuidTable;
00375             delete negQuidTable;
00376 
00377         }
00378 
00379         int x=0;
00380     }
00381 
00382     void
00383     DumpMatrix(char* name, double** matrix, int nRow, int nCol, bool bDumpFloat = true)
00384     {
00385         std::cout << std::endl << name <<" (" << nRow << " x " << nCol << "):" << std::endl;
00386 
00387         // dump order: row 0, row 1, ...
00388         for (int j=0; j<nCol; j++)
00389         {
00390             for (int i=0; i<nRow; i++)
00391             {
00392                 //std::cout << matrix[i][j] << " ";
00393                 if (bDumpFloat)
00394                     printf("%.3f ", matrix[i][j]);
00395                 else
00396                     printf("%.4d ", int(matrix[i][j]) );
00397             }
00398             std::cout << std::endl;
00399 
00400         }
00401     }
00402 
00403     void
00404     DumpMatrix(char* name, double** matrix, int nRow, int nCol, const char* file, bool bDumpFloat = true)
00405     {
00406         FILE* fp = fopen(file, "wt");
00407         if (NULL == fp)
00408         {
00409             std::cout << "File path does NOT exist: " << file << std::endl;
00410             return;
00411         }
00412 
00413         fprintf(fp,"%d\t%d\n",nRow,nCol);
00414 
00415         // dump order: row 0, row 1, ...
00416         for (int j=0; j<nCol; j++)
00417         {
00418             for (int i=0; i<nRow; i++)
00419             {
00420                 if (bDumpFloat)
00421                     fprintf(fp,"%.3f\t", matrix[i][j]);
00422                 else
00423                     fprintf(fp,"%d\t",  int(matrix[i][j]) );
00424             }
00425             fprintf(fp,"\n");
00426         }
00427 
00428         fclose(fp);
00429     }
00430 
00431     void
00432     DumpMatrixProb_c_gAsList()
00433     {
00434         std::cout << std::endl << "mProb_c_g[k][j]" <<" (" << mConceptNum << " x " << mGenreNum << "):" << std::endl;
00435         for (int j=0; j<mGenreNum; j++)
00436         {
00437             for (int k=0; k<mConceptNum; k++)
00438             {
00439                 String strGenre = tabGenre[j]->GetLabel();
00440                 String strConcept = tabConcept[k]->GetLabel();
00441                 String name = strGenre + "-" + strConcept;
00442                 printf("%s\t\t%.3f\n",name.c_str(), mProb_c_g[k][j]);
00443             }
00444             printf("\n");
00445         }
00446     }
00447 
00448     void
00449     SaveConditionalProbMatrix(const char* file)
00450     {
00451         DumpMatrix("mProb_c_g[k][j] - Bayes (file)", mProb_c_g, mConceptNum, mGenreNum, file);        
00452     }
00453 
00454     void
00455     ResetMatrix(double**& matrix, int nRow, int nCol)
00456     {
00457         for (int k=0; k<nRow; k++)
00458             memset(matrix[k],0,sizeof(double)*nCol);
00459     }
00460 
00461     void
00462     ReleaseMatrix(double**& matrix, int nRow)
00463     {
00464         for (int k=0; k<nRow; k++)
00465             delete []matrix[k];
00466 
00467         delete []matrix;
00468     }
00469 
00470     void
00471     SaveMatrix()
00472     {
00473         SaveMatrixConceptConditionalProbability(mMatrixFile,mProb_c_g, mConceptNum, mGenreNum);
00474 
00475         printf("\nSaving Prob_c_g[k][j] into file.\n");
00476     }
00477 
00478     bool
00479     LoadMatrix(bool bAnnoSet = true)
00480     {
00481         bool flag = LoadMatrixConceptConditionalProbability(mMatrixFile,mProb_c_g, mConceptNum, mGenreNum, bAnnoSet);
00482 
00483         if (flag)
00484         {
00485             DumpMatrix("mProb_c_g[k][j] - Bayes (file)", mProb_c_g, mConceptNum, mGenreNum);
00486             printf("\nLoading Prob_c_g[k][j] into file.\n");
00487             return true;
00488 
00489         } else {
00490 
00491             printf("\nLoading Prob_c_g[k][j] fails.\n");
00492             return false;
00493         }
00494         
00495     }
00496 
00497     void
00498     SaveMatrixConceptConditionalProbability(String name, double** matrix, int& nRow, int& nCol)
00499     {
00500         // mMatrixFile, mProb_c_g, mConceptNum, mGenreNum
00501 
00502         std::string fName;
00503         if (mIsImageSet)
00504             fName = mAnnoImgSet->GetFilePathAnnotation(name,true,false);
00505         else
00506             fName = mAnnoVidSet->GetFilePathAnnotation(name,true,false);
00507         /*String strAnnoSetName = mAnnoSetName;
00508         strAnnoSetName.resize(strAnnoSetName.size()-4);
00509         String fName = "../" + strAnnoSetName + "/Annotations/" + name;*/
00510 
00511         FILE* fp = fopen(fName.c_str(),"wb");
00512 
00513         if (NULL == fp)
00514         {
00515             std::cout << "File has existed, or path does NOT exist: [" << name << "]" << std::endl;
00516             return;
00517         }
00518 
00519         fwrite(&nRow, sizeof(int), 1, fp);  //mConceptNum
00520         fwrite(&nCol, sizeof(int), 1, fp);  //mGenreNum
00521 
00522         // data file: first column, second column, ...
00523         // be sure to be consistent with load funcion
00524         for (int i=0; i<nRow; i++)
00525         {
00526             for (int j=0; j<nCol; j++)
00527             {
00528                 //printf("%.3f ", matrix[i][j]);
00529                 fwrite( &(matrix[i][j]), sizeof(double), 1, fp);
00530             }
00531             //std::cout << std::endl;
00532 
00533         }
00534         fclose(fp);
00535 
00536     }
00537 
00538     bool
00539     LoadMatrixConceptConditionalProbability(String name, double** matrix, int& nRow, int& nCol, bool bAnnoSet = true)
00540     {
00541         // mMatrixFile, mProb_c_g, mConceptNum, mGenreNum
00542 
00543         std::string fName;
00544 
00545         if (bAnnoSet)
00546         {
00547             // loading from annotation set (training set)
00548 
00549             if (mIsImageSet)
00550                 fName = mAnnoImgSet->GetFilePathAnnotation(name,false,false);
00551             else
00552                 fName = mAnnoVidSet->GetFilePathAnnotation(name,false,false);
00553 
00554             /*String strAnnoSetName = mAnnoSetName;
00555             strAnnoSetName.resize(strAnnoSetName.size() - 4);
00556             fName = String("../") + strAnnoSetName + "/Annotations/" + name;*/
00557        }
00558         else
00559         {
00560             // loading from test set itself.
00561 
00562             if (mIsImageSet)
00563             {
00564                 if (NULL != mImgSet)
00565                 {
00566                     fName = mImgSet->GetFilePathAnnotation(name,false,false);
00567 
00568                     /*String strSetName = mSetName;
00569                     strSetName.resize(strSetName.size()-4);
00570                     fName = String("../") + strSetName + "/Annotations/" + name;*/
00571 
00572                 }
00573                 else
00574                 {
00575                     std::cout << "mImgSet: Not initialized." << std::endl;
00576                     return false;
00577                 }
00578             }
00579             else
00580             {
00581                 if (NULL != mVidSet)
00582                 {
00583                     fName = mVidSet->GetFilePathAnnotation(name,false,false);
00584 
00585                     /*String strSetName = mSetName;
00586                     strSetName.resize(strSetName.size()-4);
00587                     fName = String("../") + strSetName + "/Annotations/" + name;*/
00588 
00589                 }
00590                 else
00591                 {
00592                     std::cout << "mVidSet: Not initialized." << std::endl;
00593                     return false;
00594                 }
00595             }
00596 
00597             
00598         }
00599 
00600         FILE* fp = fopen(fName.c_str(),"rb");
00601 
00602         if (NULL == fp)
00603         {
00604             std::cout << "File does NOT exist: [" << fName << "]" << std::endl;
00605             return false;
00606         }
00607 
00608         int nLocalRow, nLocalCol;
00609 
00610         fread(&nLocalRow, sizeof(int), 1, fp);  //mConceptNum
00611         fread(&nLocalCol, sizeof(int), 1, fp);  //mGenreNum
00612 
00613         // verify the dimension of matrix
00614         if ( nRow == nLocalRow && nCol == nLocalCol)
00615         {
00616             // data file: first column, second column, ...
00617             // be sure to be consistent with save funcion
00618             for (int i=0; i<nRow; i++)
00619             {
00620                 for (int j=0; j<nCol; j++)
00621                 {
00622                     //printf("%.3f ", matrix[i][j]);
00623                     fread( &(matrix[i][j]), sizeof(double), 1, fp);
00624                 }
00625                 //std::cout << std::endl;
00626 
00627             }
00628 
00629             fclose(fp);
00630             return true;
00631 
00632         } else {
00633 
00634             // verification fails
00635             std::cout << "Loaded Matrix dimension is not the same as Annotation" << std::endl;
00636 
00637             fclose(fp);
00638             return false;
00639         }
00640 
00641         //fclose(fp);
00642 
00643     }
00644 
00645     void
00646     AllocateMatrix(double**& matrix, int nRow, int nCol)
00647     {
00648         // first looping rows, then looping columns
00649         matrix = new double* [nRow];
00650         for (int k=0; k<nRow; k++)
00651         {
00652             matrix[k] = new double [nCol];
00653             memset(matrix[k],0,sizeof(double)*nCol);
00654         }
00655     }
00656 
00657 protected:
00658 
00659     String mSetName;
00660     String mAnnoSetName;
00661 
00662     String mGenreSet;   //genre set: g_{j}
00663     String mConceptSet; //concept set: c_{k}
00664 
00665     //Core::Database::RawDataSet* mSet;
00666     //Core::Database::RawDataSet* mAnnoSet;
00667 
00668     Core::VideoSet::VideoSet* mVidSet;
00669     Core::VideoSet::VideoSet* mAnnoVidSet;
00670 
00671     Core::ImageSet::ImageSet* mImgSet;
00672     Core::ImageSet::ImageSet* mAnnoImgSet;
00673 
00674     AnnotationTableSet* mTabGenreSet;
00675     AnnotationTableSet* mTabConceptSet;
00676 
00677     AnnotationTable** tabConcept;
00678     AnnotationTable** tabGenre;
00679 
00680     //Core::Table::QuidTable* mAnnoQuidTable;
00681 
00682     String mMatrixFile; // file saving mProb_c_g[k][j]
00683 
00684     String mQuid;       // "Frame", or "Shot"
00685 
00686     bool mHasAnnotation;    //mAnnoSetName is not empty
00687     bool mHandleTestSet;    //mSetName is not empty
00688 
00689     bool mVerbose;
00690     bool mbDumpTable;
00691 
00692     int mIsImageSet;
00693 
00694     int mGenreNum;
00695     int mConceptNum;
00696 
00697     int mAnnoTableSize;
00698     int mTableSize;
00699 
00700     int mStart;
00701     int mEnd;
00702 
00703     double** mProb_c_g;      // conditional probability P(c_{k}|g_{j})
00704 
00705 };
00706 
00707 } // namespace Table
00708 } // namespace Core
00709 } // namespace Impala
00710 
00711 #endif

Generated on Thu Jan 13 09:04:37 2011 for ImpalaSrc by  doxygen 1.5.1