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

BayesCondProb.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_BayesCondProb_h
00002 #define Impala_Core_Table_BayesCondProb_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 BayesCondProb : public Bayes
00019 {
00020 public:
00021 
00022     BayesCondProb(String annoSetName, String genreSet, String conceptSet, String quid, 
00023                                                bool dumpTable,int nStart,int nEnd): 
00024         Bayes(annoSetName, "",genreSet, conceptSet, quid, nStart, nEnd)
00025     {
00026         mbDumpTable = dumpTable;
00027 
00028         conceptPrior = new double[mConceptNum];
00029         genrePrior = new double[mGenreNum];
00030 
00031         // mProb_c_g[][] has been initialized in Bayes class
00032         //AllocateMatrix(mProb_c_g, mConceptNum, mGenreNum);
00033 
00034         // for CalcGenreConditionalPriorProbability()
00035         AllocateMatrix(mProb_g_c, mGenreNum, mConceptNum);
00036         ResetMatrix(mProb_g_c, mGenreNum, mConceptNum);
00037 
00038     }
00039 
00040     virtual ~BayesCondProb()
00041     {
00042         delete []conceptPrior;
00043         delete []genrePrior;
00044 
00045         ReleaseMatrix(mProb_g_c, mGenreNum);
00046 
00047     }
00048 
00049     void
00050     CalcConceptPriorProbability()
00051     {   
00052         if (mAnnoTableSize == 0)
00053         {
00054             std::cout << "The table size is zero: NOT acceptable" <<std::endl;
00055             for (int k=0 ; k< mConceptNum ; k++)
00056                 conceptPrior[k] = 0;
00057             return;
00058         }
00059         std::cout<< std::endl <<"Concept Prior Probability:" <<std::endl <<std::endl;
00060 
00061         // loop for all concepts c_{k}
00062         for (int k=0 ; k< mConceptNum ; k++)
00063         {
00064             //int tabSize = tabConcept[k]->Capacity();
00065             int posNum = tabConcept[k]->GetNrPositive();
00066             String strConcept = tabConcept[k]->GetLabel();
00067 
00068             conceptPrior[k] = posNum*1.0/mAnnoTableSize;
00069             std::cout << conceptPrior[k] << " Pos=" << posNum << " [c" << k << " " << strConcept << "]" << std::endl;
00070             
00071         }
00072         std::cout << std::endl;
00073     }
00074 
00075     void
00076     CalcGenrePriorProbability()
00077     {
00078         if (mAnnoTableSize == 0)
00079         {
00080             std::cout << "The table size is zero: NOT acceptable" <<std::endl;
00081             for (int j=0 ; j < mGenreNum ; j++)
00082                 genrePrior[j] = 0;
00083             return;
00084         }
00085         std::cout<<std::endl<<"Genre Prior Probability:" <<std::endl <<std::endl;
00086 
00087         // loop for all video genres
00088         for (int j=0 ; j < mGenreNum ; j++)
00089         {
00090             //int tabSize = tabGenre[j]->Capacity();
00091             int posNum = tabGenre[j]->GetNrPositive();
00092             String strGenre = tabGenre[j]->GetLabel();
00093 
00094             genrePrior[j] = posNum*1.0/mAnnoTableSize;
00095             std::cout << genrePrior[j] << " Pos=" << posNum << " [g" << j << " " << strGenre << "]"<< std::endl;
00096 
00097         }
00098     }
00099 
00100     void
00101     CalcGenreConditionalPriorProbability()
00102     {
00103         //mProb_g_c = new double* [mGenreNum];
00104         //for (int j=0; j<mGenreNum; j++) mProb_g_c[j] = new double [mConceptNum];
00105 
00106         for (int j=0; j<mGenreNum; j++)
00107             memset(mProb_g_c[j],0,sizeof(double)*mConceptNum);
00108         
00109         for (int j=0; j<mGenreNum; j++)
00110         {      
00111             for (int k=0; k<mConceptNum; k++)
00112             {         
00113                 int countConcept = 0;
00114                 int countGenre = 0;
00115 
00116                 // loop for all the keyframes
00117                 for (int idx=0; idx<mAnnoTableSize; idx++)
00118                 {
00119                     if (tabConcept[k]->IsPositive(idx))
00120                     {
00121                         countConcept ++;
00122                         if (tabGenre[j]->IsPositive(idx))
00123                         {
00124                             countGenre++;
00125                         }
00126                     }               
00127                 }
00128 
00129                 // compute the conditional probability based on counting.
00130                 if (0 == countConcept)
00131                     mProb_g_c[j][k] = 0;
00132                 else
00133                     mProb_g_c[j][k] = countGenre*1.0/countConcept;
00134             }
00135         }
00136         DumpMatrix("mProb_g_c[j][k]", mProb_g_c, mGenreNum, mConceptNum);
00137     }
00138 
00139     void
00140     CalcConceptConditionalPriorProbability()
00141     {
00142         //double** mProb_c_g = new double* [mConceptNum];
00143         //for (int k=0; k<mConceptNum; k++) mProb_c_g[k] = new double [mGenreNum];
00144 
00145         for (int k=0; k<mConceptNum; k++)
00146             memset(mProb_c_g[k],0,sizeof(double)*mGenreNum);
00147 
00148         for (int k=0; k<mConceptNum; k++)
00149         {
00150             for (int j=0; j< mGenreNum; j++)
00151             {     
00152                 int countGenre = 0;
00153                 int countConcept = 0;
00154                 
00155                 // loop for all the keyframes
00156                 for (int idx=0; idx<mAnnoTableSize; idx++)
00157                 {
00158                     if (tabGenre[j]->IsPositive(idx))
00159                     {
00160                         countGenre ++;
00161                         if (tabConcept[k]->IsPositive(idx))
00162                         {
00163                             countConcept++;
00164                         }
00165                     }               
00166                 }
00167 
00168                 // compute the conditional probability based on counting.
00169                 if (0 == countGenre)
00170                     mProb_c_g[k][j] = 0;
00171                 else
00172                     mProb_c_g[k][j] = countConcept*1.0/countGenre;
00173             }
00174         }
00175         DumpMatrix("mProb_c_g[k][j] - counting", mProb_c_g, mConceptNum, mGenreNum);
00176     }
00177 
00178     void
00179     CalcConceptConditionalPriorProbabilityBayesRule()
00180     {
00181         //double** mProb_c_g = new double* [mConceptNum];
00182         //for (int k=0; k<mConceptNum; k++) mProb_c_g[k] = new double [mGenreNum];
00183 
00184         for (int k=0; k<mConceptNum; k++)
00185             memset(mProb_c_g[k],0,sizeof(double)*mGenreNum);
00186 
00187         for (int k=0; k<mConceptNum; k++)
00188         {
00189             for (int j=0; j< mGenreNum; j++)
00190             {     
00191                 // compute the conditional probability based on Bayes theorem
00192                 if (genrePrior[j] < 1e-10)
00193                     mProb_c_g[k][j] = 0;
00194                 else
00195                     mProb_c_g[k][j] = mProb_g_c[j][k] * conceptPrior[k] / genrePrior[j];
00196             }
00197         }
00198         DumpMatrix("mProb_c_g[k][j] - Bayes", mProb_c_g, mConceptNum, mGenreNum);
00199 
00200         DumpMatrixProb_c_gAsList();
00201     }
00202 
00203     void
00204     CalcConceptPosteriorProbability()
00205     {
00206     }
00207 
00208     void
00209     CalcBayes()
00210     {
00211         //InitializeTables();
00212         //VerifyAndSetTableSize();
00213      
00214         // step 1 - compute concept prior probability: P(c_{k})
00215         CalcConceptPriorProbability();
00216 
00217         // step 2 - compute genre prior probability: P(g_{j})
00218         CalcGenrePriorProbability();
00219         
00220         // step 3 - compute conditional probability 
00221         //             P( g_{j} | c_{k} ) = mProb_g_c [j][k]
00222         CalcGenreConditionalPriorProbability();        
00223 
00224         // step 4 - compute conditional probability 
00225         //             P( c_{k} | g_{j} ) = mProb_c_g [k][j]
00226         CalcConceptConditionalPriorProbability();
00227         
00228         // step 5 - re-compute conditional probability 
00229         //             P( c_{k} | g_{j} ) = mProb_c_g [k][j]
00230         // based on Bayes theorem, as follows:
00231         //
00232         //                     P(c_{k}|g_{j})*P(c_{k})    mProb_g_c[j][k] * conceptPrior[k]
00233         // mProb_c_g[k][j] = -------------------------- = ----------------------------------
00234         //                            P(g_{j})                     genrePrior[j]
00235         CalcConceptConditionalPriorProbabilityBayesRule();
00236 
00237     }
00238 
00239     // for annotations of the joint models:
00240     // positive: both are positive
00241     // negative: either are negative, rest of the positive pairs
00242     void
00243     SaveAnnoTableJointGenreAndConcept(int nLeastPositiveSamples=0, bool bWriteTable=false)
00244     {
00245         std::cout << std::endl;
00246         std::cout << "Saving Annotation Tables for joint probability: "<< std::endl;
00247         std::cout << "------------------------------------------------"<< std::endl;
00248 
00249         if (nLeastPositiveSamples<0)
00250             nLeastPositiveSamples = 100;
00251 
00252         int nAddTable = 0;
00253         int nSkipTable = 0;
00254 
00255         for (int j=0; j<mGenreNum; j++)
00256         {      
00257             for (int k=0; k<mConceptNum; k++)
00258             {
00259                 //AnnotationTable* annoTab = annoTabSet->GetTable(j);
00260                 Core::Table::AnnotationTable* annoTab = new Core::Table::AnnotationTable;
00261 
00262                 int countJointPos = 0;
00263 
00264                 // loop for all the keyframes
00265                 for (int idx=0; idx<mAnnoTableSize; idx++)
00266                 {
00267                     Quid q= tabGenre[j]->Get1(idx);
00268                     //std::cout << "Quid: " << QuidObj(q).ToString()  << std::endl;
00269 
00270                     if (tabGenre[j]->IsPositive(idx) && tabConcept[k]->IsPositive(idx))
00271                     {
00272                         // both genre and concept are positive
00273                         countJointPos ++;
00274                         annoTab->AddPositive(q);
00275                     }
00276                     else if (tabGenre[j]->IsSkip(idx) || tabConcept[k]->IsSkip(idx))
00277                     {
00278                         // either genre is skipped, or concept is skipped
00279                         annoTab->AddSkip(q);
00280                     }
00281                     else
00282                     {
00283                         // either genre is negative, or concept is negtive
00284                         annoTab->AddNegative(q);
00285                     }
00286                 }
00287 
00288                 String strGenreName = tabGenre[j]->GetLabel();
00289                 String strConceptName = tabConcept[k]->GetLabel();
00290                 String strTabName = strGenreName + "-" + strConceptName + ".tab";
00291 
00292                 if (countJointPos)
00293                 {
00294                     if (countJointPos >= nLeastPositiveSamples)
00295                     {
00296                         std::cout << strTabName << std::endl;
00297                         annoTab->DumpSummary();
00298                     }
00299                     else
00300                     {
00301                         std::cout << strTabName << std::endl;
00302                         std::cout << "[0-" << nLeastPositiveSamples-1 << "]: ";
00303                         annoTab->DumpSummary();
00304 
00305                         nSkipTable++;
00306                     }
00307                                            
00308                 }
00309 
00310                 //std::string fName = mAnnoVidSet->GetFilePathAnnotation("Frame/concepts_joint.txt/",false,false);
00311                 //if (fName == "") std::cout << "Path does not exist: " << fName << std::endl;
00312  
00313                 String OutPath;
00314                 String OutConceptSet1 = "concepts_joint_set3.txt";
00315                 //String OutConceptSet1 = "concepts_joint_setx.txt";
00316                             
00317                 if (bWriteTable && (countJointPos >= nLeastPositiveSamples) )
00318                 {
00319                     if (mIsImageSet)
00320                     {
00321                         OutPath = "Annotations/Image/"+OutConceptSet1+"/" + strTabName;
00322                         Core::Table::Write(annoTab, OutPath, mAnnoImgSet->GetDatabase(), true);
00323                     }
00324                     else
00325                     {
00326                         OutPath = "Annotations/Frame/"+OutConceptSet1+"/" + strTabName;
00327                         Core::Table::Write(annoTab, OutPath, mAnnoVidSet->GetDatabase(), true);
00328                     }
00329                     
00330 
00331                     nAddTable ++;
00332                 }
00333 
00334                 delete annoTab;
00335             }
00336         }
00337 
00338         std::cout << "Done. [ " << nAddTable << " (out of " << mGenreNum << "*" << mConceptNum << ")"; 
00339         std::cout << " *.tab files have been saved! ] " << std::endl;
00340         std::cout << "Skipped: " << nSkipTable << std::endl;
00341 
00342         int x=0;
00343 
00344     }
00345 
00346 
00347     // for annotations of the casecade models: Genre-specific concept models
00348     // positive: both are positive
00349     // negative: negative concept, only within the positive genres.
00350     void
00351     SaveAnnoTableSubsetConceptInGenre(bool bDumpTestSet,int nLeastPositiveSamples=0, bool bWriteTable=false)
00352     {
00353         std::cout << std::endl;
00354         std::cout << "Saving Annotation Tables subset concept-in-genre.tab: "<< std::endl;
00355         std::cout << "------------------------------------------------------"<< std::endl;
00356 
00357         if (bDumpTestSet)
00358         {
00359             nLeastPositiveSamples = 0;
00360         }
00361         else
00362         {
00363             if (nLeastPositiveSamples<0)
00364                 nLeastPositiveSamples = 100;
00365         }
00366 
00367         int nAddTable = 0;
00368         int nSkipTable = 0;
00369 
00370         for (int j=0; j<mGenreNum; j++)
00371         {      
00372             for (int k=0; k<mConceptNum; k++)
00373             {
00374                 //AnnotationTable* annoTab = annoTabSet->GetTable(j);
00375                 Core::Table::AnnotationTable* annoTab = new Core::Table::AnnotationTable;
00376 
00377                 int countConceptPositiveInGenre = 0;
00378 
00379                 // loop for all the keyframes
00380                 for (int idx=0; idx<mAnnoTableSize; idx++)
00381                 {
00382                     Quid q= tabGenre[j]->Get1(idx);
00383                     //std::cout << "Quid: " << QuidObj(q).ToString()  << std::endl;
00384 
00385                     if (bDumpTestSet)
00386                     {
00387                         // processing test set (no subset selection)
00388                         // actually the same as SaveAnnoTableJointGenreAndConcept()
00389                         if (tabGenre[j]->IsPositive(idx) && tabConcept[k]->IsPositive(idx))
00390                         {
00391                             // both genre and concept are positive
00392                             countConceptPositiveInGenre ++;
00393                             annoTab->AddPositive(q);
00394                         }
00395                         else if (tabGenre[j]->IsSkip(idx) || tabConcept[k]->IsSkip(idx))
00396                         {
00397                             // either genre is skipped, or concept is skipped
00398                             annoTab->AddSkip(q);
00399                         }
00400                         else
00401                         {
00402                             // either genre is negative, or concept is negtive
00403                             annoTab->AddNegative(q);
00404                         }
00405 
00406                      }
00407                     else
00408                     {
00409                         // processing training set, 
00410                         // Note: only processing a subset, within a specific genre type
00411                         if (tabGenre[j]->IsPositive(idx))
00412                         {
00413                             //only output the positives samples within a gerne
00414                             if (tabConcept[k]->IsPositive(idx))
00415                             {
00416                                 countConceptPositiveInGenre ++;
00417                                 annoTab->AddPositive(q);
00418                             }
00419                             else
00420                             {
00421                                 // genre is positive, but concept is negtive
00422                                 annoTab->AddNegative(q);
00423                             }
00424                         }
00425                         else
00426                         {
00427                             //Either negative samples or skipped samples for a genre will be ignored
00428                         }
00429                     }
00430                        
00431                 }
00432 
00433                 String strGenreName = tabGenre[j]->GetLabel();
00434                 String strConceptName = tabConcept[k]->GetLabel();
00435                 String strTabName = strGenreName + "-" + strConceptName + ".tab";
00436 
00437                 if (countConceptPositiveInGenre)
00438                 {
00439                     if (countConceptPositiveInGenre >= nLeastPositiveSamples)
00440                     {
00441                         std::cout << strTabName << std::endl;
00442                         annoTab->DumpSummary();
00443                     }
00444                     else
00445                     {
00446                         std::cout << strTabName << std::endl;
00447                         std::cout << "[0-" << nLeastPositiveSamples-1 <<"]: ";
00448                         annoTab->DumpSummary();
00449 
00450                         nSkipTable++;
00451                     }
00452                         
00453                     
00454                 }
00455 
00456                 //std::string fName = mAnnoVidSet->GetFilePathAnnotation("Frame/concepts_joint.txt/",false,false);
00457                 //if (fName == "") std::cout << "Path does not exist: " << fName << std::endl;
00458  
00459                 String OutPath;
00460                 String OutConceptSet2 = "concepts_subset_in_genres.txt";
00461                             
00462                 if (bWriteTable && (countConceptPositiveInGenre >= nLeastPositiveSamples) )
00463                 {
00464                     if (mIsImageSet)
00465                     {
00466                         OutPath = "Annotations/Image/"+OutConceptSet2+"/" + strTabName;
00467                         Core::Table::Write(annoTab, OutPath, mAnnoImgSet->GetDatabase(), true); 
00468                     }
00469                     else
00470                     {
00471                         OutPath = "Annotations/Frame/"+OutConceptSet2+"/" + strTabName;
00472                         Core::Table::Write(annoTab, OutPath, mAnnoVidSet->GetDatabase(), true); 
00473                     }
00474                     
00475 
00476                     nAddTable ++;
00477                 }
00478 
00479                 delete annoTab;
00480             }
00481         }
00482 
00483         std::cout << "Done. [ " << nAddTable << " (out of " << mGenreNum << "*" << mConceptNum << ")"; 
00484         std::cout << " *.tab files have been saved! ] " << std::endl;
00485         std::cout << "Skipped: " << nSkipTable << std::endl;
00486 
00487         int x=0;
00488 
00489     }
00490 
00491 
00492 
00493 protected:
00494 
00495     double* conceptPrior;
00496     double* genrePrior;
00497 
00498     double** mProb_g_c;
00499 
00500 };
00501 
00502 
00503 } // namespace Table
00504 } // namespace Core
00505 } // namespace Impala
00506 
00507 #endif

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