浏览代码

towards persistent of semseg - not runnable yet

Alexander Freytag 11 年之前
父节点
当前提交
471b2429a3
共有 5 个文件被更改,包括 265 次插入54 次删除
  1. 1 1
      semseg/SemSegContextTree.h
  2. 9 3
      semseg/SemSegNovelty.cpp
  3. 7 1
      semseg/SemSegNovelty.h
  4. 174 36
      semseg/SemanticSegmentation.cpp
  5. 74 13
      semseg/SemanticSegmentation.h

+ 1 - 1
semseg/SemSegContextTree.h

@@ -31,7 +31,7 @@ namespace OBJREC {
 
 /** Localization system */
 
-class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
+class SemSegContextTree : public SemanticSegmentation
 {
     /** Segmentation Method */
     RegionSegmentationMethod *segmentation;

+ 9 - 3
semseg/SemSegNovelty.cpp

@@ -1736,6 +1736,11 @@ void SemSegNovelty::restore ( std::istream & is, int format )
         is >> tmp; // end of block 
         tmp = this->removeEndTag ( tmp );
       } 
+      else if ( tmp.compare("SemanticSegmentation") == 0 )
+      {
+        // restore parent object
+        SemanticSegmentation::restore(is);
+      }      
       else
       {
       std::cerr << "WARNING -- unexpected SemSegNovelty object -- " << tmp << " -- for restoration... aborting" << std::endl;
@@ -1839,8 +1844,6 @@ void SemSegNovelty::store ( std::ostream & os, int format ) const
      os << "NULL" << std::endl;
     }
     os << this->createEndTag( "vclassifier" ) << std::endl;    
-        
-    //TODO classnames?
     
     //TODO
 /*    
@@ -1854,7 +1857,10 @@ void SemSegNovelty::store ( std::ostream & os, int format ) const
     {
       os << *itForbClassTrain << " " << std::endl;
     }   
-    os << this->createEndTag( "queriedRegions" ) << std::endl;   */   
+    os << this->createEndTag( "queriedRegions" ) << std::endl;   */
+
+    // store parent object
+    SemanticSegmentation::store(os);  
     
     
     // done

+ 7 - 1
semseg/SemSegNovelty.h

@@ -29,11 +29,17 @@
 
 namespace OBJREC {
 
-class SemSegNovelty : public SemanticSegmentation, public NICE::Persistent
+class SemSegNovelty : public SemanticSegmentation
 {
 
   protected:
     
+    /////////////////////////
+    /////////////////////////
+    // PROTECTED VARIABLES //
+    /////////////////////////
+    /////////////////////////    
+    
     ////////////////////////////////////////
     // variables only setable via configfile
     ////////////////////////////////////////

+ 174 - 36
semseg/SemanticSegmentation.cpp

@@ -1,8 +1,8 @@
 /**
 * @file SemanticSegmentation.cpp
 * @brief abstract interface for semantic segmentation algorithms
-* @author Erik Rodner
-* @date 03/19/2009
+* @author Erik Rodner, Alexander Freytag
+* @date 03/19/2009, latest update: 29-01-2014 (dd-mm-yyyy)
 
 */
 #include <iostream>
@@ -15,6 +15,63 @@ using namespace OBJREC;
 using namespace std;
 using namespace NICE;
 
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////     
+
+SemanticSegmentation::SemanticSegmentation ( const Config *conf,
+    const ClassNames *classNames )
+{
+  this->classNames = classNames;
+
+  Preprocess::Init ( conf );
+
+  std::string imagetype_s = conf->gS ( "main", "imagetype", "rgb" );
+
+  if ( imagetype_s == "rgb" )
+    imagetype = IMAGETYPE_RGB;
+  else if ( imagetype_s == "gray" )
+    imagetype = IMAGETYPE_GRAY;
+  else {
+    fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
+    exit ( -1 );
+  }
+}
+
+SemanticSegmentation::~SemanticSegmentation()
+{
+}
+
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                      SEGMENTATION STUFF
+    ///////////////////// ///////////////////// ///////////////////// 
+
+void SemanticSegmentation::semanticseg ( const std::string & filename,
+    NICE::Image & segresult,
+    NICE::MultiChannelImageT<double> & probabilities )
+{
+  Globals::setCurrentImgFN ( filename );
+  CachedExample *ce;
+  if ( imagetype == IMAGETYPE_RGB )
+  {
+    NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
+    ce = new CachedExample ( img );
+  } else {
+
+    NICE::Image img = Preprocess::ReadImgAdv ( filename );
+    ce = new CachedExample ( img );
+  }
+  fprintf ( stderr, "Starting Semantic Segmentation !\n" );
+  semanticseg ( ce, segresult, probabilities );
+  delete ce;
+}
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                      DATA CONVERSION
+    ///////////////////// ///////////////////// ///////////////////// 
+
 void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
 {
 #ifdef DEBUG_PRINTS
@@ -155,46 +212,127 @@ void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &
 #endif
 }
 
-SemanticSegmentation::SemanticSegmentation ( const Config *conf,
-    const ClassNames *classNames )
-{
-  this->classNames = classNames;
-
-  Preprocess::Init ( conf );
-
-  std::string imagetype_s = conf->gS ( "main", "imagetype", "rgb" );
+///////////////////// INTERFACE PERSISTENT /////////////////////
+// interface specific methods for store and restore
+///////////////////// INTERFACE PERSISTENT ///////////////////// 
 
-  if ( imagetype_s == "rgb" )
-    imagetype = IMAGETYPE_RGB;
-  else if ( imagetype_s == "gray" )
-    imagetype = IMAGETYPE_GRAY;
-  else {
-    fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
-    exit ( -1 );
+void SemanticSegmentation::restore ( std::istream & is, int format )
+{
+  //delete everything we knew so far...
+  this->clear();
+  
+  bool b_restoreVerbose ( false );
+#ifdef B_RESTOREVERBOSE
+  b_restoreVerbose = true;
+#endif  
+  
+  if ( is.good() )
+  {
+    if ( b_restoreVerbose ) 
+      std::cerr << " restore SemanticSegmentation" << std::endl;
+    
+    std::string tmp;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "SemanticSegmentation" ) )
+    {
+      std::cerr << " WARNING - attempt to restore SemanticSegmentation, but start flag " << tmp << " does not match! Aborting... " << std::endl;
+      throw;
+    }   
+    
+    is.precision (numeric_limits<double>::digits10 + 1);
+    
+    bool b_endOfBlock ( false ) ;
+    
+    while ( !b_endOfBlock )
+    {
+      is >> tmp; // start of block 
+      
+      if ( this->isEndTag( tmp, "SemanticSegmentation" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      }      
+      
+      tmp = this->removeStartTag ( tmp );
+      
+      if ( b_restoreVerbose )
+        std::cerr << " currently restore section " << tmp << " in SemanticSegmentation" << std::endl;
+           
+      if ( tmp.compare("classNames") == 0 )
+      {
+        const_cast<ClassNames*>(this->classNames)->restore ( is, format );
+        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("imagetype") == 0 )
+      {
+        is >> this->imagetype;
+        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("iterationCountSuffix") == 0 )
+      {
+        is >> this->iterationCountSuffix;
+        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      } 
+      else
+      {
+      std::cerr << "WARNING -- unexpected SemanticSegmentation object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    }
+  }
+  else
+  {
+    std::cerr << "SemanticSegmentation::restore -- InStream not initialized - restoring not possible!" << std::endl;
+    throw;
   }
+  
+  
+ //TODO check whether we also have to do something linke Preprocess::Init ( conf );
 }
 
-SemanticSegmentation::~SemanticSegmentation()
-{
+void SemanticSegmentation::store ( std::ostream & os, int format ) const
+{ 
+  if (os.good())
+  {
+    // show starting point
+    os << this->createStartTag( "SemanticSegmentation" ) << std::endl;    
+    
+    os.precision (numeric_limits<double>::digits10 + 1);
+    
+    os << this->createStartTag( "classNames" ) << std::endl;
+    this->classNames->store ( os, format );
+    os << this->createEndTag( "classNames" ) << std::endl;    
+    
+    //
+    
+    os << this->createStartTag( "imagetype" ) << std::endl;
+    os << imagetype << std::endl;
+    os << this->createEndTag( "imagetype" ) << std::endl; 
+    
+    //
+    
+    os << this->createStartTag( "iterationCountSuffix" ) << std::endl;
+    os << iterationCountSuffix << std::endl;
+    os << this->createEndTag( "iterationCountSuffix" ) << std::endl;     
+    
+    // done
+    os << this->createEndTag( "SemanticSegmentation" ) << std::endl;    
+  }
+  else
+  {
+    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
+  }
 }
 
-void SemanticSegmentation::semanticseg ( const std::string & filename,
-    NICE::Image & segresult,
-    NICE::MultiChannelImageT<double> & probabilities )
+void SemanticSegmentation::clear ()
 {
-  Globals::setCurrentImgFN ( filename );
-  CachedExample *ce;
-  if ( imagetype == IMAGETYPE_RGB )
-  {
-    NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
-    ce = new CachedExample ( img );
-  } else {
-
-    NICE::Image img = Preprocess::ReadImgAdv ( filename );
-    ce = new CachedExample ( img );
-  }
-  fprintf ( stderr, "Starting Semantic Segmentation !\n" );
-  semanticseg ( ce, segresult, probabilities );
-  delete ce;
+ //TODO
 }
 

+ 74 - 13
semseg/SemanticSegmentation.h

@@ -1,17 +1,21 @@
 /**
 * @file SemanticSegmentation.h
 * @brief abstract interface for semantic segmentation algorithms
-* @author Erik Rodner
-* @date 03/19/2009
+* @author Erik Rodner, Alexander Freytag
+* @date 03/19/2009, latest update: 29-01-2014 (dd-mm-yyyy)
 
 */
 #ifndef SEMANTICSEGMENTATIONINCLUDE
 #define SEMANTICSEGMENTATIONINCLUDE
 
-#include "vislearning/cbaselib/MultiDataset.h"
-#include "vislearning/cbaselib/LocalizationResult.h"
-#include "vislearning/cbaselib/CachedExample.h"
-#include "vislearning/cbaselib/Example.h"
+// nice-core includes
+#include <core/basics/Persistent.h>
+
+// nice-vislearning includes
+#include <vislearning/cbaselib/MultiDataset.h>
+#include <vislearning/cbaselib/LocalizationResult.h>
+#include <vislearning/cbaselib/CachedExample.h>
+#include <vislearning/cbaselib/Example.h>
 
 
 #define ROADWORKSADD fthrow(NICE::Exception, "addNewExample(const NICE::Vector & newExample, const int & newClassNo): not yet implemented!");
@@ -23,13 +27,20 @@ namespace OBJREC
 {
 
 /** abstract interface for semantic segmentation algorithms */
-class SemanticSegmentation
+class SemanticSegmentation : public NICE::Persistent
 {
 
   protected:
+    
+    /////////////////////////
+    /////////////////////////
+    // PROTECTED VARIABLES //
+    /////////////////////////
+    ///////////////////////// 
+    
     /** accessible class names and information about
         number of classes etc. */
-    const ClassNames *classNames;
+     const ClassNames * classNames; //mutable
 
     /** enum type for imagetype */
     enum
@@ -42,8 +53,18 @@ class SemanticSegmentation
     int imagetype;
     
     int iterationCountSuffix;
+    
+    /////////////////////////
+    /////////////////////////
+    //  PROTECTED METHODS  //
+    /////////////////////////
+    /////////////////////////    
 
   public:
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////     
 
     /** simple constructor
         @param conf global settings
@@ -54,6 +75,15 @@ class SemanticSegmentation
 
     /** simple destructor */
     virtual ~SemanticSegmentation();
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                      SEGMENTATION STUFF
+    ///////////////////// ///////////////////// /////////////////////      
+
+    /** load img from file call localize(CachedExample *ce) etc. */
+    void semanticseg ( const std::string & filename,
+                       NICE::Image & segresult,
+                       NICE::MultiChannelImageT<double> & probabilities );    
 
     /** this function has to be overloaded by all subclasses
         @param ce image data
@@ -66,6 +96,10 @@ class SemanticSegmentation
                                NICE::Image & segresult,
                                NICE::MultiChannelImageT<double> & probabilities ) = 0;
 
+                               
+    ///////////////////// ///////////////////// /////////////////////
+    //                      DATA CONVERSION
+    ///////////////////// ///////////////////// /////////////////////                                
     /**
      * convert different datatypes
      */
@@ -75,11 +109,12 @@ class SemanticSegmentation
     void convertLSetToExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec, const bool & removeOldDataPointer=false );
     void convertLSetToSparseExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
 
+    
 
-    /** load img from file call localize(CachedExample *ce) etc. */
-    void semanticseg ( const std::string & filename,
-                       NICE::Image & segresult,
-                       NICE::MultiChannelImageT<double> & probabilities );
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                       ONLINE LEARNING
+    ///////////////////// ///////////////////// /////////////////////      
     
     virtual void addNewExample(const NICE::Vector & newExample, const int & newClassNo)
     {
@@ -93,7 +128,11 @@ class SemanticSegmentation
     virtual void addNovelExamples()
     {
       ROADWORKSADDNOVEL;      
-    };    
+    };   
+    
+    ///////////////////// ///////////////////// /////////////////////
+    //                         GET / SET
+    ///////////////////// ///////////////////// /////////////////////      
     
     /**
      * @brief Get a pointer to the examples extracted from the most novel region seen so far
@@ -107,6 +146,28 @@ class SemanticSegmentation
     
     void setIterationCountSuffix( const int & _iterationCountSuffix) { iterationCountSuffix = _iterationCountSuffix; };
     
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////   
+    
+    /** 
+     * @brief Load active-segmentation-object from external file (stream)
+     * @author Alexander Freytag
+     */     
+    virtual void restore ( std::istream & is, int format = 0 );
+    
+    /** 
+     * @brief Save active-segmentation-object to external file (stream)
+     * @author Alexander Freytag
+     */       
+    virtual void store( std::ostream & os, int format = 0 ) const;
+    
+    /** 
+     * @brief Clear active-segmentation-object object
+     * @author Alexander Freytag
+     */    
+    virtual void clear ();    
+    
     
 
 };