Bjoern Froehlich 13 anni fa
parent
commit
0c662aef03
3 ha cambiato i file con 80 aggiunte e 39 eliminazioni
  1. 1 1
      semseg/FIShotton.h
  2. 78 35
      semseg/SemSegContextTree.cpp
  3. 1 3
      semseg/SemSegContextTree.h

+ 1 - 1
semseg/FIShotton.h

@@ -8,7 +8,7 @@
 #ifndef FIShottonINCLUDE
 #define FIShottonINCLUDE
 
-#include "core/vector/SparseVector.h"
+#include "core/vector/SparseVectorT.h"
 #include "vislearning/cbaselib/CachedExample.h"
 #include "vislearning/classifier/fpclassifier/randomforest/FPCRandomForests.h"
 

+ 78 - 35
semseg/SemSegContextTree.cpp

@@ -74,7 +74,7 @@ class SparseImageAcess: public ValueAccess
   public:
     virtual double getVal ( const Features &feats, const int &x, const int &y, const int &channel )
     {
-      //MultiChannelImageT<SparseVector> textonMap;
+      //MultiChannelImageT<SparseVectorInt> textonMap;
       //TODO: implement access
     }
 
@@ -1162,7 +1162,7 @@ inline double SemSegContextTree::getMeanProb ( const int &x, const int &y, const
   return val / ( double ) nbTrees;
 }
 
-void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<SparseVector> &infeats, NICE::MultiChannelImageT<SparseVector> &integralImage )
+void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<SparseVectorInt> &infeats, NICE::MultiChannelImageT<SparseVectorInt> &integralImage )
 {
   int xsize = infeats.width();
   int ysize = infeats.height();
@@ -1269,6 +1269,11 @@ void SemSegContextTree::computeIntegralImage ( const NICE::MultiChannelImageT<un
   }
 }
 
+inline double computeWeight(const double &d, const double &dim)
+{
+    return 1.0/(pow(2,(double)(dim-d+1)));
+}
+
 void SemSegContextTree::train ( const MultiDataset *md )
 {
   const LabeledSet train = * ( *md ) ["train"];
@@ -1281,8 +1286,8 @@ void SemSegContextTree::train ( const MultiDataset *md )
   vector<MultiChannelImageT<double> > allfeats;
   vector<MultiChannelImageT<unsigned short int> > currentfeats;
   vector<MatrixT<int> > labels;
-  vector<MultiChannelImageT<SparseVector> > textonMap;
-  vector<MultiChannelImageT<SparseVector> > integralTexton;
+  vector<MultiChannelImageT<SparseVectorInt> > textonMap;
+  vector<MultiChannelImageT<SparseVectorInt> > integralTexton;
 
 
   std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
@@ -1327,11 +1332,11 @@ void SemSegContextTree::train ( const MultiDataset *md )
 
     currentfeats.push_back ( MultiChannelImageT<unsigned short int> ( xsize, ysize, nbTrees ) );
     currentfeats[imgcounter].setAll ( 0 );
-    textonMap.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
-    integralTexton.push_back ( MultiChannelImageT<SparseVector> ( xsize / grid + 1, ysize / grid + 1, 1 ) );
-
+    textonMap.push_back ( MultiChannelImageT<SparseVectorInt> ( xsize / grid + 1, ysize / grid + 1, 1 ));
+    integralTexton.push_back ( MultiChannelImageT<SparseVectorInt> ( xsize / grid + 1, ysize / grid + 1, 1 ));
+    
     labels.push_back ( tmpMat );
-
+    
     try {
       img = ColorImage ( currentFile );
     } catch ( Exception ) {
@@ -1463,11 +1468,10 @@ void SemSegContextTree::train ( const MultiDataset *md )
   //int baseFeatSize = allfeats[0].size();
 
   vector<MultiChannelImageT<double> > integralImgs ( imgcounter, MultiChannelImageT<double>() );
-
-
-
+  
   while ( !allleaf && depth < maxDepth )
   {
+    depth++;
 #ifdef DEBUG
     cout << "depth: " << depth << endl;
 #endif
@@ -1479,6 +1483,13 @@ void SemSegContextTree::train ( const MultiDataset *md )
     timer.start();
 #endif
 
+    double weight = computeWeight(depth,maxDepth) - computeWeight(depth-1,maxDepth);
+    
+    if(depth == 1)
+    {
+      weight = computeWeight(1,maxDepth);
+    }
+    
     for ( int tree = 0; tree < nbTrees; tree++ )
     {
       int t = ( int ) forest[tree].size();
@@ -1524,8 +1535,8 @@ void SemSegContextTree::train ( const MultiDataset *md )
             forest[tree][i].right = right;
             forest[tree][left].dist = vector<double> ( classes, 0.0 );
             forest[tree][right].dist = vector<double> ( classes, 0.0 );
-            forest[tree][left].depth = depth + 1;
-            forest[tree][right].depth = depth + 1;
+            forest[tree][left].depth = depth;
+            forest[tree][right].depth = depth;
             forest[tree][left].featcounter = 0;
             forest[tree][right].featcounter = 0;
             forest[tree][left].nodeNumber = uniquenumber;
@@ -1536,8 +1547,6 @@ void SemSegContextTree::train ( const MultiDataset *md )
             uniquenumber++;
             forest[tree][right].featcounter = 0;
 
-
-
 #if 0
             timer.stop();
             cout << "time 3: " << timer.getLast() << endl;
@@ -1574,8 +1583,8 @@ void SemSegContextTree::train ( const MultiDataset *md )
                       if ( labelmap.find ( labels[iCounter] ( x, y ) ) != labelmap.end() )
                         forest[tree][left].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
                       forest[tree][left].featcounter++;
-                      SparseVector v;
-                      v.insert ( pair<int, double> ( leftu, 1.0 ) );
+                      SparseVectorInt v;
+                      v.insert ( pair<int, double> ( leftu, weight ) );
                       textonMap[iCounter] ( subx, suby ).add ( v );
                     }
                     else
@@ -1585,14 +1594,13 @@ void SemSegContextTree::train ( const MultiDataset *md )
                         forest[tree][right].dist[labelmap[labels[iCounter] ( x, y ) ]]++;
                       forest[tree][right].featcounter++;
                       //feld im subsampled finden und in diesem rechts hochzählen
-                      SparseVector v;
-                      v.insert ( pair<int, double> ( rightu, 1.0 ) );
+                      SparseVectorInt v;
+                      v.insert ( pair<int, double> ( rightu, weight ) );
                       textonMap[iCounter] ( subx, suby ).add ( v );
                     }
                   }
                 }
               }
-
             }
 #if 0
             timer.stop();
@@ -1681,7 +1689,7 @@ void SemSegContextTree::train ( const MultiDataset *md )
       timer.start();
 #endif
     }
-
+    
     //compute integral image
     int channels = classes + allfeats[0].channels();
 #if 0
@@ -1717,11 +1725,41 @@ void SemSegContextTree::train ( const MultiDataset *md )
     timer.stop();
 
     cout << "time for depth " << depth << ": " << timer.getLast() << endl;
-
 #endif
-    depth++;
   }
 
+#define WRITEGLOB
+#ifdef WRITEGLOB
+  ofstream outstream("globtrain.feat");
+  
+  for(uint i = 0; i < textonMap.size(); i++)
+  {
+    set<int> classes;
+    for ( int x = 0; x < labels[i].cols(); x++ )
+    {
+      for ( int y = 0; y < labels[i].rows(); y++ )
+      {
+        int classno = labels[i] ( x, y );
+
+        if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
+          continue;
+
+        classes.insert(classno);
+      }
+    }
+    
+    set<int>::iterator it;
+    for ( it=classes.begin() ; it != classes.end(); it++ )
+      outstream << *it << " ";
+    outstream << endl;
+    integralTexton[i](integralTexton[i].width()-1, integralTexton[i].height()-1).store(outstream);
+  }
+  
+  outstream.close();
+  
+#endif
+    cout << "uniquenumber " << uniquenumber << endl;
+    getchar();
 #ifdef DEBUG
   for ( int tree = 0; tree < nbTrees; tree++ )
   {
@@ -1801,8 +1839,8 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
   //TODO: resize image?!
 
   MultiChannelImageT<double> feats;
-  MultiChannelImageT<SparseVector> textonMap ( xsize / grid + 1, ysize / grid + 1, 1 );
-  MultiChannelImageT<SparseVector> integralTexton ( xsize / grid + 1, ysize / grid + 1, 1 );
+  MultiChannelImageT<SparseVectorInt> textonMap ( xsize / grid + 1, ysize / grid + 1, 1 );
+  MultiChannelImageT<SparseVectorInt> integralTexton ( xsize / grid + 1, ysize / grid + 1, 1 );
 
 #ifdef LOCALFEATS
   lfcw->getFeats ( img, feats );
@@ -1836,9 +1874,17 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
 
   for ( int d = 0; d < maxDepth && !allleaf; d++ )
   {
+    depth++;
+    
+    double weight = computeWeight(depth,maxDepth) - computeWeight(depth-1,maxDepth);
+    
+    if(depth == 1)
+    {
+      weight = computeWeight(1,maxDepth);
+    }
+    
     allleaf = true;
-    //TODO vielleicht parallel wenn nächste schleife auch noch parallelsiert würde, die hat mehr gewicht
-    //#pragma omp parallel for
+    
     MultiChannelImageT<unsigned short int> lastfeats = currentfeats;
 
     for ( int tree = 0; tree < nbTrees; tree++ )
@@ -1867,15 +1913,15 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
             if ( val < forest[tree][t].decision )
             {
               currentfeats.set ( x, y, forest[tree][t].left, tree );
-              SparseVector v;
-              v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, 1.0 ) );
+              SparseVectorInt v;
+              v.insert ( pair<int, double> ( forest[tree][forest[tree][t].left].nodeNumber, weight ) );
               textonMap ( subx, suby ).add ( v );
             }
             else
             {
               currentfeats.set ( x, y, forest[tree][t].right, tree );
-              SparseVector v;
-              v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, 1.0 ) );
+              SparseVectorInt v;
+              v.insert ( pair<int, double> ( forest[tree][forest[tree][t].right].nodeNumber, weight ) );
               textonMap ( subx, suby ).add ( v );
             }
 
@@ -1903,9 +1949,6 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
 
     computeIntegralImage ( currentfeats, feats, integralImg );
     computeIntegralImage ( textonMap, integralTexton );
-
-
-    depth++;
   }
 
   cout << forest[0][currentfeats.get ( xpos, ypos, 0 ) ].dist << endl;
@@ -1919,7 +1962,7 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
     {
       for ( int y = 0; y < ysize; y++, offset++ )
       {
-        double maxvalue = - numeric_limits<double>::max(); //TODO: das muss nur pro knoten gemacht werden, nicht pro pixel
+        double maxvalue = - numeric_limits<double>::max(); //TODO: das kann auch nur pro knoten gemacht werden, nicht pro pixel
         int maxindex = 0;
         uint s = forest[0][0].dist.size();
 

+ 1 - 3
semseg/SemSegContextTree.h

@@ -80,8 +80,6 @@ class ValueAccess
     virtual ValueTypes getType() = 0;
 };
 
-
-
 enum OperationTypes {
   MINUS,
   MINUSABS,
@@ -302,7 +300,7 @@ class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
      * @param integralImage output image (must be initilized)
      * @return void
      **/
-    void computeIntegralImage ( const NICE::MultiChannelImageT<SparseVector> &infeats, NICE::MultiChannelImageT<SparseVector> &integralImage );
+    void computeIntegralImage ( const NICE::MultiChannelImageT<SparseVectorInt> &infeats, NICE::MultiChannelImageT<SparseVectorInt> &integralImage );
 
     /**
      * compute best split for current settings