Przeglądaj źródła

only compute integral maps when selected

Sven Sickert 11 lat temu
rodzic
commit
8406dfbe22

+ 26 - 19
progs/testSemanticSegmentation.cpp

@@ -20,6 +20,7 @@
 #include <semseg3d/semseg/SemSegContextTree.h>
 
 #include <core/basics/ResourceStatistics.h>
+#include <core/image/Morph.h>
 
 #include <fstream>
 #include <vector>
@@ -122,6 +123,7 @@ int main ( int argc, char **argv )
   bool show_result = conf.gB ( "debug", "show_results", false );
   bool write_results = conf.gB ( "debug", "write_results", false );
   bool run_3dseg = conf.gB ( "debug", "run_3dseg", true );
+  bool postProcessing = conf.gB( "debug", "post_process", false);
   string output_type = conf.gS ( "debug", "output_type", "ppm" );
   string output_postfix = conf.gS ( "debug", "output_postfix", "" );
   string resultdir = conf.gS ( "debug", "resultdir", "." );
@@ -202,9 +204,13 @@ int main ( int argc, char **argv )
       NICE::MultiChannelImage3DT<double> probabilities;
       NICE::MultiChannelImage3DT<double> imgData;
       semseg->make3DImage ( filelist, imgData );
+
+      Timer timer;
+      timer.start();
       semseg->classify ( imgData, segresult, probabilities, filelist );
+      timer.stop();
 
-      fprintf ( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
+      fprintf ( stderr, "testSemanticSegmentation: Segmentation finished in %f seconds!\n", timer.getLastAbsolute() );
 
       // save to file
       for ( int z = 0; z < segresult.channels(); z++ )
@@ -229,9 +235,28 @@ int main ( int argc, char **argv )
             }
           }
 
+          // confusion matrix
+          NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
+          M.set ( 0 );
+          updateMatrix ( lm, lm_gt, M, forbidden_classes );
+          M_vec.push_back ( M );
+
           classNames.labelToRGB ( lm, rgb );
           classNames.labelToRGB ( lm_gt, rgb_gt );
 
+          if (postProcessing)
+          {
+            // median filter
+            for (int r = 0; r < 3; r++)
+            {
+              NICE::Image postIm(rgb.width(), rgb.height());
+              NICE::median(*(rgb.getChannel(r)), &postIm, 1);
+              for (int y = 0; y < rgb.height(); y++)
+                for (int x = 0; x < rgb.width(); x++)
+                  rgb.setPixel(x,y,r, postIm.getPixelQuick(x,y));
+            }
+          }
+
           segmentToOverlay ( orig.getChannel(1), rgb, ov_rgb );
           segmentToOverlay ( orig.getChannel(1), rgb_gt, ov_rgb_gt );
           
@@ -262,24 +287,6 @@ int main ( int argc, char **argv )
         }
       }
 
-//#pragma omp critical
-      for ( int z = 0; z < segresult.channels(); z++ )
-      {
-        for ( int y = 0 ; y < segresult.height(); y++ )
-        {
-          for ( int x = 0 ; x < segresult.width(); x++ )
-          {
-            lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
-            if ( run_3dseg )
-              lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
-          }
-        }
-        NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
-        M.set ( 0 );
-        updateMatrix ( lm, lm_gt, M, forbidden_classes );
-        M_vec.push_back ( M );
-      }
-
       // prepare for new 3d image
       filelist.clear();
       segresult.reInit(0,0,0);

+ 23 - 42
semseg/SemSegContextTree.cpp

@@ -150,8 +150,6 @@ SemSegContextTree::SemSegContextTree ( const Config *conf, const MultiDataset *m
 
   useAdditionalLayer = conf->gB ( featsec, "use_additional_layer", false );
   
-  useVariance = conf->gB ( featsec, "use_variance", false );
-
   // geometric features of hoiem
   useHoiemFeatures = conf->gB ( featsec, "use_hoiem_features", false );
   if ( useHoiemFeatures )
@@ -512,22 +510,27 @@ void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImage3DT<
   int channels = ( int ) forest[0][0].dist.size();    // channels = classes
 
   // integral images for context channels (probability maps for each class)
-#pragma omp parallel for
-  for ( int c = 0; c < channels; c++ )
+  if (ftypes > 3)
   {
-    for ( int z = 0; z < zsize; z++ )
+#pragma omp parallel for
+    for ( int c = 0; c < channels; c++ )
     {
-      for ( int y = 0; y < ysize; y++ )
+      for ( int z = 0; z < zsize; z++ )
       {
-        for ( int x = 0; x < xsize; x++ )
+        for ( int y = 0; y < ysize; y++ )
         {
-          double val = getMeanProb ( x, y, z, c, nodeIndices );
-          feats ( x, y, z, firstChannel + c ) = val;
-          feats ( x, y, z, firstChannel + channels + c ) = val;
+          for ( int x = 0; x < xsize; x++ )
+          {
+            double val = getMeanProb ( x, y, z, c, nodeIndices );
+            feats ( x, y, z, firstChannel + c ) = val;
+
+            if (ftypes == 5)
+              feats ( x, y, z, firstChannel + channels + c ) = val;
+          }
         }
       }
+      feats.calcIntegral ( firstChannel + c );
     }
-    feats.calcIntegral ( firstChannel + c );
   }
 }
 
@@ -609,9 +612,6 @@ void SemSegContextTree::train ( const LabeledSet * trainp )
 
   if ( useAdditionalLayer )
     rawChannels += 1;
-  
-  if ( useVariance )
-    rawChannels += 1; 
 
 
 ///////////////////////////// read input data /////////////////////////////////
@@ -673,7 +673,6 @@ void SemSegContextTree::train ( const LabeledSet * trainp )
       nodeIndices.push_back ( MultiChannelImage3DT<unsigned short int> ( xsize, ysize, zsize, nbTrees ) );
       nodeIndices[imgCounter].setAll ( 0 );
 
-      //TODO: resize image?!
       MultiChannelImage3DT<double> feats;
       allfeats.push_back ( feats );
 
@@ -776,8 +775,6 @@ void SemSegContextTree::train ( const LabeledSet * trainp )
     channelType.push_back ( 4 );
   }
 
-  // configure to include all possible feature types (just a subset
-  // defined by ftypes is actually used)
   // 'amountTypes' sets upper bound for usable feature types
   int amountTypes = 5;
   channelsPerType = vector<vector<int> > ( amountTypes, vector<int>() );
@@ -787,10 +784,10 @@ void SemSegContextTree::train ( const LabeledSet * trainp )
     channelsPerType[channelType[i]].push_back ( i );
   }
 
-  for ( int i = 0; i < classes; i++ )
-  {
-    channelsPerType[channelsPerType.size()-1].push_back ( i );
-  }
+//  for ( int i = 0; i < classes; i++ )
+//  {
+//    channelsPerType[channelsPerType.size()-1].push_back ( i );
+//  }
   ftypes = std::min ( amountTypes, ftypes );
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -1129,9 +1126,11 @@ void SemSegContextTree::train ( const LabeledSet * trainp )
     //compute integral images
     if ( firstiteration )
     {
+      // only add context layers if necessary
+      int multi = std::max(0, ftypes-3);
       for ( int i = 0; i < imgCounter; i++ )
       {
-        allfeats[i].addChannel ( (2*classes) + rawChannels );
+        allfeats[i].addChannel ( (multi*classes) + rawChannels );
       }
     }
 
@@ -1273,12 +1272,13 @@ void SemSegContextTree::extractBasicFeatures ( NICE::MultiChannelImage3DT<double
   int xsize = imgData.width();
   int ysize = imgData.height();
   int zsize = imgData.depth();
-  //TODO: resize image?!
 
   amountRegions = 0;
   feats.reInit ( xsize, ysize, zsize, imgData.channels() );
   feats.setAll ( 0 );
 
+  //TODO: expand imgData instead of building feats!!! --> SAVING MEMORY
+
   for ( int z = 0; z < zsize; z++ )
   {
     NICE::MultiChannelImageT<double> feats_tmp;
@@ -1462,25 +1462,6 @@ void SemSegContextTree::extractBasicFeatures ( NICE::MultiChannelImage3DT<double
       }
     }
   }
-  
-  // variance maps (textural information)
-  if ( useVariance )
-  {
-    int cchannel = feats.channels();
-    if (imagetype == IMAGETYPE_RGB)
-    {
-      feats.addChannel( 3 );
-      for (int c = 0; c < 3; c++)
-      {
-        feats.calcVariance( c, cchannel+c );
-      }
-    }
-    else
-    {
-      feats.addChannel( 1 );
-      feats.calcVariance( 0, cchannel );
-    }
-  }
 
 }
 

+ 0 - 3
semseg/SemSegContextTree.h

@@ -107,9 +107,6 @@ private:
   /** use additional input Layer or not */
   bool useAdditionalLayer;
 
-  /** use Variance map features or not */
-  bool useVariance;
-  
   /** use Regions as extra feature channel or not */
   bool useRegionFeature;
 

+ 8 - 1
semseg/SemanticSegmentation.cpp

@@ -276,7 +276,14 @@ void SemanticSegmentation::make3DImage ( const std::vector<std::string> & fileli
 
   if ( imagetype == IMAGETYPE_GRAY )
   {
-    imgData.correctShading( 0 );
+    imgData.equalizeHistogram( 0 );
+#ifdef DEBUG_PRINTS
+    for (int z = 0; z < imgData.depth(); z++)
+    {
+      NICE::Image im = imgData.getChannel( z, 0);
+      im.write( filelist[z]+"_eq.pgm");
+    }
+#endif
   }
 }