Home || Architecture || Video Search || Visual Search || Scripts || Applications || 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     void
00240     SaveAnnoTableJointGenreAndConcept(int nLeastPositiveSamples=0, bool bWriteTable=false)
00241     {
00242         std::cout << std::endl;
00243         std::cout << "Saving Annotation Tables for joint probability: "<< std::endl;
00244         std::cout << "------------------------------------------------"<< std::endl;
00245 
00246         if (nLeastPositiveSamples<0)
00247             nLeastPositiveSamples = 100;
00248 
00249         int nAddTable = 0;
00250         int nSkipTable = 0;
00251 
00252         for (int j=0; j<mGenreNum; j++)
00253         {      
00254             for (int k=0; k<mConceptNum; k++)
00255             {
00256                 //AnnotationTable* annoTab = annoTabSet->GetTable(j);
00257                 Core::Table::AnnotationTable* annoTab = new Core::Table::AnnotationTable;
00258 
00259                 int countJointPos = 0;
00260 
00261                 // loop for all the keyframes
00262                 for (int idx=0; idx<mAnnoTableSize; idx++)
00263                 {
00264                     Quid q= tabGenre[j]->Get1(idx);
00265                     //std::cout << "Quid: " << QuidObj(q).ToString()  << std::endl;
00266 
00267                     if (tabGenre[j]->IsPositive(idx) && tabConcept[k]->IsPositive(idx))
00268                     {
00269                         // both genre and concept are positive
00270                         countJointPos ++;
00271                         annoTab->AddPositive(q);
00272                     }
00273                     else if (tabGenre[j]->IsSkip(idx) || tabConcept[k]->IsSkip(idx))
00274                     {
00275                         // either genre is skipped, or concept is skipped
00276                         annoTab->AddSkip(q);
00277                     }
00278                     else
00279                     {
00280                         // either genre is negative, or concept is negtive
00281                         annoTab->AddNegative(q);
00282                     }
00283                 }
00284 
00285                 String strGenreName = tabGenre[j]->GetLabel();
00286                 String strConceptName = tabConcept[k]->GetLabel();
00287                 String strTabName = strGenreName + "-" + strConceptName + ".tab";
00288 
00289                 if (countJointPos)
00290                 {
00291                     if (countJointPos >= nLeastPositiveSamples)
00292                     {
00293                         std::cout << strTabName << std::endl;
00294                         annoTab->DumpSummary();
00295                     }
00296                     else
00297                     {
00298                         std::cout << strTabName << std::endl;
00299                         std::cout << "[0-" << nLeastPositiveSamples-1 << "]: ";
00300                         annoTab->DumpSummary();
00301 
00302                         nSkipTable++;
00303                     }
00304                                            
00305                 }
00306 
00307                 //std::string fName = mAnnoVidSet->GetFilePathAnnotation("Frame/concepts_joint.txt/",false,false);
00308                 //if (fName == "") std::cout << "Path does not exist: " << fName << std::endl;
00309  
00310                 String OutPath = "Annotations/Frame/concepts_joint_set3.txt/" + strTabName;
00311                             
00312                 if (bWriteTable && (countJointPos >= nLeastPositiveSamples) )
00313                 {
00314                     Core::Table::Write(annoTab, OutPath, mAnnoVidSet->GetDatabase(), true);
00315 
00316                     nAddTable ++;
00317                 }
00318 
00319                 delete annoTab;
00320             }
00321         }
00322 
00323         std::cout << "Done. [ " << nAddTable << " (out of " << mGenreNum << "*" << mConceptNum << ")"; 
00324         std::cout << " *.tab files have been saved! ] " << std::endl;
00325         std::cout << "Skipped: " << nSkipTable << std::endl;
00326 
00327         int x=0;
00328 
00329     }
00330 
00331 
00332     void
00333     SaveAnnoTableSubsetConceptInGenre(bool bDumpTestSet,int nLeastPositiveSamples=0, bool bWriteTable=false)
00334     {
00335         std::cout << std::endl;
00336         std::cout << "Saving Annotation Tables subset concept-in-genre.tab: "<< std::endl;
00337         std::cout << "------------------------------------------------------"<< std::endl;
00338 
00339         if (bDumpTestSet)
00340         {
00341             nLeastPositiveSamples = 0;
00342         }
00343         else
00344         {
00345             if (nLeastPositiveSamples<0)
00346                 nLeastPositiveSamples = 100;
00347         }
00348 
00349         int nAddTable = 0;
00350         int nSkipTable = 0;
00351 
00352         for (int j=0; j<mGenreNum; j++)
00353         {      
00354             for (int k=0; k<mConceptNum; k++)
00355             {
00356                 //AnnotationTable* annoTab = annoTabSet->GetTable(j);
00357                 Core::Table::AnnotationTable* annoTab = new Core::Table::AnnotationTable;
00358 
00359                 int countConceptPositiveInGenre = 0;
00360 
00361                 // loop for all the keyframes
00362                 for (int idx=0; idx<mAnnoTableSize; idx++)
00363                 {
00364                     Quid q= tabGenre[j]->Get1(idx);
00365                     //std::cout << "Quid: " << QuidObj(q).ToString()  << std::endl;
00366 
00367                     if (bDumpTestSet)
00368                     {
00369                         // processing test set (no subset selection)
00370                         // actually the same as SaveAnnoTableJointGenreAndConcept()
00371                         if (tabGenre[j]->IsPositive(idx) && tabConcept[k]->IsPositive(idx))
00372                         {
00373                             // both genre and concept are positive
00374                             countConceptPositiveInGenre ++;
00375                             annoTab->AddPositive(q);
00376                         }
00377                         else if (tabGenre[j]->IsSkip(idx) || tabConcept[k]->IsSkip(idx))
00378                         {
00379                             // either genre is skipped, or concept is skipped
00380                             annoTab->AddSkip(q);
00381                         }
00382                         else
00383                         {
00384                             // either genre is negative, or concept is negtive
00385                             annoTab->AddNegative(q);
00386                         }
00387 
00388                      }
00389                     else
00390                     {
00391                         // processing training set, 
00392                         // Note: only processing a subset, within a specific genre type
00393                         if (tabGenre[j]->IsPositive(idx))
00394                         {
00395                             //only output the positives samples within a gerne
00396                             if (tabConcept[k]->IsPositive(idx))
00397                             {
00398                                 countConceptPositiveInGenre ++;
00399                                 annoTab->AddPositive(q);
00400                             }
00401                             else
00402                             {
00403                                 // genre is positive, but concept is negtive
00404                                 annoTab->AddNegative(q);
00405                             }
00406                         }
00407                         else
00408                         {
00409                             //Either negative samples or skipped samples for a genre will be ignored
00410                         }
00411                     }
00412                        
00413                 }
00414 
00415                 String strGenreName = tabGenre[j]->GetLabel();
00416                 String strConceptName = tabConcept[k]->GetLabel();
00417                 String strTabName = strGenreName + "-" + strConceptName + ".tab";
00418 
00419                 if (countConceptPositiveInGenre)
00420                 {
00421                     if (countConceptPositiveInGenre >= nLeastPositiveSamples)
00422                     {
00423                         std::cout << strTabName << std::endl;
00424                         annoTab->DumpSummary();
00425                     }
00426                     else
00427                     {
00428                         std::cout << strTabName << std::endl;
00429                         std::cout << "[0-" << nLeastPositiveSamples-1 <<"]: ";
00430                         annoTab->DumpSummary();
00431 
00432                         nSkipTable++;
00433                     }
00434                         
00435                     
00436                 }
00437 
00438                 //std::string fName = mAnnoVidSet->GetFilePathAnnotation("Frame/concepts_joint.txt/",false,false);
00439                 //if (fName == "") std::cout << "Path does not exist: " << fName << std::endl;
00440  
00441                 String OutPath = "Annotations/Frame/concepts_subset_in_genres.txt/" + strTabName;
00442                             
00443                 if (bWriteTable && (countConceptPositiveInGenre >= nLeastPositiveSamples) )
00444                 {
00445                     Core::Table::Write(annoTab, OutPath, mAnnoVidSet->GetDatabase(), true); 
00446 
00447                     nAddTable ++;
00448                 }
00449 
00450                 delete annoTab;
00451             }
00452         }
00453 
00454         std::cout << "Done. [ " << nAddTable << " (out of " << mGenreNum << "*" << mConceptNum << ")"; 
00455         std::cout << " *.tab files have been saved! ] " << std::endl;
00456         std::cout << "Skipped: " << nSkipTable << std::endl;
00457 
00458         int x=0;
00459 
00460     }
00461 
00462 
00463 
00464 protected:
00465 
00466     double* conceptPrior;
00467     double* genrePrior;
00468 
00469     double** mProb_g_c;
00470 
00471 };
00472 
00473 
00474 } // namespace Table
00475 } // namespace Core
00476 } // namespace Impala
00477 
00478 #endif

Generated on Fri Mar 19 09:31:20 2010 for ImpalaSrc by  doxygen 1.5.1