GPHIKRawClassifier.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // classification related variables //
  49. //////////////////////////////////////
  50. /** memorize whether the classifier was already trained*/
  51. bool b_isTrained;
  52. /** Gaussian label noise for model regularization */
  53. double d_noise;
  54. ILSConjugateGradients *solver;
  55. /** object performing feature quantization */
  56. NICE::Quantization *q;
  57. typedef double ** PrecomputedType;
  58. /** precomputed arrays A (1 per class) needed for classification without quantization */
  59. std::map< uint, PrecomputedType > precomputedA;
  60. /** precomputed arrays B (1 per class) needed for classification without quantization */
  61. std::map< uint, PrecomputedType > precomputedB;
  62. /** precomputed LUTs (1 per class) needed for classification with quantization */
  63. std::map< uint, double * > precomputedT;
  64. uint *nnz_per_dimension;
  65. uint num_examples;
  66. uint num_dimension;
  67. double f_tolerance;
  68. GMHIKernelRaw *gm;
  69. std::set<uint> knownClasses;
  70. /////////////////////////
  71. /////////////////////////
  72. // PROTECTED METHODS //
  73. /////////////////////////
  74. /////////////////////////
  75. /////////////////////////
  76. /////////////////////////
  77. // PUBLIC METHODS //
  78. /////////////////////////
  79. /////////////////////////
  80. public:
  81. /**
  82. * @brief default constructor
  83. */
  84. GPHIKRawClassifier( );
  85. /**
  86. * @brief standard constructor
  87. */
  88. GPHIKRawClassifier( const NICE::Config *_conf ,
  89. const std::string & s_confSection = "GPHIKRawClassifier"
  90. );
  91. /**
  92. * @brief simple destructor
  93. */
  94. ~GPHIKRawClassifier();
  95. /**
  96. * @brief Setup internal variables and objects used
  97. * @param conf Config file to specify variable settings
  98. * @param s_confSection
  99. */
  100. void initFromConfig(const NICE::Config *_conf,
  101. const std::string & s_confSection
  102. );
  103. ///////////////////// ///////////////////// /////////////////////
  104. // GET / SET
  105. ///////////////////// ///////////////////// /////////////////////
  106. /**
  107. * @brief Return currently known class numbers
  108. */
  109. std::set<uint> getKnownClassNumbers ( ) const;
  110. ///////////////////// ///////////////////// /////////////////////
  111. // CLASSIFIER STUFF
  112. ///////////////////// ///////////////////// /////////////////////
  113. /**
  114. * @brief classify a given example with the previously learned model
  115. * @author Alexander Freytag, Erik Rodner
  116. * @param example (SparseVector) to be classified given in a sparse representation
  117. * @param result (int) class number of most likely class
  118. * @param scores (SparseVector) classification scores for known classes
  119. */
  120. void classify ( const NICE::SparseVector * _example,
  121. uint & _result,
  122. NICE::SparseVector & _scores
  123. ) const;
  124. /**
  125. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  126. * @date 18-10-2012 (dd-mm-yyyy)
  127. * @author Alexander Freytag, Erik Rodner
  128. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  129. * @param labels (Vector) class labels (multi-class)
  130. */
  131. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  132. const NICE::Vector & _labels
  133. );
  134. /**
  135. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  136. * @author Alexander Freytag, Erik Rodner
  137. * @param examples examples to use given in a sparse data structure
  138. * @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)
  139. */
  140. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  141. std::map<uint, NICE::Vector> & _binLabels
  142. );
  143. };
  144. }
  145. #endif