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