GPHIKRawClassifier.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @file GPHIKRawClassifier.h
  3. * @brief ..
  4. * @author Erik Rodner
  5. * @date 16-09-2015 (dd-mm-yyyy)
  6. */
  7. #ifndef _NICE_GPHIKRAWCLASSIFIERINCLUDE
  8. #define _NICE_GPHIKRAWCLASSIFIERINCLUDE
  9. // STL includes
  10. #include <string>
  11. #include <limits>
  12. #include <set>
  13. // NICE-core includes
  14. #include <core/basics/Config.h>
  15. #include <core/basics/Persistent.h>
  16. #include <core/vector/SparseVectorT.h>
  17. #include <core/algebra/ILSConjugateGradients.h>
  18. //
  19. #include "quantization/Quantization.h"
  20. #include "GMHIKernelRaw.h"
  21. namespace NICE {
  22. /**
  23. * @class GPHIKRawClassifier
  24. * @brief ...
  25. * @author Erik Rodner, Alexander Freytag
  26. */
  27. class GPHIKRawClassifier
  28. {
  29. protected:
  30. /////////////////////////
  31. /////////////////////////
  32. // PROTECTED VARIABLES //
  33. /////////////////////////
  34. /////////////////////////
  35. ///////////////////////////////////
  36. // output/debug related settings //
  37. ///////////////////////////////////
  38. /** verbose flag for useful output*/
  39. bool b_verbose;
  40. /** debug flag for several outputs useful for debugging*/
  41. bool b_debug;
  42. //////////////////////////////////////
  43. // general specifications //
  44. //////////////////////////////////////
  45. /** Header in configfile where variable settings are stored */
  46. std::string confSection;
  47. //////////////////////////////////////
  48. // EigenValue Decomposition //
  49. //////////////////////////////////////
  50. bool b_eig_verbose;
  51. int i_eig_value_max_iterations;
  52. //////////////////////////////////////
  53. // classification related variables //
  54. //////////////////////////////////////
  55. /** memorize whether the classifier was already trained*/
  56. bool b_isTrained;
  57. /** Gaussian label noise for model regularization */
  58. double d_noise;
  59. ILSConjugateGradients *solver;
  60. /** object performing feature quantization */
  61. NICE::Quantization *q;
  62. typedef double ** PrecomputedType;
  63. /** precomputed arrays A (1 per class) needed for classification without quantization */
  64. std::map< uint, PrecomputedType > precomputedA;
  65. /** precomputed arrays B (1 per class) needed for classification without quantization */
  66. std::map< uint, PrecomputedType > precomputedB;
  67. /** precomputed LUTs (1 per class) needed for classification with quantization */
  68. std::map< uint, double * > precomputedT;
  69. uint *nnz_per_dimension;
  70. uint num_examples;
  71. uint num_dimension;
  72. double f_tolerance;
  73. GMHIKernelRaw *gm;
  74. std::set<uint> knownClasses;
  75. /////////////////////////
  76. /////////////////////////
  77. // PROTECTED METHODS //
  78. /////////////////////////
  79. /////////////////////////
  80. void clearSetsOfTablesAandB();
  81. void clearSetsOfTablesT();
  82. /////////////////////////
  83. /////////////////////////
  84. // PUBLIC METHODS //
  85. /////////////////////////
  86. /////////////////////////
  87. public:
  88. /**
  89. * @brief default constructor
  90. */
  91. GPHIKRawClassifier( );
  92. /**
  93. * @brief standard constructor
  94. */
  95. GPHIKRawClassifier( const NICE::Config *_conf ,
  96. const std::string & s_confSection = "GPHIKRawClassifier"
  97. );
  98. /**
  99. * @brief simple destructor
  100. */
  101. ~GPHIKRawClassifier();
  102. /**
  103. * @brief Setup internal variables and objects used
  104. * @param conf Config file to specify variable settings
  105. * @param s_confSection
  106. */
  107. void initFromConfig(const NICE::Config *_conf,
  108. const std::string & s_confSection
  109. );
  110. ///////////////////// ///////////////////// /////////////////////
  111. // GET / SET
  112. ///////////////////// ///////////////////// /////////////////////
  113. /**
  114. * @brief Return currently known class numbers
  115. */
  116. std::set<uint> getKnownClassNumbers ( ) const;
  117. ///////////////////// ///////////////////// /////////////////////
  118. // CLASSIFIER STUFF
  119. ///////////////////// ///////////////////// /////////////////////
  120. /**
  121. * @brief classify a given example with the previously learned model
  122. * @author Alexander Freytag, Erik Rodner
  123. * @param example (SparseVector) to be classified given in a sparse representation
  124. * @param result (int) class number of most likely class
  125. * @param scores (SparseVector) classification scores for known classes
  126. */
  127. void classify ( const NICE::SparseVector * _example,
  128. uint & _result,
  129. NICE::SparseVector & _scores
  130. ) const;
  131. /**
  132. * @brief classify a given example with the previously learned model
  133. * @author Alexander Freytag, Erik Rodner
  134. * @param example (SparseVector) to be classified given in a sparse representation
  135. * @param result (int) class number of most likely class
  136. * @param scores (Vector) classification scores for known classes
  137. */
  138. void classify ( const NICE::SparseVector * _example,
  139. uint & _result,
  140. NICE::Vector & _scores
  141. ) const;
  142. /**
  143. * @brief classify a given set of examples with the previously learned model
  144. * @author Alexander Freytag, Erik Rodner
  145. * @param examples ((std::vector< NICE::SparseVector *>)) to be classified given in a sparse representation
  146. * @param results (Vector) class number of most likely class per example
  147. * @param scores (NICE::Matrix) classification scores for known classes and test examples
  148. */
  149. void classify ( const std::vector< const NICE::SparseVector *> _examples,
  150. NICE::Vector & _results,
  151. NICE::Matrix & _scores
  152. ) const;
  153. /**
  154. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  155. * @date 18-10-2012 (dd-mm-yyyy)
  156. * @author Alexander Freytag, Erik Rodner
  157. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  158. * @param labels (Vector) class labels (multi-class)
  159. */
  160. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  161. const NICE::Vector & _labels
  162. );
  163. /**
  164. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  165. * @author Alexander Freytag, Erik Rodner
  166. * @param examples examples to use given in a sparse data structure
  167. * @param binLabels corresponding binary labels with class no. There is no need here that every examples has only on positive entry in this set (1,-1)
  168. */
  169. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  170. std::map<uint, NICE::Vector> & _binLabels
  171. );
  172. };
  173. }
  174. #endif