소스 검색

SemanticSegmentation as abstract class for 2D and 3D method

Sven Sickert 11 년 전
부모
커밋
b3a754a4e2
4개의 변경된 파일153개의 추가작업 그리고 10개의 파일을 삭제
  1. 7 0
      semseg/SemSegContextTree3D.cpp
  2. 5 0
      semseg/SemSegContextTree3D.h
  3. 67 6
      semseg/SemanticSegmentation.cpp
  4. 74 4
      semseg/SemanticSegmentation.h

+ 7 - 0
semseg/SemSegContextTree3D.cpp

@@ -2270,3 +2270,10 @@ void SemSegContextTree3D::restore ( std::istream & is, int format )
 
   is >> uniquenumber;
 }
+
+//###################### DEPRECATED #########################//
+
+void SemSegContextTree3D::semanticseg ( OBJREC::CachedExample *ce,
+                               NICE::Image & segresult,
+                               NICE::MultiChannelImageT<double> & probabilities )
+{}

+ 5 - 0
semseg/SemSegContextTree3D.h

@@ -243,6 +243,11 @@ public:
    */
   void train ( const MultiDataset *md );
 
+  // deprecated stuff
+  virtual void semanticseg ( OBJREC::CachedExample *ce,
+                             NICE::Image & segresult,
+                             NICE::MultiChannelImageT<double> & probabilities );
+
   bool active3DMode ()
   {
     return run3Dseg;

+ 67 - 6
semseg/SemanticSegmentation.cpp

@@ -7,11 +7,12 @@
 */
 #include <iostream>
 
+#include "core/image/MultiChannelImage3DT.h"
+#include "core/basics/StringTools.h"
+
 #include "SemanticSegmentation.h"
 #include "vislearning/baselib/Preprocess.h"
 #include "vislearning/baselib/Globals.h"
-#include "core/image/MultiChannelImage3DT.h"
-#include "core/basics/StringTools.h"
 
 using namespace OBJREC;
 using namespace std;
@@ -29,6 +30,14 @@ SemanticSegmentation::SemanticSegmentation ( const Config *conf,
                                              const ClassNames *classNames )
     : iterationCountSuffix(1)
 {
+  ///////////
+  // same code as in empty constructor - duplication can be avoided with C++11 allowing for constructor delegation
+  ///////////
+
+  ///////////
+  // here comes the new code part different from the empty constructor
+  ///////////
+
   this->classNames = classNames;
 
   this->initFromConfig( conf );
@@ -55,6 +64,29 @@ void SemanticSegmentation::initFromConfig(const Config* conf, const string& s_co
   Preprocess::Init ( conf );
 }
 
+///////////////////// ///////////////////// /////////////////////
+//                      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
@@ -90,21 +122,34 @@ void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, Lab
 #endif
 }
 
-void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec )
+void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec, const bool & removeOldDataPointer )
 {
 #ifdef DEBUG_PRINTS
   cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
 #endif
   for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
   {
-    for ( int j = 0; j < ( int ) iter->second.size(); j++ )
+    for ( int j = 0; j < (int)iter->second.size(); j++ )
     {
       NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
       Example ex ( vec );
       examples.push_back ( pair<int, Example> ( iter->first, ex ) );
     }
   }
-  lvec.clear();
+
+  if (!removeOldDataPointer)
+  {
+    //NOTE this is only useful, if our classifier does NOT need the data explicitely
+    lvec.clear();
+  }
+  else
+  {
+    lvec.removePointersToDataWithoutDeletion();
+    //after setting all the pointers to NULL, we can savely clear the LSet without deleting the previously
+    //stored features, which might be needed somewhere else, e.g., in the VCNearestNeighbour
+    lvec.clear();
+  }
+
 #ifdef DEBUG_PRINTS
   cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
 #endif
@@ -128,7 +173,11 @@ void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSe
     {
       if ( examples[i].second.svec != NULL )
       {
-        throw ( "Transform SVEC to VEC not yet implemented" );
+        NICE::Vector v;
+        examples[i].second.svec->convertToVectorT(v);
+        lvec.add ( examples[i].first, v );
+        delete examples[i].second.svec;
+        examples[i].second.svec = NULL;
       }
       else
       {
@@ -183,6 +232,18 @@ void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &
 #endif
 }
 
+
+void SemanticSegmentation::setIterationCountSuffix( const int & _iterationCountSuffix)
+{
+  this->iterationCountSuffix = _iterationCountSuffix;
+}
+
+void SemanticSegmentation::setClassNames ( const OBJREC::ClassNames * _classNames )
+{
+  this->classNames = _classNames;
+}
+
+
 void SemanticSegmentation::getDepthVector ( const LabeledSet *Files,
                                             vector<int> & depthVec,
                                             const bool run3Dseg )

+ 74 - 4
semseg/SemanticSegmentation.h

@@ -8,17 +8,24 @@
 #ifndef SEMANTICSEGMENTATIONINCLUDE
 #define SEMANTICSEGMENTATIONINCLUDE
 
+// standard library includes
+#include <vector>
+
 // nice-core includes
 #include <core/basics/Persistent.h>
-
-#include <vector>
 #include "core/image/MultiChannelImage3DT.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!");
+#define ROADWORKSADDNOVEL fthrow(NICE::Exception, "addNovelExamples(): not yet implemented!");
+#define ROADWORKSGETNOVEL fthrow(NICE::Exception, "getNovelExamples(): not yet implemented!");
+
 namespace OBJREC
 {
 
@@ -27,6 +34,13 @@ class SemanticSegmentation : public NICE::Persistent
 {
 
   protected:
+
+    /////////////////////////
+    /////////////////////////
+    // PROTECTED VARIABLES //
+    /////////////////////////
+    /////////////////////////
+
     /** accessible class names and information about
         number of classes etc. */
     const ClassNames *classNames;
@@ -43,13 +57,22 @@ class SemanticSegmentation : public NICE::Persistent
 
     int iterationCountSuffix;
 
+    /////////////////////////
+    /////////////////////////
+    //  PROTECTED METHODS  //
+    /////////////////////////
+    /////////////////////////
+
   public:
 
     ///////////////////// ///////////////////// /////////////////////
     //                   CONSTRUCTORS / DESTRUCTORS
     ///////////////////// ///////////////////// /////////////////////
 
-    /** default constructor */
+    /** default constructor
+     * @author Alexander Freytag
+     * @date 06-02-2014 ( dd-mm-yyy )
+    */
     SemanticSegmentation ( );
 
     /** simple constructor
@@ -74,6 +97,22 @@ class SemanticSegmentation : public NICE::Persistent
     //                      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
+        @param segresult result of the semantic segmentation with a label for each
+        pixel
+        @param probabilities multi-channel image with one channel for each class and
+               corresponding probabilities for each pixel
+    */
+    virtual void semanticseg ( OBJREC::CachedExample *ce,
+                               NICE::Image & segresult,
+                               NICE::MultiChannelImageT<double> & probabilities ) = 0;
+
     /**
      * @brief Classification function (has to be overloaded by all subclasses)
      * @author Sven Sickert
@@ -113,13 +152,44 @@ class SemanticSegmentation : public NICE::Persistent
     void convertVVectorToExamples ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
     void convertExamplesToVVector ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
     void convertExamplesToLSet ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
-    void convertLSetToExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
+    void convertLSetToExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec, const bool & removeOldDataPointer=false );
     void convertLSetToSparseExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
 
+
+
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                       ONLINE LEARNING
+    ///////////////////// ///////////////////// /////////////////////
+
+    virtual void addNewExample(const NICE::Vector & newExample, const int & newClassNo)
+    {
+      ROADWORKSADD;
+    }
+    /**
+     * @brief Add those examples, which belong to the most novel region seen so far
+     *
+     * @return void
+     **/
+    virtual void addNovelExamples()
+    {
+      ROADWORKSADDNOVEL;
+    }
+
     ///////////////////// ///////////////////// /////////////////////
     //                         GET / SET
     ///////////////////// ///////////////////// /////////////////////
 
+    /**
+     * @brief Get a pointer to the examples extracted from the most novel region seen so far
+     *
+     * @return Examples *
+     **/
+    virtual const Examples * getNovelExamples() const
+    {
+      ROADWORKSGETNOVEL;
+    }
+
     /**
      * @brief Collect information about the depth of 3d images
      * @author Sven Sickert