Explorar el Código

added empty constructor to allow for easy restorability options

Alexander Freytag hace 11 años
padre
commit
b7cc790863
Se han modificado 2 ficheros con 57 adiciones y 7 borrados
  1. 34 7
      GPHIKClassifierNICE.cpp
  2. 23 0
      GPHIKClassifierNICE.h

+ 34 - 7
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);

+ 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 */