GPHIKClassifier.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @file GPHIKClassifier.h
  3. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning) (Interface)
  4. * @author Alexander Freytag, Erik Rodner
  5. * @date 01-02-2012 (dd-mm-yyyy)
  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. namespace NICE {
  21. /**
  22. * @class GPHIKClassifier
  23. * @brief Main interface for our GP HIK classifier (similar to the feature pool classifier interface in vislearning)
  24. * @author Alexander Freytag, Erik Rodner
  25. */
  26. class GPHIKClassifier : public NICE::Persistent, public NICE::OnlineLearnable
  27. {
  28. protected:
  29. /////////////////////////
  30. /////////////////////////
  31. // PROTECTED VARIABLES //
  32. /////////////////////////
  33. /////////////////////////
  34. ///////////////////////////////////
  35. // output/debug related settings //
  36. ///////////////////////////////////
  37. /** verbose flag for useful output*/
  38. bool verbose;
  39. /** debug flag for several outputs useful for debugging*/
  40. bool debug;
  41. //////////////////////////////////////
  42. // general specifications //
  43. //////////////////////////////////////
  44. /** Header in configfile where variable settings are stored */
  45. std::string confSection;
  46. //////////////////////////////////////
  47. // classification related variables //
  48. //////////////////////////////////////
  49. /** memorize whether the classifier was already trained*/
  50. bool b_isTrained;
  51. /** Main object doing all the jobs: training, classification, optimization, ... */
  52. NICE::FMKGPHyperparameterOptimization *gphyper;
  53. /** Gaussian label noise for model regularization */
  54. double noise;
  55. enum VarianceApproximation{
  56. APPROXIMATE_ROUGH,
  57. APPROXIMATE_FINE,
  58. EXACT,
  59. NONE
  60. };
  61. /** Which technique for variance approximations shall be used */
  62. VarianceApproximation varianceApproximation;
  63. /**compute the uncertainty prediction during classification?*/
  64. bool uncertaintyPredictionForClassification;
  65. /////////////////////////
  66. /////////////////////////
  67. // PROTECTED METHODS //
  68. /////////////////////////
  69. /////////////////////////
  70. public:
  71. /**
  72. * @brief default constructor
  73. * @author Alexander Freytag
  74. * @date 05-02-2014 ( dd-mm-yyyy)
  75. */
  76. GPHIKClassifier( );
  77. /**
  78. * @brief standard constructor
  79. * @author Alexander Freytag
  80. */
  81. GPHIKClassifier( const NICE::Config *conf , const std::string & s_confSection = "GPHIKClassifier" );
  82. /**
  83. * @brief simple destructor
  84. * @author Alexander Freytag
  85. */
  86. ~GPHIKClassifier();
  87. /**
  88. * @brief Setup internal variables and objects used
  89. * @author Alexander Freytag
  90. * @param conf Config file to specify variable settings
  91. * @param s_confSection
  92. */
  93. void initFromConfig(const NICE::Config *conf, const std::string & s_confSection);
  94. ///////////////////// ///////////////////// /////////////////////
  95. // GET / SET
  96. ///////////////////// ///////////////////// /////////////////////
  97. /**
  98. * @brief Return currently known class numbers
  99. * @author Alexander Freytag
  100. */
  101. std::set<int> getKnownClassNumbers ( ) const;
  102. ///////////////////// ///////////////////// /////////////////////
  103. // CLASSIFIER STUFF
  104. ///////////////////// ///////////////////// /////////////////////
  105. /**
  106. * @brief classify a given example with the previously learnt model
  107. * @date 19-06-2012 (dd-mm-yyyy)
  108. * @author Alexander Freytag
  109. * @param example (SparseVector) to be classified given in a sparse representation
  110. * @param result (int) class number of most likely class
  111. * @param scores (SparseVector) classification scores for known classes
  112. */
  113. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores ) const;
  114. /**
  115. * @brief classify a given example with the previously learnt model
  116. * @date 19-06-2012 (dd-mm-yyyy)
  117. * @author Alexander Freytag
  118. * @param example (SparseVector) to be classified given in a sparse representation
  119. * @param result (int) class number of most likely class
  120. * @param scores (SparseVector) classification scores for known classes
  121. * @param uncertainty (double*) predictive variance of the classification result, if computed
  122. */
  123. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  124. /**
  125. * @brief classify a given example with the previously learnt model
  126. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  127. * @date 18-06-2013 (dd-mm-yyyy)
  128. * @author Alexander Freytag
  129. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  130. * @param result (int) class number of most likely class
  131. * @param scores (SparseVector) classification scores for known classes
  132. */
  133. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores ) const;
  134. /**
  135. * @brief classify a given example with the previously learnt model
  136. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  137. * @date 18-06-2013 (dd-mm-yyyy)
  138. * @author Alexander Freytag
  139. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  140. * @param result (int) class number of most likely class
  141. * @param scores (SparseVector) classification scores for known classes
  142. * @param uncertainty (double) predictive variance of the classification result, if computed
  143. */
  144. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  145. /**
  146. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  147. * @date 18-10-2012 (dd-mm-yyyy)
  148. * @author Alexander Freytag
  149. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  150. * @param labels (Vector) class labels (multi-class)
  151. */
  152. void train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels );
  153. /**
  154. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  155. * @date 19-06-2012 (dd-mm-yyyy)
  156. * @author Alexander Freytag
  157. * @param examples examples to use given in a sparse data structure
  158. * @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)
  159. */
  160. void train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels );
  161. /**
  162. * @brief Clone classifier object
  163. * @author Alexander Freytag
  164. */
  165. GPHIKClassifier *clone () const;
  166. /**
  167. * @brief prediction of classification uncertainty
  168. * @date 19-06-2012 (dd-mm-yyyy)
  169. * @author Alexander Freytag
  170. * @param examples example for which the classification uncertainty shall be predicted, given in a sparse representation
  171. * @param uncertainty contains the resulting classification uncertainty
  172. */
  173. void predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const;
  174. /**
  175. * @brief prediction of classification uncertainty
  176. * @date 19-12-2013 (dd-mm-yyyy)
  177. * @author Alexander Freytag
  178. * @param examples example for which the classification uncertainty shall be predicted, given in a non-sparse representation
  179. * @param uncertainty contains the resulting classification uncertainty
  180. */
  181. void predictUncertainty( const NICE::Vector * example, double & uncertainty ) const;
  182. ///////////////////// INTERFACE PERSISTENT /////////////////////
  183. // interface specific methods for store and restore
  184. ///////////////////// INTERFACE PERSISTENT /////////////////////
  185. /**
  186. * @brief Load classifier from external file (stream)
  187. * @author Alexander Freytag
  188. */
  189. void restore ( std::istream & is, int format = 0 );
  190. /**
  191. * @brief Save classifier to external file (stream)
  192. * @author Alexander Freytag
  193. */
  194. void store ( std::ostream & os, int format = 0 ) const;
  195. /**
  196. * @brief Clear classifier object
  197. * @author Alexander Freytag
  198. */
  199. void clear ();
  200. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  201. // interface specific methods for incremental extensions
  202. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  203. /**
  204. * @brief add a new example
  205. * @author Alexander Freytag
  206. */
  207. virtual void addExample( const NICE::SparseVector * example,
  208. const double & label,
  209. const bool & performOptimizationAfterIncrement = true
  210. );
  211. /**
  212. * @brief add several new examples
  213. * @author Alexander Freytag
  214. */
  215. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  216. const NICE::Vector & newLabels,
  217. const bool & performOptimizationAfterIncrement = true
  218. );
  219. };
  220. }
  221. #endif