GPHIKClassifier.cpp 17 KB

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