소스 검색

SemSegNovelty persistent with tiny tricks

Alexander Freytag 11 년 전
부모
커밋
ae5486d7ef
2개의 변경된 파일124개의 추가작업 그리고 16개의 파일을 삭제
  1. 118 13
      semseg/SemSegNovelty.cpp
  2. 6 3
      semseg/SemSegNovelty.h

+ 118 - 13
semseg/SemSegNovelty.cpp

@@ -36,6 +36,9 @@ SemSegNovelty::SemSegNovelty ( )
   // those two guys need to be NULL, since only one of them will be active later on
   this->classifier = NULL;
   this->vclassifier = NULL;
+  
+  // this one here as well
+  this->regionSeg = NULL;
 }
 
 SemSegNovelty::SemSegNovelty ( const Config * _conf,
@@ -66,12 +69,26 @@ SemSegNovelty::~SemSegNovelty()
   }
   
   // clean-up
+  
+  ///////////////////////////////
+  //     FEATURE EXTRACTION    //
+  ///////////////////////////////   
+  if ( featExtract != NULL )
+    delete featExtract;
+
+  ///////////////////////////////
+  //     CLASSIFICATION STUFF  //
+  ///////////////////////////////   
   if ( classifier != NULL )
     delete classifier;
   if ( vclassifier != NULL )
     delete vclassifier;
-  if ( featExtract != NULL )
-    delete featExtract;
+  
+  ///////////////////////////////
+  //     SEGMENTATION STUFF    //
+  ///////////////////////////////   
+  if ( this->regionSeg != NULL )
+    delete this->regionSeg;
 }
 
 void SemSegNovelty::initFromConfig(const Config* conf, const string _confSection) 
@@ -100,15 +117,15 @@ void SemSegNovelty::initFromConfig(const Config* conf, const string _confSection
   //distance to next descriptor during testing
   this->testWSize = conf->gI (_confSection, "test_window_size", 10);
   // select your segmentation method here
-  std::string rsMethode = conf->gS ( _confSection, "segmentation", "none" );
+  this->s_rsMethode = conf->gS ( _confSection, "segmentation", "none" );
  
-  if(rsMethode == "none")
+  if( this->s_rsMethode == "none" )
   {
     regionSeg = NULL;
   }
   else
   {
-    RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod(conf, rsMethode);    
+    RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod( conf, this->s_rsMethode );    
     if ( reuseSegmentation )
       regionSeg = new RSCache ( conf, tmpRegionSeg );
     else
@@ -1864,6 +1881,15 @@ void SemSegNovelty::restore ( std::istream & is, int format )
       //     SEGMENTATION STUFF    //
       ///////////////////////////////
       //TODO regionSeg
+      else if ( tmp.compare("s_rsMethode") == 0 )
+      { 
+        is >> this->s_rsMethode;
+	// theoretically, we should properly store and restore the regionSeg object. However,  its parent class does not provide
+	// a Persistent interface yet. Hence, we perform this tiny workaround which works, since regionSeg is not changed over time...
+	// only be aware of parameters originally set via config...
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
       //NOTE regionSeg seems really important to keep track off
       else if ( tmp.compare("reuseSegmentation") == 0 )
       { 
@@ -1871,8 +1897,41 @@ void SemSegNovelty::restore ( std::istream & is, int format )
         is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       }       
-      //TODO queriedRegions
-      //NOTE queriedRegions seems really important to keep track off
+      else if ( tmp.compare("queriedRegions") == 0 )
+      {
+        is >> tmp; // size
+        int queriedRegionsSize ( 0 );
+        is >> queriedRegionsSize;
+        queriedRegions.clear();
+
+        if ( b_restoreVerbose ) 
+          std::cerr << "restore queriedRegions with size: " << queriedRegionsSize << std::endl;
+        for ( int i = 0; i < queriedRegionsSize; i++ )
+        {
+	  // restore key
+          std::string key;
+          is >> key;
+	  
+	  // restore values -- inner loop over sets
+	  is >> tmp; // size
+	  int regionsOfImgSize ( 0 );
+	  is >> regionsOfImgSize;
+
+	  std::set< int > regionsOfImg;
+	  regionsOfImg.clear();
+	    
+	    for (int i = 0; i < regionsOfImgSize; i++)
+	    {
+	      int idxRegion;
+	      is >> idxRegion;        
+	      regionsOfImg.insert ( idxRegion );
+	    }
+          queriedRegions.insert ( std::pair<std::string, std::set< int > > ( key, regionsOfImg ) );
+        }
+        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }            
       //
       //TODO currentRegionToQuery
       //
@@ -1889,6 +1948,28 @@ void SemSegNovelty::restore ( std::istream & is, int format )
       std::cerr << "WARNING -- unexpected SemSegNovelty object -- " << tmp << " -- for restoration... aborting" << std::endl;
       throw;
       }
+      
+      // INSTANTIATE (YET) NON-RESTORABLE OBJECTS
+      //TODO destructor of regionSeg is non-virtual so far - change this accordingly!
+      if ( this->regionSeg != NULL ) 
+	delete this->regionSeg;
+      
+      if( this->s_rsMethode == "none" )
+      {
+	this->regionSeg = NULL;
+      }
+      else
+      {
+	//NOTE using an empty config file might not be save...
+	NICE::Config tmpConfEmpty;
+	RegionSegmentationMethod *tmpRegionSeg = GenericRegionSegmentationMethodSelection::selectRegionSegmentationMethod( &tmpConfEmpty, this->s_rsMethode );    
+	if ( reuseSegmentation )
+	  this->regionSeg = new RSCache ( &tmpConfEmpty, tmpRegionSeg );
+	else
+	  this->regionSeg = tmpRegionSeg;
+      }      
+      
+      // done restoration
     }
   }
   else
@@ -2062,17 +2143,41 @@ void SemSegNovelty::store ( std::ostream & os, int format ) const
     
     ///////////////////////////////
     //     SEGMENTATION STUFF    //
-    ///////////////////////////////     
-    //TODO regionSeg
-    //NOTE regionSeg seems really important to keep track off
+    ///////////////////////////////
+    
+    // theoretically, we should properly store and restore the regionSeg object. However,  its parent class does not provide
+    // a Persistent interface yet. Hence, we perform this tiny workaround which works, since regionSeg is not changed over time...
+    // only be aware of parameters originally set via config...    
+    os << this->createStartTag( "s_rsMethode" ) << std::endl;
+    os << this->s_rsMethode << std::endl;
+    os << this->createStartTag( "s_rsMethode" ) << std::endl;    
     
     os << this->createStartTag( "reuseSegmentation" ) << std::endl;
     os << this->reuseSegmentation << std::endl;
     os << this->createStartTag( "reuseSegmentation" ) << std::endl;    
     
-    //TODO queriedRegions
-    //NOTE queriedRegions seems really important to keep track off
-    //std::map<std::string,std::set<int> >
+    os << this->createStartTag( "queriedRegions" ) << std::endl;
+    os << "size: " << queriedRegions.size() << std::endl;
+    std::map< std::string, std::set< int > >::const_iterator itQueriedRegions = queriedRegions.begin();
+    for ( uint i = 0; i < queriedRegions.size(); i++ )
+    {
+      // store key
+      os << itQueriedRegions->first << std::endl;
+      
+      // store values -- inner loop over sets
+      os << "size: " << ( itQueriedRegions->second ).size() << std::endl;
+
+      for ( std::set< int >::const_iterator itRegionsOfImg = ( itQueriedRegions->second ).begin();
+	    itRegionsOfImg != ( itQueriedRegions->second ).end();
+	    itRegionsOfImg++
+	  )
+      {
+	os << *itRegionsOfImg << " " << std::endl;
+      }       
+      
+      itQueriedRegions++;
+    } 
+    os << this->createStartTag( "queriedRegions" ) << std::endl;
     //
     //TODO currentRegionToQuery
 

+ 6 - 3
semseg/SemSegNovelty.h

@@ -43,9 +43,7 @@ class SemSegNovelty : public SemanticSegmentation
     ////////////////////////////////////////
     // variables only setable via configfile
     ////////////////////////////////////////
-/*    
-    //! Configuration File
-    NICE::Config *conf; */   
+  
     
     ///////////////////////////////
     //     FEATURE EXTRACTION    //
@@ -105,6 +103,7 @@ class SemSegNovelty : public SemanticSegmentation
     /////////////////////////////// 
     
     //! just store the name of our classifier
+    // Theoretically redundant, but currently makes things easier for store and restore...
     std::string classifierString;
     
     //! Classifier
@@ -141,6 +140,10 @@ class SemSegNovelty : public SemanticSegmentation
     //     SEGMENTATION STUFF    //
     /////////////////////////////// 
     
+    //! just store the name of our segmentation method. 
+    // Theoretically redundant, but currently makes things easier for store and restore...
+    std::string s_rsMethode;
+    
     //! low level Segmentation method
     RegionSegmentationMethod *regionSeg;