GPHIKRawClassifier.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. }
  66. void GPHIKRawClassifier::initFromConfig(const Config *_conf,
  67. const string & _confSection
  68. )
  69. {
  70. this->d_noise = _conf->gD( _confSection, "noise", 0.01);
  71. this->confSection = _confSection;
  72. this->b_verbose = _conf->gB( _confSection, "verbose", false);
  73. this->b_debug = _conf->gB( _confSection, "debug", false);
  74. }
  75. ///////////////////// ///////////////////// /////////////////////
  76. // GET / SET
  77. ///////////////////// ///////////////////// /////////////////////
  78. std::set<uint> GPHIKRawClassifier::getKnownClassNumbers ( ) const
  79. {
  80. if ( ! this->b_isTrained )
  81. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  82. fthrow(Exception, "GPHIKRawClassifier::getKnownClassNumbers() not yet implemented");
  83. }
  84. ///////////////////// ///////////////////// /////////////////////
  85. // CLASSIFIER STUFF
  86. ///////////////////// ///////////////////// /////////////////////
  87. void GPHIKRawClassifier::classify ( const SparseVector * _example,
  88. uint & _result,
  89. SparseVector & _scores
  90. ) const
  91. {
  92. if ( ! this->b_isTrained )
  93. fthrow(Exception, "Classifier not trained yet -- aborting!" );
  94. _scores.clear();
  95. if ( this->b_debug )
  96. {
  97. std::cerr << "GPHIKRawClassifier::classify (sparse)" << std::endl;
  98. _example->store( std::cerr );
  99. }
  100. // MAGIC happens here....
  101. // ...
  102. if ( this->b_debug )
  103. {
  104. _scores.store ( std::cerr );
  105. std::cerr << "_result: " << _result << std::endl;
  106. }
  107. if ( _scores.size() == 0 ) {
  108. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << _example->size() );
  109. }
  110. }
  111. void GPHIKRawClassifier::classify ( const NICE::Vector * _example,
  112. uint & _result,
  113. SparseVector & _scores
  114. ) const
  115. {
  116. fthrow(Exception, "GPHIKRawClassifier::classify( Vector ... ) not yet implemented");
  117. }
  118. /** training process */
  119. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  120. const NICE::Vector & _labels
  121. )
  122. {
  123. // security-check: examples and labels have to be of same size
  124. if ( _examples.size() != _labels.size() )
  125. {
  126. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  127. }
  128. set<uint> classes;
  129. for ( uint i = 0; i < _labels.size(); i++ )
  130. classes.insert((uint)_labels[i]);
  131. std::map<uint, NICE::Vector> binLabels;
  132. for ( set<uint>::const_iterator j = classes.begin(); j != classes.end(); j++ )
  133. {
  134. uint current_class = *j;
  135. Vector labels_binary ( _labels.size() );
  136. for ( uint i = 0; i < _labels.size(); i++ )
  137. labels_binary[i] = ( _labels[i] == current_class ) ? 1.0 : -1.0;
  138. binLabels.insert ( pair<uint, NICE::Vector>( current_class, labels_binary) );
  139. }
  140. train ( _examples, binLabels );
  141. }
  142. void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *> & _examples,
  143. std::map<uint, NICE::Vector> & _binLabels
  144. )
  145. {
  146. // security-check: examples and labels have to be of same size
  147. for ( std::map< uint, NICE::Vector >::const_iterator binLabIt = _binLabels.begin();
  148. binLabIt != _binLabels.end();
  149. binLabIt++
  150. )
  151. {
  152. if ( _examples.size() != binLabIt->second.size() )
  153. {
  154. fthrow(Exception, "Given examples do not match label vector in size -- aborting!" );
  155. }
  156. }
  157. if ( this->b_verbose )
  158. std::cerr << "GPHIKRawClassifier::train" << std::endl;
  159. Timer t;
  160. t.start();
  161. // sort examples in each dimension and "transpose" the feature matrix
  162. // set up the GenericMatrix interface
  163. GMHIKernelRaw gm ( _examples );
  164. IterativeLinearSolver *ils = new ILSConjugateGradients();
  165. // solve linear equations for each class
  166. for ( map<uint, NICE::Vector>::const_iterator i = _binLabels.begin();
  167. i != _binLabels.end(); i++ )
  168. {
  169. const Vector & y = i->second;
  170. Vector alpha;
  171. ils->solveLin( gm, y, alpha );
  172. // TODO: get lookup tables, A, B, etc. and store them
  173. }
  174. delete ils;
  175. t.stop();
  176. if ( this->b_verbose )
  177. std::cerr << "Time used for setting up the fmk object: " << t.getLast() << std::endl;
  178. //indicate that we finished training successfully
  179. this->b_isTrained = true;
  180. // clean up all examples ??
  181. if ( this->b_verbose )
  182. std::cerr << "Learning finished" << std::endl;
  183. }