GPHIKClassifier.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @file GPHIKClassifier.h
  3. * @author Erik Rodner, Alexander Freytag
  4. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning) (Interface)
  5. * @date 02/01/2012
  6. */
  7. #ifndef _NICE_GPHIKCLASSIFIERINCLUDE
  8. #define _NICE_GPHIKCLASSIFIERINCLUDE
  9. // STL includes
  10. #include <string>
  11. #include <limits>
  12. // NICE-core includes
  13. #include <core/basics/Config.h>
  14. #include <core/basics/Persistent.h>
  15. //
  16. #include <core/vector/SparseVectorT.h>
  17. // gp-hik-core includes
  18. #include "gp-hik-core/FMKGPHyperparameterOptimization.h"
  19. #include "gp-hik-core/OnlineLearnable.h"
  20. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  21. namespace NICE {
  22. /**
  23. * @class GPHIKClassifier
  24. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning)
  25. * @author Erik Rodner, Alexander Freytag
  26. */
  27. class GPHIKClassifier : public NICE::Persistent, public NICE::OnlineLearnable
  28. {
  29. protected:
  30. std::string confSection;
  31. double noise;
  32. enum VarianceApproximation{
  33. APPROXIMATE_ROUGH,
  34. APPROXIMATE_FINE,
  35. EXACT,
  36. NONE
  37. };
  38. VarianceApproximation varianceApproximation;
  39. /**compute the uncertainty prediction during classification?*/
  40. bool uncertaintyPredictionForClassification;
  41. NICE::Config *confCopy;
  42. NICE::ParameterizedFunction *pf;
  43. NICE::FMKGPHyperparameterOptimization *gphyper;
  44. /** verbose flag for useful output*/
  45. bool verbose;
  46. /** debug flag for several outputs useful for debugging*/
  47. bool debug;
  48. /**
  49. * @brief classify a given example with the previously learnt model
  50. * @param pe example to be classified given in a sparse representation
  51. */
  52. void init(const NICE::Config *conf, const std::string & s_confSection);
  53. public:
  54. /** simple constructor */
  55. GPHIKClassifier( const NICE::Config *conf = NULL, const std::string & s_confSection = "GPHIKClassifier" );
  56. /** simple destructor */
  57. ~GPHIKClassifier();
  58. ///////////////////// ///////////////////// /////////////////////
  59. // GET / SET
  60. ///////////////////// ///////////////////// /////////////////////
  61. std::set<int> getKnownClassNumbers ( ) const;
  62. ///////////////////// ///////////////////// /////////////////////
  63. // CLASSIFIER STUFF
  64. ///////////////////// ///////////////////// /////////////////////
  65. /**
  66. * @brief classify a given example with the previously learnt model
  67. * @date 19-06-2012 (dd-mm-yyyy)
  68. * @author Alexander Freytag
  69. * @param example (SparseVector) to be classified given in a sparse representation
  70. * @param result (int) class number of most likely class
  71. * @param scores (SparseVector) classification scores for known classes
  72. */
  73. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores ) const;
  74. /**
  75. * @brief classify a given example with the previously learnt model
  76. * @date 19-06-2012 (dd-mm-yyyy)
  77. * @author Alexander Freytag
  78. * @param example (SparseVector) to be classified given in a sparse representation
  79. * @param result (int) class number of most likely class
  80. * @param scores (SparseVector) classification scores for known classes
  81. * @param uncertainty (double*) predictive variance of the classification result, if computed
  82. */
  83. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  84. /**
  85. * @brief classify a given example with the previously learnt model
  86. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  87. * @date 18-06-2013 (dd-mm-yyyy)
  88. * @author Alexander Freytag
  89. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  90. * @param result (int) class number of most likely class
  91. * @param scores (SparseVector) classification scores for known classes
  92. */
  93. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores ) const;
  94. /**
  95. * @brief classify a given example with the previously learnt model
  96. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  97. * @date 18-06-2013 (dd-mm-yyyy)
  98. * @author Alexander Freytag
  99. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  100. * @param result (int) class number of most likely class
  101. * @param scores (SparseVector) classification scores for known classes
  102. * @param uncertainty (double) predictive variance of the classification result, if computed
  103. */
  104. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  105. /**
  106. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  107. * @date 18-10-2012 (dd-mm-yyyy)
  108. * @author Alexander Freytag
  109. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  110. * @param labels (Vector) class labels (multi-class)
  111. */
  112. void train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels );
  113. /**
  114. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  115. * @date 19-06-2012 (dd-mm-yyyy)
  116. * @author Alexander Freytag
  117. * @param examples examples to use given in a sparse data structure
  118. * @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)
  119. */
  120. void train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels );
  121. GPHIKClassifier *clone () const;
  122. /**
  123. * @brief prediction of classification uncertainty
  124. * @date 19-06-2012 (dd-mm-yyyy)
  125. * @author Alexander Freytag
  126. * @param examples example for which the classification uncertainty shall be predicted, given in a sparse representation
  127. * @param uncertainty contains the resulting classification uncertainty
  128. */
  129. void predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const;
  130. /**
  131. * @brief prediction of classification uncertainty
  132. * @date 19-12-2013 (dd-mm-yyyy)
  133. * @author Alexander Freytag
  134. * @param examples example for which the classification uncertainty shall be predicted, given in a non-sparse representation
  135. * @param uncertainty contains the resulting classification uncertainty
  136. */
  137. void predictUncertainty( const NICE::Vector * example, double & uncertainty ) const;
  138. ///////////////////// INTERFACE PERSISTENT /////////////////////
  139. // interface specific methods for store and restore
  140. ///////////////////// INTERFACE PERSISTENT /////////////////////
  141. void restore ( std::istream & is, int format = 0 );
  142. void store ( std::ostream & os, int format = 0 ) const;
  143. void clear ();
  144. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  145. // interface specific methods for incremental extensions
  146. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  147. virtual void addExample( const NICE::SparseVector * example,
  148. const double & label,
  149. const bool & performOptimizationAfterIncrement = true
  150. );
  151. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  152. const NICE::Vector & newLabels,
  153. const bool & performOptimizationAfterIncrement = true
  154. );
  155. };
  156. }
  157. #endif