GPHIKRawClassifier.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "GPHIKRawClassifier.h"
  16. #include "GMHIKernelRaw.h"
  17. using namespace std;
  18. using namespace NICE;
  19. /////////////////////////////////////////////////////
  20. /////////////////////////////////////////////////////
  21. // PROTECTED METHODS
  22. /////////////////////////////////////////////////////
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25. /////////////////////////////////////////////////////
  26. // PUBLIC METHODS
  27. /////////////////////////////////////////////////////
  28. /////////////////////////////////////////////////////
  29. GPHIKRawClassifier::GPHIKRawClassifier( )
  30. {
  31. this->b_isTrained = false;
  32. this->confSection = "";
  33. this->nnz_per_dimension = NULL;
  34. this->q = NULL;
  35. this->gm = NULL;
  36. // in order to be sure about all necessary variables be setup with default values, we
  37. // run initFromConfig with an empty config
  38. NICE::Config tmpConfEmpty ;
  39. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  40. }
  41. GPHIKRawClassifier::GPHIKRawClassifier( const Config *_conf,
  42. const string & _confSection
  43. )
  44. {
  45. ///////////
  46. // same code as in empty constructor - duplication can be avoided with C++11 allowing for constructor delegation
  47. ///////////
  48. this->b_isTrained = false;
  49. this->confSection = "";
  50. this->nnz_per_dimension = NULL;
  51. this->q = NULL;
  52. this->gm = NULL;
  53. ///////////
  54. // here comes the new code part different from the empty constructor
  55. ///////////
  56. this->confSection = _confSection;
  57. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  58. // an emtpy config (using default values thereby) when calling the train-method
  59. if ( _conf != NULL )
  60. {
  61. this->initFromConfig( _conf, _confSection );
  62. }
  63. else
  64. {
  65. // if no config was given, we create an empty one
  66. NICE::Config tmpConfEmpty ;
  67. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  68. }
  69. }
  70. GPHIKRawClassifier::~GPHIKRawClassifier()
  71. {
  72. delete this->solver;
  73. this->solver = NULL;
  74. if (gm != NULL)
  75. delete gm;
  76. }
  77. void GPHIKRawClassifier::initFromConfig(const Config *_conf,
  78. const string & _confSection
  79. )
  80. {
  81. this->d_noise = _conf->gD( _confSection, "noise", 0.01);
  82. this->confSection = _confSection;
  83. this->b_verbose = _conf->gB( _confSection, "verbose", false);
  84. this->b_debug = _conf->gB( _confSection, "debug", false);
  85. this->f_tolerance = _conf->gD( _confSection, "f_tolerance", 1e-10);
  86. //FIXME this is not used in that way for the standard GPHIKClassifier
  87. //string ilssection = "FMKGPHyperparameterOptimization";
  88. string ilssection = _confSection;
  89. uint ils_max_iterations = _conf->gI( ilssection, "ils_max_iterations", 1000 );
  90. double ils_min_delta = _conf->gD( ilssection, "ils_min_delta", 1e-7 );
  91. double ils_min_residual = _conf->gD( ilssection, "ils_min_residual", 1e-7 );
  92. bool ils_verbose = _conf->gB( ilssection, "ils_verbose", false );
  93. this->solver = new ILSConjugateGradients( ils_verbose,
  94. ils_max_iterations,
  95. ils_min_delta,
  96. ils_min_residual
  97. );
  98. // variables for the eigen value decomposition technique
  99. this->b_eig_verbose = _conf->gB ( _confSection, "eig_verbose", false );
  100. this->i_eig_value_max_iterations = _conf->gI ( _confSection, "eig_value_max_iterations", 10 );
  101. if ( this->b_verbose )
  102. {
  103. std::cerr << "GPHIKRawClassifier::initFromConfig " <<std::endl;
  104. std::cerr << " confSection " << confSection << std::endl;
  105. std::cerr << " d_noise " << d_noise << std::endl;
  106. std::cerr << " f_tolerance " << f_tolerance << std::endl;
  107. std::cerr << " ils_max_iterations " << ils_max_iterations << std::endl;
  108. std::cerr << " ils_min_delta " << ils_min_delta << std::endl;
  109. std::cerr << " ils_min_residual " << ils_min_residual << std::endl;
  110. std::cerr << " ils_verbose " << ils_verbose << std::endl;
  111. std::cerr << " b_eig_verbose " << b_eig_verbose << std::endl;
  112. std::cerr << " i_eig_value_max_iterations " << i_eig_value_max_iterations << std::endl;
  113. }
  114. }
  115. ///////////////////// ///////////////////// /////////////////////
  116. // GET / SET
  117. ///////////////////// ///////////////////// /////////////////////
  118. std::set<uint> GPHIKRawClassifier::getKnownClassNumbers ( ) const
  119. {
  120. if ( ! this->b_isTrained )
  121. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  122. return this->knownClasses;
  123. }
  124. ///////////////////// ///////////////////// /////////////////////
  125. // CLASSIFIER STUFF
  126. ///////////////////// ///////////////////// /////////////////////
  127. void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
  128. uint & _result,
  129. SparseVector & _scores
  130. ) const
  131. {
  132. if ( ! this->b_isTrained )
  133. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  134. _scores.clear();
  135. GMHIKernelRaw::sparseVectorElement **dataMatrix = gm->getDataMatrix();
  136. uint maxClassNo = 0;
  137. for ( std::map<uint, PrecomputedType>::const_iterator i = this->precomputedA.begin() ; i != this->precomputedA.end(); i++ )
  138. {
  139. uint classno = i->first;
  140. maxClassNo = std::max ( maxClassNo, classno );
  141. double beta = 0;
  142. if ( this->q != NULL ) {
  143. std::map<uint, double *>::const_iterator j = this->precomputedT.find ( classno );
  144. double *T = j->second;
  145. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++ )
  146. {
  147. uint dim = i->first;
  148. double v = i->second;
  149. uint qBin = q->quantize( v, dim );
  150. beta += T[dim * q->getNumberOfBins() + qBin];
  151. }
  152. } else {
  153. const PrecomputedType & A = i->second;
  154. std::map<uint, PrecomputedType>::const_iterator j = this->precomputedB.find ( classno );
  155. const PrecomputedType & B = j->second;
  156. for (SparseVector::const_iterator i = _xstar->begin(); i != _xstar->end(); i++)
  157. {
  158. uint dim = i->first;
  159. double fval = i->second;
  160. uint nnz = this->nnz_per_dimension[dim];
  161. uint nz = this->num_examples - nnz;
  162. if ( nnz == 0 ) continue;
  163. // useful
  164. //if ( fval < this->f_tolerance ) continue;
  165. uint position = 0;
  166. //this->X_sorted.findFirstLargerInDimension(dim, fval, position);
  167. GMHIKernelRaw::sparseVectorElement fval_element;
  168. fval_element.value = fval;
  169. //std::cerr << "value to search for " << fval << endl;
  170. //std::cerr << "data matrix in dimension " << dim << endl;
  171. //for (int j = 0; j < nnz; j++)
  172. // std::cerr << dataMatrix[dim][j].value << std::endl;
  173. GMHIKernelRaw::sparseVectorElement *it = upper_bound ( dataMatrix[dim], dataMatrix[dim] + nnz, fval_element );
  174. position = distance ( dataMatrix[dim], it );
  175. // add zero elements
  176. if ( fval_element.value > 0.0 )
  177. position += nz;
  178. bool posIsZero ( position == 0 );
  179. if ( !posIsZero )
  180. position--;
  181. double firstPart = 0.0;
  182. if ( !posIsZero && ((position-nz) < this->num_examples) )
  183. firstPart = (A[dim][position-nz]);
  184. double secondPart( B[dim][this->num_examples-1-nz]);
  185. if ( !posIsZero && (position >= nz) )
  186. secondPart -= B[dim][position-nz];
  187. // but apply using the transformed one
  188. beta += firstPart + secondPart* fval;
  189. }
  190. }
  191. _scores[ classno ] = beta;
  192. }
  193. _scores.setDim ( *this->knownClasses.rbegin() + 1 );
  194. if ( this->knownClasses.size() > 2 )
  195. { // multi-class classification
  196. _result = _scores.maxElement();
  197. }
  198. else if ( this->knownClasses.size() == 2 ) // binary setting
  199. {
  200. uint class1 = *(this->knownClasses.begin());
  201. uint class2 = *(this->knownClasses.rbegin());
  202. uint class_for_which_we_have_a_score = _scores.begin()->first;
  203. uint class_for_which_we_dont_have_a_score = (class1 == class_for_which_we_have_a_score ? class2 : class1);
  204. _scores[class_for_which_we_dont_have_a_score] = - _scores[class_for_which_we_have_a_score];
  205. _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;
  206. }
  207. }
  208. /** training process */
  209. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  210. const NICE::Vector & _labels
  211. )
  212. {
  213. // security-check: examples and labels have to be of same size
  214. if ( _examples.size() != _labels.size() )
  215. {
  216. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  217. }
  218. this->num_examples = _examples.size();
  219. this->knownClasses.clear();
  220. for ( uint i = 0; i < _labels.size(); i++ )
  221. this->knownClasses.insert((uint)_labels[i]);
  222. std::map<uint, NICE::Vector> binLabels;
  223. for ( set<uint>::const_iterator j = knownClasses.begin(); j != knownClasses.end(); j++ )
  224. {
  225. uint current_class = *j;
  226. Vector labels_binary ( _labels.size() );
  227. for ( uint i = 0; i < _labels.size(); i++ )
  228. labels_binary[i] = ( _labels[i] == current_class ) ? 1.0 : -1.0;
  229. binLabels.insert ( pair<uint, NICE::Vector>( current_class, labels_binary) );
  230. }
  231. // handle special binary case
  232. if ( knownClasses.size() == 2 )
  233. {
  234. std::map<uint, NICE::Vector>::iterator it = binLabels.begin();
  235. it++;
  236. binLabels.erase( binLabels.begin(), it );
  237. }
  238. this->train ( _examples, binLabels );
  239. }
  240. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  241. std::map<uint, NICE::Vector> & _binLabels
  242. )
  243. {
  244. // security-check: examples and labels have to be of same size
  245. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  246. binLabIt != _binLabels.end();
  247. binLabIt++
  248. )
  249. {
  250. if ( _examples.size() != binLabIt->second.size() )
  251. {
  252. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  253. }
  254. }
  255. if ( this->b_verbose )
  256. std::cerr << "GPHIKRawClassifier::train" << std::endl;
  257. Timer t;
  258. t.start();
  259. precomputedA.clear();
  260. precomputedB.clear();
  261. precomputedT.clear();
  262. // sort examples in each dimension and "transpose" the feature matrix
  263. // set up the GenericMatrix interface
  264. if (gm != NULL)
  265. delete gm;
  266. gm = new GMHIKernelRaw ( _examples, this->d_noise );
  267. nnz_per_dimension = gm->getNNZPerDimension();
  268. // compute largest eigenvalue of our kernel matrix
  269. // note: this guy is shared among all categories,
  270. // since the kernel matrix is shared as well
  271. NICE::Vector eigenMax;
  272. NICE::Matrix eigenMaxV;
  273. // for reproducibility during debuggin
  274. srand ( 0 );
  275. srand48 ( 0 );
  276. NICE::EigValues * eig = new EVArnoldi ( this->b_eig_verbose ,
  277. this->i_eig_value_max_iterations
  278. );
  279. eig->getEigenvalues( *gm, eigenMax, eigenMaxV, 1 /*rank*/ );
  280. delete eig;
  281. // set simple jacobi pre-conditioning
  282. NICE::Vector diagonalElements;
  283. gm->getDiagonalElements ( diagonalElements );
  284. solver->setJacobiPreconditioner ( diagonalElements );
  285. // solve linear equations for each class
  286. // be careful when parallising this!
  287. for ( std::map<uint, NICE::Vector>::const_iterator i = _binLabels.begin();
  288. i != _binLabels.end();
  289. i++
  290. )
  291. {
  292. uint classno = i->first;
  293. if (b_verbose)
  294. std::cerr << "Training for class " << classno << endl;
  295. const NICE::Vector & y = i->second;
  296. NICE::Vector alpha;
  297. /** About finding a good initial solution (see also GPLikelihoodApproximation)
  298. * K~ = K + sigma^2 I
  299. *
  300. * K~ \approx lambda_max v v^T
  301. * \lambda_max v v^T * alpha = k_* | multiply with v^T from left
  302. * => \lambda_max v^T alpha = v^T k_*
  303. * => alpha = k_* / lambda_max could be a good initial start
  304. * If we put everything in the first equation this gives us
  305. * v = k_*
  306. * This reduces the number of iterations by 5 or 8
  307. */
  308. alpha = (y * (1.0 / eigenMax[0]) );
  309. solver->solveLin( *gm, y, alpha );
  310. // TODO: get lookup tables, A, B, etc. and store them
  311. gm->updateTables(alpha);
  312. double **A = gm->getTableA();
  313. double **B = gm->getTableB();
  314. precomputedA.insert ( pair<uint, PrecomputedType> ( classno, A ) );
  315. precomputedB.insert ( pair<uint, PrecomputedType> ( classno, B ) );
  316. }
  317. t.stop();
  318. if ( this->b_verbose )
  319. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  320. //indicate that we finished training successfully
  321. this->b_isTrained = true;
  322. // clean up all examples ??
  323. if ( this->b_verbose )
  324. std::cerr << "Learning finished" << std::endl;
  325. }