瀏覽代碼

CRSplineReg: Added constructor to specify dimension in which the data should be sorted. Fixed a rare bug, where the prediction-result could become NAN.

Frank Prüfer 11 年之前
父節點
當前提交
bf04610c4b

+ 1 - 1
regression/progs/testSplineRegression.cpp

@@ -206,7 +206,7 @@ void testFrame (  Config conf,
     
 
     cerr << "Initializing NPRegression...";
-    CRSplineReg *spline = new CRSplineReg ();
+    CRSplineReg *spline = new CRSplineReg ( &conf );
     cerr << "Finished." << endl;
     
     cerr << "Teaching the NPRegression algorithm...";

+ 15 - 34
regression/splineregression/CRSplineReg.cpp

@@ -21,9 +21,15 @@ using namespace OBJREC;
 using namespace std;
 using namespace NICE;
 
-CRSplineReg::CRSplineReg ( )
+CRSplineReg::CRSplineReg (  const NICE::Config *_conf )
 {
-  tau = 0.5;
+  tau = _conf->gD("CRSplineReg","tau",0.5);
+  sortDim = _conf->gI("CRSplineReg","sortDim",0);
+}
+
+CRSplineReg::CRSplineReg (  uint sDim )
+{
+  sortDim = sDim;
 }
 
 CRSplineReg::CRSplineReg ( const CRSplineReg & src ) : RegressionAlgorithm ( src )
@@ -31,6 +37,7 @@ CRSplineReg::CRSplineReg ( const CRSplineReg & src ) : RegressionAlgorithm ( src
   tau = src.tau;
   dataSet = src.dataSet;
   labelSet = src.labelSet;
+  sortDim = src.sortDim;
 }
 
 CRSplineReg::~CRSplineReg()
@@ -76,7 +83,6 @@ double CRSplineReg::predict ( const NICE::Vector & x )
     exit(-1);
   }
   int dimension = dataSet[0].size();
-  int sortDim = 0;
 
   FullVector data ( dataSet.size()+1 );
   
@@ -150,36 +156,10 @@ double CRSplineReg::predict ( const NICE::Vector & x )
     points(3,dimension) = labelSet[sortedInd[index-1]];     
   }
 
-//   NICE::Vector P(dimension);
-//   double y;
-//   double diff,bestT;
-//   double bestDiff = std::numeric_limits<double>::max();
-//   double b0,b1,b2,b3;
-// 
-//   for ( double t = 0.0; t <= 1.0; t += 0.02 ){
-//     b0 = tau * (-(t*t*t) + 2*t*t - t);
-//     b1 = tau * (3*t*t*t - 5*t*t + 2);
-//     b2 = tau * (-3*t*t*t + 4*t*t + t);
-//     b3 = tau * (t*t*t - t*t);    
-// 
-// #pragma omp parallel for  
-//     for ( uint i = 0; i < dimension; i++ ){
-//       P[i] = b0*points(0,i) + b1*points(1,i) + b2*points(2,i) + b3*points(3,i);
-//     }
-//     
-//     diff = (P-x).normL2();
-//     if ( diff < bestDiff ){
-//       bestDiff = diff;
-//       bestT = t;
-//     }
-//   }
-//   b0 = tau * (-(bestT*bestT*bestT) + 2*bestT*bestT - bestT);
-//   b1 = tau * (3*bestT*bestT*bestT - 5*bestT*bestT + 2);
-//   b2 = tau * (-3*bestT*bestT*bestT + 4*bestT*bestT + bestT);
-//   b3 = tau * (bestT*bestT*bestT - bestT*bestT); 
-
-  double t = (x[sortDim]-points(1,sortDim)) / (points(2,sortDim)-points(1,sortDim));
-//   cerr<<"t: "<<t<<endl;
+  double t = (x[sortDim]-points(1,sortDim)) / (points(2,sortDim)-points(1,sortDim));	//this is just some kind of heuristic
+  if ( t != t || t < 0 || t > 1){	//check if t is NAN, -inf or inf (happens in the farthest right or left case from above)
+    t = 0.5;
+  }
 
   //P(t) = b0*P0 + b1*P1 + b2*P2 + b3*P3    
   NICE::Vector P(dimension);
@@ -198,7 +178,7 @@ double CRSplineReg::predict ( const NICE::Vector & x )
   
   double diff1 = (P-x).normL2();
   uint counter = 1;
-  while ( diff1 > 1e-5 && counter <= 21){
+  while ( diff1 > 1e-5 && counter <= 21){	//adjust t to fit data better
     double tmp = t;;
     if (tmp > 0.5)
       tmp = 1 - tmp;
@@ -232,6 +212,7 @@ double CRSplineReg::predict ( const NICE::Vector & x )
   }
   
   y = b0*points(0,dimension) + b1*points(1,dimension) + b2*points(2,dimension) + b3*points(3,dimension);
+
   return y;
   
 }

+ 7 - 1
regression/splineregression/CRSplineReg.h

@@ -24,6 +24,9 @@ class CRSplineReg : public RegressionAlgorithm
     /** smoothness parameter */
     double tau;
     
+    /** dimension which is used for sorting the data (maybe use something like PCA to determine this variable) */
+    uint sortDim;
+    
     /** set of data points */
     NICE::VVector dataSet;
     
@@ -32,7 +35,10 @@ class CRSplineReg : public RegressionAlgorithm
   
   public:
     /** simple constructor */
-    CRSplineReg();
+    CRSplineReg( const NICE::Config *_conf );
+    
+    /** simple constructor specifying in which dimension data should be sorted*/
+    CRSplineReg( uint sDim );
     
     /** copy constructor */
     CRSplineReg ( const CRSplineReg & src );