KCNullSpaceNovelty.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * @file KCNullSpaceNovelty.cpp
  3. * @brief Novelty detection with kernel null space methods (Kernel Null Foley Sammon Transform - KNFST)
  4. * @author Paul Bodesheim
  5. * @date 26/11/2012
  6. */
  7. #include <iostream>
  8. #include <sstream>
  9. #include "core/vector/Algorithms.h"
  10. #include "KCNullSpaceNovelty.h"
  11. #include <limits>
  12. #undef DEBUG
  13. using namespace NICE;
  14. using namespace std;
  15. using namespace OBJREC;
  16. KCNullSpaceNovelty::KCNullSpaceNovelty( const Config *conf, Kernel *kernelFunction, const string & section )
  17. : KernelClassifier ( conf, kernelFunction )
  18. {
  19. this->maxClassNo = 0;
  20. this->verbose = conf->gB( section, "verbose", false );
  21. }
  22. KCNullSpaceNovelty::KCNullSpaceNovelty( const KCNullSpaceNovelty &vcova ): KernelClassifier(vcova)
  23. {
  24. verbose = vcova.verbose;
  25. dimNullSpace = vcova.dimNullSpace;
  26. oneClassSetting = vcova.oneClassSetting;
  27. trainingSetStatistic.clear();
  28. std::map<int,int>::iterator it;
  29. for ( it = ( (std::map<int,int>)vcova.trainingSetStatistic ).begin(); it != vcova.trainingSetStatistic.end(); it++ )
  30. {
  31. trainingSetStatistic.insert(pair<int,int>( (*it).first,(*it).second ));
  32. }
  33. nullProjectionDirections.resize( vcova.nullProjectionDirections.rows(),vcova.nullProjectionDirections.cols() );
  34. nullProjectionDirections.set( 0.0 );
  35. for(int i = 0; i < (int)vcova.nullProjectionDirections.rows(); i++)
  36. {
  37. for(int j = 0; j < (int)vcova.nullProjectionDirections.cols(); j++)
  38. {
  39. nullProjectionDirections(i,j) = vcova.nullProjectionDirections(i,j);
  40. }
  41. }
  42. targetPoints.clear();
  43. for(int i = 0; i < (int)vcova.targetPoints.size(); i++)
  44. {
  45. targetPoints.push_back( NICE::Vector(vcova.targetPoints[i]) );
  46. }
  47. eigenBasis.resize( vcova.eigenBasis.rows(),vcova.eigenBasis.cols() );
  48. eigenBasis.set( 0.0 );
  49. for(int i = 0; i < (int)vcova.eigenBasis.rows(); i++)
  50. {
  51. for(int j = 0; j < (int)vcova.eigenBasis.cols(); j++)
  52. {
  53. eigenBasis(i,j) = vcova.eigenBasis(i,j);
  54. }
  55. }
  56. }
  57. KCNullSpaceNovelty::~KCNullSpaceNovelty()
  58. {
  59. }
  60. void KCNullSpaceNovelty::teach ( KernelData *kernelData, const NICE::Vector & y )
  61. {
  62. NICE::Vector labels(y);
  63. maxClassNo = (int)labels.Max();
  64. /** check if we are in a one-class setting */
  65. int minClassNo = (int)labels.Min();
  66. if (maxClassNo == minClassNo)
  67. {
  68. oneClassSetting = true;
  69. if (verbose)
  70. std::cerr << "KCNullSpaceNovelty::teach: one-class setting" << std::endl;
  71. computeTrainingSetStatistic(labels);
  72. /** one-class setting: add a row and a column of zeros to the kernel matrix representing dot products with origin in kernel feature space*/
  73. kernelData->increase_size_by_One();
  74. kernelData->getKernelMatrix() (kernelData->getKernelMatrix().rows()-1, kernelData->getKernelMatrix().cols()-1) = 0.0;
  75. labels.append(minClassNo+1);
  76. }
  77. else
  78. {
  79. oneClassSetting = false;
  80. if (verbose)
  81. std::cerr << "KCNullSpaceNovelty::teach: multi-class setting" << std::endl;
  82. computeTrainingSetStatistic(labels);
  83. }
  84. if (verbose)
  85. std::cerr << "KCNullSpaceNovelty::teach: compute null projection directions..." << std::endl;
  86. computeNullProjectionDirections(kernelData,labels);
  87. if (verbose)
  88. std::cerr << "KCNullSpaceNovelty::teach: compute target points..." << std::endl;
  89. computeTargetPoints(kernelData,labels);
  90. }
  91. std::map<int,int> KCNullSpaceNovelty::getTrainingSetStatistic()
  92. {
  93. return trainingSetStatistic;
  94. }
  95. NICE::Matrix KCNullSpaceNovelty::getNullProjectionDirections()
  96. {
  97. return nullProjectionDirections;
  98. }
  99. NICE::VVector KCNullSpaceNovelty::getTargetPoints()
  100. {
  101. return targetPoints;
  102. }
  103. int KCNullSpaceNovelty::getNullSpaceDimension()
  104. {
  105. return dimNullSpace;
  106. }
  107. bool KCNullSpaceNovelty::isOneClass()
  108. {
  109. return oneClassSetting;
  110. }
  111. void KCNullSpaceNovelty::computeTrainingSetStatistic(const NICE::Vector & y)
  112. {
  113. trainingSetStatistic.clear();
  114. std::map<int,int>::iterator it;
  115. for ( uint i = 0 ; i < y.size(); i++ )
  116. {
  117. it = trainingSetStatistic.find ( (int)y[i] );
  118. if ( it == trainingSetStatistic.end() )
  119. {
  120. trainingSetStatistic.insert(it, pair<int,int>( (int)y[i],1 ));
  121. }
  122. else
  123. {
  124. it->second += 1;
  125. }
  126. }
  127. }
  128. void KCNullSpaceNovelty::computeBasisUsingKernelPCA(const KernelData *kernelData)
  129. {
  130. NICE::Matrix K (kernelData->getKernelMatrix());
  131. /** let K represent dot products of zero mean data in kernel feature space */
  132. centerKernelMatrix(K);
  133. /** get eigenvectors and eigenvalues (descreasing order) of centered kernel matrix*/
  134. NICE::Matrix eigenVectors(K.rows(), K.cols(), 0.0);
  135. NICE::Vector eigenValues(K.rows(), 0.0);
  136. eigenvectorvalues(K, eigenVectors, eigenValues);
  137. /** only use eigenvectors of non-zero eigenvalues */
  138. int j(0);
  139. for (size_t i=0; i<K.rows(); i++)
  140. {
  141. if ( eigenValues(i) < 1e-12 )
  142. {
  143. eigenVectors.deleteCol(i);
  144. }
  145. else
  146. {
  147. j++;
  148. }
  149. }
  150. eigenValues.resize(j);
  151. /** scale eigenvectors with eigenvalues */
  152. double scale(0.0);
  153. for (size_t c=0; c<eigenVectors.cols(); c++)
  154. {
  155. scale = 1.0/sqrt(eigenValues(c));
  156. for (size_t r=0; r<eigenVectors.rows(); r++)
  157. eigenVectors(r,c) *= scale;
  158. }
  159. eigenBasis = eigenVectors;
  160. }
  161. void KCNullSpaceNovelty::centerKernelMatrix(NICE::Matrix & kernelMatrix)
  162. {
  163. NICE::Matrix onesK (kernelMatrix.rows(), kernelMatrix.cols(), 1.0/kernelMatrix.rows());
  164. kernelMatrix = kernelMatrix - onesK*kernelMatrix - kernelMatrix*onesK + onesK*kernelMatrix*onesK;
  165. }
  166. void KCNullSpaceNovelty::computeNullProjectionDirections ( const KernelData *kernelData, const NICE::Vector & y )
  167. {
  168. /** obtain Kernel PCA basis */
  169. if (verbose)
  170. std::cerr << "KCNullSpaceNovelty::computeNullProjectionDirections: compute kernel PCA basis..." << std::endl;
  171. computeBasisUsingKernelPCA(kernelData);
  172. /** set matrix IM=(I-M) with I being the unit matrix and M being a matrix with all entries equal to 1/n where n is the number of training samples */
  173. NICE::Matrix IM (y.size(),y.size(),-1.0/y.size());
  174. IM.addIdentity(1.0);
  175. /** compute matrix IL=(I-L) with I being the unit matrix and L being a block matrix where in each block of class samples each entry is equal to 1/numClassSamples */
  176. NICE::Matrix IL (y.size(),y.size(),0.0);
  177. IL.addIdentity(1.0);
  178. for (size_t c=0; c<IL.cols(); c++)
  179. {
  180. /** if sample with index r is in the same class as sample with index c, then insert the value 1/numClassSamples */
  181. for (size_t r=0; IL.rows(); r++)
  182. {
  183. if ( y(r) == y(c) )
  184. {
  185. IL(r,c) -= 1.0/trainingSetStatistic[(int)y(r)];;
  186. }
  187. }
  188. }
  189. /** compute Matrix H = ((I-M)*basisvecs)^T * kernelMatrix * (I-L) with I being the unit matrix */
  190. NICE::Matrix H (y.size(),y.size(),0.0);
  191. IM = IM*eigenBasis;
  192. H = IM.transpose() * kernelData->getKernelMatrix() * IL;
  193. /** obtain matrix T = H * H^T */
  194. NICE::Matrix T = H*H.transpose();
  195. /** get eigenvectors and eigenvalues (descreasing order) of T */
  196. NICE::Matrix eigenVectors(T.rows(), T.cols(), 0.0);
  197. NICE::Vector eigenValues(T.rows(), 0.0);
  198. eigenvectorvalues(T, eigenVectors, eigenValues);
  199. /** only use eigenvectors of zero eigenvalues (null space!!!) but at least one eigenvector according to the smallest eigenvalue (therefore start at index i=T.rows()-2)*/
  200. for (int i=T.rows()-2; i>=0; i--)
  201. {
  202. if ( eigenValues(i) > 1e-12 )
  203. {
  204. eigenVectors.deleteCol(i);
  205. }
  206. }
  207. /** compute null projection directions */
  208. nullProjectionDirections = IM*eigenVectors;
  209. dimNullSpace = nullProjectionDirections.cols();
  210. if (verbose)
  211. std::cerr << "KCNullSpaceNovelty::computeNullProjectionDirections: computation done" << std::endl;
  212. }
  213. void KCNullSpaceNovelty::computeTargetPoints ( const KernelData *kernelData, const NICE::Vector & y )
  214. {
  215. targetPoints.clear();
  216. NICE::Vector targetPoint (dimNullSpace, 0.0);
  217. int classLabel(0);
  218. if (oneClassSetting)
  219. {
  220. std::map<int,int>::iterator it;
  221. it = trainingSetStatistic.begin();
  222. classLabel = (*it).first;
  223. for (size_t i=0; i<y.size(); i++)
  224. {
  225. if ( classLabel == y(i) )
  226. {
  227. /** project each training sample in the null space */
  228. targetPoint += nullProjectionDirections.transpose() * kernelData->getKernelMatrix().getColumn(i);
  229. }
  230. }
  231. /** average the projections of all class samples due to numerical noise */
  232. targetPoint /= (*it).second;
  233. /** we only have one target point in an one-class setting */
  234. targetPoints.push_back(targetPoint);
  235. }
  236. else
  237. {
  238. /** create averaging vectors for each class, necessary to compute target points at the end of this method */
  239. std::vector<NICE::Vector> averagingVectors;
  240. averagingVectors.clear();
  241. NICE::Vector averagingVector(y.size(),0.0);
  242. /** insert one averaging vector and one target point vector for each class */
  243. std::map<int,int>::iterator it;
  244. int numClassSamples(0);
  245. for ( it = trainingSetStatistic.begin(); it != trainingSetStatistic.end(); it++ )
  246. {
  247. /** create current averaging vector */
  248. classLabel = (*it).first;
  249. numClassSamples = (*it).second;
  250. for ( size_t j = 0; j < y.size(); j++ )
  251. {
  252. if ( classLabel == y[j] )
  253. {
  254. averagingVector(j) = 1.0/numClassSamples;
  255. }
  256. else
  257. {
  258. averagingVector(j) = 0.0;
  259. }
  260. }
  261. /** insert averaging vector for current class*/
  262. averagingVectors.push_back(averagingVector);
  263. /** insert a null vector for the target point of the current class */
  264. targetPoints.push_back(targetPoint);
  265. }
  266. /** compute target points using previously created averaging vectors: average for each class the projections of the class samples in the null space */
  267. for ( size_t i = 0 ; i < targetPoints.size(); i++ )
  268. {
  269. targetPoints[i] = nullProjectionDirections.transpose() * kernelData->getKernelMatrix() * averagingVectors[i];
  270. }
  271. }
  272. if (verbose)
  273. std::cerr << "KCNullSpaceNovelty::computeTargetPoints: computation done" << std::endl;
  274. }
  275. ClassificationResult KCNullSpaceNovelty::classifyKernel ( const NICE::Vector & kernelVector, double kernelSelf ) const
  276. {
  277. if ( targetPoints.size() <= 0 )
  278. fthrow(Exception, "The classifier was not trained with training data (use teach(...))");
  279. NICE::Vector projection(dimNullSpace,0.0);
  280. projection = nullProjectionDirections.transpose() * kernelVector;
  281. FullVector scores ( trainingSetStatistic.size() );
  282. scores.set(0);
  283. std::map<int,int>::iterator it;
  284. int iter(0);
  285. for ( it = ( (std::map<int,int>)trainingSetStatistic ).begin(); it != trainingSetStatistic.end(); it++ )
  286. {
  287. scores[iter] = -(targetPoints[iter] - projection).normL2();
  288. iter++;
  289. }
  290. ClassificationResult r ( scores.maxElement(), scores );
  291. return r;
  292. }
  293. KCNullSpaceNovelty* KCNullSpaceNovelty::clone(void) const
  294. {
  295. KCNullSpaceNovelty *classifier = new KCNullSpaceNovelty( *this );
  296. return classifier;
  297. }
  298. void KCNullSpaceNovelty::clear()
  299. {
  300. //nothing to clear
  301. }
  302. void KCNullSpaceNovelty::restore(std::istream& ifs, int type)
  303. {
  304. ifs.precision (numeric_limits<double>::digits10 + 1);
  305. ifs >> maxClassNo;
  306. ifs >> oneClassSetting;
  307. ifs >> dimNullSpace;
  308. ifs >> eigenBasis;
  309. int k(0);
  310. int classLabel(0);
  311. int numClassSamples(0);
  312. ifs >> k;
  313. trainingSetStatistic.clear();
  314. for (int i=0; i<k; i++)
  315. {
  316. ifs >> classLabel;
  317. ifs >> numClassSamples;
  318. trainingSetStatistic.insert( pair<int,int>(classLabel,numClassSamples) );
  319. }
  320. ifs >> nullProjectionDirections;
  321. NICE::Vector targetPoint;
  322. ifs >> k;
  323. for (int i=0; i<k; i++)
  324. {
  325. ifs >> targetPoint;
  326. targetPoints.push_back(targetPoint);
  327. }
  328. KernelClassifier::restore(ifs,type);
  329. }
  330. void KCNullSpaceNovelty::store(std::ostream& ofs, int type) const
  331. {
  332. ofs.precision (numeric_limits<double>::digits10 + 1);
  333. ofs << maxClassNo << endl;
  334. ofs << oneClassSetting << endl;
  335. ofs << dimNullSpace << endl;
  336. ofs << eigenBasis << endl;
  337. ofs << trainingSetStatistic.size() << endl;
  338. std::map<int,int>::iterator it;
  339. for (it = ( (std::map<int,int>)trainingSetStatistic ).begin() ; it != trainingSetStatistic.end(); it++)
  340. {
  341. ofs << (*it).first << endl;
  342. ofs << (*it).second << endl;
  343. }
  344. ofs << nullProjectionDirections << endl;
  345. ofs << targetPoints.size() << endl;
  346. for (size_t k=0; k<targetPoints.size(); k++)
  347. {
  348. ofs << targetPoints[k] << endl;
  349. }
  350. KernelClassifier::store(ofs,type);
  351. }