|
@@ -20,58 +20,54 @@ using namespace NICE;
|
|
|
using namespace OBJREC;
|
|
|
|
|
|
|
|
|
-FPCGPHIK::FPCGPHIK( const Config *conf, const string & confSection )
|
|
|
+void FPCGPHIK::init ( const NICE::Config *conf, const std::string & s_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->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", false );
|
|
|
|
|
|
- classifier = new GPHIKClassifier(conf, confSection);
|
|
|
+ this->classifier = new GPHIKClassifier(conf, s_confSection);
|
|
|
}
|
|
|
|
|
|
-FPCGPHIK::~FPCGPHIK()
|
|
|
+FPCGPHIK::FPCGPHIK( )
|
|
|
{
|
|
|
- if ( classifier != NULL )
|
|
|
- delete classifier;
|
|
|
+ this->classifier = NULL;
|
|
|
}
|
|
|
|
|
|
-ClassificationResult FPCGPHIK::classify ( Example & pe )
|
|
|
-{
|
|
|
-
|
|
|
- NICE::SparseVector *svec;// = pe.svec;
|
|
|
-
|
|
|
- // was only a NICE::Vector given?
|
|
|
- // Than we had to allocate a new NICE::SparseVector and converted the given NICE::Vector into it.
|
|
|
- bool newvec = false;
|
|
|
+FPCGPHIK::FPCGPHIK( const Config *conf, const string & confSection )
|
|
|
+{
|
|
|
+ this->classifier = NULL;
|
|
|
|
|
|
- if ( pe.svec != NULL )
|
|
|
+ // 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 )
|
|
|
{
|
|
|
- svec = pe.svec;
|
|
|
+ this->init(conf, confSection);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- NICE::Vector x;
|
|
|
-
|
|
|
- x = * ( pe.vec );
|
|
|
-
|
|
|
- svec = new NICE::SparseVector ( x );
|
|
|
+}
|
|
|
|
|
|
- svec->setDim ( x.size() );
|
|
|
+FPCGPHIK::~FPCGPHIK()
|
|
|
+{
|
|
|
+ if ( this->classifier != NULL )
|
|
|
+ delete this->classifier;
|
|
|
+ this->classifier = NULL;
|
|
|
+}
|
|
|
|
|
|
- newvec = true;
|
|
|
- }
|
|
|
-
|
|
|
- ClassificationResult result ( this->classify( svec ) );
|
|
|
-
|
|
|
- if ( newvec )
|
|
|
- delete svec;
|
|
|
+ClassificationResult FPCGPHIK::classify ( Example & pe )
|
|
|
+{
|
|
|
+ const SparseVector *svec = pe.svec;
|
|
|
|
|
|
- return result;
|
|
|
+ if ( svec == NULL )
|
|
|
+ fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
|
|
|
+ return this->classify( svec );
|
|
|
}
|
|
|
|
|
|
ClassificationResult FPCGPHIK::classify ( const NICE::SparseVector * example )
|
|
|
{
|
|
|
+ if ( this->classifier == NULL )
|
|
|
+ fthrow(Exception, "Classifier not trained yet -- aborting!" );
|
|
|
+
|
|
|
NICE::SparseVector scores;
|
|
|
int result;
|
|
|
|
|
@@ -112,6 +108,13 @@ ClassificationResult FPCGPHIK::classify ( const NICE::SparseVector * example )
|
|
|
/** training process */
|
|
|
void FPCGPHIK::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);
|
|
@@ -177,10 +180,7 @@ void FPCGPHIK::train ( FeaturePool & fp, Examples & examples )
|
|
|
/** training process */
|
|
|
void FPCGPHIK::train ( const std::vector< const SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
|
|
|
{
|
|
|
-
|
|
|
- std::cerr << "call internal train method " << std::endl;
|
|
|
classifier->train(examples, binLabels);
|
|
|
- std::cerr << "training done" << std::endl;
|
|
|
}
|
|
|
|
|
|
void FPCGPHIK::clear ()
|
|
@@ -210,18 +210,62 @@ void FPCGPHIK::predictUncertainty( const NICE::SparseVector * example, double &
|
|
|
classifier->predictUncertainty(example, uncertainty);
|
|
|
}
|
|
|
|
|
|
-//---------------------------------------------------------------------
|
|
|
-// protected methods
|
|
|
-//---------------------------------------------------------------------
|
|
|
+///////////////////// INTERFACE PERSISTENT /////////////////////
|
|
|
+// interface specific methods for store and restore
|
|
|
+///////////////////// INTERFACE PERSISTENT /////////////////////
|
|
|
void FPCGPHIK::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, "FPCGPHIK" ) )
|
|
|
+ {
|
|
|
+ std::cerr << " WARNING - attempt to restore FPCGPHIK, 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, "FPCGPHIK" ) )
|
|
|
+ {
|
|
|
+ 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 FPCGPHIK object -- " << tmp << " -- for restoration... aborting" << std::endl;
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ } // while-loop
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -235,9 +279,19 @@ void FPCGPHIK::store ( std::ostream & os, int format ) const
|
|
|
{
|
|
|
os.precision (numeric_limits<double>::digits10 + 1);
|
|
|
|
|
|
+ // show starting point
|
|
|
+ os << this->createStartTag( "FPCGPHIK" ) << std::endl;
|
|
|
+
|
|
|
+ os << this->createStartTag( "classifier" ) << std::endl;
|
|
|
classifier->store(os, format);
|
|
|
+ os << this->createEndTag( "classifier" ) << std::endl;
|
|
|
|
|
|
- os << "performOptimizationAfterIncrement: " << performOptimizationAfterIncrement << std::endl;
|
|
|
+ os << this->createStartTag( "performOptimizationAfterIncrement" ) << std::endl;
|
|
|
+ os << performOptimizationAfterIncrement << std::endl;
|
|
|
+ os << this->createEndTag( "performOptimizationAfterIncrement" ) << std::endl;
|
|
|
+
|
|
|
+ // done
|
|
|
+ os << this->createEndTag( "FPCGPHIK" ) << std::endl;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -245,6 +299,10 @@ void FPCGPHIK::store ( std::ostream & os, int format ) const
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+///////////////////// INTERFACE ONLINE LEARNABLE (SIMILAR) /////////////////////
|
|
|
+// interface specific methods for incremental extensions
|
|
|
+///////////////////// INTERFACE ONLINE LEARNABLE (SIMILAR) /////////////////////
|
|
|
+
|
|
|
void FPCGPHIK::addExample( const Example & pe, const double & label)
|
|
|
{
|
|
|
const SparseVector *svec = pe.svec;
|
|
@@ -258,7 +316,7 @@ void FPCGPHIK::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;
|