GPHIKRawClassifier.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /**
  2. * @file GPHIKRawClassifier.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. #include <core/algebra/ILSConjugateGradients.h>
  13. #include <core/algebra/EigValues.h>
  14. // gp-hik-core includes
  15. #include "gp-hik-core/GPHIKRawClassifier.h"
  16. #include "gp-hik-core/GMHIKernelRaw.h"
  17. //
  18. #include "gp-hik-core/quantization/Quantization1DAequiDist0To1.h"
  19. #include "gp-hik-core/quantization/Quantization1DAequiDist0ToMax.h"
  20. #include "gp-hik-core/quantization/QuantizationNDAequiDist0ToMax.h"
  21. using namespace std;
  22. using namespace NICE;
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25. // PROTECTED METHODS
  26. /////////////////////////////////////////////////////
  27. /////////////////////////////////////////////////////
  28. void GPHIKRawClassifier::clearSetsOfTablesAandB( )
  29. {
  30. // delete all LUTs A which are needed when no quantization is activated
  31. for ( std::map< uint,PrecomputedType >::iterator itA = this->precomputedA.begin();
  32. itA != this->precomputedA.end();
  33. itA++
  34. )
  35. {
  36. for ( uint idxDim = 0 ; idxDim < this->num_dimension; idxDim++ )
  37. {
  38. if ( (itA->second)[idxDim] != NULL )
  39. delete [] (itA->second)[idxDim];
  40. }
  41. delete [] itA->second;
  42. }
  43. this->precomputedA.clear();
  44. // delete all LUTs B which are needed when no quantization is activated
  45. for ( std::map< uint,PrecomputedType >::iterator itB = this->precomputedB.begin();
  46. itB != this->precomputedB.end();
  47. itB++
  48. )
  49. {
  50. for ( uint idxDim = 0 ; idxDim < this->num_dimension; idxDim++ )
  51. {
  52. if ( (itB->second)[idxDim] != NULL )
  53. delete [] (itB->second)[idxDim];
  54. }
  55. delete [] itB->second;
  56. }
  57. this->precomputedB.clear();
  58. }
  59. void GPHIKRawClassifier::clearSetsOfTablesT( )
  60. {
  61. // delete all LUTs used for quantization
  62. for ( std::map< uint, double * >::iterator itT = this->precomputedT.begin();
  63. itT != this->precomputedT.end();
  64. itT++
  65. )
  66. {
  67. delete [] itT->second;
  68. }
  69. this->precomputedT.clear();
  70. }
  71. /////////////////////////////////////////////////////
  72. /////////////////////////////////////////////////////
  73. // PUBLIC METHODS
  74. /////////////////////////////////////////////////////
  75. /////////////////////////////////////////////////////
  76. GPHIKRawClassifier::GPHIKRawClassifier( )
  77. {
  78. this->b_isTrained = false;
  79. this->confSection = "";
  80. this->nnz_per_dimension = NULL;
  81. this->num_examples = 0;
  82. this->num_dimension = 0;
  83. this->solver = NULL;
  84. this->q = NULL;
  85. this->gm = NULL;
  86. // in order to be sure about all necessary variables be setup with default values, we
  87. // run initFromConfig with an empty config
  88. NICE::Config tmpConfEmpty ;
  89. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  90. }
  91. GPHIKRawClassifier::GPHIKRawClassifier( const Config *_conf,
  92. const string & _confSection
  93. )
  94. {
  95. ///////////
  96. // same code as in empty constructor - duplication can be avoided with C++11 allowing for constructor delegation
  97. ///////////
  98. this->b_isTrained = false;
  99. this->confSection = "";
  100. this->nnz_per_dimension = NULL;
  101. this->num_examples = 0;
  102. this->num_dimension = 0;
  103. this->solver = NULL;
  104. this->q = NULL;
  105. this->gm = NULL;
  106. ///////////
  107. // here comes the new code part different from the empty constructor
  108. ///////////
  109. this->confSection = _confSection;
  110. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  111. // an emtpy config (using default values thereby) when calling the train-method
  112. if ( _conf != NULL )
  113. {
  114. this->initFromConfig( _conf, _confSection );
  115. }
  116. else
  117. {
  118. // if no config was given, we create an empty one
  119. NICE::Config tmpConfEmpty ;
  120. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  121. }
  122. }
  123. GPHIKRawClassifier::~GPHIKRawClassifier()
  124. {
  125. if ( this->solver != NULL )
  126. {
  127. delete this->solver;
  128. this->solver = NULL;
  129. }
  130. if ( this->gm != NULL)
  131. {
  132. delete this->gm;
  133. this->gm = NULL;
  134. }
  135. this->clearSetsOfTablesAandB();
  136. this->clearSetsOfTablesT();
  137. if ( this->q != NULL )
  138. {
  139. delete this->q;
  140. this->q = NULL;
  141. }
  142. }
  143. void GPHIKRawClassifier::initFromConfig(const Config *_conf,
  144. const string & _confSection
  145. )
  146. {
  147. this->d_noise = _conf->gD( _confSection, "noise", 0.01);
  148. this->confSection = _confSection;
  149. this->b_verbose = _conf->gB( _confSection, "verbose", false);
  150. this->b_debug = _conf->gB( _confSection, "debug", false);
  151. this->f_tolerance = _conf->gD( _confSection, "f_tolerance", 1e-10);
  152. //FIXME this is not used in that way for the standard GPHIKClassifier
  153. //string ilssection = "FMKGPHyperparameterOptimization";
  154. string ilssection = _confSection;
  155. uint ils_max_iterations = _conf->gI( ilssection, "ils_max_iterations", 1000 );
  156. double ils_min_delta = _conf->gD( ilssection, "ils_min_delta", 1e-7 );
  157. double ils_min_residual = _conf->gD( ilssection, "ils_min_residual", 1e-7 );
  158. bool ils_verbose = _conf->gB( ilssection, "ils_verbose", false );
  159. this->solver = new ILSConjugateGradients( ils_verbose,
  160. ils_max_iterations,
  161. ils_min_delta,
  162. ils_min_residual
  163. );
  164. if ( this->b_verbose )
  165. {
  166. std::cerr << "GPHIKRawClassifier::initFromConfig " <<std::endl;
  167. std::cerr << " confSection " << confSection << std::endl;
  168. std::cerr << " d_noise " << d_noise << std::endl;
  169. std::cerr << " f_tolerance " << f_tolerance << std::endl;
  170. std::cerr << " ils_max_iterations " << ils_max_iterations << std::endl;
  171. std::cerr << " ils_min_delta " << ils_min_delta << std::endl;
  172. std::cerr << " ils_min_residual " << ils_min_residual << std::endl;
  173. std::cerr << " ils_verbose " << ils_verbose << std::endl;
  174. }
  175. //quantization during classification?
  176. bool useQuantization = _conf->gB ( _confSection, "use_quantization", false );
  177. if ( this->b_verbose )
  178. {
  179. std::cerr << "_confSection: " << _confSection << std::endl;
  180. std::cerr << "use_quantization: " << useQuantization << std::endl;
  181. }
  182. if ( _conf->gB ( _confSection, "use_quantization", false ) )
  183. {
  184. int numBins = _conf->gI ( _confSection, "num_bins", 100 );
  185. if ( this->b_verbose )
  186. std::cerr << "FMKGPHyperparameterOptimization: quantization initialized with " << numBins << " bins." << std::endl;
  187. std::string s_quantType = _conf->gS( _confSection, "s_quantType", "1d-aequi-0-1" );
  188. if ( s_quantType == "1d-aequi-0-1" )
  189. {
  190. this->q = new NICE::Quantization1DAequiDist0To1 ( numBins );
  191. }
  192. else if ( s_quantType == "1d-aequi-0-max" )
  193. {
  194. this->q = new NICE::Quantization1DAequiDist0ToMax ( numBins );
  195. }
  196. else if ( s_quantType == "nd-aequi-0-max" )
  197. {
  198. this->q = new NICE::QuantizationNDAequiDist0ToMax ( numBins );
  199. }
  200. else
  201. {
  202. fthrow(Exception, "Quantization type is unknown " << s_quantType);
  203. }
  204. }
  205. else
  206. {
  207. this->q = NULL;
  208. }
  209. }
  210. ///////////////////// ///////////////////// /////////////////////
  211. // GET / SET
  212. ///////////////////// ///////////////////// /////////////////////
  213. std::set<uint> GPHIKRawClassifier::getKnownClassNumbers ( ) const
  214. {
  215. if ( ! this->b_isTrained )
  216. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  217. return this->knownClasses;
  218. }
  219. ///////////////////// ///////////////////// /////////////////////
  220. // CLASSIFIER STUFF
  221. ///////////////////// ///////////////////// /////////////////////
  222. void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
  223. uint & _result,
  224. SparseVector & _scores
  225. ) const
  226. {
  227. if ( ! this->b_isTrained )
  228. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  229. _scores.clear();
  230. // classification with quantization of test inputs
  231. if ( this->q != NULL )
  232. {
  233. uint maxClassNo = 0;
  234. for ( std::map< uint, double * >::const_iterator itT = this->precomputedT.begin() ;
  235. itT != this->precomputedT.end();
  236. itT++
  237. )
  238. {
  239. uint classno = itT->first;
  240. maxClassNo = std::max ( maxClassNo, classno );
  241. double beta = 0;
  242. double *T = itT->second;
  243. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++ )
  244. {
  245. uint dim = i->first;
  246. double v = i->second;
  247. uint qBin = this->q->quantize( v, dim );
  248. beta += T[dim * this->q->getNumberOfBins() + qBin];
  249. }//for-loop over dimensions of test input
  250. _scores[ classno ] = beta;
  251. }//for-loop over 1-vs-all models
  252. }
  253. // classification with exact test inputs, i.e., no quantization involved
  254. else
  255. {
  256. uint maxClassNo = 0;
  257. for ( std::map<uint, PrecomputedType>::const_iterator i = this->precomputedA.begin() ; i != this->precomputedA.end(); i++ )
  258. {
  259. uint classno = i->first;
  260. maxClassNo = std::max ( maxClassNo, classno );
  261. double beta = 0;
  262. GMHIKernelRaw::sparseVectorElement **dataMatrix = this->gm->getDataMatrix();
  263. const PrecomputedType & A = i->second;
  264. std::map<uint, PrecomputedType>::const_iterator j = this->precomputedB.find ( classno );
  265. const PrecomputedType & B = j->second;
  266. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++)
  267. {
  268. uint dim = i->first;
  269. double fval = i->second;
  270. uint nnz = this->nnz_per_dimension[dim];
  271. uint nz = this->num_examples - nnz;
  272. if ( nnz == 0 ) continue;
  273. // useful
  274. //if ( fval < this->f_tolerance ) continue;
  275. uint position = 0;
  276. //this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  277. GMHIKernelRaw::sparseVectorElement fval_element;
  278. fval_element.value = fval;
  279. //std::cerr << "value to search for " << fval << endl;
  280. //std::cerr << "data matrix in dimension " << dim << endl;
  281. //for (int j = 0; j < nnz; j++)
  282. // std::cerr << dataMatrix[dim][j].value << std::endl;
  283. GMHIKernelRaw::sparseVectorElement *it = upper_bound ( dataMatrix[dim], dataMatrix[dim] + nnz, fval_element );
  284. position = distance ( dataMatrix[dim], it );
  285. // /*// add zero elements
  286. // if ( fval_element.value > 0.0 )
  287. // position += nz;*/
  288. bool posIsZero ( position == 0 );
  289. // special case 1:
  290. // new example is smaller than all known examples
  291. // -> resulting value = fval * sum_l=1^n alpha_l
  292. if ( position == 0 )
  293. {
  294. beta += fval * B[ dim ][ nnz - 1 ];
  295. }
  296. // special case 2:
  297. // new example is equal to or larger than the largest training example in this dimension
  298. // -> the term B[ dim ][ nnz-1 ] - B[ dim ][ indexElem ] is equal to zero and vanishes, which is logical, since all elements are smaller than the remaining prototypes!
  299. else if ( position == nnz )
  300. {
  301. beta += A[ dim ][ nnz - 1 ];
  302. }
  303. // standard case: new example is larger then the smallest element, but smaller then the largest one in the corrent dimension
  304. else
  305. {
  306. beta += A[ dim ][ position - 1 ] + fval * B[ dim ][ position - 1 ];
  307. }
  308. // // correct upper bound to correct position, only possible if new example is not the smallest value in this dimension
  309. // if ( !posIsZero )
  310. // position--;
  311. //
  312. //
  313. // double firstPart = 0.0;
  314. // if ( !posIsZero )
  315. // firstPart = ( A[ dim ][ position ] );
  316. //
  317. // double secondPart( B[ dim ][ this->num_examples-1-nz ]);
  318. // if ( !posIsZero && (position >= nz) )
  319. // secondPart -= B[dim][ position ];
  320. //
  321. // // but apply using the transformed one
  322. // beta += firstPart + secondPart* fval;
  323. }//for-loop over dimensions of test input
  324. _scores[ classno ] = beta;
  325. }//for-loop over 1-vs-all models
  326. } // if-condition wrt quantization
  327. _scores.setDim ( *this->knownClasses.rbegin() + 1 );
  328. if ( this->knownClasses.size() > 2 )
  329. { // multi-class classification
  330. _result = _scores.maxElement();
  331. }
  332. else if ( this->knownClasses.size() == 2 ) // binary setting
  333. {
  334. uint class1 = *(this->knownClasses.begin());
  335. uint class2 = *(this->knownClasses.rbegin());
  336. uint class_for_which_we_have_a_score = _scores.begin()->first;
  337. uint class_for_which_we_dont_have_a_score = (class1 == class_for_which_we_have_a_score ? class2 : class1);
  338. _scores[class_for_which_we_dont_have_a_score] = - _scores[class_for_which_we_have_a_score];
  339. _result = _scores[class_for_which_we_have_a_score] > 0.0 ? class_for_which_we_have_a_score : class_for_which_we_dont_have_a_score;
  340. }
  341. }
  342. /** training process */
  343. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  344. const NICE::Vector & _labels
  345. )
  346. {
  347. // security-check: examples and labels have to be of same size
  348. if ( _examples.size() != _labels.size() )
  349. {
  350. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  351. }
  352. this->num_examples = _examples.size();
  353. this->knownClasses.clear();
  354. for ( uint i = 0; i < _labels.size(); i++ )
  355. this->knownClasses.insert((uint)_labels[i]);
  356. std::map<uint, NICE::Vector> binLabels;
  357. for ( set<uint>::const_iterator j = knownClasses.begin(); j != knownClasses.end(); j++ )
  358. {
  359. uint current_class = *j;
  360. Vector labels_binary ( _labels.size() );
  361. for ( uint i = 0; i < _labels.size(); i++ )
  362. {
  363. labels_binary[i] = ( _labels[i] == current_class ) ? 1.0 : -1.0;
  364. }
  365. binLabels.insert ( std::pair<uint, NICE::Vector>( current_class, labels_binary) );
  366. }
  367. // handle special binary case
  368. if ( knownClasses.size() == 2 )
  369. {
  370. std::map<uint, NICE::Vector>::iterator it = binLabels.begin();
  371. it++;
  372. binLabels.erase( binLabels.begin(), it );
  373. }
  374. this->train ( _examples, binLabels );
  375. }
  376. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  377. std::map<uint, NICE::Vector> & _binLabels
  378. )
  379. {
  380. // security-check: examples and labels have to be of same size
  381. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  382. binLabIt != _binLabels.end();
  383. binLabIt++
  384. )
  385. {
  386. if ( _examples.size() != binLabIt->second.size() )
  387. {
  388. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  389. }
  390. }
  391. if ( this->b_verbose )
  392. std::cerr << "GPHIKRawClassifier::train" << std::endl;
  393. Timer t;
  394. t.start();
  395. this->clearSetsOfTablesAandB();
  396. this->clearSetsOfTablesT();
  397. // sort examples in each dimension and "transpose" the feature matrix
  398. // set up the GenericMatrix interface
  399. if ( this->gm != NULL )
  400. delete this->gm;
  401. this->gm = new GMHIKernelRaw ( _examples, this->d_noise, this->q );
  402. this->nnz_per_dimension = this->gm->getNNZPerDimension();
  403. this->num_dimension = this->gm->getNumberOfDimensions();
  404. // compute largest eigenvalue of our kernel matrix
  405. // note: this guy is shared among all categories,
  406. // since the kernel matrix is shared as well
  407. NICE::Vector eigenMax;
  408. NICE::Matrix eigenMaxV;
  409. // for reproducibility during debuggin
  410. //FIXME
  411. srand ( 0 );
  412. srand48 ( 0 );
  413. NICE::EigValues * eig = new EVArnoldi ( false /* verbose flag */,
  414. 10 /*_maxiterations*/
  415. );
  416. eig->getEigenvalues( *gm, eigenMax, eigenMaxV, 1 /*rank*/ );
  417. delete eig;
  418. // set simple jacobi pre-conditioning
  419. NICE::Vector diagonalElements;
  420. this->gm->getDiagonalElements ( diagonalElements );
  421. this->solver->setJacobiPreconditioner ( diagonalElements );
  422. // solve linear equations for each class
  423. // be careful when parallising this!
  424. for ( std::map<uint, NICE::Vector>::const_iterator i = _binLabels.begin();
  425. i != _binLabels.end();
  426. i++
  427. )
  428. {
  429. uint classno = i->first;
  430. if (b_verbose)
  431. std::cerr << "Training for class " << classno << endl;
  432. const NICE::Vector & y = i->second;
  433. NICE::Vector alpha;
  434. /** About finding a good initial solution (see also GPLikelihoodApproximation)
  435. * K~ = K + sigma^2 I
  436. *
  437. * K~ \approx lambda_max v v^T
  438. * \lambda_max v v^T * alpha = k_* | multiply with v^T from left
  439. * => \lambda_max v^T alpha = v^T k_*
  440. * => alpha = k_* / lambda_max could be a good initial start
  441. * If we put everything in the first equation this gives us
  442. * v = k_*
  443. * This reduces the number of iterations by 5 or 8
  444. */
  445. alpha = (y * (1.0 / eigenMax[0]) );
  446. this->solver->solveLin( *gm, y, alpha );
  447. // get lookup tables, A, B, etc. and store them
  448. this->gm->updateTablesAandB( alpha );
  449. double **A = this->gm->getTableA();
  450. double **B = this->gm->getTableB();
  451. this->precomputedA.insert ( std::pair<uint, PrecomputedType> ( classno, A ) );
  452. this->precomputedB.insert ( std::pair<uint, PrecomputedType> ( classno, B ) );
  453. // Quantization for classification?
  454. if ( this->q != NULL )
  455. {
  456. this->gm->updateTableT( alpha );
  457. double *T = this->gm->getTableT ( );
  458. this->precomputedT.insert( std::pair<uint, double * > ( classno, T ) );
  459. }
  460. }
  461. // NOTE if quantization is turned on, we do not need LUTs A and B anymore
  462. if ( this->q != NULL )
  463. {
  464. this->clearSetsOfTablesAandB();
  465. }
  466. t.stop();
  467. if ( this->b_verbose )
  468. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  469. //indicate that we finished training successfully
  470. this->b_isTrained = true;
  471. // clean up all examples ??
  472. if ( this->b_verbose )
  473. std::cerr << "Learning finished" << std::endl;
  474. }