GPHIKClassifier.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 b_verbose;
  39. /** debug flag for several outputs useful for debugging*/
  40. bool b_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 d_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 ,
  82. const std::string & s_confSection = "GPHIKClassifier"
  83. );
  84. /**
  85. * @brief simple destructor
  86. * @author Alexander Freytag
  87. */
  88. ~GPHIKClassifier();
  89. /**
  90. * @brief Setup internal variables and objects used
  91. * @author Alexander Freytag
  92. * @param conf Config file to specify variable settings
  93. * @param s_confSection
  94. */
  95. void initFromConfig(const NICE::Config *_conf,
  96. const std::string & s_confSection
  97. );
  98. ///////////////////// ///////////////////// /////////////////////
  99. // GET / SET
  100. ///////////////////// ///////////////////// /////////////////////
  101. /**
  102. * @brief Return currently known class numbers
  103. * @author Alexander Freytag
  104. */
  105. std::set<uint> getKnownClassNumbers ( ) const;
  106. ///////////////////// ///////////////////// /////////////////////
  107. // CLASSIFIER STUFF
  108. ///////////////////// ///////////////////// /////////////////////
  109. /**
  110. * @brief classify a given example with the previously learnt model
  111. * @date 19-06-2012 (dd-mm-yyyy)
  112. * @author Alexander Freytag
  113. * @param example (SparseVector) to be classified given in a sparse representation
  114. * @param result (int) class number of most likely class
  115. * @param scores (SparseVector) classification scores for known classes
  116. */
  117. void classify ( const NICE::SparseVector * _example,
  118. uint & _result,
  119. NICE::SparseVector & _scores
  120. ) const;
  121. /**
  122. * @brief classify a given example with the previously learnt model
  123. * @date 19-06-2012 (dd-mm-yyyy)
  124. * @author Alexander Freytag
  125. * @param example (SparseVector) to be classified given in a sparse representation
  126. * @param result (uint) class number of most likely class
  127. * @param scores (SparseVector) classification scores for known classes
  128. * @param uncertainty (double*) predictive variance of the classification result, if computed
  129. */
  130. void classify ( const NICE::SparseVector * _example,
  131. uint & _result,
  132. NICE::SparseVector & _scores,
  133. double & _uncertainty
  134. ) const;
  135. /**
  136. * @brief classify a given example with the previously learnt model
  137. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  138. * @date 18-06-2013 (dd-mm-yyyy)
  139. * @author Alexander Freytag
  140. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  141. * @param result (int) class number of most likely class
  142. * @param scores (SparseVector) classification scores for known classes
  143. */
  144. void classify ( const NICE::Vector * _example,
  145. uint & _result,
  146. NICE::SparseVector & _scores
  147. ) const;
  148. /**
  149. * @brief classify a given example with the previously learnt model
  150. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  151. * @date 18-06-2013 (dd-mm-yyyy)
  152. * @author Alexander Freytag
  153. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  154. * @param result (uint) class number of most likely class
  155. * @param scores (SparseVector) classification scores for known classes
  156. * @param uncertainty (double) predictive variance of the classification result, if computed
  157. */
  158. void classify ( const NICE::Vector * _example,
  159. uint & _result,
  160. NICE::SparseVector & _scores,
  161. double & _uncertainty
  162. ) const;
  163. /**
  164. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  165. * @date 18-10-2012 (dd-mm-yyyy)
  166. * @author Alexander Freytag
  167. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  168. * @param labels (Vector) class labels (multi-class)
  169. */
  170. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  171. const NICE::Vector & _labels
  172. );
  173. /**
  174. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  175. * @date 19-06-2012 (dd-mm-yyyy)
  176. * @author Alexander Freytag
  177. * @param examples examples to use given in a sparse data structure
  178. * @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)
  179. */
  180. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  181. std::map<uint, NICE::Vector> & _binLabels
  182. );
  183. /**
  184. * @brief Clone classifier object
  185. * @author Alexander Freytag
  186. */
  187. GPHIKClassifier *clone () const;
  188. /**
  189. * @brief prediction of classification uncertainty
  190. * @date 19-06-2012 (dd-mm-yyyy)
  191. * @author Alexander Freytag
  192. * @param examples example for which the classification uncertainty shall be predicted, given in a sparse representation
  193. * @param uncertainty contains the resulting classification uncertainty
  194. */
  195. void predictUncertainty( const NICE::SparseVector * _example,
  196. double & _uncertainty
  197. ) const;
  198. /**
  199. * @brief prediction of classification uncertainty
  200. * @date 19-12-2013 (dd-mm-yyyy)
  201. * @author Alexander Freytag
  202. * @param examples example for which the classification uncertainty shall be predicted, given in a non-sparse representation
  203. * @param uncertainty contains the resulting classification uncertainty
  204. */
  205. void predictUncertainty( const NICE::Vector * _example,
  206. double & _uncertainty
  207. ) const;
  208. ///////////////////// INTERFACE PERSISTENT /////////////////////
  209. // interface specific methods for store and restore
  210. ///////////////////// INTERFACE PERSISTENT /////////////////////
  211. /**
  212. * @brief Load classifier from external file (stream)
  213. * @author Alexander Freytag
  214. */
  215. void restore ( std::istream & _is,
  216. int _format = 0
  217. );
  218. /**
  219. * @brief Save classifier to external file (stream)
  220. * @author Alexander Freytag
  221. */
  222. void store ( std::ostream & _os,
  223. int _format = 0
  224. ) const;
  225. /**
  226. * @brief Clear classifier object
  227. * @author Alexander Freytag
  228. */
  229. void clear ();
  230. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  231. // interface specific methods for incremental extensions
  232. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  233. /**
  234. * @brief add a new example
  235. * @author Alexander Freytag
  236. */
  237. virtual void addExample( const NICE::SparseVector * _example,
  238. const double & _label,
  239. const bool & _performOptimizationAfterIncrement = true
  240. );
  241. /**
  242. * @brief add several new examples
  243. * @author Alexander Freytag
  244. */
  245. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  246. const NICE::Vector & _newLabels,
  247. const bool & _performOptimizationAfterIncrement = true
  248. );
  249. };
  250. }
  251. #endif