瀏覽代碼

latest changes on RANSACReg and CRSplineReg by Frank

Sven Sickert 11 年之前
父節點
當前提交
e25694971a

+ 12 - 16
regression/linregression/RANSACReg.cpp

@@ -10,6 +10,7 @@
 #endif
 
 #include <iostream>
+#include <ctime>
 
 #include "vislearning/regression/linregression/LinRegression.h"
 #include "vislearning/regression/linregression/RANSACReg.h"
@@ -21,6 +22,8 @@ using namespace NICE;
 
 RANSACReg::RANSACReg ( const Config *_conf )
 {
+  if ( _conf->gB("RANSACReg","start_random_generator" ) )
+    std::srand ( unsigned ( std::time(0) ) );
   threshold = _conf->gD("RANSACReg","threshold",0.5);
   iter = _conf->gI("RANSACReg","iterations",10);
 }
@@ -39,22 +42,16 @@ RANSACReg::~RANSACReg()
 {
 }
 
-void RANSACReg::teach ( const NICE::VVector & dataSet, const NICE::Vector & labelSet )
+RANSACReg* RANSACReg::clone ( void ) const
 {
-  //for iter iterations do
-    //choose random subset of n points (n = dataSet[0].size()+1)
-    //do LinRegression on subset
-    //get modelParameters
-    //test how many points, which are not in subset, are close to model (use threshold and distancefunc here) -> these points are consneus set
-    //if consensus set contains more points than any previous one, take this model as best_model
-  //maybe compute best_model again with all points of best_consensusSet  
-  //store best_model and maybe best_consensusSet
-  
+  return new RANSACReg(*this);
+}
+
+void RANSACReg::teach ( const NICE::VVector & dataSet, const NICE::Vector & labelSet )
+{ 
   NICE::VVector best_CS(0,0);
   std::vector<double> best_labelCS;
   
-  cerr<<"Size of training data: "<<dataSet.size()<<endl;
-  
   vector<int> indices;
   for ( uint i = 0; i < dataSet.size(); i++ )
     indices.push_back(i);
@@ -78,6 +75,7 @@ void RANSACReg::teach ( const NICE::VVector & dataSet, const NICE::Vector & labe
     NICE::VVector current_CS;
     std::vector<double> current_labelCS;
     
+#pragma omp parallel for    
     for ( uint j = n; j < indices.size(); j++ ){	//compute distance between each datapoint and current model
       double lengthNormalVector = 0; 
       double sum = 0;
@@ -87,10 +85,9 @@ void RANSACReg::teach ( const NICE::VVector & dataSet, const NICE::Vector & labe
       }
       lengthNormalVector = sqrt(lengthNormalVector);
       
-      double distance = ( sum - labelSet[indices[j]] )/ lengthNormalVector;
-//       cerr<<"distance: "<<distance<<endl;
-      
+      double distance = ( sum - labelSet[indices[j]] ) / lengthNormalVector;
 
+#pragma omp critical
       if ( abs(distance) < threshold ){	//if point is close to model, it belongs to consensus set
 	current_CS.push_back ( dataSet[indices[j]] );
 	current_labelCS.push_back ( labelSet[indices[j]] );
@@ -103,7 +100,6 @@ void RANSACReg::teach ( const NICE::VVector & dataSet, const NICE::Vector & labe
     }
   }
   
-  cerr<<"Size of best_CS: "<<best_CS.size()<<endl;
   LinRegression *best_linReg = new LinRegression ();	//compute best_model again with all points of best_consensusSet
   best_linReg->teach ( best_CS, (NICE::Vector)best_labelCS );
   modelParams = best_linReg->getModelParams();    

+ 3 - 1
regression/linregression/RANSACReg.h

@@ -9,7 +9,6 @@
 #define RANSACREGINCLUDE
 
 #include "core/vector/VectorT.h"
-#include "core/vector/VVector.h"
 #include "core/vector/MatrixT.h"
 
 #include "core/basics/Config.h"
@@ -50,6 +49,9 @@ class RANSACReg : public RegressionAlgorithm
     /** simple destructor */
     virtual ~RANSACReg();
     
+    /** clone function */
+    RANSACReg* clone (void) const;
+    
     /** predict response using simple vector */
     double predict ( const NICE::Vector & x );
     

+ 5 - 0
regression/splineregression/CRSplineReg.cpp

@@ -44,6 +44,11 @@ CRSplineReg::~CRSplineReg()
 {
 }
 
+CRSplineReg* CRSplineReg::clone ( void ) const
+{
+  return new CRSplineReg(*this);
+}
+
 void CRSplineReg::teach ( const NICE::VVector & _dataSet, const NICE::Vector & _labelSet)
 {
     fprintf (stderr, "teach using all !\n");

+ 3 - 0
regression/splineregression/CRSplineReg.h

@@ -46,6 +46,9 @@ class CRSplineReg : public RegressionAlgorithm
     /** simple destructor */
     virtual ~CRSplineReg();
     
+    /** clone function */
+    CRSplineReg* clone (void) const;    
+    
     /** predict response using simple vector */
     double predict ( const NICE::Vector & x );