GPHIKClassifier.h 9.8 KB

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