GPHIKRawClassifier.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. double * GPHIKRawClassifier::computeTableT ( const NICE::Vector & _alpha
  29. )
  30. {
  31. if (this->q == NULL )
  32. {
  33. return NULL;
  34. }
  35. //
  36. // number of quantization bins
  37. uint hmax = _q->getNumberOfBins();
  38. // store (transformed) prototypes
  39. double * prototypes = new double [ hmax * this->num_dimension ];
  40. double * p_prototypes = prototypes;
  41. for (uint dim = 0; dim < this->num_dimension; dim++)
  42. {
  43. for ( uint i = 0 ; i < hmax ; i++ )
  44. {
  45. if ( _pf != NULL )
  46. {
  47. *p_prototypes = _pf->f ( dim, _q->getPrototype( i, dim ) );
  48. } else
  49. {
  50. *p_prototypes = _q->getPrototype( i, dim );
  51. }
  52. p_prototypes++;
  53. }
  54. }
  55. //allocate memory for the LUT
  56. double *Tlookup = new double [ hmax * this->num_dimension ];
  57. // loop through all dimensions
  58. for (uint dim = 0; dim < this->ui_d; dim++)
  59. {
  60. if ( nnz_per_dimension[dim] == 0 )
  61. continue;
  62. double alphaSumTotalInDim(0.0);
  63. double alphaTimesXSumTotalInDim(0.0);
  64. for ( SortedVectorSparse<double>::const_elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  65. {
  66. alphaSumTotalInDim += _alpha[i->second.first];
  67. alphaTimesXSumTotalInDim += _alpha[i->second.first] * i->second.second;
  68. }
  69. }
  70. //don't waste memory
  71. delete [] prototypes;
  72. return Tlookup;
  73. }
  74. /////////////////////////////////////////////////////
  75. /////////////////////////////////////////////////////
  76. // PUBLIC METHODS
  77. /////////////////////////////////////////////////////
  78. /////////////////////////////////////////////////////
  79. GPHIKRawClassifier::GPHIKRawClassifier( )
  80. {
  81. this->b_isTrained = false;
  82. this->confSection = "";
  83. this->nnz_per_dimension = 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->q = NULL;
  102. this->gm = NULL;
  103. ///////////
  104. // here comes the new code part different from the empty constructor
  105. ///////////
  106. this->confSection = _confSection;
  107. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  108. // an emtpy config (using default values thereby) when calling the train-method
  109. if ( _conf != NULL )
  110. {
  111. this->initFromConfig( _conf, _confSection );
  112. }
  113. else
  114. {
  115. // if no config was given, we create an empty one
  116. NICE::Config tmpConfEmpty ;
  117. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  118. }
  119. }
  120. GPHIKRawClassifier::~GPHIKRawClassifier()
  121. {
  122. delete this->solver;
  123. this->solver = NULL;
  124. if (gm != NULL)
  125. delete gm;
  126. }
  127. void GPHIKRawClassifier::initFromConfig(const Config *_conf,
  128. const string & _confSection
  129. )
  130. {
  131. this->d_noise = _conf->gD( _confSection, "noise", 0.01);
  132. this->confSection = _confSection;
  133. this->b_verbose = _conf->gB( _confSection, "verbose", false);
  134. this->b_debug = _conf->gB( _confSection, "debug", false);
  135. this->f_tolerance = _conf->gD( _confSection, "f_tolerance", 1e-10);
  136. //FIXME this is not used in that way for the standard GPHIKClassifier
  137. //string ilssection = "FMKGPHyperparameterOptimization";
  138. string ilssection = _confSection;
  139. uint ils_max_iterations = _conf->gI( ilssection, "ils_max_iterations", 1000 );
  140. double ils_min_delta = _conf->gD( ilssection, "ils_min_delta", 1e-7 );
  141. double ils_min_residual = _conf->gD( ilssection, "ils_min_residual", 1e-7 );
  142. bool ils_verbose = _conf->gB( ilssection, "ils_verbose", false );
  143. this->solver = new ILSConjugateGradients( ils_verbose,
  144. ils_max_iterations,
  145. ils_min_delta,
  146. ils_min_residual
  147. );
  148. if ( this->b_verbose )
  149. {
  150. std::cerr << "GPHIKRawClassifier::initFromConfig " <<std::endl;
  151. std::cerr << " confSection " << confSection << std::endl;
  152. std::cerr << " d_noise " << d_noise << std::endl;
  153. std::cerr << " f_tolerance " << f_tolerance << std::endl;
  154. std::cerr << " ils_max_iterations " << ils_max_iterations << std::endl;
  155. std::cerr << " ils_min_delta " << ils_min_delta << std::endl;
  156. std::cerr << " ils_min_residual " << ils_min_residual << std::endl;
  157. std::cerr << " ils_verbose " << ils_verbose << std::endl;
  158. }
  159. //quantization during classification?
  160. bool useQuantization = _conf->gB ( _confSection, "use_quantization", false );
  161. if ( this->b_verbose )
  162. {
  163. std::cerr << "_confSection: " << _confSection << std::endl;
  164. std::cerr << "use_quantization: " << useQuantization << std::endl;
  165. }
  166. if ( _conf->gB ( _confSection, "use_quantization", false ) )
  167. {
  168. int numBins = _conf->gI ( _confSection, "num_bins", 100 );
  169. if ( this->b_verbose )
  170. std::cerr << "FMKGPHyperparameterOptimization: quantization initialized with " << numBins << " bins." << std::endl;
  171. std::string s_quantType = _conf->gS( _confSection, "s_quantType", "1d-aequi-0-1" );
  172. if ( s_quantType == "1d-aequi-0-1" )
  173. {
  174. this->q = new NICE::Quantization1DAequiDist0To1 ( numBins );
  175. }
  176. else if ( s_quantType == "1d-aequi-0-max" )
  177. {
  178. this->q = new NICE::Quantization1DAequiDist0ToMax ( numBins );
  179. }
  180. else if ( s_quantType == "nd-aequi-0-max" )
  181. {
  182. this->q = new NICE::QuantizationNDAequiDist0ToMax ( numBins );
  183. }
  184. else
  185. {
  186. fthrow(Exception, "Quantization type is unknown " << s_quantType);
  187. }
  188. }
  189. else
  190. {
  191. this->q = NULL;
  192. }
  193. }
  194. ///////////////////// ///////////////////// /////////////////////
  195. // GET / SET
  196. ///////////////////// ///////////////////// /////////////////////
  197. std::set<uint> GPHIKRawClassifier::getKnownClassNumbers ( ) const
  198. {
  199. if ( ! this->b_isTrained )
  200. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  201. return this->knownClasses;
  202. }
  203. ///////////////////// ///////////////////// /////////////////////
  204. // CLASSIFIER STUFF
  205. ///////////////////// ///////////////////// /////////////////////
  206. void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
  207. uint & _result,
  208. SparseVector & _scores
  209. ) const
  210. {
  211. if ( ! this->b_isTrained )
  212. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  213. _scores.clear();
  214. GMHIKernelRaw::sparseVectorElement **dataMatrix = gm->getDataMatrix();
  215. uint maxClassNo = 0;
  216. for ( std::map<uint, PrecomputedType>::const_iterator i = this->precomputedA.begin() ; i != this->precomputedA.end(); i++ )
  217. {
  218. uint classno = i->first;
  219. maxClassNo = std::max ( maxClassNo, classno );
  220. double beta = 0;
  221. if ( this->q != NULL ) {
  222. std::map<uint, double *>::const_iterator j = this->precomputedT.find ( classno );
  223. double *T = j->second;
  224. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++ )
  225. {
  226. uint dim = i->first;
  227. double v = i->second;
  228. uint qBin = q->quantize( v, dim );
  229. beta += T[dim * q->getNumberOfBins() + qBin];
  230. }
  231. } else {
  232. const PrecomputedType & A = i->second;
  233. std::map<uint, PrecomputedType>::const_iterator j = this->precomputedB.find ( classno );
  234. const PrecomputedType & B = j->second;
  235. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++)
  236. {
  237. uint dim = i->first;
  238. double fval = i->second;
  239. uint nnz = this->nnz_per_dimension[dim];
  240. uint nz = this->num_examples - nnz;
  241. if ( nnz == 0 ) continue;
  242. // useful
  243. //if ( fval < this->f_tolerance ) continue;
  244. uint position = 0;
  245. //this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  246. GMHIKernelRaw::sparseVectorElement fval_element;
  247. fval_element.value = fval;
  248. //std::cerr << "value to search for " << fval << endl;
  249. //std::cerr << "data matrix in dimension " << dim << endl;
  250. //for (int j = 0; j < nnz; j++)
  251. // std::cerr << dataMatrix[dim][j].value << std::endl;
  252. GMHIKernelRaw::sparseVectorElement *it = upper_bound ( dataMatrix[dim], dataMatrix[dim] + nnz, fval_element );
  253. position = distance ( dataMatrix[dim], it );
  254. // add zero elements
  255. if ( fval_element.value > 0.0 )
  256. position += nz;
  257. bool posIsZero ( position == 0 );
  258. if ( !posIsZero )
  259. position--;
  260. double firstPart = 0.0;
  261. if ( !posIsZero && ((position-nz) < this->num_examples) )
  262. firstPart = (A[dim][position-nz]);
  263. double secondPart( B[dim][this->num_examples-1-nz]);
  264. if ( !posIsZero && (position >= nz) )
  265. secondPart -= B[dim][position-nz];
  266. // but apply using the transformed one
  267. beta += firstPart + secondPart* fval;
  268. }
  269. }
  270. _scores[ classno ] = beta;
  271. }
  272. _scores.setDim ( *this->knownClasses.rbegin() + 1 );
  273. if ( this->knownClasses.size() > 2 )
  274. { // multi-class classification
  275. _result = _scores.maxElement();
  276. }
  277. else if ( this->knownClasses.size() == 2 ) // binary setting
  278. {
  279. uint class1 = *(this->knownClasses.begin());
  280. uint class2 = *(this->knownClasses.rbegin());
  281. uint class_for_which_we_have_a_score = _scores.begin()->first;
  282. uint class_for_which_we_dont_have_a_score = (class1 == class_for_which_we_have_a_score ? class2 : class1);
  283. _scores[class_for_which_we_dont_have_a_score] = - _scores[class_for_which_we_have_a_score];
  284. _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;
  285. }
  286. }
  287. /** training process */
  288. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  289. const NICE::Vector & _labels
  290. )
  291. {
  292. // security-check: examples and labels have to be of same size
  293. if ( _examples.size() != _labels.size() )
  294. {
  295. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  296. }
  297. this->num_examples = _examples.size();
  298. this->knownClasses.clear();
  299. for ( uint i = 0; i < _labels.size(); i++ )
  300. this->knownClasses.insert((uint)_labels[i]);
  301. std::map<uint, NICE::Vector> binLabels;
  302. for ( set<uint>::const_iterator j = knownClasses.begin(); j != knownClasses.end(); j++ )
  303. {
  304. uint current_class = *j;
  305. Vector labels_binary ( _labels.size() );
  306. for ( uint i = 0; i < _labels.size(); i++ )
  307. labels_binary[i] = ( _labels[i] == current_class ) ? 1.0 : -1.0;
  308. binLabels.insert ( pair<uint, NICE::Vector>( current_class, labels_binary) );
  309. }
  310. // handle special binary case
  311. if ( knownClasses.size() == 2 )
  312. {
  313. std::map<uint, NICE::Vector>::iterator it = binLabels.begin();
  314. it++;
  315. binLabels.erase( binLabels.begin(), it );
  316. }
  317. this->train ( _examples, binLabels );
  318. }
  319. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  320. std::map<uint, NICE::Vector> & _binLabels
  321. )
  322. {
  323. // security-check: examples and labels have to be of same size
  324. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  325. binLabIt != _binLabels.end();
  326. binLabIt++
  327. )
  328. {
  329. if ( _examples.size() != binLabIt->second.size() )
  330. {
  331. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  332. }
  333. }
  334. if ( this->b_verbose )
  335. std::cerr << "GPHIKRawClassifier::train" << std::endl;
  336. Timer t;
  337. t.start();
  338. precomputedA.clear();
  339. precomputedB.clear();
  340. precomputedT.clear();
  341. // sort examples in each dimension and "transpose" the feature matrix
  342. // set up the GenericMatrix interface
  343. if (gm != NULL)
  344. delete gm;
  345. gm = new GMHIKernelRaw ( _examples, this->d_noise, this->q );
  346. this->nnz_per_dimension = gm->getNNZPerDimension();
  347. this->num_dimension = gm->getNumberOfDimensions();
  348. // compute largest eigenvalue of our kernel matrix
  349. // note: this guy is shared among all categories,
  350. // since the kernel matrix is shared as well
  351. NICE::Vector eigenMax;
  352. NICE::Matrix eigenMaxV;
  353. // for reproducibility during debuggin
  354. srand ( 0 );
  355. srand48 ( 0 );
  356. NICE::EigValues * eig = new EVArnoldi ( false /* verbose flag */,
  357. 10 /*_maxiterations*/
  358. );
  359. eig->getEigenvalues( *gm, eigenMax, eigenMaxV, 1 /*rank*/ );
  360. delete eig;
  361. // set simple jacobi pre-conditioning
  362. NICE::Vector diagonalElements;
  363. gm->getDiagonalElements ( diagonalElements );
  364. solver->setJacobiPreconditioner ( diagonalElements );
  365. // solve linear equations for each class
  366. // be careful when parallising this!
  367. for ( std::map<uint, NICE::Vector>::const_iterator i = _binLabels.begin();
  368. i != _binLabels.end();
  369. i++
  370. )
  371. {
  372. uint classno = i->first;
  373. if (b_verbose)
  374. std::cerr << "Training for class " << classno << endl;
  375. const NICE::Vector & y = i->second;
  376. NICE::Vector alpha;
  377. /** About finding a good initial solution (see also GPLikelihoodApproximation)
  378. * K~ = K + sigma^2 I
  379. *
  380. * K~ \approx lambda_max v v^T
  381. * \lambda_max v v^T * alpha = k_* | multiply with v^T from left
  382. * => \lambda_max v^T alpha = v^T k_*
  383. * => alpha = k_* / lambda_max could be a good initial start
  384. * If we put everything in the first equation this gives us
  385. * v = k_*
  386. * This reduces the number of iterations by 5 or 8
  387. */
  388. alpha = (y * (1.0 / eigenMax[0]) );
  389. solver->solveLin( *gm, y, alpha );
  390. // TODO: get lookup tables, A, B, etc. and store them
  391. gm->updateTables(alpha);
  392. double **A = gm->getTableA();
  393. double **B = gm->getTableB();
  394. precomputedA.insert ( pair<uint, PrecomputedType> ( classno, A ) );
  395. precomputedB.insert ( pair<uint, PrecomputedType> ( classno, B ) );
  396. // Quantization for classification?
  397. if ( this->q != NULL )
  398. {
  399. // (2) then compute the corresponding look-up table T
  400. double **B = gm->getTableT();
  401. double *T = this->computeTableT ( alpha );
  402. precomputedT.insert( pair<uint, PrecomputedType> ( classno, T ) );
  403. }
  404. }
  405. t.stop();
  406. if ( this->b_verbose )
  407. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  408. //indicate that we finished training successfully
  409. this->b_isTrained = true;
  410. // clean up all examples ??
  411. if ( this->b_verbose )
  412. std::cerr << "Learning finished" << std::endl;
  413. }