GPHIKClassifier.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 classify a given example with the previously learnt model
  165. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  166. * @date 18-06-2013 (dd-mm-yyyy)
  167. * @author Alexander Freytag
  168. * @param example (sparse Vector) to be classified given in a non-sparse representation
  169. * @param result (uint) class number of most likely class
  170. * @param scores (non-sparse vector) classification scores for known classes
  171. * @param uncertainty (double) predictive variance of the classification result, if computed
  172. */
  173. void classify ( const NICE::SparseVector * _example,
  174. uint & _result,
  175. NICE::Vector & _scores,
  176. double & _uncertainty
  177. ) const;
  178. /**
  179. * @brief classify a given set of examples with the previously learned model
  180. * @author Alexander Freytag, Erik Rodner
  181. * @param examples ((std::vector< NICE::SparseVector *>)) to be classified given in a sparse representation
  182. * @param results (Vector) class number of most likely class per example
  183. * @param scores (NICE::Matrix) classification scores for known classes and test examples
  184. * @param _uncertainties (NICE::Vector) predictive variance for each test input, if computed
  185. */
  186. void classify ( const std::vector< const NICE::SparseVector *> _examples,
  187. NICE::Vector & _results,
  188. NICE::Matrix & _scores,
  189. NICE::Vector & _uncertainties
  190. ) const;
  191. /**
  192. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  193. * @date 18-10-2012 (dd-mm-yyyy)
  194. * @author Alexander Freytag
  195. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  196. * @param labels (Vector) class labels (multi-class)
  197. */
  198. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  199. const NICE::Vector & _labels
  200. );
  201. /**
  202. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  203. * @date 19-06-2012 (dd-mm-yyyy)
  204. * @author Alexander Freytag
  205. * @param examples examples to use given in a sparse data structure
  206. * @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)
  207. */
  208. void train ( const std::vector< const NICE::SparseVector *> & _examples,
  209. std::map<uint, NICE::Vector> & _binLabels
  210. );
  211. /**
  212. * @brief Clone classifier object
  213. * @author Alexander Freytag
  214. */
  215. GPHIKClassifier *clone () const;
  216. /**
  217. * @brief prediction of classification uncertainty
  218. * @date 19-06-2012 (dd-mm-yyyy)
  219. * @author Alexander Freytag
  220. * @param examples example for which the classification uncertainty shall be predicted, given in a sparse representation
  221. * @param uncertainty contains the resulting classification uncertainty
  222. */
  223. void predictUncertainty( const NICE::SparseVector * _example,
  224. double & _uncertainty
  225. ) const;
  226. /**
  227. * @brief prediction of classification uncertainty
  228. * @date 19-12-2013 (dd-mm-yyyy)
  229. * @author Alexander Freytag
  230. * @param examples example for which the classification uncertainty shall be predicted, given in a non-sparse representation
  231. * @param uncertainty contains the resulting classification uncertainty
  232. */
  233. void predictUncertainty( const NICE::Vector * _example,
  234. double & _uncertainty
  235. ) const;
  236. ///////////////////// INTERFACE PERSISTENT /////////////////////
  237. // interface specific methods for store and restore
  238. ///////////////////// INTERFACE PERSISTENT /////////////////////
  239. /**
  240. * @brief Load classifier from external file (stream)
  241. * @author Alexander Freytag
  242. */
  243. void restore ( std::istream & _is,
  244. int _format = 0
  245. );
  246. /**
  247. * @brief Save classifier to external file (stream)
  248. * @author Alexander Freytag
  249. */
  250. void store ( std::ostream & _os,
  251. int _format = 0
  252. ) const;
  253. /**
  254. * @brief Clear classifier object
  255. * @author Alexander Freytag
  256. */
  257. void clear ();
  258. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  259. // interface specific methods for incremental extensions
  260. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  261. /**
  262. * @brief add a new example
  263. * @author Alexander Freytag
  264. */
  265. virtual void addExample( const NICE::SparseVector * _example,
  266. const double & _label,
  267. const bool & _performOptimizationAfterIncrement = true
  268. );
  269. /**
  270. * @brief add several new examples
  271. * @author Alexander Freytag
  272. */
  273. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & _newExamples,
  274. const NICE::Vector & _newLabels,
  275. const bool & _performOptimizationAfterIncrement = true
  276. );
  277. };
  278. }
  279. #endif