Browse Source

added more functionality in featureLearning

Alexander Freytag 12 years ago
parent
commit
0556d840be

+ 2 - 4
featureLearning/FeatureLearningPrototypes.cpp

@@ -238,7 +238,7 @@ bool FeatureLearningPrototypes::loadInitialCodebook ( )
     }
     catch (...)
     {
-      std::cerr << "Error while loading initial codebook" << std::endl;
+      std::cerr << "Error while loading initial codebook from " << cacheInitialCodebook << std::endl;
       return false;
     }  
     return true;
@@ -465,9 +465,7 @@ NICE::FloatImage FeatureLearningPrototypes::evaluateCurrentCodebookByDistance (
 
       noveltyImageRGB.writePPM( destination );
     }
-    
-    ImageDisplay imgDisp;
-    
+        
     // now look where the closest features for the current cluster indices are
     int tmpProtCnt ( 0 );
     for (NICE::VVector::const_iterator protIt = prototypes.begin(); protIt != prototypes.end(); protIt++, tmpProtCnt++)

+ 3 - 1
featureLearning/FeatureLearningPrototypes.h

@@ -16,6 +16,7 @@
 #include <vislearning/cbaselib/MultiDataset.h>
 // 
 #include <vislearning/features/localfeatures/GenericLocalFeatureSelection.h>
+#include <vislearning/features/simplefeatures/CodebookPrototypes.h>
 // 
 #include <vislearning/math/cluster/ClusterAlgorithm.h>
 
@@ -55,7 +56,8 @@ namespace OBJREC
       LocalFeatureRepresentation * featureExtractor;
       
       //! The currently known "cluster" centers, which are used for BoW histogram computation 
-      NICE::VVector prototypes;     
+//       NICE::VVector prototypes;     
+      OBJREC::CodebookPrototypes prototypes;   
             
       int newImageCounter;
       //just needed for visualisation

+ 9 - 5
featureLearning/FeatureLearningRegionBased.cpp

@@ -77,7 +77,7 @@ void FeatureLearningRegionBased::learnNewFeatures ( const std::string & _filenam
   std::cerr << " EXTRACT FEATURES FROM UNSEEN IMAGE" << std::endl;
   Globals::setCurrentImgFN ( _filename );
   featureExtractor->extractFeatures ( img, newFeatures, positions );  
-  
+    
   //normalization :)
   for ( NICE::VVector::iterator i = newFeatures.begin();
         i != newFeatures.end();
@@ -178,7 +178,11 @@ void FeatureLearningRegionBased::learnNewFeatures ( const std::string & _filenam
   imageToPseudoColorWithRangeSpecification( regionRelevanceImage, regionRelevanceScoreImageRGB, 0 /* min */, minimalImageAmountForAcceptableRegions /* max */ );  
 
     if ( b_showResults )
-      showImage(regionScoreImageRGB, "Current (new) image with region scores"); 
+    {
+      showImage(regionNoveltyScoreImageRGB, "Current (new) image with region NOVELTY scores"); 
+      showImage(regionRelevanceScoreImageRGB, "Current (new) image with region RELEVANCE scores"); 
+      showImage(regionScoreImageRGB, "Current (new) image with FINAL region scores"); 
+    }
     else 
     {
       std::vector< std::string > list2;
@@ -202,7 +206,7 @@ void FeatureLearningRegionBased::learnNewFeatures ( const std::string & _filenam
   
   NICE::Vector representative ( newFeatures.begin()->size(), 0.0 );
   
-//   //first try: average feature vectors of the "best" region
+//   //FIRST TRY: average feature vectors of the "best" region
 //   NICE::VVector::const_iterator posIt = positions.begin();
 //   for ( NICE::VVector::const_iterator featIt = newFeatures.begin();
 //         featIt != newFeatures.end();
@@ -221,7 +225,7 @@ void FeatureLearningRegionBased::learnNewFeatures ( const std::string & _filenam
 //   //normalization
 //   representative.normalizeL1();
   
-//   //second try: simply take the first feature vector of the "best" region (although this one should lay on the border, and so on...)
+//   //SECOND TRY: simply take the first feature vector of the "best" region (although this one should lay on the border, and so on...)
 //   NICE::VVector::const_iterator posIt = positions.begin();
 //   for ( NICE::VVector::const_iterator featIt = newFeatures.begin();
 //         featIt != newFeatures.end();
@@ -239,7 +243,7 @@ void FeatureLearningRegionBased::learnNewFeatures ( const std::string & _filenam
 //     break;
 //   }   
   
-  //third try: simply take the feature vector of the "best" region with largest novelty score within this region
+  //THIRD TRY: simply take the feature vector of the "best" region with largest novelty score within this region
   // ... (hopefully, this is no outlier wrt to this region...)
   
   double maxNovelty ( 0.0 );

+ 1 - 0
featureLearning/progs/libdepend.inc

@@ -4,3 +4,4 @@ $(call PKG_DEPEND_INT,vislearning/features/)
 $(call PKG_DEPEND_INT,vislearning/math/)
 $(call PKG_DEPEND_INT,vislearning/noveltyDetection/)
 $(call PKG_DEPEND_INT,segmentation/)
+$(call PKG_DEPEND_EXT_ESSENTIAL,VLFEAT)

+ 146 - 71
featureLearning/progs/testFeatureLearning.cpp

@@ -42,14 +42,30 @@ int main( int argc, char **argv )
 
   Config * conf = new Config ( argc, argv );
   
-  bool showTrainingImages= conf->gB( "featureLearning", "showTrainingImages", false );
-  bool showTestImages= conf->gB( "featureLearning", "showTestImages", false );
+  bool showTrainingImages = conf->gB( "featureLearning", "showTrainingImages", false );
+  bool showTestImages = conf->gB( "featureLearning", "showTestImages", false );
+  bool showResults = conf->gB( "featureLearning", "showResults", false );
   
   ResourceStatistics rs;
   std::string resultdir;
   resultdir = conf->gS( "featureLearning", "resultdir", "/tmp/");
   
   
+  NICE::ImageT<int> noveltyScale ( 20, 100 );
+  for (int j = 0; j < 100; j++)
+  {
+    for (int i = 0; i < 20; i++)
+      noveltyScale(i,j) = 99-j;
+  }
+  
+  NICE::ColorImage noveltyScaleRGB (noveltyScale.width(), noveltyScale.height() );
+  imageToPseudoColor( noveltyScale, noveltyScaleRGB );
+  
+  std::string destinationNoveltyScale ( resultdir + "_" + "_noveltyScale.ppm");
+  noveltyScaleRGB.writePPM( destinationNoveltyScale );   
+  
+  
+  
   //**********************************************
   //
   //      READ INITIAL TRAINING SET TO COMPUTE
@@ -112,7 +128,16 @@ int main( int argc, char **argv )
       std::cerr << "now do image To pseude color and show the initial cluster assignments" << std::endl;
       NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
       imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );      
-      showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
+      
+      if ( showResults )
+        showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
+      else
+      {
+        std::vector< std::string > list2;
+        StringTools::split ( filename, '/', list2 );        
+        std::string destination ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_00_initialClusterAssignments.ppm");
+        imgClusterAssignmentsRGB.writePPM( destination );        
+      }
   }
   
   //**********************************************
@@ -152,9 +177,31 @@ int main( int argc, char **argv )
       
       NICE::ColorImage orig( filename );
       
+      NICE::ImageT< int > imgClusterAssignments;
+      imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , false /* beforeComputingNewFeatures */, false /* _binaryShowLatestPrototype*/ );      
+      
+      NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
+      imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );       
+      
       NICE::FloatImage noveltyImageBefore;
       noveltyImageBefore = featureLearning->evaluateCurrentCodebookByDistance( filename , true /* beforeComputingNewFeatures */ );
       
+      NICE::ColorImage noveltyImageBeforeRGB (noveltyImageBefore.width(), noveltyImageBefore.height() );
+      imageToPseudoColor( noveltyImageBefore, noveltyImageBeforeRGB );        
+      
+      if ( showResults )
+        showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
+      else
+      {
+        std::vector< std::string > list2;
+        StringTools::split ( filename, '/', list2 );        
+        std::string destination ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_00_initialClusterAssignments.ppm");
+        imgClusterAssignmentsRGB.writePPM( destination );        
+        
+        std::string destinationNovelty ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_01_initialClusterDistances.ppm");
+        noveltyImageBeforeRGB.writePPM( destinationNovelty );          
+      }      
+      
       //**********************************************
       //
       //             IS THIS IMAGE NOVEL?
@@ -175,81 +222,109 @@ int main( int argc, char **argv )
         std::cerr << " --- NOVEL --- " << std::endl;
       }
       
-      //**********************************************
-      //
-      //       LEARN NEW FEATURE FOR A NOVEL IMAGE
-      //
-      //**********************************************       
-      
-      featureLearning->learnNewFeatures( filename );
-      
-      //and update the codebook pointer within our novelty detection algorithm
-      //TODO this should be done, but currently we do not care about
-//       novDetector->setCodebook( featureLearning->getCurrentCodebook() );      
-      
-      //**********************************************
-      //
-      //       EVALUATE HOW WELL THE CURRENT IMAGE
-      //            CAN BE EXPLAINED AFTER 
-      //           COMPUTING A NEW FEATURE
-      //
-      //       SHOULD WE REPEAT THIS UNTIL THE IMAGE
-      //       IS NOT CLASSIFIED AS "NOVEL" ANYMORE?
-      //
-      //**********************************************       
-      
-      NICE::FloatImage noveltyImageAfter;
-      noveltyImageAfter = featureLearning->evaluateCurrentCodebookByDistance( filename , false /* beforeComputingNewFeatures */ );
-      
-      NICE::FloatImage noveltyImageDifference ( noveltyImageAfter.width(), noveltyImageAfter.height());
-      for ( uint y = 0 ; y < ( uint ) noveltyImageAfter.height() ; y++ )
+      while ( b_isImageNovel ) 
       {
-        for ( uint x = 0 ; x < ( uint ) noveltyImageAfter.width(); x++ )
+              
+        //**********************************************
+        //
+        //       LEARN NEW FEATURE FOR A NOVEL IMAGE
+        //
+        //**********************************************       
+        
+        featureLearning->learnNewFeatures( filename );
+        
+        //and update the codebook pointer within our novelty detection algorithm
+        //TODO this should be done, but currently we do not care about
+  //       novDetector->setCodebook( featureLearning->getCurrentCodebook() );      
+        
+        //**********************************************
+        //
+        //       EVALUATE HOW WELL THE CURRENT IMAGE
+        //            CAN BE EXPLAINED AFTER 
+        //           COMPUTING A NEW FEATURE
+        //
+        //       SHOULD WE REPEAT THIS UNTIL THE IMAGE
+        //       IS NOT CLASSIFIED AS "NOVEL" ANYMORE?
+        //
+        //**********************************************       
+        
+        NICE::FloatImage noveltyImageAfter;
+        noveltyImageAfter = featureLearning->evaluateCurrentCodebookByDistance( filename , false /* beforeComputingNewFeatures */ );
+        
+        NICE::FloatImage noveltyImageDifference ( noveltyImageAfter.width(), noveltyImageAfter.height());
+        for ( uint y = 0 ; y < ( uint ) noveltyImageAfter.height() ; y++ )
         {
-          noveltyImageDifference(x,y) = fabs ( noveltyImageBefore(x,y) - noveltyImageAfter(x,y) );
+          for ( uint x = 0 ; x < ( uint ) noveltyImageAfter.width(); x++ )
+          {
+            noveltyImageDifference(x,y) = fabs ( noveltyImageBefore(x,y) - noveltyImageAfter(x,y) );
+          }
         }
-      }
-      
-      std::cerr << "min diff: " << noveltyImageDifference.min() << " and max diff: " << noveltyImageDifference.max() << std::endl;
-      
-      NICE::ColorImage noveltyImageDifferenceRGB (noveltyImageAfter.width(), noveltyImageAfter.height() );
-      imageToPseudoColor( noveltyImageDifference, noveltyImageDifferenceRGB );      
-      
-      std::vector< std::string > list2;
-      StringTools::split ( filename, '/', list2 );      
+        
+        std::cerr << "min diff: " << noveltyImageDifference.min() << " and max diff: " << noveltyImageDifference.max() << std::endl;
+        
+        NICE::ColorImage noveltyImageDifferenceRGB (noveltyImageAfter.width(), noveltyImageAfter.height() );
+        imageToPseudoColor( noveltyImageDifference, noveltyImageDifferenceRGB );      
+        
+        std::vector< std::string > list2;
+        StringTools::split ( filename, '/', list2 );      
 
-      std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_4_differenceOfNovelties.ppm");
-      noveltyImageDifferenceRGB.writePPM( destination );      
+        if ( showResults )
+        {
+          showImage( noveltyImageDifferenceRGB, "Difference of novelty images" );
+        }
+        else
+        {          
+          std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_02_initialLoopClusterAssignments.ppm");  
+          imgClusterAssignmentsRGB.writePPM( destination );    
 
-      NICE::ImageT< int > imgClusterAssignmentsBinary;
-      imgClusterAssignmentsBinary = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, true /* _binaryShowLatestPrototype*/ );      
-      
-      NICE::ImageT< int > imgClusterAssignments;
-      imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, false /* _binaryShowLatestPrototype*/ );
+          std::string destinationNoveltyDifference ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_03_noveltyDifferences.ppm");  
+          noveltyImageDifferenceRGB.writePPM( destinationNoveltyDifference );                        
+        }
 
-      
-      NICE::ColorImage imgClusterAssignmentsBinaryRGB (imgClusterAssignmentsBinary.width(), imgClusterAssignmentsBinary.height() );
-      imageToPseudoColor( imgClusterAssignmentsBinary, imgClusterAssignmentsBinaryRGB );   
-      
-      NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
-      imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );           
-      
-      if ( false )
-      {
-        showImage(imgClusterAssignmentsBinaryRGB, "cluster Assignments Binary (latest prototype)" ) ;      
-        showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
+        NICE::ImageT< int > imgClusterAssignmentsBinary;
+        imgClusterAssignmentsBinary = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, true /* _binaryShowLatestPrototype*/ );      
+        
+        NICE::ImageT< int > imgClusterAssignments;
+        imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, false /* _binaryShowLatestPrototype*/ );
+
+        
+        NICE::ColorImage imgClusterAssignmentsBinaryRGB (imgClusterAssignmentsBinary.width(), imgClusterAssignmentsBinary.height() );
+        imageToPseudoColor( imgClusterAssignmentsBinary, imgClusterAssignmentsBinaryRGB );   
+        
+        NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
+        imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );           
+        
+        if ( showResults )
+        {
+          showImage(imgClusterAssignmentsBinaryRGB, "cluster Assignments Binary (latest prototype)" ) ;      
+          showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
+        }
+        else
+        {
+            std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_5_clusterAssignments.ppm");
+            std::string destinationBinary ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_6_clusterAssignmentsBinary.ppm");
+            
+            imgClusterAssignmentsRGB.writePPM( destination );
+            imgClusterAssignmentsBinaryRGB.writePPM( destinationBinary );
+        }      
+        
+        //check, whether the image will still be seen as novel or not
+        // i.e., are we able to "explain" the image with the lately computed codebook entry?
+        b_isImageNovel = novDetector->evaluateNoveltyOfImage( noveltyImageAfter ) ;
+        
+        if ( ! b_isImageNovel )
+        {
+          std::cerr << " --- NOT NOVEL ANYMORE --- " << std::endl << std::endl;
+        }
+        else
+        {
+          std::cerr << " --- STILL NOVEL --- " << std::endl;
+          noveltyImageBefore = noveltyImageAfter;
+        }        
+        
+        //artifial break for the moment
+        break;
       }
-      else
-      {
-          std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_5_clusterAssignments.ppm");
-          std::string destinationBinary ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_6_clusterAssignmentsBinary.ppm");
-          
-          imgClusterAssignmentsRGB.writePPM( destination );
-          imgClusterAssignmentsBinaryRGB.writePPM( destinationBinary );
-      }      
-      
-      
-    
 
       
       imageCnt++;