GPHIKRawClassifier.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // gp-hik-core includes
  14. #include "GPHIKRawClassifier.h"
  15. #include "GMHIKernelRaw.h"
  16. using namespace std;
  17. using namespace NICE;
  18. /////////////////////////////////////////////////////
  19. /////////////////////////////////////////////////////
  20. // PROTECTED METHODS
  21. /////////////////////////////////////////////////////
  22. /////////////////////////////////////////////////////
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25. // PUBLIC METHODS
  26. /////////////////////////////////////////////////////
  27. /////////////////////////////////////////////////////
  28. GPHIKRawClassifier::GPHIKRawClassifier( )
  29. {
  30. this->b_isTrained = false;
  31. this->confSection = "";
  32. // in order to be sure about all necessary variables be setup with default values, we
  33. // run initFromConfig with an empty config
  34. NICE::Config tmpConfEmpty ;
  35. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  36. }
  37. GPHIKRawClassifier::GPHIKRawClassifier( const Config *_conf,
  38. const string & _confSection
  39. )
  40. {
  41. ///////////
  42. // same code as in empty constructor - duplication can be avoided with C++11 allowing for constructor delegation
  43. ///////////
  44. this->b_isTrained = false;
  45. this->confSection = "";
  46. ///////////
  47. // here comes the new code part different from the empty constructor
  48. ///////////
  49. this->confSection = _confSection;
  50. // if no config file was given, we either restore the classifier from an external file, or run ::init with
  51. // an emtpy config (using default values thereby) when calling the train-method
  52. if ( _conf != NULL )
  53. {
  54. this->initFromConfig( _conf, _confSection );
  55. }
  56. else
  57. {
  58. // if no config was given, we create an empty one
  59. NICE::Config tmpConfEmpty ;
  60. this->initFromConfig ( &tmpConfEmpty, this->confSection );
  61. }
  62. }
  63. GPHIKRawClassifier::~GPHIKRawClassifier()
  64. {
  65. delete solver;
  66. }
  67. void GPHIKRawClassifier::initFromConfig(const Config *_conf,
  68. const string & _confSection
  69. )
  70. {
  71. this->d_noise = _conf->gD( _confSection, "noise", 0.01);
  72. this->confSection = _confSection;
  73. this->b_verbose = _conf->gB( _confSection, "verbose", false);
  74. this->b_debug = _conf->gB( _confSection, "debug", false);
  75. string ilssection = "FMKGPHyperparameterOptimization";
  76. uint ils_max_iterations = _conf->gI( ilssection, "ils_max_iterations", 1000 );
  77. double ils_min_delta = _conf->gD( ilssection, "ils_min_delta", 1e-7 );
  78. double ils_min_residual = _conf->gD( ilssection, "ils_min_residual", 1e-7 );
  79. bool ils_verbose = _conf->gB( ilssection, "ils_verbose", false );
  80. this->solver = new ILSConjugateGradients( ils_verbose, ils_max_iterations, ils_min_delta, ils_min_residual );
  81. }
  82. ///////////////////// ///////////////////// /////////////////////
  83. // GET / SET
  84. ///////////////////// ///////////////////// /////////////////////
  85. std::set<uint> GPHIKRawClassifier::getKnownClassNumbers ( ) const
  86. {
  87. if ( ! this->b_isTrained )
  88. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  89. fthrow(Exception, "GPHIKRawClassifier::getKnownClassNumbers() not yet implemented");
  90. }
  91. ///////////////////// ///////////////////// /////////////////////
  92. // CLASSIFIER STUFF
  93. ///////////////////// ///////////////////// /////////////////////
  94. void GPHIKRawClassifier::classify ( const SparseVector * _example,
  95. uint & _result,
  96. SparseVector & _scores
  97. ) const
  98. {
  99. if ( ! this->b_isTrained )
  100. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  101. _scores.clear();
  102. if ( this->b_debug )
  103. {
  104. std::cerr << "GPHIKRawClassifier::classify (sparse)" << std::endl;
  105. _example->store( std::cerr );
  106. }
  107. // MAGIC happens here....
  108. // ...
  109. if ( this->b_debug )
  110. {
  111. _scores.store ( std::cerr );
  112. std::cerr << "_result: " << _result << std::endl;
  113. }
  114. if ( _scores.size() == 0 ) {
  115. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << _example->size() );
  116. }
  117. }
  118. void GPHIKRawClassifier::classify ( const NICE::Vector * _example,
  119. uint & _result,
  120. SparseVector & _scores
  121. ) const
  122. {
  123. fthrow(Exception, "GPHIKRawClassifier::classify( Vector ... ) not yet implemented");
  124. }
  125. /** training process */
  126. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  127. const NICE::Vector & _labels
  128. )
  129. {
  130. // security-check: examples and labels have to be of same size
  131. if ( _examples.size() != _labels.size() )
  132. {
  133. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  134. }
  135. set<uint> classes;
  136. for ( uint i = 0; i < _labels.size(); i++ )
  137. classes.insert((uint)_labels[i]);
  138. std::map<uint, NICE::Vector> binLabels;
  139. for ( set<uint>::const_iterator j = classes.begin(); j != classes.end(); j++ )
  140. {
  141. uint current_class = *j;
  142. Vector labels_binary ( _labels.size() );
  143. for ( uint i = 0; i < _labels.size(); i++ )
  144. labels_binary[i] = ( _labels[i] == current_class ) ? 1.0 : -1.0;
  145. binLabels.insert ( pair<uint, NICE::Vector>( current_class, labels_binary) );
  146. }
  147. train ( _examples, binLabels );
  148. }
  149. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  150. std::map<uint, NICE::Vector> & _binLabels
  151. )
  152. {
  153. // security-check: examples and labels have to be of same size
  154. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  155. binLabIt != _binLabels.end();
  156. binLabIt++
  157. )
  158. {
  159. if ( _examples.size() != binLabIt->second.size() )
  160. {
  161. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  162. }
  163. }
  164. if ( this->b_verbose )
  165. std::cerr << "GPHIKRawClassifier::train" << std::endl;
  166. Timer t;
  167. t.start();
  168. // sort examples in each dimension and "transpose" the feature matrix
  169. // set up the GenericMatrix interface
  170. GMHIKernelRaw gm ( _examples, this->d_noise );
  171. // solve linear equations for each class
  172. for ( map<uint, NICE::Vector>::const_iterator i = _binLabels.begin();
  173. i != _binLabels.end(); i++ )
  174. {
  175. const Vector & y = i->second;
  176. Vector alpha;
  177. solver->solveLin( gm, y, alpha );
  178. // TODO: get lookup tables, A, B, etc. and store them
  179. }
  180. t.stop();
  181. if ( this->b_verbose )
  182. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  183. //indicate that we finished training successfully
  184. this->b_isTrained = true;
  185. // clean up all examples ??
  186. if ( this->b_verbose )
  187. std::cerr << "Learning finished" << std::endl;
  188. }