GPHIKClassifier.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /**
  68. * @brief Setup internal variables and objects used
  69. * @author Alexander Freytag
  70. * @param conf Config file to specify variable settings
  71. * @param s_confSection
  72. */
  73. void init(const NICE::Config *conf, const std::string & s_confSection);
  74. public:
  75. /**
  76. * @brief standard constructor
  77. * @author Alexander Freytag
  78. */
  79. GPHIKClassifier( const NICE::Config *conf = NULL, const std::string & s_confSection = "GPHIKClassifier" );
  80. /**
  81. * @brief simple destructor
  82. * @author Alexander Freytag
  83. */
  84. ~GPHIKClassifier();
  85. ///////////////////// ///////////////////// /////////////////////
  86. // GET / SET
  87. ///////////////////// ///////////////////// /////////////////////
  88. /**
  89. * @brief Return currently known class numbers
  90. * @author Alexander Freytag
  91. */
  92. std::set<int> getKnownClassNumbers ( ) const;
  93. ///////////////////// ///////////////////// /////////////////////
  94. // CLASSIFIER STUFF
  95. ///////////////////// ///////////////////// /////////////////////
  96. /**
  97. * @brief classify a given example with the previously learnt model
  98. * @date 19-06-2012 (dd-mm-yyyy)
  99. * @author Alexander Freytag
  100. * @param example (SparseVector) to be classified given in a sparse representation
  101. * @param result (int) class number of most likely class
  102. * @param scores (SparseVector) classification scores for known classes
  103. */
  104. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores ) const;
  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. * @param uncertainty (double*) predictive variance of the classification result, if computed
  113. */
  114. void classify ( const NICE::SparseVector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  115. /**
  116. * @brief classify a given example with the previously learnt model
  117. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times*
  118. * @date 18-06-2013 (dd-mm-yyyy)
  119. * @author Alexander Freytag
  120. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  121. * @param result (int) class number of most likely class
  122. * @param scores (SparseVector) classification scores for known classes
  123. */
  124. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores ) const;
  125. /**
  126. * @brief classify a given example with the previously learnt model
  127. * NOTE: whenever possible, you should the sparse version to obtain significantly smaller computation times
  128. * @date 18-06-2013 (dd-mm-yyyy)
  129. * @author Alexander Freytag
  130. * @param example (non-sparse Vector) to be classified given in a non-sparse representation
  131. * @param result (int) class number of most likely class
  132. * @param scores (SparseVector) classification scores for known classes
  133. * @param uncertainty (double) predictive variance of the classification result, if computed
  134. */
  135. void classify ( const NICE::Vector * example, int & result, NICE::SparseVector & scores, double & uncertainty ) const;
  136. /**
  137. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  138. * @date 18-10-2012 (dd-mm-yyyy)
  139. * @author Alexander Freytag
  140. * @param examples (std::vector< NICE::SparseVector *>) training data given in a sparse representation
  141. * @param labels (Vector) class labels (multi-class)
  142. */
  143. void train ( const std::vector< const NICE::SparseVector *> & examples, const NICE::Vector & labels );
  144. /**
  145. * @brief train this classifier using a given set of examples and a given set of binary label vectors
  146. * @date 19-06-2012 (dd-mm-yyyy)
  147. * @author Alexander Freytag
  148. * @param examples examples to use given in a sparse data structure
  149. * @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)
  150. */
  151. void train ( const std::vector< const NICE::SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels );
  152. /**
  153. * @brief Clone classifier object
  154. * @author Alexander Freytag
  155. */
  156. GPHIKClassifier *clone () const;
  157. /**
  158. * @brief prediction of classification uncertainty
  159. * @date 19-06-2012 (dd-mm-yyyy)
  160. * @author Alexander Freytag
  161. * @param examples example for which the classification uncertainty shall be predicted, given in a sparse representation
  162. * @param uncertainty contains the resulting classification uncertainty
  163. */
  164. void predictUncertainty( const NICE::SparseVector * example, double & uncertainty ) const;
  165. /**
  166. * @brief prediction of classification uncertainty
  167. * @date 19-12-2013 (dd-mm-yyyy)
  168. * @author Alexander Freytag
  169. * @param examples example for which the classification uncertainty shall be predicted, given in a non-sparse representation
  170. * @param uncertainty contains the resulting classification uncertainty
  171. */
  172. void predictUncertainty( const NICE::Vector * example, double & uncertainty ) const;
  173. ///////////////////// INTERFACE PERSISTENT /////////////////////
  174. // interface specific methods for store and restore
  175. ///////////////////// INTERFACE PERSISTENT /////////////////////
  176. /**
  177. * @brief Load classifier from external file (stream)
  178. * @author Alexander Freytag
  179. */
  180. void restore ( std::istream & is, int format = 0 );
  181. /**
  182. * @brief Save classifier to external file (stream)
  183. * @author Alexander Freytag
  184. */
  185. void store ( std::ostream & os, int format = 0 ) const;
  186. /**
  187. * @brief Clear classifier object
  188. * @author Alexander Freytag
  189. */
  190. void clear ();
  191. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  192. // interface specific methods for incremental extensions
  193. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  194. /**
  195. * @brief add a new example
  196. * @author Alexander Freytag
  197. */
  198. virtual void addExample( const NICE::SparseVector * example,
  199. const double & label,
  200. const bool & performOptimizationAfterIncrement = true
  201. );
  202. /**
  203. * @brief add several new examples
  204. * @author Alexander Freytag
  205. */
  206. virtual void addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  207. const NICE::Vector & newLabels,
  208. const bool & performOptimizationAfterIncrement = true
  209. );
  210. };
  211. }
  212. #endif