Parcourir la source

Merge branch 'master' of dbv.inf-cv.uni-jena.de:nice/nice-gp-hik-exp into cmake_mergebranch

Johannes Ruehle il y a 11 ans
Parent
commit
5cc47cd7e4
2 fichiers modifiés avec 118 ajouts et 14 suppressions
  1. 95 14
      GPHIKClassifierNICE.cpp
  2. 23 0
      GPHIKClassifierNICE.h

+ 95 - 14
GPHIKClassifierNICE.cpp

@@ -18,21 +18,38 @@ using namespace std;
 using namespace NICE;
 using namespace OBJREC;
 
+void GPHIKClassifierNICE::init ( const NICE::Config *conf, const std::string & s_confSection )
+{
+  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->classifier = new GPHIKClassifier(conf, s_confSection);
+}
+
+GPHIKClassifierNICE::GPHIKClassifierNICE( ) 
+{
+  this->classifier = NULL;
+}
 
 GPHIKClassifierNICE::GPHIKClassifierNICE( const Config *conf, const string & confSection ) 
 {
-  this->verbose = conf->gB(confSection, "verbose", false);
-  this->useSimpleBalancing = conf->gB(confSection, "use_simple_balancing", false);
-  this->minSamples = conf->gI(confSection, "min_samples", -1);
-  this->performOptimizationAfterIncrement = conf->gB(confSection, "performOptimizationAfterIncrement", true);
+  this->classifier = NULL;
   
-  classifier = new GPHIKClassifier(conf, confSection);
+  // if no config file was given, we either restore the classifier from an external file, or run ::init with 
+  // an emtpy config (using default values thereby) when calling the train-method
+  if ( conf != NULL )
+  {
+    this->init(conf, confSection);
+  }
 }
 
 GPHIKClassifierNICE::~GPHIKClassifierNICE()
 {
-  if ( classifier != NULL )
-    delete classifier;
+  if ( this->classifier != NULL )
+    delete this->classifier;
+  this->classifier = NULL;
 }
 
 ClassificationResult GPHIKClassifierNICE::classify ( Example & pe )
@@ -46,6 +63,9 @@ ClassificationResult GPHIKClassifierNICE::classify ( Example & pe )
 
 ClassificationResult GPHIKClassifierNICE::classify ( const NICE::SparseVector * example )
 {
+  if ( this->classifier == NULL )
+    fthrow(Exception, "Classifier not trained yet -- aborting!" );
+  
   NICE::SparseVector scores;
   int result;
   
@@ -86,6 +106,13 @@ ClassificationResult GPHIKClassifierNICE::classify ( const NICE::SparseVector *
 /** training process */
 void GPHIKClassifierNICE::train ( FeaturePool & fp, Examples & examples )
 {
+  if ( this->classifier == NULL )
+  {
+    std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
+    NICE::Config tmpConfEmpty ;
+    this->init ( &tmpConfEmpty );
+  }  
+  
   // we completely ignore the feature pool :)
   //
   initRand(0);
@@ -187,12 +214,56 @@ void GPHIKClassifierNICE::predictUncertainty( const NICE::SparseVector * example
 void GPHIKClassifierNICE::restore ( std::istream & is, int format )
 {
   if (is.good())
-  {
-    classifier->restore(is, format);  
-    
+  {    
     std::string tmp;
-    is >> tmp; //"performOptimizationAfterIncrement: "
-    is >> this->performOptimizationAfterIncrement;
+    is >> tmp; //class name 
+    
+    if ( ! this->isStartTag( tmp, "GPHIKClassifierNICE" ) )
+    {
+      std::cerr << " WARNING - attempt to restore GPHIKClassifierNICE, but start flag " << tmp << " does not match! Aborting... " << std::endl;
+      throw;
+    } 
+    
+    is.precision (numeric_limits<double>::digits10 + 1);
+    
+    bool b_endOfBlock ( false ) ;
+    
+    while ( !b_endOfBlock )
+    {
+      is >> tmp; // start of block 
+      
+      if ( this->isEndTag( tmp, "GPHIKClassifierNICE" ) )
+      {
+        b_endOfBlock = true;
+        continue;
+      } 
+
+      tmp = this->removeStartTag( tmp );
+
+      if ( tmp.compare("classifier") == 0 )
+      {
+        if ( classifier == NULL )
+          classifier = new NICE::GPHIKClassifier();
+        
+        //then, load everything that we stored explicitely,
+        // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
+        classifier->restore(is, format);  
+          
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }
+      else if ( tmp.compare("performOptimizationAfterIncrement") == 0 )
+      {
+        is >> performOptimizationAfterIncrement;        
+        is >> tmp; // end of block 
+        tmp = this->removeEndTag ( tmp );
+      }      
+      else
+      {
+      std::cerr << "WARNING -- unexpected GPHIKClassifierNICE object -- " << tmp << " -- for restoration... aborting" << std::endl;
+      throw;
+      }
+    } // while-loop
   }
   else
   {
@@ -206,9 +277,19 @@ void GPHIKClassifierNICE::store ( std::ostream & os, int format ) const
   {
     os.precision (numeric_limits<double>::digits10 + 1);
     
+    // show starting point
+    os << this->createStartTag( "GPHIKClassifierNICE" ) << std::endl;        
+    
+    os << this->createStartTag( "classifier" ) << std::endl;
     classifier->store(os, format);
+    os << this->createEndTag( "classifier" ) << std::endl;
+    
+    os << this->createStartTag( "performOptimizationAfterIncrement" ) << std::endl;  
+    os << performOptimizationAfterIncrement << std::endl;
+    os << this->createEndTag( "performOptimizationAfterIncrement" ) << std::endl;     
     
-    os << "performOptimizationAfterIncrement: " << performOptimizationAfterIncrement << std::endl;
+    // done
+    os << this->createEndTag( "GPHIKClassifierNICE" ) << std::endl;
   }
   else
   {
@@ -252,4 +333,4 @@ void GPHIKClassifierNICE::addMultipleExamples( Examples & newExamples)
   }  
   
   classifier->addMultipleExamples(sparseExamples, y, this->performOptimizationAfterIncrement);  
-}
+}

+ 23 - 0
GPHIKClassifierNICE.h

@@ -32,6 +32,12 @@ class GPHIKClassifierNICE : public FeaturePoolClassifier
 
   protected:
     
+    /////////////////////////
+    /////////////////////////
+    // PROTECTED VARIABLES //
+    /////////////////////////
+    /////////////////////////    
+    
     NICE::GPHIKClassifier * classifier;
     
     /** verbose flag for useful output*/
@@ -43,10 +49,27 @@ class GPHIKClassifierNICE : public FeaturePoolClassifier
     
     /** When adding new examples, do we want to run a whole optimization of all involved hyperparameters? default: true*/
     bool performOptimizationAfterIncrement;
+    
+    /////////////////////////
+    /////////////////////////
+    //  PROTECTED METHODS  //
+    /////////////////////////
+    /////////////////////////    
+    
+    /** 
+    * @brief Setup internal variables and objects used
+    * @author Alexander Freytag
+    * @param conf Config file to specify variable settings
+    * @param s_confSection
+    */    
+    void init ( const NICE::Config *conf, const std::string & s_confSection = "GPHIKClassifier" );
 
   public:
 
     /** simple constructor */
+    GPHIKClassifierNICE( );
+    
+    /** default constructor */
     GPHIKClassifierNICE( const NICE::Config *conf, const std::string & confSection = "GPHIKClassifier" );
       
     /** simple destructor */