GPHIKClassifier.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 )
  121. {
  122. double tmpUncertainty;
  123. this->classify( example, result, scores, tmpUncertainty );
  124. }
  125. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores )
  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 )
  131. {
  132. if (gphyper == NULL)
  133. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  134. scores.clear();
  135. int classno = 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. result = scores.maxElement();
  140. if (uncertaintyPredictionForClassification)
  141. {
  142. if (varianceApproximation != NONE)
  143. {
  144. this->predictUncertainty( example, uncertainty );
  145. }
  146. else
  147. {
  148. //do nothing
  149. uncertainty = std::numeric_limits<double>::max();
  150. }
  151. }
  152. else
  153. {
  154. //do nothing
  155. uncertainty = std::numeric_limits<double>::max();
  156. }
  157. }
  158. void GPHIKClassifier::classify ( const NICE::Vector * example, int & result, SparseVector & scores, double & uncertainty )
  159. {
  160. if (gphyper == NULL)
  161. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  162. scores.clear();
  163. int classno = gphyper->classify ( *example, scores );
  164. if ( scores.size() == 0 ) {
  165. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  166. }
  167. result = scores.maxElement();
  168. if (uncertaintyPredictionForClassification)
  169. {
  170. if (varianceApproximation != NONE)
  171. {
  172. this->predictUncertainty( example, uncertainty );
  173. }
  174. else
  175. {
  176. //do nothing
  177. uncertainty = std::numeric_limits<double>::max();
  178. }
  179. }
  180. else
  181. {
  182. //do nothing
  183. uncertainty = std::numeric_limits<double>::max();
  184. }
  185. }
  186. /** training process */
  187. void GPHIKClassifier::train ( const std::vector< NICE::SparseVector *> & examples, const NICE::Vector & labels )
  188. {
  189. if (verbose)
  190. {
  191. std::cerr << "GPHIKClassifier::train" << std::endl;
  192. }
  193. if ( this->confCopy == NULL )
  194. {
  195. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  196. NICE::Config tmpConfEmpty ;
  197. this->init ( &tmpConfEmpty, this->confSection );
  198. }
  199. Timer t;
  200. t.start();
  201. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  202. t.stop();
  203. if (verbose)
  204. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  205. if (gphyper != NULL)
  206. delete gphyper;
  207. if ( ( varianceApproximation != APPROXIMATE_FINE) )
  208. confCopy->sI ( confSection, "nrOfEigenvaluesToConsiderForVarApprox", 0);
  209. gphyper = new FMKGPHyperparameterOptimization ( confCopy, pf, fmk, confSection );
  210. if (verbose)
  211. cerr << "Learning ..." << endl;
  212. // go go go
  213. gphyper->optimize ( labels );
  214. if (verbose)
  215. std::cerr << "optimization done" << std::endl;
  216. if ( ( varianceApproximation != NONE ) )
  217. {
  218. std::cerr << "now prepare for the uncertainty prediction" << std::endl;
  219. switch (varianceApproximation)
  220. {
  221. case APPROXIMATE_ROUGH:
  222. {
  223. gphyper->prepareVarianceApproximationRough();
  224. break;
  225. }
  226. case APPROXIMATE_FINE:
  227. {
  228. gphyper->prepareVarianceApproximationFine();
  229. break;
  230. }
  231. case EXACT:
  232. {
  233. //nothing to prepare
  234. break;
  235. }
  236. default:
  237. {
  238. //nothing to prepare
  239. }
  240. }
  241. }
  242. // clean up all examples ??
  243. if (verbose)
  244. std::cerr << "Learning finished" << std::endl;
  245. }
  246. /** training process */
  247. void GPHIKClassifier::train ( const std::vector< SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
  248. {
  249. if (verbose)
  250. std::cerr << "GPHIKClassifier::train" << std::endl;
  251. if ( this->confCopy == NULL )
  252. {
  253. std::cerr << "WARNING -- No config used so far, initialize values with empty config file now..." << std::endl;
  254. NICE::Config tmpConfEmpty ;
  255. this->init ( &tmpConfEmpty, this->confSection );
  256. }
  257. Timer t;
  258. t.start();
  259. FastMinKernel *fmk = new FastMinKernel ( examples, noise, this->debug );
  260. t.stop();
  261. if (verbose)
  262. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  263. if (gphyper != NULL)
  264. delete gphyper;
  265. gphyper = new FMKGPHyperparameterOptimization ( confCopy, pf, fmk, confSection );
  266. if (verbose)
  267. cerr << "Learning ..." << endl;
  268. // go go go
  269. gphyper->optimize ( binLabels );
  270. if (verbose)
  271. std::cerr << "optimization done, now prepare for the uncertainty prediction" << std::endl;
  272. if ( ( varianceApproximation != NONE ) )
  273. {
  274. std::cerr << "now prepare for the uncertainty prediction" << std::endl;
  275. switch (varianceApproximation)
  276. {
  277. case APPROXIMATE_ROUGH:
  278. {
  279. gphyper->prepareVarianceApproximationRough();
  280. break;
  281. }
  282. case APPROXIMATE_FINE:
  283. {
  284. gphyper->prepareVarianceApproximationFine();
  285. break;
  286. }
  287. case EXACT:
  288. {
  289. //nothing to prepare
  290. break;
  291. }
  292. default:
  293. {
  294. //nothing to prepare
  295. }
  296. }
  297. }
  298. // clean up all examples ??
  299. if (verbose)
  300. std::cerr << "Learning finished" << std::endl;
  301. }
  302. GPHIKClassifier *GPHIKClassifier::clone () const
  303. {
  304. fthrow(Exception, "GPHIKClassifier: clone() not yet implemented" );
  305. return NULL;
  306. }
  307. void GPHIKClassifier::predictUncertainty( const NICE::SparseVector * example, double & uncertainty )
  308. {
  309. if (gphyper == NULL)
  310. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  311. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  312. switch (varianceApproximation)
  313. {
  314. case APPROXIMATE_ROUGH:
  315. {
  316. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  317. break;
  318. }
  319. case APPROXIMATE_FINE:
  320. {
  321. std::cerr << "predict uncertainty fine" << std::endl;
  322. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  323. break;
  324. }
  325. case EXACT:
  326. {
  327. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  328. break;
  329. }
  330. default:
  331. {
  332. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  333. // uncertainty = numeric_limits<double>::max();
  334. // break;
  335. }
  336. }
  337. }
  338. void GPHIKClassifier::predictUncertainty( const NICE::Vector * example, double & uncertainty )
  339. {
  340. if (gphyper == NULL)
  341. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  342. //we directly store the predictive variances in the vector, that contains the classification uncertainties lateron to save storage
  343. switch (varianceApproximation)
  344. {
  345. case APPROXIMATE_ROUGH:
  346. {
  347. gphyper->computePredictiveVarianceApproximateRough( *example, uncertainty );
  348. break;
  349. }
  350. case APPROXIMATE_FINE:
  351. {
  352. std::cerr << "predict uncertainty fine" << std::endl;
  353. gphyper->computePredictiveVarianceApproximateFine( *example, uncertainty );
  354. break;
  355. }
  356. case EXACT:
  357. {
  358. gphyper->computePredictiveVarianceExact( *example, uncertainty );
  359. break;
  360. }
  361. default:
  362. {
  363. fthrow(Exception, "GPHIKClassifier - your settings disabled the variance approximation needed for uncertainty prediction.");
  364. // uncertainty = numeric_limits<double>::max();
  365. // break;
  366. }
  367. }
  368. }
  369. ///////////////////// INTERFACE PERSISTENT /////////////////////
  370. // interface specific methods for store and restore
  371. ///////////////////// INTERFACE PERSISTENT /////////////////////
  372. void GPHIKClassifier::restore ( std::istream & is, int format )
  373. {
  374. //delete everything we knew so far...
  375. this->clear();
  376. bool b_restoreVerbose ( false );
  377. #ifdef B_RESTOREVERBOSE
  378. b_restoreVerbose = true;
  379. #endif
  380. if ( is.good() )
  381. {
  382. if ( b_restoreVerbose )
  383. std::cerr << " restore GPHIKClassifier" << std::endl;
  384. std::string tmp;
  385. is >> tmp; //class name
  386. if ( ! this->isStartTag( tmp, "GPHIKClassifier" ) )
  387. {
  388. std::cerr << " WARNING - attempt to restore GPHIKClassifier, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  389. throw;
  390. }
  391. if (pf != NULL)
  392. {
  393. delete pf;
  394. pf = NULL;
  395. }
  396. if ( confCopy != NULL )
  397. {
  398. delete confCopy;
  399. confCopy = NULL;
  400. }
  401. if (gphyper != NULL)
  402. {
  403. delete gphyper;
  404. gphyper = NULL;
  405. }
  406. is.precision (numeric_limits<double>::digits10 + 1);
  407. bool b_endOfBlock ( false ) ;
  408. while ( !b_endOfBlock )
  409. {
  410. is >> tmp; // start of block
  411. if ( this->isEndTag( tmp, "GPHIKClassifier" ) )
  412. {
  413. b_endOfBlock = true;
  414. continue;
  415. }
  416. tmp = this->removeStartTag ( tmp );
  417. if ( b_restoreVerbose )
  418. std::cerr << " currently restore section " << tmp << " in GPHIKClassifier" << std::endl;
  419. if ( tmp.compare("confSection") == 0 )
  420. {
  421. is >> confSection;
  422. is >> tmp; // end of block
  423. tmp = this->removeEndTag ( tmp );
  424. }
  425. else if ( tmp.compare("pf") == 0 )
  426. {
  427. is >> tmp; // start of block
  428. if ( this->isEndTag( tmp, "pf" ) )
  429. {
  430. std::cerr << " ParameterizedFunction object can not be restored. Aborting..." << std::endl;
  431. throw;
  432. }
  433. std::string transform = this->removeStartTag ( tmp );
  434. if ( transform == "PFAbsExp" )
  435. {
  436. this->pf = new PFAbsExp ();
  437. } else if ( transform == "PFExp" ) {
  438. this->pf = new PFExp ();
  439. } else {
  440. fthrow(Exception, "Transformation type is unknown " << transform);
  441. }
  442. pf->restore(is, format);
  443. is >> tmp; // end of block
  444. tmp = this->removeEndTag ( tmp );
  445. }
  446. else if ( tmp.compare("ConfigCopy") == 0 )
  447. {
  448. // possibly obsolete safety checks
  449. if ( confCopy == NULL )
  450. confCopy = new Config;
  451. confCopy->clear();
  452. //we do not want to read until the end of the file
  453. confCopy->setIoUntilEndOfFile( false );
  454. //load every options we determined explicitely
  455. confCopy->restore(is, format);
  456. is >> tmp; // end of block
  457. tmp = this->removeEndTag ( tmp );
  458. }
  459. else if ( tmp.compare("gphyper") == 0 )
  460. {
  461. if ( gphyper == NULL )
  462. gphyper = new NICE::FMKGPHyperparameterOptimization();
  463. //then, load everything that we stored explicitely,
  464. // including precomputed matrices, LUTs, eigenvalues, ... and all that stuff
  465. gphyper->restore(is, format);
  466. is >> tmp; // end of block
  467. tmp = this->removeEndTag ( tmp );
  468. }
  469. else
  470. {
  471. std::cerr << "WARNING -- unexpected GPHIKClassifier object -- " << tmp << " -- for restoration... aborting" << std::endl;
  472. throw;
  473. }
  474. }
  475. //load every settings as well as default options
  476. std::cerr << "run this->init" << std::endl;
  477. this->init(confCopy, confSection);
  478. std::cerr << "run gphyper->initialize" << std::endl;
  479. gphyper->initialize ( confCopy, pf, NULL, confSection );
  480. }
  481. else
  482. {
  483. std::cerr << "GPHIKClassifier::restore -- InStream not initialized - restoring not possible!" << std::endl;
  484. throw;
  485. }
  486. }
  487. void GPHIKClassifier::store ( std::ostream & os, int format ) const
  488. {
  489. if (gphyper == NULL)
  490. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  491. if (os.good())
  492. {
  493. // show starting point
  494. os << this->createStartTag( "GPHIKClassifier" ) << std::endl;
  495. os.precision (numeric_limits<double>::digits10 + 1);
  496. os << this->createStartTag( "confSection" ) << std::endl;
  497. os << confSection << std::endl;
  498. os << this->createEndTag( "confSection" ) << std::endl;
  499. os << this->createStartTag( "pf" ) << std::endl;
  500. pf->store(os, format);
  501. os << this->createEndTag( "pf" ) << std::endl;
  502. os << this->createStartTag( "ConfigCopy" ) << std::endl;
  503. //we do not want to read until end of file for restoring
  504. confCopy->setIoUntilEndOfFile(false);
  505. confCopy->store(os,format);
  506. os << this->createEndTag( "ConfigCopy" ) << std::endl;
  507. os << this->createStartTag( "gphyper" ) << std::endl;
  508. //store the underlying data
  509. //will be done in gphyper->store(of,format)
  510. //store the optimized parameter values and all that stuff
  511. gphyper->store(os, format);
  512. os << this->createEndTag( "gphyper" ) << std::endl;
  513. // done
  514. os << this->createEndTag( "GPHIKClassifier" ) << std::endl;
  515. }
  516. else
  517. {
  518. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  519. }
  520. }
  521. void GPHIKClassifier::clear ()
  522. {
  523. if ( gphyper != NULL )
  524. {
  525. delete gphyper;
  526. gphyper = NULL;
  527. }
  528. if (pf != NULL)
  529. {
  530. delete pf;
  531. pf = NULL;
  532. }
  533. if ( confCopy != NULL )
  534. {
  535. delete confCopy;
  536. confCopy = NULL;
  537. }
  538. }
  539. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  540. // interface specific methods for incremental extensions
  541. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  542. void GPHIKClassifier::addExample( const NICE::SparseVector * example,
  543. const double & label,
  544. const bool & performOptimizationAfterIncrement
  545. )
  546. {
  547. if ( this->gphyper == NULL )
  548. fthrow(Exception, "Classifier not initially trained yet -- aborting!" );
  549. //TODO add option for starting with empty classifier!
  550. // -> call train() with converted input here
  551. //***done*** // TODO add option to go from 2 to 3 classes! ***done***
  552. // TODO add option going from 1 to 2 classes without adding new alpha vector
  553. //***done*** // TODO check variance matrices in update ***done***
  554. // TODO add check option for variance update
  555. // TODO adapt code for addMultipleExamples
  556. this->gphyper->addExample( example, label, performOptimizationAfterIncrement );
  557. std::cerr << " --- GPHIKClassifierIL::addExample done --- " << std::endl;
  558. }
  559. void GPHIKClassifier::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  560. const NICE::Vector & newLabels,
  561. const bool & performOptimizationAfterIncrement
  562. )
  563. {
  564. //are new examples available? If not, nothing has to be done
  565. if ( newExamples.size() < 1)
  566. return;
  567. if ( this->gphyper == NULL )
  568. fthrow(Exception, "Classifier not initially trained yet -- aborting!" );
  569. //TODO add option for starting with empty classifier!
  570. this->gphyper->addMultipleExamples( newExamples, newLabels, performOptimizationAfterIncrement );
  571. }