GPHIKRawClassifier.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 GPHIKClassifier
  24. * @brief ...
  25. * @author Erik Rodner
  26. */
  27. class GPHIKRawClassifier //: public NICE::Persistent
  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. double f_tolerance;
  67. GMHIKernelRaw *gm;
  68. std::set<uint> knownClasses;
  69. /////////////////////////
  70. /////////////////////////
  71. // PROTECTED METHODS //
  72. /////////////////////////
  73. /////////////////////////
  74. public:
  75. /**
  76. * @brief default constructor
  77. */
  78. GPHIKRawClassifier( );
  79. /**
  80. * @brief standard constructor
  81. */
  82. GPHIKRawClassifier( const NICE::Config *_conf ,
  83. const std::string & s_confSection = "GPHIKRawClassifier"
  84. );
  85. /**
  86. * @brief simple destructor
  87. */
  88. ~GPHIKRawClassifier();
  89. /**
  90. * @brief Setup internal variables and objects used
  91. * @param conf Config file to specify variable settings
  92. * @param s_confSection
  93. */
  94. void initFromConfig(const NICE::Config *_conf,
  95. const std::string & s_confSection
  96. );
  97. ///////////////////// ///////////////////// /////////////////////
  98. // GET / SET
  99. ///////////////////// ///////////////////// /////////////////////
  100. /**
  101. * @brief Return currently known class numbers
  102. */
  103. std::set<uint> getKnownClassNumbers ( ) const;
  104. ///////////////////// ///////////////////// /////////////////////
  105. // CLASSIFIER STUFF
  106. ///////////////////// ///////////////////// /////////////////////
  107. /**
  108. * @brief classify a given example with the previously learned model
  109. * @author Alexander Freytag, Erik Rodner
  110. * @param example (SparseVector) to be classified given in a sparse representation
  111. * @param result (int) class number of most likely class
  112. * @param scores (SparseVector) classification scores for known classes
  113. */
  114. void classify ( const NICE::SparseVector * _example,
  115. uint & _result,
  116. NICE::SparseVector & _scores
  117. ) const;
  118. /**
  119. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  120. * @date 18-10-2012 (dd-mm-yyyy)
  121. * @author Alexander Freytag, Erik Rodner
  122. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  123. * @param labels (Vector) class labels (multi-class)
  124. */
  125. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  126. const NICE::Vector & _labels
  127. );
  128. /**
  129. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  130. * @author Alexander Freytag, Erik Rodner
  131. * @param examples examples to use given in a sparse data structure
  132. * @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)
  133. */
  134. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  135. std::map<uint, NICE::Vector> & _binLabels
  136. );
  137. };
  138. }
  139. #endif