Selaa lähdekoodia

converted files to new persistent-layout of gp-hik-core

Alexander Freytag 11 vuotta sitten
vanhempi
commit
391bb3ad04

+ 2 - 2
GPHIKClassifierNICE.cpp

@@ -23,7 +23,7 @@ void GPHIKClassifierNICE::init ( const NICE::Config *conf, const std::string & s
   this->verbose = conf->gB( s_confSection, "verbose", false );
   this->useSimpleBalancing = conf->gB( s_confSection, "use_simple_balancing", false );
   this->minSamples = conf->gI( s_confSection, "min_samples", -1 );
-  this->performOptimizationAfterIncrement = conf->gB( s_confSection, "performOptimizationAfterIncrement", true );
+  this->performOptimizationAfterIncrement = conf->gB( s_confSection, "performOptimizationAfterIncrement", false );
   
   this->classifier = new GPHIKClassifier(conf, s_confSection);
 }
@@ -314,7 +314,7 @@ void GPHIKClassifierNICE::addMultipleExamples( Examples & newExamples)
     return;
   
   // (multi-class) label vector
-  Vector y ( newExamples.size() );
+  NICE::Vector y ( newExamples.size() );
 
   // flat structure of our training data
   std::vector< const SparseVector * > sparseExamples;

+ 9 - 30
progs/eccv2012-15scenes.cpp

@@ -1,27 +1,27 @@
 /** 
 * @file eccv2012-15scenes.cpp
 * @brief ECCV 2012 Experiment with 15 Scenes
-* @author Erik Rodner
+* @author Erik Rodner, Alexander Freytag
 * @date 01/17/2012
-
 */
 
+// STL includes
 #include <vector>
 
+// NICE-core includes
 #include <core/basics/vectorio.h>
 #include <core/basics/Timer.h>
 #include <core/basics/Config.h>
 
-#include <vislearning/cbaselib/MultiDataset.h>
+// NICE-vislearning includes
 #include <vislearning/baselib/Globals.h>
+// 
+#include <vislearning/cbaselib/MultiDataset.h>
 
+
+// gp-hik-core includes
 #include <gp-hik-core/FastMinKernel.h>
 #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
-#include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
-#include <gp-hik-core/parameterizedFunctions/PFWeightedDim.h>
-#include <gp-hik-core/parameterizedFunctions/PFExp.h>
-#include <gp-hik-core/parameterizedFunctions/PFMKL.h>
-
 #include <gp-hik-core/tools.h>
 
 using namespace std;
@@ -43,27 +43,6 @@ int main (int argc, char **argv)
 
   Config conf ( argc, argv );
  
-  string pf_s = conf.gS("main", "transform", "absexp");
-  ParameterizedFunction *pf;
-
-  double parameterLowerBound = conf.gD("GPHIKClassifier", "parameter_lower_bound", 1.0 );
-  double parameterUpperBound = conf.gD("GPHIKClassifier", "parameter_upper_bound", 5.0 );
-  if ( pf_s == "absexp" )
-    pf = new PFAbsExp( 1.0, parameterLowerBound, parameterUpperBound );
-  else if ( pf_s == "exp" )
-    pf = new PFExp ( 1.0, parameterLowerBound, parameterUpperBound );
-  else if ( pf_s == "weighted" )
-    pf = new PFWeightedDim ( conf.gI("main", "dimension"), 0.0, 5.0 );
-  else if ( pf_s == "MKL" ) {
-      std::cerr << "use MKL feature transformation specific for VISAPP experiments" << std::endl;
-      std::set<int> steps; steps.insert(4000); steps.insert(6000); //specific for VISAPP
-      pf = new PFMKL( steps, parameterLowerBound, parameterUpperBound );
-    }
-  else
-    fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");
-
-  cerr << "Transformation type: " << pf_s << endl;
-
   string ext = conf.gS("main", "ext", ".txt"); 
   cerr << "Using cache extension: " << ext << endl;
 
@@ -94,7 +73,7 @@ int main (int argc, char **argv)
   FastMinKernel *fmk = new FastMinKernel ( trainData, noise );
 
 
-  FMKGPHyperparameterOptimization hyper ( &conf, pf, fmk );
+  FMKGPHyperparameterOptimization hyper ( &conf, fmk );
 
   hyper.optimize ( y );
  

+ 14 - 15
progs/eccv2012-AwA.cpp

@@ -44,31 +44,31 @@ int main (int argc, char **argv)
 {   
   std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
 
-  Config conf ( argc, argv );
+  NICE::Config conf ( argc, argv );
  
   string pf_s = conf.gS("main", "transform", "absexp");
   int nrRuns = conf.gI("main", "nrRuns", 1);
   int dim = conf.gI("main", "dim", 2000);
-  ParameterizedFunction *pf;
-
-  double parameterLowerBound = conf.gI("HIKGP", "parameter_lower_bound", 1.0 );
-  double parameterUpperBound = conf.gI("HIKGP", "parameter_upper_bound", 5.0 );
+  
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_upper_bound", 5.0 );
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_lower_bound", 1.0 );
+  
   if ( pf_s == "absexp" )
-    pf = new PFAbsExp( 1.0, parameterLowerBound, parameterUpperBound );
+    conf.sS( "FMKGPHyperparameterOptimization", "transform", "absexp" );
   else if ( pf_s == "exp" )
-    pf = new PFExp ( 1.0, parameterLowerBound, parameterUpperBound );
+     conf.sS( "FMKGPHyperparameterOptimization", "transform", "exp" );
   else
-    fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");
+    fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");  
   
-  cerr << "Transformation type: " << pf_s << endl;
+  std::cerr << "Transformation type: " << pf_s << std::endl;
 
-  string ext = conf.gS("main", "ext", ".txt"); 
-  cerr << "Using cache extension: " << ext << endl;
+  std::string ext = conf.gS("main", "ext", ".txt"); 
+  std::cerr << "Using cache extension: " << ext << std::endl;
 
   
   // read training set
-  vector< vector<double> > trainData;
-  Vector y;
+  std::vector< std::vector<double> > trainData;
+  NICE::Vector y;
   
   double AARR(0.0); // averaged average recognition rate :)
   
@@ -97,7 +97,7 @@ int main (int argc, char **argv)
     FastMinKernel *fmk = new FastMinKernel ( trainData, noise, dim );
 
 
-    FMKGPHyperparameterOptimization hyper ( &conf, pf, fmk );
+    FMKGPHyperparameterOptimization hyper ( &conf, fmk );
 
     hyper.optimize ( y );
     
@@ -157,6 +157,5 @@ int main (int argc, char **argv)
   
   std::cerr << "final averaged recognition rate: " << AARR << std::endl;
 
-  delete pf;
   return 0;
 }

+ 27 - 19
progs/eccv2012-synthetic.cpp

@@ -5,27 +5,34 @@
 * @date 17-02-2012 (dd-mm-yyyy)
 */
 
+// STL includes
 #include <vector>
 #include <fstream>
 #include <iostream>
 #include <sstream>
 #include <limits>
 
+// NICE-core includes
 #include <core/basics/vectorio.h>
 #include <core/basics/Config.h>
 #include <core/basics/numerictools.h>
 #include <core/basics/Timer.h>
+// 
 #include <core/image/Histogram.h>
+// 
 #include <core/vector/VectorT.h>
 
+// NICE-vislearning includes
 #include <vislearning/cbaselib/ClassificationResults.h>
 
+// gp-hik-core includes
 #include <gp-hik-core/FastMinKernel.h>
 #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
+// 
 #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
 #include <gp-hik-core/parameterizedFunctions/PFExp.h>
 #include <gp-hik-core/parameterizedFunctions/PFWeightedDim.h>
-
+// 
 #include <gp-hik-core/tools.h>
 
 
@@ -184,25 +191,25 @@ int main (int argc, char **argv)
   
   bool printRandomDistribution = conf.gB("main", "printRandomDistribution", false);
   
-  int dim(8);
-  double weightsLowerBound(numeric_limits<double>::min( ));
-  double weightsUpperBound(numeric_limits<double>::max( ));
-//   double weightsLowerBound(-1.0 * weightsUpperBound);
-   
-  ParameterizedFunction *pfBaseline;
-  ParameterizedFunction *pfFeatRel;
-
+  
+  confBaseline.sD( "FMKGPHyperparameterOptimization", "parameter_upper_bound", numeric_limits<double>::max( ) );
+  confBaseline.sD( "FMKGPHyperparameterOptimization", "parameter_lower_bound", numeric_limits<double>::min( ) );
+  
   if ( pf_baseline_s == "absexp" )
-    pfBaseline = new PFAbsExp();
+    confBaseline.sS( "FMKGPHyperparameterOptimization", "transform", "absexp" );
   else if ( pf_baseline_s == "exp" )
-    pfBaseline = new PFExp();
+     confBaseline.sS( "FMKGPHyperparameterOptimization", "transform", "exp" );
   else
-    fthrow(Exception, "Parameterized function type " << pf_baseline_s << " not yet implemented");
+    fthrow(Exception, "Parameterized function type " << pf_baseline_s << " not yet implemented");  
   
-  if ( pf_featRel_s == "weightedDim" )
-    pfFeatRel = new PFWeightedDim(dim,weightsLowerBound,weightsUpperBound);//(pfBaseline);
-  else 
-    fthrow(Exception, "Parameterized function type " << pf_featRel_s << " not yet implemented");
+ 
+  
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_upper_bound", numeric_limits<double>::max( ) );
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_lower_bound", numeric_limits<double>::min( ) );  
+  conf.sS( "FMKGPHyperparameterOptimization", "transform", "weightedDim" );
+  int dim ( 8 );
+  conf.sI( "FMKGPHyperparameterOptimization", "pf_dim", dim );
+
   
   std::cerr << "Transformation type baseline: " << pf_baseline_s << std::endl;
   std::cerr << "Transformation type FeatRel: " << pf_featRel_s << std::endl;
@@ -298,8 +305,9 @@ int main (int argc, char **argv)
      
       //baseline without feature relevance
       double noise = 0.1;
-      FastMinKernel *fmkBaseline = new FastMinKernel ( trainData, noise, dim );
-      FMKGPHyperparameterOptimization hyperBaseline ( &confBaseline, pfBaseline, fmkBaseline );
+      FastMinKernel *fmkBaseline = new FastMinKernel ( trainData, noise );    
+  
+      FMKGPHyperparameterOptimization hyperBaseline ( &confBaseline,  fmkBaseline );
       hyperBaseline.optimize ( yTrain );
       
       //with optimization of feature relevance (= optimization of weights for each dimension)
@@ -310,7 +318,7 @@ int main (int argc, char **argv)
 //       pfFeatRel->applyFunctionToFeatureMatrix(fmkFeatRel->featureMatrix());
 //       fmkFeatRel->featureMatrix().print();
       
-      FMKGPHyperparameterOptimization hyperFeatRel ( &conf, pfFeatRel, fmkFeatRel );
+      FMKGPHyperparameterOptimization hyperFeatRel ( &conf, fmkFeatRel, "FMKGPHyperparameterOptimization" /*config section*/ );
       hyperFeatRel.optimize ( yTrain );
       std::cerr << "meanValues: ";
       for (std::vector<double>::const_iterator meanIt = meanValues.begin(); meanIt != meanValues.end(); meanIt++)

+ 16 - 9
progs/testImageNetBinary.cpp

@@ -43,20 +43,29 @@ int main (int argc, char **argv)
 
   cerr << "Positive class is " << positiveClass << endl;
   
-  ParameterizedFunction *pf;
   
-  string pf_s = conf.gS("main", "transform", "absexp");
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_upper_bound", 5.0 );
+  conf.sD( "FMKGPHyperparameterOptimization", "parameter_lower_bound", 1.0 );
+  
+  std::string pf_s = conf.gS("main", "transform", "absexp");
+  
   if ( pf_s == "absexp" )
-    pf = new PFAbsExp( 1.0 );
+    conf.sS( "FMKGPHyperparameterOptimization", "transform", "absexp" );
   else if ( pf_s == "exp" )
-    pf = new PFExp ( 1.0 );
+     conf.sS( "FMKGPHyperparameterOptimization", "transform", "exp" );
   else if ( pf_s == "weighted" )
-    pf = new PFWeightedDim ( conf.gI("main", "dimension"), 0.0, 5.0 );
+  {
+    conf.sS( "FMKGPHyperparameterOptimization", "transform", "weightedDim" );
+    conf.sI( "FMKGPHyperparameterOptimization", "pf_dim", conf.gI("main", "dimension") );
+  }
   else
-    fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");
+    fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");  
+  
+  std::cerr << "Transformation type: " << pf_s << std::endl;
+  
 
   double noise = conf.gD("GPHIKClassifier", "noise", 0.1);
-  FMKGPHyperparameterOptimization hypopt ( &conf, pf ); 
+  FMKGPHyperparameterOptimization hypopt ( &conf ); 
 
   sparse_t data;
   NICE::Vector y;
@@ -137,8 +146,6 @@ int main (int argc, char **argv)
   double perfvalue = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
 
   cerr << "Performance: " << perfvalue << endl;
-
-  delete pf;
   
   return 0;
 }

+ 61 - 41
tests/TestGPHIKClassifier.cpp

@@ -185,7 +185,11 @@ void myClassifierStoreRestoreTest( GPHIKClassifierNICE & classifier, const Matri
 
 }
 
-void myClassifierILTest( GPHIKClassifierNICE & classifierRetrain, GPHIKClassifierNICE & classifierIL, const Matrix & mX, const Vector & vY )
+void myClassifierILTest( OBJREC::GPHIKClassifierNICE & classifierRetrain,
+                         OBJREC::GPHIKClassifierNICE & classifierIL,
+                         const Matrix & mX,
+                         const Vector & vY
+                       )
 {
  
   if (verboseStartEnd)
@@ -220,58 +224,72 @@ void myClassifierILTest( GPHIKClassifierNICE & classifierRetrain, GPHIKClassifie
 
   if ( verbose ) 
     std::cerr << "learning ..." << std::endl;
+  Timer t;
+  t.start();
   classifierIL.train ( fp, examples ); 
+  t.stop();
+  std::cerr << "Time used for initial training: " << t.getLast() << std::endl;  
   
   //choose next example(s)
   
+  int i_numExamplesToAdd ( 2 );
+  
   Examples newExamples;
   for ( uint i = 0 ; i < vY.size() ; i++ )
   {
     if ( i % 4 == 3 )
     {
       Example example;
-      example.svec = new SparseVector;
+      example.svec = new SparseVector();
       example.svec->setDim(3);
       example.svec->set ( 0, mX(i,0) );
       example.svec->set ( 1, mX(i,1) );
       example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
-      newExamples.push_back ( pair<int, Example> ( vY[i], example ) );
-    }  
+      newExamples.push_back ( std::pair<int, Example> ( vY[i], example ) );
+    } 
+   
+   if ( newExamples.size() == i_numExamplesToAdd )
+     break;
   }
-
-//   if ( verbose ) 
-    std::cerr << std::endl << " =============== " << std::endl << "incremental learning ..." << std::endl;
-  
-  // add them to classifierIL
-//   std::cerr << "We add several new examples" << std::endl;
-  Timer t;
-  t.start();  
-//   for (uint i = 0; i < newExamples.size(); i++)
-  for (uint i = 0; i < 1; i++)
-  {
-    classifierIL.addExample( newExamples[i].second, newExamples[i].first);      
-  }  
   
-  t.stop();  
-  std::cerr << "Time used for incremental training: " << t.getLast() << std::endl;
-
+  i_numExamplesToAdd = std::min ( i_numExamplesToAdd, (int) newExamples.size() );
   //add the new features to feature pool needed for batch training
-//   for (uint i = 0; i < newExamples.size(); i++)
-  for (uint i = 0; i < 2; i++)
+  for (uint i = 0; i < i_numExamplesToAdd; i++)
   {  
     examples.push_back( newExamples[i] );
   }
   
-  std::cerr << std::endl << " =============== " << std::endl << "We train the second classifier from the scratch with the additional new example" << std::endl;
+  std::cerr << std::endl << " =============== " << std::endl << "We train the second classifier from scratch with the additional new example" << std::endl;
   t.start(); 
   
   classifierRetrain.train ( fp, examples );  
   
   t.stop();  
-  std::cerr << "Time used for batch training: " << t.getLast() << std::endl;  
+  std::cerr << "Time used for batch training: " << t.getLast() << std::endl;   
+
+  if ( verbose ) 
+    std::cerr << std::endl << " =============== " << std::endl << "incremental learning ..." << std::endl;
+  
+  // add them to classifierIL
+  t.start();  
+  if ( verbose )
+    std::cerr << "We add " << i_numExamplesToAdd << " new examples" << std::endl;
+  if ( i_numExamplesToAdd > 1 )
+    classifierIL.addMultipleExamples( newExamples );      
+  else if ( i_numExamplesToAdd == 1 )
+    classifierIL.addExample( newExamples[0].second, newExamples[0].first);      
+  else
+  {
+    //nothing to do
+  }
+  
+  t.stop();  
+  std::cerr << "Time used for incremental training: " << t.getLast() << std::endl;
+
+ 
   
   //evaluate both and compare the resulting scores
-//  if ( verbose )
+ if ( verbose )
     std::cerr << "testing ..." << std::endl;
   for ( uint i = 0 ; i < vY.size() ; i++ )
     if ( i % 2 == 0 )
@@ -301,6 +319,7 @@ void myClassifierILTest( GPHIKClassifierNICE & classifierRetrain, GPHIKClassifie
       {
         if ( fabs(resultIL.scores[i] - resultBatch.scores[i]) > 10e-3)
         {
+          std::cerr << " resultIL.scores[i]: " << resultIL.scores[i] << " resultBatch.scores[i]: " << resultBatch.scores[i] << std::endl;
           equal = false;
           break;
         }        
@@ -320,20 +339,21 @@ void TestGPHIKClassifier::testGPHIKClassifier()
   if (verboseStartEnd)
     std::cerr << "================== TestGPHIKClassifier::testGPHIKClassifier ===================== " << std::endl;
 
-  Config conf;
+  NICE::Config conf;
   conf.sD( "GPHIKClassifier", "noise", 0.01 );
   conf.sD( "GPHIKClassifier", "parameter_lower_bound", 0.5 );
   conf.sD( "GPHIKClassifier", "parameter_upper_bound", 3.5 );
-  conf.sI( "GPHIKClassifier", "uncertaintyPrediction", 1);
 //   conf.sS( "GPHIKClassifier", "optimization_method", "none");
+//   conf.sD( "GPHIKClassifier", "performOptimizationAfterIncrement", false );
   conf.sS( "GPHIKClassifier", "optimization_method", "downhillsimplex");
-  conf.sB( "GPHIKClassifier", "uncertaintyPredictionForClassification", true);
+  conf.sD( "GPHIKClassifier", "performOptimizationAfterIncrement", true );
+  conf.sB( "GPHIKClassifier", "uncertaintyPredictionForClassification", false);
 
-  GPHIKClassifierNICE * classifier  = new GPHIKClassifierNICE ( &conf );
+  OBJREC::GPHIKClassifierNICE * classifier  = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
   
-  Matrix mX;
-  Vector vY;
-  Vector vY_multi;
+  NICE::Matrix mX;
+  NICE::Vector vY;
+  NICE::Vector vY_multi;
 
 //   ifstream ifs ("toyExample1.data", ios::in);
 //   ifstream ifs ("toyExampleLargeScale.data", ios::in);
@@ -366,8 +386,8 @@ void TestGPHIKClassifier::testGPHIKClassifier()
   if (classifier != NULL)
     delete classifier;
   
-  classifier  = new GPHIKClassifierNICE ( &conf );
-    GPHIKClassifierNICE * classifierBatch = new GPHIKClassifierNICE ( &conf ); 
+  classifier  = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
+  OBJREC::GPHIKClassifierNICE * classifierBatch = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" ); 
   
   myClassifierILTest( *classifierBatch, *classifier, mX, vY );
   
@@ -376,8 +396,8 @@ void TestGPHIKClassifier::testGPHIKClassifier()
   if (classifierBatch != NULL)
     delete classifierBatch;
   
-  classifier  = new GPHIKClassifierNICE ( &conf );
-  classifierBatch = new GPHIKClassifierNICE ( &conf );  
+  classifier  = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
+  classifierBatch = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );  
 
   if ( verbose )
     std::cerr << "Multi-class classification test " << std::endl; 
@@ -393,8 +413,8 @@ void TestGPHIKClassifier::testGPHIKClassifier()
   if (classifierBatch != NULL)
     delete classifierBatch;
   
-  classifier  = new GPHIKClassifierNICE ( &conf );
-  classifierBatch = new GPHIKClassifierNICE ( &conf ); 
+  classifier  = new GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
+  classifierBatch = new GPHIKClassifierNICE ( &conf, "GPHIKClassifier" ); 
   
   myClassifierILTest( *classifierBatch, *classifier, mX, vY_multi );
   
@@ -512,10 +532,10 @@ void TestGPHIKClassifier::testGPHIKVariance()
       ClassificationResult rVarApproxFine2 = classifierVarApproxFine2.classify ( example );
       ClassificationResult rExact = classifierVarExact.classify ( example );
       
-      if (verbose)
-      {
+//       if (verbose)
+//       {
         std::cerr << "approxUnc: " << r.uncertainty << " approxUncQuant: " << rQuant.uncertainty<< " approxUncFine1: " << rVarApproxFine1.uncertainty << " approxUncFine2: " << rVarApproxFine2.uncertainty << " exactUnc: " << rExact.uncertainty << std::endl;
-      }
+//       }
 
       CPPUNIT_ASSERT ( r.uncertainty <=  (1.0 + noise) ); //using the "standard" HIK, this is the upper bound
       CPPUNIT_ASSERT ( r.uncertainty >  rVarApproxFine1.uncertainty);