|
@@ -18,21 +18,38 @@ using namespace std;
|
|
using namespace NICE;
|
|
using namespace NICE;
|
|
using namespace OBJREC;
|
|
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 )
|
|
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()
|
|
GPHIKClassifierNICE::~GPHIKClassifierNICE()
|
|
{
|
|
{
|
|
- if ( classifier != NULL )
|
|
|
|
- delete classifier;
|
|
|
|
|
|
+ if ( this->classifier != NULL )
|
|
|
|
+ delete this->classifier;
|
|
|
|
+ this->classifier = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
ClassificationResult GPHIKClassifierNICE::classify ( Example & pe )
|
|
ClassificationResult GPHIKClassifierNICE::classify ( Example & pe )
|
|
@@ -46,6 +63,9 @@ ClassificationResult GPHIKClassifierNICE::classify ( Example & pe )
|
|
|
|
|
|
ClassificationResult GPHIKClassifierNICE::classify ( const NICE::SparseVector * example )
|
|
ClassificationResult GPHIKClassifierNICE::classify ( const NICE::SparseVector * example )
|
|
{
|
|
{
|
|
|
|
+ if ( this->classifier == NULL )
|
|
|
|
+ fthrow(Exception, "Classifier not trained yet -- aborting!" );
|
|
|
|
+
|
|
NICE::SparseVector scores;
|
|
NICE::SparseVector scores;
|
|
int result;
|
|
int result;
|
|
|
|
|
|
@@ -86,6 +106,13 @@ ClassificationResult GPHIKClassifierNICE::classify ( const NICE::SparseVector *
|
|
/** training process */
|
|
/** training process */
|
|
void GPHIKClassifierNICE::train ( FeaturePool & fp, Examples & examples )
|
|
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 :)
|
|
// we completely ignore the feature pool :)
|
|
//
|
|
//
|
|
initRand(0);
|
|
initRand(0);
|
|
@@ -187,12 +214,56 @@ void GPHIKClassifierNICE::predictUncertainty( const NICE::SparseVector * example
|
|
void GPHIKClassifierNICE::restore ( std::istream & is, int format )
|
|
void GPHIKClassifierNICE::restore ( std::istream & is, int format )
|
|
{
|
|
{
|
|
if (is.good())
|
|
if (is.good())
|
|
- {
|
|
|
|
- classifier->restore(is, format);
|
|
|
|
-
|
|
|
|
|
|
+ {
|
|
std::string tmp;
|
|
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
|
|
else
|
|
{
|
|
{
|
|
@@ -206,9 +277,19 @@ void GPHIKClassifierNICE::store ( std::ostream & os, int format ) const
|
|
{
|
|
{
|
|
os.precision (numeric_limits<double>::digits10 + 1);
|
|
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);
|
|
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
|
|
else
|
|
{
|
|
{
|
|
@@ -252,4 +333,4 @@ void GPHIKClassifierNICE::addMultipleExamples( Examples & newExamples)
|
|
}
|
|
}
|
|
|
|
|
|
classifier->addMultipleExamples(sparseExamples, y, this->performOptimizationAfterIncrement);
|
|
classifier->addMultipleExamples(sparseExamples, y, this->performOptimizationAfterIncrement);
|
|
-}
|
|
|
|
|
|
+}
|