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