Parcourir la source

moved function getDepthVector() to SemSegTools

Sven Sickert il y a 9 ans
Parent
commit
07f8b63ad2

+ 1 - 1
progs/testSemanticSegmentation3D.cpp

@@ -50,7 +50,7 @@ void startClassification (SemanticSegmentation *semseg,
     string output_type = conf.gS ( "debug", "output_type", "ppm" );
 
     vector< int > zsizeVec;
-    semseg->getDepthVector ( testFiles, zsizeVec, run_3Dseg );
+    SemSegTools::getDepthVector ( testFiles, zsizeVec, run_3Dseg );
     int depthCount = 0, idx = 0;
     vector< string > filelist;
     NICE::MultiChannelImageT<int> segresult;

+ 136 - 59
semseg/SemSegTools.cpp

@@ -8,6 +8,7 @@
 #include <iostream>
 #include <iomanip>
 
+#include "core/basics/StringTools.h"
 #include "SemSegTools.h"
 
 using namespace OBJREC;
@@ -235,98 +236,174 @@ void SemSegTools::saveResultsToImageFile(
     outStr = out.str();
 }
 
-void SemSegTools::collectTrainingExamples ( 
+void SemSegTools::getDepthVector (
+        const LabeledSet *Files,
+        std::vector<int> & depthVec,
+        const bool run3Dseg )
+{
+    std::string oldName;
+    int zsize = 0;
+    bool isInit = false;
+
+    for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
+    {
+        for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
+             jt != it->second.end(); jt++)
+        {
+            ImageInfo & info = *(*jt);
+            std::string file = info.img();
+
+            std::vector< std::string > list;
+            StringTools::split ( file, '/', list );
+            std::string filename = list.back();
+            uint found = filename.find_last_of ( "_" );
+            if (run3Dseg && found < filename.size() && found-3 > 0 )
+            {
+                std::string curName = filename.substr ( found-3,3 );
+                if ( !isInit )
+                {
+                    oldName = curName;
+                    isInit = true;
+                }
+                if ( curName.compare ( oldName ) == 0 ) // if strings match up
+                {
+                    zsize++;
+                }
+                else
+                {
+                    depthVec.push_back ( zsize );
+                    zsize = 1;
+                    oldName = curName;
+                }
+            }
+            else
+            {
+                zsize = 1;
+                depthVec.push_back ( zsize );
+            }
+
+        }
+    }
+    depthVec.push_back ( zsize );
+}
+
+void SemSegTools::collectTrainingExamples (
         const Config * conf,
         const std::string & section,
         const LabeledSet & train,
         const ClassNames & cn,
         Examples & examples,
-        vector<CachedExample *> & imgexamples )
+        vector<CachedExample *> & imgexamples,
+        const bool run3Dseg )
 {
     assert ( train.count() > 0 );
     examples.clear();
     imgexamples.clear();
 
+    vector<int> zsizeVec;
+    SemSegTools::getDepthVector ( &train, zsizeVec, true );
+
     int grid_size_x = conf->gI(section, "grid_size_x", 5 );
     int grid_size_y = conf->gI(section, "grid_size_y", 5 );
+    int grid_size_z = conf->gI(section, "grid_size_z", 5 );
     int grid_border_x = conf->gI(section, "grid_border_x", 20 );
     int grid_border_y = conf->gI(section, "grid_border_y", 20 );
+    int grid_border_z = conf->gI(section, "grid_border_z", 20 );
 
     std::string selection = conf->gS(section, "train_selection" );
 
     set<int> classnoSelection;
     cn.getSelection ( selection, classnoSelection );
-    
+
     bool useExcludedAsBG = conf->gB(section, "use_excluded_as_background", false );
 
     int backgroundClassNo = 0;
-    
+
     if ( useExcludedAsBG )
     {
         backgroundClassNo = cn.classno("various");
         assert ( backgroundClassNo >= 0 );
     }
 
-    LOOP_ALL_S (train)
-    {
-        EACH_INFO(image_classno,imgInfo);
-        std::string imgfn = imgInfo.img();
-
-        if ( ! imgInfo.hasLocalizationInfo() ) {
-            std::cerr << "WARNING: NO localization info found for "
-                      << imgfn << " !" << std::endl;
-            continue;
-        }
-
-        int xsize, ysize;
-        CachedExample *ce = new CachedExample ( imgfn );
-        ce->getImageSize ( xsize, ysize );
-        imgexamples.push_back ( ce );
-
-        const LocalizationResult *locResult = imgInfo.localization();
-        if ( locResult->size() <= 0 ) {
-            std::cerr << "WARNING: NO ground truth polygons found for "
-                      << imgfn << " !" << std::endl;
-            continue;
-        }
-
-        std::cerr << "SemSegTools: Collecting pixel examples from localization info: "
-                  << imgfn << std::endl;
+    int depthCount = 0;
+    int imgCounter = 0;
+    vector<std::string> filelist;
+    NICE::MultiChannelImageT<int> pixelLabels;
 
-        NICE::ImageT<int> pixelLabels (xsize, ysize);
-        pixelLabels.set(0);
-        locResult->calcLabeledImage ( pixelLabels, cn.getBackgroundClass() );
+    for (LabeledSet::const_iterator it = train.begin(); it != train.end(); it++)
+    {
+        for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
+             jt != it->second.end(); jt++)
+        {
+            int classno = it->first;
+            ImageInfo & info = *(*jt);
+            std::string file = info.img();
+            filelist.push_back ( file );
+            depthCount++;
+
+            const LocalizationResult *locResult = info.localization();
+
+            // getting groundtruth
+            NICE::ImageT<int> pL;
+            pL.resize ( locResult->xsize, locResult->ysize );
+            pL.set ( 0 );
+            locResult->calcLabeledImage ( pL, cn.getBackgroundClass() );
+            pixelLabels.addChannel ( pL );
+
+            if ( locResult->size() <= 0 ) {
+                std::cerr << "WARNING: NO ground truth polygons found for "
+                          << file << " !" << std::endl;
+                continue;
+            }
 
-#ifdef DEBUG_LOCALIZATION
-        NICE::Image img (imgfn);
-        showImage(img);
-        showImage(pixelLabels);
-#endif
+            std::cerr << "SemSegTools: Collecting pixel examples from localization info: "
+                      << file << std::endl;
 
-        Example pce ( ce, 0, 0 );
-        for ( int x = 0 ; x < xsize ; x += grid_size_x )
-            for ( int y = 0 ; y < ysize ; y += grid_size_y )
+            int depthBoundary = 0;
+            if ( run3Dseg )
             {
-                if ( (x >= grid_border_x) &&
-                     ( y >= grid_border_y ) && ( x < xsize - grid_border_x ) &&
-                     ( y < ysize - grid_border_x ) )
-                {
-                    pce.x = x; pce.y = y;
-                    int classno = pixelLabels.getPixel(x,y);
-
-                    if ( classnoSelection.find(classno) != classnoSelection.end() ) {
-                        examples.push_back ( pair<int, Example> (
-                                                 classno,
-                                                 pce // FIXME: offset handling
-                                                 ) );
-                    } else if ( useExcludedAsBG ) {
-                        examples.push_back ( pair<int, Example> (
-                                                 backgroundClassNo,
-                                                 pce // FIXME: offset handling
-                                                 ) );
-                    }
-                }
+                depthBoundary = zsizeVec[imgCounter];
             }
+
+            if ( depthCount < depthBoundary ) continue;
+
+            int xsize, ysize, zsize;
+            CachedExample *ce = new CachedExample ( filelist );
+            ce->getImageSize3 ( xsize, ysize, zsize );
+            imgexamples.push_back ( ce );
+
+            // drawing actual examples
+            Example pce ( ce, 0, 0, 0 );
+            for ( int z = 0; z < zsize; z += grid_size_z )
+                for ( int x = 0 ; x < xsize ; x += grid_size_x )
+                    for ( int y = 0 ; y < ysize ; y += grid_size_y )
+                        if ( ( x >= grid_border_x ) &&
+                             ( y >= grid_border_y ) &&
+                             ( z >= grid_border_z ) &&
+                             ( x < xsize - grid_border_x ) &&
+                             ( y < ysize - grid_border_y ) &&
+                             ( z < zsize - grid_border_z ) )
+                        {
+                            pce.x = x; pce.y = y; pce.z = z;
+                            int classno = pixelLabels.get(x,y,(unsigned int)z);
+
+                            if ( classnoSelection.find(classno) != classnoSelection.end() )
+                            {
+                                examples.push_back (
+                                            pair<int, Example> ( classno, pce ) );
+                            } else if ( useExcludedAsBG )
+                            {
+                                examples.push_back (
+                                            pair<int, Example> ( backgroundClassNo, pce ) );
+                            }
+                        }
+
+            // prepare for new 3D image
+            filelist.clear();
+            pixelLabels.reInit ( 0,0,0 );
+            depthCount = 0;
+            imgCounter++;
+        }
     }
 
     std::cerr << "total number of examples: " << (int)examples.size() << std::endl;

+ 15 - 2
semseg/SemSegTools.h

@@ -89,6 +89,18 @@ class SemSegTools
             const std::string & file,
             std::string & outStr );
 
+    /**
+     * @brief Collect information about the depth of 3d images
+     * @author Sven Sickert
+     * @param Files a labeled set of data
+     * @param depthVec output of depth values
+     * @param run3dseg whether slice counting is necessary or not
+     */
+    static void getDepthVector (
+            const LabeledSet *Files,
+            std::vector<int> & depthVec,
+            const bool run3dseg );
+
     /** collect pixel-wise training examples
         from a set of images
         @param conf includes settings about grid size etc.
@@ -97,6 +109,7 @@ class SemSegTools
         @param cn classNames object
         @param examples resulting pixel-wise examples
         @param imgexamples image based caching structure referenced by pixel-wise examples
+        @param run3Dseg whether to run in 3D segmentation mode or not
     */
     static void collectTrainingExamples (
             const NICE::Config * conf,
@@ -104,8 +117,8 @@ class SemSegTools
             const LabeledSet & train,
             const ClassNames & cn,
             Examples & examples,
-            std::vector<CachedExample *> & imgexamples );
-
+            std::vector<CachedExample *> & imgexamples,
+            const bool run3Dseg = false );
 };
 
 

+ 2 - 54
semseg/SemanticSegmentation.cpp

@@ -7,10 +7,8 @@
 */
 #include "SemanticSegmentation.h"
 
-#include <core/basics/StringTools.h>
-
-#include <vislearning/baselib/Preprocess.h>
-#include <vislearning/baselib/Globals.h>
+#include "vislearning/baselib/Preprocess.h"
+#include "vislearning/baselib/Globals.h"
 
 #include <iostream>
 
@@ -263,56 +261,6 @@ void SemanticSegmentation::setClassNames ( const OBJREC::ClassNames * _className
   this->classNames = _classNames;
 }
 
-void SemanticSegmentation::getDepthVector ( const LabeledSet *Files,
-                                            vector<int> & depthVec,
-                                            const bool run3Dseg )
-{
-  std::string oldName;
-  int zsize = 0;
-  bool isInit = false;
-
-  for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
-  {
-    for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
-         jt != it->second.end(); jt++)
-    {
-      ImageInfo & info = *(*jt);
-      std::string file = info.img();
-
-      std::vector< std::string > list;
-      StringTools::split ( file, '/', list );
-      std::string filename = list.back();
-      uint found = filename.find_last_of ( "_" );
-      if (run3Dseg && found < filename.size() && found-3 > 0 )
-      {
-        std::string curName = filename.substr ( found-3,3 );
-        if ( !isInit )
-        {
-          oldName = curName;
-          isInit = true;
-        }
-        if ( curName.compare ( oldName ) == 0 ) // if strings match up
-        {
-          zsize++;
-        }
-        else
-        {
-          depthVec.push_back ( zsize );
-          zsize = 1;
-          oldName = curName;
-        }
-      }
-      else
-      {
-        zsize = 1;
-        depthVec.push_back ( zsize );
-      }
-
-    }
-  }
-  depthVec.push_back ( zsize );
-}
-
 void SemanticSegmentation::make3DImage ( const std::vector<std::string> & filelist,
     NICE::MultiChannelImage3DT<double> & imgData )
 {

+ 0 - 9
semseg/SemanticSegmentation.h

@@ -201,15 +201,6 @@ class SemanticSegmentation : public NICE::Persistent
       ROADWORKSGETNOVEL;
     }
 
-    /**
-     * @brief Collect information about the depth of 3d images
-     * @author Sven Sickert
-     * @param Files a labeled set of data
-     * @param depthVec output of depth values
-     * @param run3dseg whether slice counting is necessary or not
-     */
-    void getDepthVector ( const LabeledSet *Files, std::vector<int> & depthVec, const bool run3dseg );
-
     /**
      * @brief Save probability maps of all classes to iamge files
      * @author Sven Sickert