KCGPOneClass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @file KCGPOneClass.cpp
  3. * @brief One-Class Gaussian Process Regression for Classification
  4. * @author Erik Rodner + Mi.Ke.
  5. * @date 12/03/2010
  6. */
  7. #include <iostream>
  8. #include <typeinfo>
  9. #include "core/vector/Algorithms.h"
  10. #include "vislearning/regression/gpregression/RegGaussianProcess.h"
  11. #include "KCGPOneClass.h"
  12. using namespace std;
  13. using namespace NICE;
  14. using namespace OBJREC;
  15. KCGPOneClass::KCGPOneClass( const Config *conf, Kernel *kernel, const string & section )
  16. : KernelClassifier ( conf, kernel )
  17. {
  18. //overwrite the default optimization options, since we don't want to perform standard loo or marginal likelihood stuff
  19. Config config(*conf);
  20. string modestr = config.gS(section,"detection_mode");
  21. //enforce not to use the optimization routines (wich would lead to a disaster in the 1-class case)
  22. config.sB(section, "optimize_parameters", false );
  23. regressionAlgorithm = new RegGaussianProcess ( &config, kernel, section );
  24. if(strcmp("mean",modestr.c_str())==0){
  25. mode=MEAN_DETECTION_MODE;cerr << "One-class classification via GP predictive _mean_ !!!"<<endl;
  26. }
  27. if(strcmp("variance",modestr.c_str())==0){
  28. mode=VARIANCE_DETECTION_MODE;cerr << "One-class classification via GP predictive _variance_ !!!"<<endl;
  29. }
  30. //should be beneficial when the amount of training data is small compared to test data (given a well-conditioned kernel)
  31. computeInverse = config.gB(section,"compute_inverse",false);
  32. staticNoise = conf->gD(section, "static_noise", 0.0);
  33. }
  34. KCGPOneClass::KCGPOneClass( const KCGPOneClass & src ) : KernelClassifier ( src )
  35. {
  36. regressionAlgorithm = src.regressionAlgorithm->clone();
  37. y = src.y;
  38. }
  39. KCGPOneClass::~KCGPOneClass()
  40. {
  41. delete regressionAlgorithm;
  42. }
  43. void KCGPOneClass::teach ( KernelData *kernelData, const NICE::Vector & y )
  44. {
  45. if ( y.size() <= 0 ) {
  46. fthrow(Exception, "Number of training vectors is zero!");
  47. }
  48. if ( almostZero(kernelData->getKernelMatrix().Max()) ) {
  49. fthrow(Exception, "Kernel matrix seems to be not positive definite!");
  50. }
  51. //cerr << kernelMatrix << endl;
  52. this->y.resize ( y.size() );
  53. this->y = y;
  54. this->y = 2*this->y;
  55. this->y += -1.0;
  56. if ( (this->y.Min() != 1) || (this->y.Max() != 1) ) {
  57. fthrow(Exception, "This classifier is suitable only for one-class classification problems, i.e. max(y) = min(y) = 1");
  58. }
  59. if ( staticNoise != 0.0 )
  60. kernelData->getKernelMatrix().addIdentity ( staticNoise );
  61. regressionAlgorithm->teach ( kernelData, this->y );
  62. if(mode==VARIANCE_DETECTION_MODE){
  63. if(computeInverse){
  64. kernelData->updateInverseKernelMatrix();
  65. InverseKernelMatrix = kernelData->getInverseKernelMatrix();
  66. }else{
  67. this->kernelData=kernelData;
  68. }
  69. }
  70. }
  71. ClassificationResult KCGPOneClass::classifyKernel ( const NICE::Vector & kernelVector, double kernelSelf ) const
  72. {
  73. FullVector scores ( 2 );
  74. scores[0] = 0.0;
  75. if(mode==MEAN_DETECTION_MODE){
  76. double yEstimate = regressionAlgorithm->predictKernel ( kernelVector,kernelSelf );
  77. scores[1] = yEstimate;
  78. }
  79. if(mode==VARIANCE_DETECTION_MODE){
  80. if(computeInverse){
  81. NICE::Vector kInvkstar = InverseKernelMatrix*kernelVector;
  82. double sigmaEstimate = kernelSelf - kernelVector.scalarProduct ( kInvkstar );
  83. scores[1] = 1.0-sigmaEstimate;
  84. }else{
  85. NICE::Vector kInvkstar;
  86. kInvkstar.resize ( kernelVector.size() );
  87. kernelData->computeInverseKernelMultiply ( kernelVector, kInvkstar);
  88. double sigmaEstimate = kernelSelf - kernelVector.scalarProduct ( kInvkstar );
  89. scores[1] = 1.0-sigmaEstimate;
  90. }
  91. }
  92. ClassificationResult r ( scores[1]<0.5 ? 0 : 1, scores );
  93. return r;
  94. }
  95. KCGPOneClass *KCGPOneClass::clone() const
  96. {
  97. return new KCGPOneClass ( *this );
  98. }