浏览代码

context fixed

Bjoern Froehlich 13 年之前
父节点
当前提交
b1c51741dd
共有 2 个文件被更改,包括 60 次插入53 次删除
  1. 58 51
      semseg/SemSegContextTree.cpp
  2. 2 2
      semseg/SemSegContextTree.h

+ 58 - 51
semseg/SemSegContextTree.cpp

@@ -10,7 +10,6 @@
 #include "core/basics/numerictools.h"
 #include "core/basics/numerictools.h"
 
 
 #include <omp.h>
 #include <omp.h>
-
 #include <iostream>
 #include <iostream>
 
 
 #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
 #define BOUND(x,min,max) (((x)<(min))?(min):((x)>(max)?(max):(x)))
@@ -326,6 +325,9 @@ SemSegContextTree::SemSegContextTree( const Config *conf, const MultiDataset *md
 	string segmentationtype = conf->gS(section, "segmentation_type", "meanshift");
 	string segmentationtype = conf->gS(section, "segmentation_type", "meanshift");
 	
 	
 	useGaussian = conf->gB(section, "use_gaussian", true);
 	useGaussian = conf->gB(section, "use_gaussian", true);
+
+	if(useGaussian)
+		throw("there something wrong with using gaussian! first fix it!");
 	
 	
 	pixelWiseLabeling = false;
 	pixelWiseLabeling = false;
 	
 	
@@ -370,7 +372,7 @@ SemSegContextTree::~SemSegContextTree()
 {
 {
 }
 }
 
 
-void SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &feats, vector<vector<vector<int> > > &currentfeats, vector<MultiChannelImageT<double> > &integralImgs, const vector<vector<vector<int> > > &labels, int node, Operation *&splitop, double &splitval)
+double SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &feats, vector<vector<vector<int> > > &currentfeats, vector<MultiChannelImageT<double> > &integralImgs, const vector<vector<vector<int> > > &labels, int node, Operation *&splitop, double &splitval)
 {
 {
 
 
 	int imgCount = 0, featdim = 0;
 	int imgCount = 0, featdim = 0;
@@ -411,7 +413,7 @@ void SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &
 	if(featcounter < minFeats)
 	if(featcounter < minFeats)
 	{
 	{
 		cout << "only " << featcounter << " feats in current node -> it's a leaf" << endl;
 		cout << "only " << featcounter << " feats in current node -> it's a leaf" << endl;
-		return;
+		return 0.0;
 	}
 	}
 	
 	
 	vector<double> fraction(a.size(),0.0);
 	vector<double> fraction(a.size(),0.0);
@@ -453,7 +455,6 @@ void SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &
 			}
 			}
 		}
 		}
 	}
 	}
-
 	//cout << "size: " << selFeats.size() << endl;
 	//cout << "size: " << selFeats.size() << endl;
 	//getchar();
 	//getchar();
 	
 	
@@ -470,36 +471,42 @@ void SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &
 	if(globent < 0.5)
 	if(globent < 0.5)
 	{
 	{
 		cout << "globent to small: " << globent << endl;
 		cout << "globent to small: " << globent << endl;
-		return;
+		return 0.0;
 	}
 	}
 
 
-	int classes = (int)labelmap.size();
+	int classes = (int)tree[0].dist.size();
 	featsel.clear();
 	featsel.clear();
 	for(int i = 0; i < featsPerSplit; i++)
 	for(int i = 0; i < featsPerSplit; i++)
 	{
 	{
 		int x1, x2, y1, y2;
 		int x1, x2, y1, y2;
+		int ft = (int)((double)rand()/(double)RAND_MAX*(double)ftypes);
+		
+		int tmpws = windowSize;
+		
+		if(integralImgs[0].width() == 0)
+			ft = 0;
+		
+		if(ft > 0)
+		{
+			tmpws *= 2;
+		}
 		
 		
 		if(useGaussian)
 		if(useGaussian)
 		{
 		{
-			double sigma = (double)windowSize/2.0;
-			x1 = randGaussDouble(sigma)*(double)windowSize;
-			x2 = randGaussDouble(sigma)*(double)windowSize;
-			y1 = randGaussDouble(sigma)*(double)windowSize;
-			y2 = randGaussDouble(sigma)*(double)windowSize;
+			double sigma = (double)tmpws/2.0;
+			x1 = randGaussDouble(sigma)*(double)tmpws;
+			x2 = randGaussDouble(sigma)*(double)tmpws;
+			y1 = randGaussDouble(sigma)*(double)tmpws;
+			y2 = randGaussDouble(sigma)*(double)tmpws;
 		}
 		}
 		else
 		else
 		{
 		{
-			x1 = (int)((double)rand()/(double)RAND_MAX*(double)windowSize)-windowSize/2;
-			x2 = (int)((double)rand()/(double)RAND_MAX*(double)windowSize)-windowSize/2;
-			y1 = (int)((double)rand()/(double)RAND_MAX*(double)windowSize)-windowSize/2;
-			y2 = (int)((double)rand()/(double)RAND_MAX*(double)windowSize)-windowSize/2;
+			x1 = (int)((double)rand()/(double)RAND_MAX*(double)tmpws)-tmpws/2;
+			x2 = (int)((double)rand()/(double)RAND_MAX*(double)tmpws)-tmpws/2;
+			y1 = (int)((double)rand()/(double)RAND_MAX*(double)tmpws)-tmpws/2;
+			y2 = (int)((double)rand()/(double)RAND_MAX*(double)tmpws)-tmpws/2;
 		}
 		}
-		
-		int ft = (int)((double)rand()/(double)RAND_MAX*(double)ftypes);
-
-		if(integralImgs[0].width() == 0)
-			ft = 0;
-		
+				
 		if(ft == 0)
 		if(ft == 0)
 		{
 		{
 			int f1 = (int)((double)rand()/(double)RAND_MAX*(double)featdim);
 			int f1 = (int)((double)rand()/(double)RAND_MAX*(double)featdim);
@@ -616,6 +623,7 @@ void SemSegContextTree::getBestSplit(const vector<MultiChannelImageT<double> > &
 #ifdef debug
 #ifdef debug
 	cout << "globent: " << globent <<  " bestig " << bestig << " splitval: " << splitval << endl;
 	cout << "globent: " << globent <<  " bestig " << bestig << " splitval: " << splitval << endl;
 #endif
 #endif
+	return bestig;
 }
 }
 
 
 void SemSegContextTree::computeIntegralImage(const vector<vector<int> > &currentfeats, MultiChannelImageT<double> &integralImage)
 void SemSegContextTree::computeIntegralImage(const vector<vector<int> > &currentfeats, MultiChannelImageT<double> &integralImage)
@@ -629,17 +637,18 @@ void SemSegContextTree::computeIntegralImage(const vector<vector<int> > &current
 	for(int c = 0; c < channels; c++)
 	for(int c = 0; c < channels; c++)
 	{
 	{
 		integralImage.set(0,0,tree[currentfeats[0][0]].dist[c], c);
 		integralImage.set(0,0,tree[currentfeats[0][0]].dist[c], c);
+
 		
 		
 		//first column
 		//first column
 		for(int y = 1; y < ysize; y++)
 		for(int y = 1; y < ysize; y++)
 		{
 		{
-			integralImage.set(0,0,tree[currentfeats[0][y]].dist[c]+integralImage.get(0,y,c), c);
+			integralImage.set(0,y,tree[currentfeats[0][y]].dist[c]+integralImage.get(0,y,c), c);
 		}
 		}
 		
 		
 		//first row
 		//first row
 		for(int x = 1; x < xsize; x++)
 		for(int x = 1; x < xsize; x++)
 		{
 		{
-			integralImage.set(0,0,tree[currentfeats[x][0]].dist[c]+integralImage.get(x,0,c), c);
+			integralImage.set(x,0,tree[currentfeats[x][0]].dist[c]+integralImage.get(x,0,c), c);
 		}
 		}
 		
 		
 		//rest
 		//rest
@@ -648,7 +657,7 @@ void SemSegContextTree::computeIntegralImage(const vector<vector<int> > &current
 			for(int x = 1; x < xsize; x++)
 			for(int x = 1; x < xsize; x++)
 			{
 			{
 				double val = tree[currentfeats[x][y]].dist[c]+integralImage.get(x,y-1,c)+integralImage.get(x-1,y,c)-integralImage.get(x-1,y-1,c);
 				double val = tree[currentfeats[x][y]].dist[c]+integralImage.get(x,y-1,c)+integralImage.get(x-1,y,c)-integralImage.get(x-1,y-1,c);
-				integralImage.set(0, 0, val, c);
+				integralImage.set(x, y, val, c);
 			}
 			}
 		}
 		}
 	}
 	}
@@ -808,13 +817,11 @@ void SemSegContextTree::train ( const MultiDataset *md )
 	while(!allleaf && depth < maxDepth)
 	while(!allleaf && depth < maxDepth)
 	{
 	{
 		allleaf = true;
 		allleaf = true;
-		//TODO vielleicht parallel wenn nächste schleife trotzdem noch parallelsiert würde, die hat mehr gewicht
-
 		int t = (int) tree.size();
 		int t = (int) tree.size();
 		int s = startnode;
 		int s = startnode;
 		startnode = t;
 		startnode = t;
 		vector<vector<vector<int> > > lastfeats = currentfeats;
 		vector<vector<vector<int> > > lastfeats = currentfeats;
-		
+//TODO vielleicht parallel wenn nächste schleife trotzdem noch parallelsiert würde, die hat mehr gewicht		
 //#pragma omp parallel for
 //#pragma omp parallel for
 		for(int i = s; i < t; i++)
 		for(int i = s; i < t; i++)
 		{
 		{
@@ -823,9 +830,8 @@ void SemSegContextTree::train ( const MultiDataset *md )
 				Operation *splitfeat = NULL;
 				Operation *splitfeat = NULL;
 				double splitval;
 				double splitval;
 				
 				
-				getBestSplit(allfeats, lastfeats, integralImgs, labels, i, splitfeat, splitval);
+				double bestig = getBestSplit(allfeats, lastfeats, integralImgs, labels, i, splitfeat, splitval);
 				tree[i].feat = splitfeat;
 				tree[i].feat = splitfeat;
-				
 				tree[i].decision = splitval;
 				tree[i].decision = splitval;
 				
 				
 				if(splitfeat != NULL)
 				if(splitfeat != NULL)
@@ -841,6 +847,7 @@ void SemSegContextTree::train ( const MultiDataset *md )
 					tree[right].dist = vector<double>(classes, 0.0);
 					tree[right].dist = vector<double>(classes, 0.0);
 					tree[left].depth = depth+1;
 					tree[left].depth = depth+1;
 					tree[right].depth = depth+1;
 					tree[right].depth = depth+1;
+					
 #pragma omp parallel for
 #pragma omp parallel for
 					for(int iCounter = 0; iCounter < imgcounter; iCounter++)
 					for(int iCounter = 0; iCounter < imgcounter; iCounter++)
 					{
 					{
@@ -888,7 +895,27 @@ void SemSegContextTree::train ( const MultiDataset *md )
 					if(lcounter <= 0 || rcounter <= 0)
 					if(lcounter <= 0 || rcounter <= 0)
 					{
 					{
 						cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
 						cout << "lcounter : " << lcounter << " rcounter: " << rcounter << endl;
-						cout << "splitval: " << splitval << endl;
+						cout << "splitval: " << splitval << " splittype: " << splitfeat->writeInfos() << endl;
+						cout << "bestig: " << bestig << endl;
+						for(int iCounter = 0; iCounter < imgcounter; iCounter++)
+						{
+							int xsize = currentfeats[iCounter].size();
+							int ysize = currentfeats[iCounter][0].size();
+							int counter = 0;
+							for(int x = 0; x < xsize; x++)
+							{
+								for(int y = 0; y < ysize; y++)
+								{
+									if(lastfeats[iCounter][x][y] == i)
+									{
+										if(++counter > 30)
+											break;
+										double val = splitfeat->getVal(allfeats[iCounter],lastfeats[iCounter], tree, integralImgs[iCounter],x,y);
+										cout << "splitval: " << splitval << " val: " << val << endl;
+									}
+								}
+							}
+						}
 						assert(lcounter > 0 && rcounter > 0);
 						assert(lcounter > 0 && rcounter > 0);
 					}
 					}
 					for(uint d = 0; d < tree[left].dist.size(); d++)
 					for(uint d = 0; d < tree[left].dist.size(); d++)
@@ -924,16 +951,6 @@ void SemSegContextTree::train ( const MultiDataset *md )
 			computeIntegralImage(currentfeats[i],integralImgs[i]);
 			computeIntegralImage(currentfeats[i],integralImgs[i]);
 		}
 		}
 		
 		
-		if(depth == 4)
-		{
-			cout << "learn: ";
-			for(int x = 20; x < 30; x++)
-			{
-				cout << currentfeats[0][x][20] << " ";
-			}
-			cout << endl;
-		}
-		
 		depth++;
 		depth++;
 #ifdef DEBUG
 #ifdef DEBUG
 		cout << "depth: " << depth << endl;
 		cout << "depth: " << depth << endl;
@@ -1046,17 +1063,7 @@ void SemSegContextTree::semanticseg ( CachedExample *ce, NICE::Image & segresult
 		}
 		}
 		
 		
 		computeIntegralImage(currentfeats,integralImg);
 		computeIntegralImage(currentfeats,integralImg);
-		
-		if(depth == 4)
-		{
-			cout << "learn: ";
-			for(int x = 20; x < 30; x++)
-			{
-				cout << currentfeats[x][20] << " ";
-			}
-			cout << endl;
-		}
-		
+				
 		depth++;
 		depth++;
 	}
 	}
 	
 	

+ 2 - 2
semseg/SemSegContextTree.h

@@ -150,7 +150,6 @@ class SemSegContextTree : public SemanticSegmentation
 	
 	
 	/** use Gaussian distributed features based on the feature position */
 	/** use Gaussian distributed features based on the feature position */
 	bool useGaussian;
 	bool useGaussian;
-
 	
 	
     public:
     public:
 	/** simple constructor */
 	/** simple constructor */
@@ -191,8 +190,9 @@ class SemSegContextTree : public SemanticSegmentation
 	 * @param node current node
 	 * @param node current node
 	 * @param splitfeat output feature position
 	 * @param splitfeat output feature position
 	 * @param splitval 
 	 * @param splitval 
+	 * @return best information gain
 	 */
 	 */
-	void getBestSplit(const std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<std::vector<std::vector<int> > > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<std::vector<std::vector<int> > > &labels, int node, Operation *&splitfeat, double &splitval);
+	double getBestSplit(const std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<std::vector<std::vector<int> > > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<std::vector<std::vector<int> > > &labels, int node, Operation *&splitfeat, double &splitval);
 
 
 };
 };