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

BayesFusion.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_BayesFusion_h
00002 #define Impala_Core_Table_BayesFusion_h
00003 
00004 #include "Core/Table/Bayes.h"
00005 
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Table
00011 {
00012 
00013 
00018 class BayesFusion : public Bayes
00019 {
00020 public:
00021 
00022     BayesFusion(String annoSetName, String genreSet, String conceptSet, String quid, 
00023                 String setName, String genrePath, String conceptPath, 
00024                 int nStart,int nEnd, bool bWriteTable): 
00025          Bayes(annoSetName, setName, genreSet, conceptSet, quid, nStart, nEnd)
00026     {
00027 
00028         mSetName = setName;
00029 
00030         mGenrePath = genrePath;
00031         mConceptPath = conceptPath;
00032         
00033         // Note: mVidSet/mImgSet has been initialized in Bayes class
00034         if (mIsImageSet)
00035         {
00036             mGenreSimSet = Core::Table::SimilarityTableSet::MakeFromFile(genrePath, mImgSet->GetDatabase());
00037             mConceptSimSet = Core::Table::SimilarityTableSet::MakeFromFile(conceptPath, mImgSet->GetDatabase());
00038         }
00039         else
00040         {
00041             mKeyframes = new Core::VideoSet::Keyframes(mVidSet, "keyframes"); 
00042             mGenreSimSet = Core::Table::SimilarityTableSet::MakeFromFile(genrePath, mVidSet->GetDatabase());
00043             mConceptSimSet = Core::Table::SimilarityTableSet::MakeFromFile(conceptPath, mVidSet->GetDatabase());
00044         }
00045         mQuidTable = mGenreSimSet->GetQuidTable();
00046         mTableSize = mQuidTable->Size();
00047 
00048         Core::Table::QuidTable* conceptQuidTable = mConceptSimSet->GetQuidTable();
00049         int conceptTableSize = conceptQuidTable->Size();
00050 
00051         //_ASSERT(mTableSize == conceptTableSize);
00052         if (mTableSize != conceptTableSize)
00053         {
00054             printf("ERROR: Quid Table is NOT the same for concept and genre.\n");
00055             return;
00056         }
00057 
00058 
00059         if ((mEnd == -1) || (mEnd > mQuidTable->Size()))
00060             mEnd = mQuidTable->Size();
00061 
00062         mWriteTable = bWriteTable;
00063 
00064         genreSimTable = NULL;
00065         conceptSimTable = NULL;
00066 
00067     }
00068 
00069     virtual ~BayesFusion()
00070     {
00071         if (NULL != genreSimTable)
00072             delete []genreSimTable;
00073         if (NULL != conceptSimTable)
00074             delete []conceptSimTable;
00075 
00076         delete mGenreSimSet;
00077         delete mConceptSimSet;
00078 
00079         delete mKeyframes;
00080     }
00081 
00082     void
00083     DoFusion(double alpha, bool bUseAnnoMatrix = true)
00084     {
00085         if (Initialization())
00086         {
00087             mbDumpTable = false;
00088             if (mbDumpTable)
00089             {
00090                 DumpGenreSimilarityTableSet();
00091                 DumpConceptSimilarityTableSet();   
00092             }
00093       
00094             ExecuteBayesFusion(alpha, bUseAnnoMatrix);
00095         }
00096         else
00097         {
00098             std::cout << "There are errors for initialization()." << std::endl;
00099         }
00100 
00101         int x=0;
00102     }
00103 
00104     bool
00105     Initialization()
00106     {
00107         //(LoadNumFromAnnotation())
00108         //AllocateMatrix(mProb_c_g, mConceptNum, mGenreNum);
00109 
00110         bool success = LoadMatrix(true);
00111         if (success)
00112         {
00113             for (int j=0; j<mGenreNum; j++)
00114                 genreSimTable = new SimilarityTableSet::SimTableType*[mGenreNum];
00115 
00116             for (int k=0; k<mConceptNum; k++)
00117                 conceptSimTable = new SimilarityTableSet::SimTableType*[mConceptNum];
00118 
00119             return true;
00120 
00121         } else {
00122             return false;
00123         }        
00124     }
00125 
00126     void
00127     DumpGenreSimilarityTableSet()
00128     { 
00129         std::cout << "SimilarityTableSet " << mGenreSimSet->GetDescription() << std::endl << std::endl;
00130 
00131         //for (int j=0 ; j<mGenreSimSet->NrTables() ; j++)
00132         //_ASSERT(mGenreNum == mGenreSimSet->NrTables());
00133         for (int j=0 ; j<mGenreNum ; j++)
00134         {
00135             //SimilarityTableSet::SimTableType* genreSimTable = mGenreSimSet->GetSimTable(j);
00136             genreSimTable[j] = mGenreSimSet->GetSimTable(j);
00137 
00138             std::cout << "table " << j << ", name = " << mGenreSimSet->GetName(j)
00139                       << std::endl;
00140             if (genreSimTable[j]->Size() != mQuidTable->Size())
00141             {
00142                 //ILOG_ERROR("DumpRanking: simtable size doesn't match");
00143                 printf("DumpRanking: simtable size doesn't match\n");
00144                 continue;
00145             }
00146             String genre = mGenreSimSet->GetName(j);
00147             for (int i=mStart ; i<mEnd ; i++)
00148             {
00149                 Quid quid = mQuidTable->Get1(i);
00150                 //std::cout << i << ", ";
00151                 std::cout << genre << ", ";
00152 
00153                 if (!mIsImageSet)
00154                 {
00155                     int keyId = mKeyframes->GetFrameId(quid);
00156                     if (keyId != -1)
00157                         std::cout << FileNameBase(mKeyframes->GetName(keyId)) << ", ";
00158                     else
00159                         std::cout << mVidSet->QuidToString(quid, true) << ", ";
00160                 }
00161                 std::cout << genreSimTable[j]->Get1(i) << std::endl;
00162             }
00163             std::cout << std::endl;
00164         }
00165         
00166     }
00167 
00168     void
00169     DumpConceptSimilarityTableSet()
00170     {
00171         std::cout << "SimilarityTableSet " << mConceptSimSet->GetDescription() << std::endl << std::endl;
00172 
00173         //for (int k=0 ; k<mConceptSimSet->NrTables() ; k++)
00174         //_ASSERT(mConceptNum == mConceptSimSet->NrTables());
00175         for (int k=0 ; k<mConceptNum ; k++)
00176         {
00177             //SimilarityTableSet::SimTableType* conceptSimTable = mConceptSimSet->GetSimTable(k);
00178             conceptSimTable[k] = mConceptSimSet->GetSimTable(k);
00179 
00180             std::cout << "table " << k << ", name = " << mConceptSimSet->GetName(k)
00181                       << std::endl;
00182             if (conceptSimTable[k]->Size() != mQuidTable->Size())
00183             {
00184                 //ILOG_ERROR("DumpRanking: simtable size doesn't match");
00185                 printf("DumpRanking: simtable size doesn't match\n");
00186                 continue;
00187             }
00188             String concept = mConceptSimSet->GetName(k);
00189             for (int i=mStart ; i<mEnd ; i++)
00190             {
00191                 Quid quid = mQuidTable->Get1(i);
00192                 //std::cout << i << ", ";
00193                 std::cout << concept << ", ";
00194 
00195                 if (!mIsImageSet)
00196                 {
00197                     int keyId = mKeyframes->GetFrameId(quid);
00198                     if (keyId != -1)
00199                         std::cout << FileNameBase(mKeyframes->GetName(keyId)) << ", ";
00200                     else
00201                         std::cout << mVidSet->QuidToString(quid, true) << ", ";
00202                 }
00203                 std::cout << conceptSimTable[k]->Get1(i) << std::endl;
00204             }
00205             std::cout << std::endl;
00206         }
00207     }
00208 
00209     void
00210     ExecuteBayesFusion(double alpha, bool bUseAnnoMatrix = true)
00211     {
00212         if (!bUseAnnoMatrix)
00213         {
00214             // load Prob_c_g[][] from ground truth of test set
00215             LoadMatrix(false);
00216         }
00217 
00218         // load genre similarity tables
00219         for (int j=0 ; j<mGenreNum ; j++)
00220             genreSimTable[j] = mGenreSimSet->GetSimTable(j);
00221 
00222         //for (int k=0 ; k<mConceptSimSet->NrTables() ; k++)
00223         //_ASSERT(mConceptNum == mConceptSimSet->NrTables());
00224         for (int k=0 ; k<mConceptNum ; k++)
00225         {
00226             //SimilarityTableSet::SimTableType* conceptSimTable = mConceptSimSet->GetSimTable(k);
00227             conceptSimTable[k] = mConceptSimSet->GetSimTable(k);
00228 
00229             std::cout << "table " << k << ", name = " << mConceptSimSet->GetName(k)
00230                       << std::endl;
00231             if (conceptSimTable[k]->Size() != mQuidTable->Size())
00232             {
00233                 //ILOG_ERROR("DumpRanking: simtable size doesn't match");
00234                 printf("DumpRanking: simtable size doesn't match\n");
00235                 continue;
00236             }
00237 
00238             int nForwardCount = 0;
00239             int nBackwardCount = 0;
00240             double diffForward = 0;
00241             double diffBackward = 0;
00242 
00243             int nPosOutRangeCount=0;
00244             int nNegOutRangeCount=0;
00245 
00246             String concept = mConceptSimSet->GetName(k);
00247             //for (int i=mStart ; i<mEnd ; i++)
00248             for (int i=0 ; i<mTableSize ; i++)
00249             {
00250                 Quid quid = mQuidTable->Get1(i);
00251                 //std::cout << concept << ", ";
00252                 /*if (!mIsImageSet)
00253                 {
00254                     int keyId = mKeyframes->GetFrameId(quid);
00255                     if (keyId != -1)
00256                         std::cout << FileNameBase(mKeyframes->GetName(keyId)) << ", ";
00257                     else
00258                         std::cout << mVidSet->QuidToString(quid, true) << ", ";
00259                 }*/
00260                 std::cout << conceptSimTable[k]->Get1(i) << std::endl;
00261 
00262                 // update the concept scores
00263                 Real64 conceptOldScore = conceptSimTable[k]->Get1(i);
00264 
00265                 Real64 sum = 0;
00266                 for (int j=0; j<mGenreNum; j++)
00267                 {
00268                     Real64 genreScore = genreSimTable[j]->Get1(i);
00269 
00270                     sum += mProb_c_g[k][j] * genreScore;
00271 
00272                     //std::cout << mProb_c_g[k][j] << "*" <<genreScore<< "+" << std:endl;
00273                 }
00274                 //std::cout << std::endl;
00275 
00276                 //Real64 conceptNewScore = sum/mGenreNum;
00277                 //Real64 conceptNewScore = sum;
00278                 //double alpha = 0.95;
00279                 Real64 conceptNewScore = conceptOldScore * alpha + (1-alpha)* sum;
00280                 conceptSimTable[k]->Set1(i,conceptNewScore); // set the new concept score
00281 
00282                 // check positive samples
00283                 Real64 diff;
00284                 if (tabConcept[k]->IsPositive(i))
00285                 {
00286                     diff = conceptNewScore - conceptOldScore;
00287                     if (diff > 0)
00288                     {
00289                         diffForward += diff;
00290                         nForwardCount ++;
00291                     } 
00292                     else 
00293                     {
00294                         diffBackward += diff;
00295                         nBackwardCount ++;
00296                     }
00297 
00298                     if (conceptNewScore > 1 && mVerbose)
00299                     {
00300                         //std::cout << "[+ out of range] New Score: " << conceptNewScore << std::endl;
00301                         nPosOutRangeCount ++;
00302                     }
00303                 }
00304                 else
00305                 {
00306                     if (conceptNewScore > 1 && mVerbose)
00307                     {
00308                         //std::cout << "[- out of range] New Score: " << conceptNewScore << std::endl;
00309                         nNegOutRangeCount ++;
00310                     }
00311                 }
00312             }
00313             //std::cout << std::endl;
00314 
00315             if (mVerbose)
00316             {
00317                 std::cout << "+ [pos moving] ";
00318                 std::cout << "Forward " << nForwardCount << ": " <<  diffForward << ", ";
00319                 std::cout << "Backward " << nBackwardCount << ": " <<  diffBackward << std::endl;
00320 
00321                 std::cout << "+ [out of range]: " << nPosOutRangeCount << " in total" << std::endl;
00322                 std::cout << "- [out of range]: " << nNegOutRangeCount << " in total" << std::endl;
00323             }              
00324         }
00325 
00326         mConceptSimSet->ComputeRanks(true);
00327           
00328         if (mWriteTable)
00329         {
00330             //mImgSet->GetFilePathSimilarityIndex(conceptSet, model,feature, "names.txt",false, false);
00331             //mVidSet->GetFilePathSimilarityIndex(conceptSet, model,feature, "names.txt",false, false);
00332             //Impala::CString outpath = ".\\SimilarityIndex\\concepts.txt\\chi2\\both2-3sift-p112213\\";
00333             String OutPath = mConceptPath;
00334             OutPath.resize(OutPath.size() - 9); // remove "names.txt"
00335 
00336             if (mIsImageSet)
00337                 mConceptSimSet->Save(OutPath, mImgSet->GetDatabase(),true);
00338             else
00339                 mConceptSimSet->Save(OutPath, mVidSet->GetDatabase(),true);
00340             std::cout << "Saving files: " << OutPath << std::endl;
00341         }
00342 
00343     }
00344 
00345     /*bool
00346     LoadNumFromAnnotation()
00347     {
00348         bool flag = LoadConceptGenreNumFromAnnotation(mMatrixFile, mConceptNum, mGenreNum);
00349 
00350         if (flag)
00351         {
00352             _ASSERT(mGenreNum>0 && mConceptNum>0);
00353             return true;
00354         } else
00355             return false;
00356     }*/
00357 
00358     bool
00359     LoadConceptGenreNumFromAnnotation(String name, int& nRow, int& nCol)
00360     {
00361         std::string fName;
00362         
00363         if (mIsImageSet)
00364             fName = mAnnoImgSet->GetFilePathAnnotation(name,false,false);
00365         else
00366             fName = mAnnoVidSet->GetFilePathAnnotation(name,false,false);
00367         FILE* fp = fopen(fName.c_str(),"rb");
00368 
00369         if (NULL == fp)
00370         {
00371             std::cout << "File does NOT exist: " << name << "" << std::endl;
00372             return false;
00373         }
00374 
00375         fread(&nRow, sizeof(int), 1, fp);  //mConceptNum
00376         fread(&nCol, sizeof(int), 1, fp);  //mGenreNum
00377 
00378         fclose(fp);
00379 
00380         return true;
00381 
00382     }
00383 
00384 
00385 protected:
00386 
00387     SimilarityTableSet* mGenreSimSet;
00388     SimilarityTableSet* mConceptSimSet;
00389 
00390     SimilarityTableSet::SimTableType** genreSimTable;
00391     SimilarityTableSet::SimTableType** conceptSimTable;
00392 
00393     Core::Table::QuidTable* mQuidTable;  // only for similiarty table set
00394 
00395     String mConceptPath;
00396     String mGenrePath;
00397 
00398     Core::VideoSet::Keyframes* mKeyframes;
00399 
00400     bool mWriteTable;
00401 
00402 };
00403 
00404 } // namespace Table
00405 } // namespace Core
00406 } // namespace Impala
00407 
00408 #endif

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