GPHIKClassifier.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /**
  2. * @file GPHIKClassifier.cpp
  3. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning) (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/01/2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // NICE-core includes
  10. #include <core/basics/numerictools.h>
  11. #include <core/basics/Timer.h>
  12. // gp-hik-core includes
  13. #include "GPHIKClassifier.h"
  14. #include "gp-hik-core/parameterizedFunctions/PFAbsExp.h"
  15. #include "gp-hik-core/parameterizedFunctions/PFExp.h"
  16. #include "gp-hik-core/parameterizedFunctions/PFMKL.h"
  17. using namespace std;
  18. using namespace NICE;
  19. /////////////////////////////////////////////////////
  20. /////////////////////////////////////////////////////
  21. // PROTECTED METHODS
  22. /////////////////////////////////////////////////////
  23. /////////////////////////////////////////////////////
  24. void GPHIKClassifier::init(const Config *conf, const string & s_confSection)
  25. {
  26. double parameterUpperBound = conf->gD(confSection, "parameter_upper_bound", 5.0 );
  27. double parameterLowerBound = conf->gD(confSection, "parameter_lower_bound", 1.0 );
  28. this->noise = conf->gD(confSection, "noise", 0.01);
  29. string transform = conf->gS(confSection, "transform", "absexp" );
  30. if (pf == NULL)
  31. {
  32. if ( transform == "absexp" )
  33. {
  34. this->pf = new PFAbsExp( 1.0, parameterLowerBound, parameterUpperBound );
  35. } else if ( transform == "exp" ) {
  36. this->pf = new PFExp( 1.0, parameterLowerBound, parameterUpperBound );
  37. }else if ( transform == "MKL" ) {
  38. //TODO generic, please :) load from a separate file or something like this!
  39. std::set<int> steps; steps.insert(4000); steps.insert(6000); //specific for VISAPP
  40. this->pf = new PFMKL( steps, parameterLowerBound, parameterUpperBound );
  41. } else {
  42. fthrow(Exception, "Transformation type is unknown " << transform);
  43. }
  44. }
  45. else
  46. {
  47. //we already know the pf from the restore-function
  48. }
  49. this->confSection = confSection;
  50. this->verbose = conf->gB(confSection, "verbose", false);
  51. this->debug = conf->gB(confSection, "debug", false);
  52. this->uncertaintyPredictionForClassification = conf->gB( confSection, "uncertaintyPredictionForClassification", false );
  53. if (confCopy != conf)
  54. {
  55. this->confCopy = new Config ( *conf );
  56. //we do not want to read until end of file for restoring
  57. confCopy->setIoUntilEndOfFile(false);
  58. }
  59. //how do we approximate the predictive variance for classification uncertainty?
  60. string s_varianceApproximation = conf->gS(confSection, "varianceApproximation", "approximate_fine"); //default: fine approximative uncertainty prediction
  61. if ( (s_varianceApproximation.compare("approximate_rough") == 0) || ((s_varianceApproximation.compare("1") == 0)) )
  62. {
  63. this->varianceApproximation = APPROXIMATE_ROUGH;
  64. }
  65. else if ( (s_varianceApproximation.compare("approximate_fine") == 0) || ((s_varianceApproximation.compare("2") == 0)) )
  66. {
  67. this->varianceApproximation = APPROXIMATE_FINE;
  68. }
  69. else if ( (s_varianceApproximation.compare("exact") == 0) || ((s_varianceApproximation.compare("3") == 0)) )
  70. {
  71. this->varianceApproximation = EXACT;
  72. }
  73. else
  74. {
  75. this->varianceApproximation = NONE;
  76. }
  77. if ( this->verbose )
  78. std::cerr << "varianceApproximationStrategy: " << s_varianceApproximation << std::endl;
  79. }
  80. /////////////////////////////////////////////////////
  81. /////////////////////////////////////////////////////
  82. // PUBLIC METHODS
  83. /////////////////////////////////////////////////////
  84. /////////////////////////////////////////////////////
  85. GPHIKClassifier::GPHIKClassifier( const Config *conf, const string & s_confSection )
  86. {
  87. //default settings, may be overwritten lateron
  88. gphyper = NULL;
  89. pf = NULL;
  90. confCopy = NULL;
  91. //just a default value
  92. uncertaintyPredictionForClassification = false;
  93. this->confSection = s_confSection;
  94. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  95. // an emtpy config (using default values thereby) when calling the train-method
  96. if ( conf != NULL )
  97. this->init(conf, confSection);
  98. }
  99. GPHIKClassifier::~GPHIKClassifier()
  100. {
  101. if ( gphyper != NULL )
  102. delete gphyper;
  103. if (pf != NULL)
  104. delete pf;
  105. if ( confCopy != NULL )
  106. delete confCopy;
  107. }
  108. ///////////////////// ///////////////////// /////////////////////
  109. // GET / SET
  110. ///////////////////// ///////////////////// /////////////////////
  111. std::set<int> GPHIKClassifier::getKnownClassNumbers ( ) const
  112. {
  113. if (gphyper == NULL)
  114. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  115. return gphyper->getKnownClassNumbers();
  116. }
  117. ///////////////////// ///////////////////// /////////////////////
  118. // CLASSIFIER STUFF
  119. ///////////////////// ///////////////////// /////////////////////
  120. void GPHIKClassifier::classify ( const SparseVector * example, int & result, SparseVector & scores ) const
  121. {
  122. double tmpUncertainty;
  123. this->classify( example, result, scores, tmpUncertainty );
  124. }
  125. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores ) const
  126. {
  127. double tmpUncertainty;
  128. this->classify( example, result, scores, tmpUncertainty );
  129. }
  130. void GPHIKClassifier::classify ( const SparseVector * example, int & result, SparseVector & scores, double & uncertainty ) const
  131. {
  132. if (gphyper == NULL)
  133. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  134. scores.clear();
  135. result = gphyper->classify ( *example, scores );
  136. if ( scores.size() == 0 ) {
  137. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  138. }
  139. if (uncertaintyPredictionForClassification)
  140. {
  141. if (varianceApproximation != NONE)
  142. {
  143. this->predictUncertainty( example, uncertainty );
  144. }
  145. else
  146. {
  147. //do nothing
  148. uncertainty = std::numeric_limits<double>::max();
  149. }
  150. }
  151. else
  152. {
  153. //do nothing
  154. uncertainty = std::numeric_limits<double>::max();
  155. }
  156. }
  157. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores, double & uncertainty ) const
  158. {
  159. if (gphyper == NULL)
  160. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  161. scores.clear();
  162. result = gphyper->classify ( *example, scores );
  163. if ( scores.size() == 0 ) {
  164. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  165. }
  166. if (uncertaintyPredictionForClassification)
  167. {
  168. if (varianceApproximation != NONE)
  169. {
  170. this->predictUncertainty( example, uncertainty );
  171. }
  172. else
  173. {
  174. //do nothing
  175. uncertainty = std::numeric_limits<double>::max();
  176. }
  177. }
  178. else
  179. {
  180. //do nothing
  181. uncertainty = std::numeric_limits<double>::max();
  182. }
  183. }
  184. /** training process */
  185. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels )
  186. {
  187. if (verbose)
  188. {
  189. std::cerr << "GPHIKClassifier::train" << std::endl;
  190. }
  191. if ( this->confCopy == NULL )
  192. {
  193. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  194. NICE::Config tmpConfEmpty ;
  195. this->init ( &tmpConfEmpty, this->confSection );
  196. }
  197. Timer t;
  198. t.start();
  199. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  200. t.stop();
  201. if (verbose)
  202. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  203. if (gphyper != NULL)
  204. delete gphyper;
  205. if ( ( varianceApproximation != APPROXIMATE_FINE) )
  206. confCopy->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 0);
  207. gphyper = new FMKGPHyperparameterOptimization ( confCopy, pf, fmk, confSection );
  208. if (verbose)
  209. cerr << "Learning ..." << endl;
  210. // go go go
  211. gphyper->optimize ( labels );
  212. if (verbose)
  213. std::cerr << "optimization done" << std::endl;
  214. if ( ( varianceApproximation != NONE ) )
  215. {
  216. switch (varianceApproximation)
  217. {
  218. case APPROXIMATE_ROUGH:
  219. {
  220. gphyper->prepareVarianceApproximationRough();
  221. break;
  222. }
  223. case APPROXIMATE_FINE:
  224. {
  225. gphyper->prepareVarianceApproximationFine();
  226. break;
  227. }
  228. case EXACT:
  229. {
  230. //nothing to prepare
  231. break;
  232. }
  233. default:
  234. {
  235. //nothing to prepare
  236. }
  237. }
  238. }
  239. // clean up all examples ??
  240. if (verbose)
  241. std::cerr << "Learning finished" << std::endl;
  242. }
  243. /** training process */
  244. void GPHIKClassifier::train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
  245. {
  246. if (verbose)
  247. std::cerr << "GPHIKClassifier::train" << std::endl;
  248. if ( this->confCopy == NULL )
  249. {
  250. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  251. NICE::Config tmpConfEmpty ;
  252. this->init ( &tmpConfEmpty, this->confSection );
  253. }
  254. Timer t;
  255. t.start();
  256. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  257. t.stop();
  258. if (verbose)
  259. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  260. if (gphyper != NULL)
  261. delete gphyper;
  262. gphyper = new FMKGPHyperparameterOptimization ( confCopy, pf, fmk, confSection );
  263. if (verbose)
  264. cerr << "Learning ..." << endl;
  265. // go go go
  266. gphyper->optimize ( binLabels );
  267. if (verbose)
  268. std::cerr << "optimization done, now prepare for the uncertainty prediction" << std::endl;
  269. if ( ( varianceApproximation != NONE ) )
  270. {
  271. switch (varianceApproximation)
  272. {
  273. case APPROXIMATE_ROUGH:
  274. {
  275. gphyper->prepareVarianceApproximationRough();
  276. break;
  277. }
  278. case APPROXIMATE_FINE:
  279. {
  280. gphyper->prepareVarianceApproximationFine();
  281. break;
  282. }
  283. case EXACT:
  284. {
  285. //nothing to prepare
  286. break;
  287. }
  288. default:
  289. {
  290. //nothing to prepare
  291. }
  292. }
  293. }
  294. // clean up all examples ??
  295. if (verbose)
  296. std::cerr << "Learning finished" << std::endl;
  297. }
  298. GPHIKClassifier *GPHIKClassifier::clone () const
  299. {
  300. fthrow(Exception, "GPHIKClassifier: clone() not yet implemented" );
  301. return NULL;
  302. }
  303. void GPHIKClassifier::predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const
  304. {
  305. if (gphyper == NULL)
  306. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  307. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  308. switch (varianceApproximation)
  309. {
  310. case APPROXIMATE_ROUGH:
  311. {
  312. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  313. break;
  314. }
  315. case APPROXIMATE_FINE:
  316. {
  317. std::cerr << "predict uncertainty fine" << std::endl;
  318. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  319. break;
  320. }
  321. case EXACT:
  322. {
  323. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  324. break;
  325. }
  326. default:
  327. {
  328. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  329. // uncertainty = numeric_limits<double>::max();
  330. // break;
  331. }
  332. }
  333. }
  334. void GPHIKClassifier::predictUncertainty( const NICE::Vector * example, double & uncertainty ) const
  335. {
  336. if (gphyper == NULL)
  337. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  338. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  339. switch (varianceApproximation)
  340. {
  341. case APPROXIMATE_ROUGH:
  342. {
  343. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  344. break;
  345. }
  346. case APPROXIMATE_FINE:
  347. {
  348. std::cerr << "predict uncertainty fine" << std::endl;
  349. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  350. break;
  351. }
  352. case EXACT:
  353. {
  354. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  355. break;
  356. }
  357. default:
  358. {
  359. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  360. // uncertainty = numeric_limits<double>::max();
  361. // break;
  362. }
  363. }
  364. }
  365. ///////////////////// INTERFACE PERSISTENT /////////////////////
  366. // interface specific methods for store and restore
  367. ///////////////////// INTERFACE PERSISTENT /////////////////////
  368. void GPHIKClassifier::restore ( std::istream & is, int format )
  369. {
  370. //delete everything we knew so far...
  371. this->clear();
  372. bool b_restoreVerbose ( false );
  373. #ifdef B_RESTOREVERBOSE
  374. b_restoreVerbose = true;
  375. #endif
  376. if ( is.good() )
  377. {
  378. if ( b_restoreVerbose )
  379. std::cerr << " restore GPHIKClassifier" << std::endl;
  380. std::string tmp;
  381. is >> tmp; //class name
  382. if ( ! this->isStartTag( tmp, "GPHIKClassifier" ) )
  383. {
  384. std::cerr << " WARNING - attempt to restore GPHIKClassifier, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  385. throw;
  386. }
  387. if (pf != NULL)
  388. {
  389. delete pf;
  390. pf = NULL;
  391. }
  392. if ( confCopy != NULL )
  393. {
  394. delete confCopy;
  395. confCopy = NULL;
  396. }
  397. if (gphyper != NULL)
  398. {
  399. delete gphyper;
  400. gphyper = NULL;
  401. }
  402. is.precision (numeric_limits<double>::digits10 + 1);
  403. bool b_endOfBlock ( false ) ;
  404. while ( !b_endOfBlock )
  405. {
  406. is >> tmp; // start of block
  407. if ( this->isEndTag( tmp, "GPHIKClassifier" ) )
  408. {
  409. b_endOfBlock = true;
  410. continue;
  411. }
  412. tmp = this->removeStartTag ( tmp );
  413. if ( b_restoreVerbose )
  414. std::cerr << " currently restore section " << tmp << " in GPHIKClassifier" << std::endl;
  415. if ( tmp.compare("confSection") == 0 )
  416. {
  417. is >> confSection;
  418. is >> tmp; // end of block
  419. tmp = this->removeEndTag ( tmp );
  420. }
  421. else if ( tmp.compare("pf") == 0 )
  422. {
  423. is >> tmp; // start of block
  424. if ( this->isEndTag( tmp, "pf" ) )
  425. {
  426. std::cerr << " ParameterizedFunction object can not be restored. Aborting..." << std::endl;
  427. throw;
  428. }
  429. std::string transform = this->removeStartTag ( tmp );
  430. if ( transform == "PFAbsExp" )
  431. {
  432. this->pf = new PFAbsExp ();
  433. } else if ( transform == "PFExp" ) {
  434. this->pf = new PFExp ();
  435. } else {
  436. fthrow(Exception, "Transformation type is unknown " << transform);
  437. }
  438. pf->restore(is, format);
  439. is >> tmp; // end of block
  440. tmp = this->removeEndTag ( tmp );
  441. }
  442. else if ( tmp.compare("ConfigCopy") == 0 )
  443. {
  444. // possibly obsolete safety checks
  445. if ( confCopy == NULL )
  446. confCopy = new Config;
  447. confCopy->clear();
  448. //we do not want to read until the end of the file
  449. confCopy->setIoUntilEndOfFile( false );
  450. //load every options we determined explicitely
  451. confCopy->restore(is, format);
  452. is >> tmp; // end of block
  453. tmp = this->removeEndTag ( tmp );
  454. }
  455. else if ( tmp.compare("gphyper") == 0 )
  456. {
  457. if ( gphyper == NULL )
  458. gphyper = new NICE::FMKGPHyperparameterOptimization();
  459. //then, load everything that we stored explicitely,
  460. // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
  461. gphyper->restore(is, format);
  462. is >> tmp; // end of block
  463. tmp = this->removeEndTag ( tmp );
  464. }
  465. else
  466. {
  467. std::cerr << "WARNING -- unexpected GPHIKClassifier object -- " << tmp << " -- for restoration... aborting" << std::endl;
  468. throw;
  469. }
  470. }
  471. //load every settings as well as default options
  472. std::cerr << "run this->init" << std::endl;
  473. this->init(confCopy, confSection);
  474. std::cerr << "run gphyper->initialize" << std::endl;
  475. gphyper->initialize ( confCopy, pf, NULL, confSection );
  476. }
  477. else
  478. {
  479. std::cerr << "GPHIKClassifier::restore -- InStream not initialized - restoring not possible!" << std::endl;
  480. throw;
  481. }
  482. }
  483. void GPHIKClassifier::store ( std::ostream & os, int format ) const
  484. {
  485. if (gphyper == NULL)
  486. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  487. if (os.good())
  488. {
  489. // show starting point
  490. os << this->createStartTag( "GPHIKClassifier" ) << std::endl;
  491. os.precision (numeric_limits<double>::digits10 + 1);
  492. os << this->createStartTag( "confSection" ) << std::endl;
  493. os << confSection << std::endl;
  494. os << this->createEndTag( "confSection" ) << std::endl;
  495. os << this->createStartTag( "pf" ) << std::endl;
  496. pf->store(os, format);
  497. os << this->createEndTag( "pf" ) << std::endl;
  498. os << this->createStartTag( "ConfigCopy" ) << std::endl;
  499. //we do not want to read until end of file for restoring
  500. confCopy->setIoUntilEndOfFile(false);
  501. confCopy->store(os,format);
  502. os << this->createEndTag( "ConfigCopy" ) << std::endl;
  503. os << this->createStartTag( "gphyper" ) << std::endl;
  504. //store the underlying data
  505. //will be done in gphyper->store(of,format)
  506. //store the optimized parameter values and all that stuff
  507. gphyper->store(os, format);
  508. os << this->createEndTag( "gphyper" ) << std::endl;
  509. // done
  510. os << this->createEndTag( "GPHIKClassifier" ) << std::endl;
  511. }
  512. else
  513. {
  514. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  515. }
  516. }
  517. void GPHIKClassifier::clear ()
  518. {
  519. if ( gphyper != NULL )
  520. {
  521. delete gphyper;
  522. gphyper = NULL;
  523. }
  524. if (pf != NULL)
  525. {
  526. delete pf;
  527. pf = NULL;
  528. }
  529. if ( confCopy != NULL )
  530. {
  531. delete confCopy;
  532. confCopy = NULL;
  533. }
  534. }
  535. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  536. // interface specific methods for incremental extensions
  537. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  538. void GPHIKClassifier::addExample( const NICE::SparseVector * example,
  539. const double & label,
  540. const bool & performOptimizationAfterIncrement
  541. )
  542. {
  543. if ( this->gphyper == NULL )
  544. {
  545. //call train method instead
  546. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  547. std::vector< const NICE::SparseVector *> examplesVec;
  548. examplesVec.push_back ( example );
  549. NICE::Vector labelsVec ( 1 , label );
  550. this->train ( examplesVec, labelsVec );
  551. }
  552. else
  553. {
  554. this->gphyper->addExample( example, label, performOptimizationAfterIncrement );
  555. }
  556. }
  557. void GPHIKClassifier::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  558. const NICE::Vector & newLabels,
  559. const bool & performOptimizationAfterIncrement
  560. )
  561. {
  562. //are new examples available? If not, nothing has to be done
  563. if ( newExamples.size() < 1)
  564. return;
  565. if ( this->gphyper == NULL )
  566. {
  567. //call train method instead
  568. std::cerr << "Classifier not initially trained yet -- run initial training instead of incremental extension!" << std::endl;
  569. this->train ( newExamples, newLabels );
  570. }
  571. else
  572. {
  573. this->gphyper->addMultipleExamples( newExamples, newLabels, performOptimizationAfterIncrement );
  574. }
  575. }