testNullSpaceNovelty.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @file testNullSpaceNovelty.cpp
  3. * @brief test function for class KCNullSpaceNovelty
  4. * @author Paul Bodesheim
  5. * @date 28-11-2012 (dd-mm-yyyy)
  6. */
  7. #include <ctime>
  8. #include <time.h>
  9. #ifdef NICE_USELIB_MATIO
  10. #include "core/basics/Config.h"
  11. #include "core/basics/Timer.h"
  12. #include "core/vector/Algorithms.h"
  13. #include "core/vector/SparseVectorT.h"
  14. #include "vislearning/classifier/kernelclassifier/KCNullSpaceNovelty.h"
  15. #include "vislearning/math/kernels/KernelData.h"
  16. #include "vislearning/cbaselib/ClassificationResults.h"
  17. #include "vislearning/baselib/ProgressBar.h"
  18. #include "core/matlabAccess/MatFileIO.h"
  19. #include "vislearning/matlabAccessHighLevel/ImageNetData.h"
  20. using namespace std;
  21. using namespace NICE;
  22. using namespace OBJREC;
  23. // --------------- THE KERNEL FUNCTION ( exponential kernel with euclidian distance ) ----------------------
  24. double measureDistance ( const NICE::SparseVector & a, const NICE::SparseVector & b, const double & sigma = 2.0)
  25. {
  26. double inner_sum(0.0);
  27. double d;
  28. //new version, where we needed on average 0.001707 s for each test sample
  29. NICE::SparseVector::const_iterator aIt = a.begin();
  30. NICE::SparseVector::const_iterator bIt = b.begin();
  31. //compute the euclidian distance between both feature vectores (given as SparseVectors)
  32. while ( (aIt != a.end()) && (bIt != b.end()) )
  33. {
  34. if (aIt->first == bIt->first)
  35. {
  36. d = ( aIt->second - bIt->second );
  37. inner_sum += d * d;
  38. aIt++;
  39. bIt++;
  40. }
  41. else if ( aIt->first < bIt->first)
  42. {
  43. inner_sum += aIt->second * aIt->second;
  44. aIt++;
  45. }
  46. else
  47. {
  48. inner_sum += bIt->second * bIt->second;
  49. bIt++;
  50. }
  51. }
  52. //compute remaining values, if b reached the end but not a
  53. while (aIt != a.end())
  54. {
  55. inner_sum += aIt->second * aIt->second;
  56. aIt++;
  57. }
  58. //compute remaining values, if a reached the end but not b
  59. while (bIt != b.end())
  60. {
  61. inner_sum += bIt->second * bIt->second;
  62. bIt++;
  63. }
  64. //normalization of the exponent
  65. inner_sum /= (2.0*sigma*sigma);
  66. //finally, compute the RBF-kernel score (RBF = radial basis function)
  67. return exp(-inner_sum);
  68. }
  69. // --------------- THE KERNEL FUNCTION ( HIK ) ----------------------
  70. double minimumDistance ( const NICE::SparseVector & a, const NICE::SparseVector & b )
  71. {
  72. double inner_sum(0.0);
  73. NICE::SparseVector::const_iterator aIt = a.begin();
  74. NICE::SparseVector::const_iterator bIt = b.begin();
  75. //compute the minimum distance between both feature vectores (given as SparseVectors)
  76. while ( (aIt != a.end()) && (bIt != b.end()) )
  77. {
  78. if (aIt->first == bIt->first)
  79. {
  80. inner_sum += std::min( aIt->second , bIt->second );
  81. aIt++;
  82. bIt++;
  83. }
  84. else if ( aIt->first < bIt->first)
  85. {
  86. aIt++;
  87. }
  88. else
  89. {
  90. bIt++;
  91. }
  92. }
  93. return inner_sum;
  94. }
  95. /**
  96. test the basic functionality of fast-hik hyperparameter optimization
  97. */
  98. int main (int argc, char **argv)
  99. {
  100. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  101. Config conf ( argc, argv );
  102. string resultsfile = conf.gS("main", "results", "results.txt" );
  103. int nrOfExamplesPerClass = conf.gI("main", "nrOfExamplesPerClass", 100);
  104. nrOfExamplesPerClass = std::min(nrOfExamplesPerClass, 100); // we do not have more than 100 examples per class
  105. // -------- read ImageNet data --------------
  106. std::vector<SparseVector> trainingData;
  107. NICE::Vector y;
  108. std::cerr << "Reading ImageNet data ..." << std::endl;
  109. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  110. string imageNetPath;
  111. if (imageNetLocal)
  112. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  113. else
  114. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  115. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  116. imageNetTrain.preloadData( "train", "training" );
  117. trainingData = imageNetTrain.getPreloadedData();
  118. y = imageNetTrain.getPreloadedLabels();
  119. std::cerr << "Reading of training data finished" << std::endl;
  120. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  121. std::cerr << "y.size(): " << y.size() << std::endl;
  122. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  123. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  124. imageNetTest.preloadData ( "val", "testing" );
  125. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  126. // -------- select training set -------------
  127. NICE::Vector knownClassLabels(5,0.0);
  128. for (int k=1; k<6; k++)
  129. knownClassLabels(k-1) = k;
  130. std::vector<SparseVector> currentTrainingData;
  131. currentTrainingData.clear();
  132. NICE::Vector currentTrainingLabels(nrOfExamplesPerClass*knownClassLabels.size(),0);
  133. int k(0);
  134. for (size_t i = 0; i < y.size(); i++)
  135. {
  136. for (size_t j=0; j<knownClassLabels.size(); j++)
  137. {
  138. if ( y[i] == knownClassLabels[j] )
  139. {
  140. currentTrainingLabels(k) = knownClassLabels[j];
  141. currentTrainingData.push_back(trainingData[i]);
  142. k++;
  143. break;
  144. }
  145. }
  146. }
  147. Timer tTrain;
  148. tTrain.start();
  149. //compute the kernel matrix
  150. NICE::Matrix kernelMatrix(nrOfExamplesPerClass*knownClassLabels.size(), nrOfExamplesPerClass*knownClassLabels.size(), 0.0);
  151. double kernelScore(0.0);
  152. int cl(0);
  153. for (size_t i = 0; i < kernelMatrix.rows(); i++)
  154. {
  155. for (size_t j = i; j < kernelMatrix.cols(); j++)
  156. {
  157. kernelScore = minimumDistance(currentTrainingData[i],currentTrainingData[j]);
  158. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  159. if (i != j)
  160. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  161. }
  162. }
  163. KernelData kernelData( &conf, kernelMatrix, "Kernel", false );
  164. KCNullSpaceNovelty knfst( &conf);
  165. knfst.teach(&kernelData, currentTrainingLabels);
  166. tTrain.stop();
  167. std::cerr << "Time used for training " << cl << ": " << tTrain.getLast() << std::endl;
  168. std::cerr << "training set statistic: " << std::endl;
  169. std::map<int,int>::iterator itt;
  170. for (itt = ( (std::map<int,int>) knfst.getTrainingSetStatistic() ).begin(); itt != knfst.getTrainingSetStatistic().end(); itt++)
  171. std::cerr << (*itt).first << " " << (*itt).second << std::endl;
  172. std::cerr << "one-class setting?: " << knfst.isOneClass() << std::endl;
  173. std::cerr << "null space dimension: "<< knfst.getNullSpaceDimension() << std::endl;
  174. std::cerr << "target points: " << std::endl;
  175. for (size_t k=0; k<knfst.getTargetPoints().size(); k++)
  176. std::cerr << knfst.getTargetPoints()[k] << std::endl;
  177. std::cerr << "training done - now perform the evaluation" << std::endl;
  178. // ------------------------------ TESTING ------------------------------
  179. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  180. ClassificationResults results;
  181. ProgressBar pb;
  182. Timer tTest;
  183. tTest.start();
  184. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  185. {
  186. pb.update ( imageNetTest.getNumPreloadedExamples() );
  187. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  188. //compute (self) similarities
  189. double kernelSelf (minimumDistance(svec,svec) );
  190. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  191. for (int j = 0; j < nrOfExamplesPerClass; j++)
  192. {
  193. kernelVector[j] = minimumDistance(currentTrainingData[j],svec);
  194. }
  195. ClassificationResult r;
  196. r = knfst.classifyKernel( kernelVector, kernelSelf);
  197. // set ground truth label
  198. r.classno_groundtruth = 0;
  199. for (size_t j=0; j<knownClassLabels.size(); j++)
  200. {
  201. if ( ((int)imageNetTest.getPreloadedLabel ( i )) == knownClassLabels[j] )
  202. {
  203. r.classno_groundtruth = 1;
  204. break;
  205. }
  206. }
  207. //remember the results for the evaluation lateron
  208. results.push_back ( r );
  209. }
  210. tTest.stop();
  211. std::cerr << "Time used for evaluation: " << tTest.getLast() << std::endl;
  212. double timeForSingleExample(0.0);
  213. timeForSingleExample = tTest.getLast()/imageNetTest.getNumPreloadedExamples();
  214. std::cerr.precision(10);
  215. std::cerr << "time used for evaluation single elements: " << timeForSingleExample << std::endl;
  216. // run the AUC-evaluation
  217. double perfvalue( 0.0 );
  218. perfvalue = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  219. std::cerr << " novelty detection performance: " << perfvalue << std::endl;
  220. return 0;
  221. }
  222. #else
  223. int main (int argc, char **argv)
  224. {
  225. }
  226. #endif