testImageNetBinaryBruteForce.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * @file testImageNetBinaryBruteForce.cpp
  3. * @brief perform ImageNet tests with binary tasks for OCC using GP mean and variance, sophisticated approximations of both, Parzen Density Estimation and SVDD
  4. * @author Alexander Lütz
  5. * @date 23-05-2012 (dd-mm-yyyy)
  6. */
  7. #include <ctime>
  8. #include <time.h>
  9. #include "core/basics/Config.h"
  10. #include "core/basics/Timer.h"
  11. #include "core/algebra/CholeskyRobust.h"
  12. #include "core/vector/Algorithms.h"
  13. #include "core/vector/SparseVectorT.h"
  14. #include "vislearning/cbaselib/ClassificationResults.h"
  15. #include "vislearning/baselib/ProgressBar.h"
  16. #include "fast-hik/tools.h"
  17. #include "fast-hik/MatFileIO.h"
  18. #include "fast-hik/ImageNetData.h"
  19. using namespace std;
  20. using namespace NICE;
  21. using namespace OBJREC;
  22. // --------------- THE KERNEL FUNCTION ( exponential kernel with euclidian distance ) ----------------------
  23. double measureDistance ( const NICE::SparseVector & a, const NICE::SparseVector & b, const double & sigma = 2.0)//, const bool & verbose = false)
  24. {
  25. double inner_sum(0.0);
  26. double d;
  27. //new version, where we needed on average 0.001707 s for each test sample
  28. NICE::SparseVector::const_iterator aIt = a.begin();
  29. NICE::SparseVector::const_iterator bIt = b.begin();
  30. while ( (aIt != a.end()) && (bIt != b.end()) )
  31. {
  32. if (aIt->first == bIt->first)
  33. {
  34. d = ( aIt->second - bIt->second );
  35. inner_sum += d * d;
  36. aIt++;
  37. bIt++;
  38. }
  39. else if ( aIt->first < bIt->first)
  40. {
  41. inner_sum += aIt->second * aIt->second;
  42. aIt++;
  43. }
  44. else
  45. {
  46. inner_sum += bIt->second * bIt->second;
  47. bIt++;
  48. }
  49. }
  50. //compute remaining values, if b reached the end but not a
  51. while (aIt != a.end())
  52. {
  53. inner_sum += aIt->second * aIt->second;
  54. aIt++;
  55. }
  56. //compute remaining values, if a reached the end but not b
  57. while (bIt != b.end())
  58. {
  59. inner_sum += bIt->second * bIt->second;
  60. bIt++;
  61. }
  62. inner_sum /= (2.0*sigma*sigma);
  63. return exp(-inner_sum);
  64. }
  65. void readParameters(const string & filename, const int & size, NICE::Vector & parameterVector)
  66. {
  67. parameterVector.resize(size);
  68. parameterVector.set(0.0);
  69. ifstream is(filename.c_str());
  70. if ( !is.good() )
  71. fthrow(IOException, "Unable to read parameters.");
  72. //
  73. string tmp;
  74. int cnt(0);
  75. while (! is.eof())
  76. {
  77. is >> tmp;
  78. parameterVector[cnt] = atof(tmp.c_str());
  79. cnt++;
  80. }
  81. //
  82. is.close();
  83. }
  84. //------------------- TRAINING METHODS --------------------
  85. void inline trainGPVarApprox(NICE::Vector & matrixDInv, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber)
  86. {
  87. std::cerr << "nrOfExamplesPerClass : " << nrOfExamplesPerClass << std::endl;
  88. Timer tTrainPreciseTimer;
  89. tTrainPreciseTimer.start();
  90. // time_t time;
  91. // std::cerr <<
  92. std::cerr << time(NULL) << std::endl;
  93. //tic tTrainPrecise
  94. clock_t tTrainPreciseStart = clock() * CLOCKS_PER_SEC;
  95. usleep(35);
  96. matrixDInv.resize(nrOfExamplesPerClass);
  97. matrixDInv.set(0.0);
  98. //compute D
  99. //start with adding some noise, if necessary
  100. if (noise != 0.0)
  101. matrixDInv.set(noise);
  102. else
  103. matrixDInv.set(0.0);
  104. for (int i = 0; i < nrOfExamplesPerClass; i++)
  105. {
  106. for (int j = i; j < nrOfExamplesPerClass; j++)
  107. {
  108. matrixDInv[i] += kernelMatrix(i,j);
  109. if (i != j)
  110. matrixDInv[j] += kernelMatrix(i,j);
  111. }
  112. }
  113. //compute its inverse
  114. for (int i = 0; i < nrOfExamplesPerClass; i++)
  115. {
  116. matrixDInv[i] = 1.0 / matrixDInv[i];
  117. }
  118. tTrainPreciseTimer.stop();
  119. std::cerr << "Precise time used for training class " << classNumber << ": " << tTrainPreciseTimer.getLast() << std::endl;
  120. //toc tTrainPrecise
  121. clock_t currentTime = clock() * CLOCKS_PER_SEC;
  122. float tTrainPrecise = (float) (currentTime - tTrainPreciseStart);
  123. std::cerr << "start time: " << tTrainPreciseStart << std::endl;
  124. std::cerr << "current time: " << currentTime << std::endl;
  125. std::cerr << "Precise time used for GPVarApprox training class " << classNumber << ": " << currentTime-tTrainPreciseStart << std::endl;
  126. std::cerr << "final time in system clock whatever:" << std::endl;
  127. std::cerr << time(NULL) << std::endl;
  128. }
  129. void inline trainGPVar(NICE::Matrix & choleskyMatrix, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber)
  130. {
  131. /* Timer tTrainPrecise;
  132. tTrainPrecise.start(); */
  133. //tic tTrainPrecise
  134. time_t tTrainPreciseStart = clock();
  135. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  136. choleskyMatrix.resize(nrOfExamplesPerClass, nrOfExamplesPerClass);
  137. choleskyMatrix.set(0.0);
  138. cr.robustChol ( kernelMatrix, choleskyMatrix );
  139. // tTrainPrecise.stop();
  140. // std::cerr << "Precise time used for training class " << classNumber << ": " << tTrainPrecise.getLast() << std::endl;
  141. //toc tTrainPrecise
  142. time_t currentTime = clock();
  143. float tTrainPrecise = (float) (currentTime - tTrainPreciseStart);
  144. std::cerr << "start time: " << tTrainPreciseStart << std::endl;
  145. std::cerr << "current time: " << currentTime << std::endl;
  146. std::cerr << "Precise time used for GPVar training class " << classNumber << ": " << tTrainPrecise/CLOCKS_PER_SEC << std::endl;
  147. }
  148. void inline trainGPMeanApprox(NICE::Vector & GPMeanApproxRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber)
  149. {
  150. /* Timer tTrainPrecise;
  151. tTrainPrecise.start(); */
  152. //tic tTrainPrecise
  153. time_t tTrainPreciseStart = clock();
  154. NICE::Vector matrixDInv(nrOfExamplesPerClass,0.0);
  155. //compute D
  156. //start with adding some noise, if necessary
  157. if (noise != 0.0)
  158. matrixDInv.set(noise);
  159. else
  160. matrixDInv.set(0.0);
  161. for (int i = 0; i < nrOfExamplesPerClass; i++)
  162. {
  163. for (int j = i; j < nrOfExamplesPerClass; j++)
  164. {
  165. matrixDInv[i] += kernelMatrix(i,j);
  166. if (i != j)
  167. matrixDInv[j] += kernelMatrix(i,j);
  168. }
  169. }
  170. //compute its inverse (and multiply every element with the label vector, which contains only one-entries...)
  171. GPMeanApproxRightPart.resize(nrOfExamplesPerClass);
  172. for (int i = 0; i < nrOfExamplesPerClass; i++)
  173. {
  174. GPMeanApproxRightPart[i] = 1.0 / matrixDInv[i];
  175. }
  176. // tTrainPrecise.stop();
  177. // std::cerr << "Precise time used for training class " << classNumber << ": " << tTrainPrecise.getLast() << std::endl;
  178. //toc tTrainPrecise
  179. time_t currentTime = clock();
  180. float tTrainPrecise = (float) (currentTime - tTrainPreciseStart);
  181. std::cerr << "start time: " << tTrainPreciseStart << std::endl;
  182. std::cerr << "current time: " << currentTime << std::endl;
  183. std::cerr << "Precise time used for GPMeanApprox training class " << classNumber << ": " << tTrainPrecise/CLOCKS_PER_SEC << std::endl;
  184. }
  185. void inline trainGPMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber)
  186. {
  187. /* Timer tTrainPrecise;
  188. tTrainPrecise.start(); */
  189. //tic tTrainPrecise
  190. time_t tTrainPreciseStart = clock();
  191. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  192. NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  193. cr.robustChol ( kernelMatrix, choleskyMatrix );
  194. GPMeanRightPart.resize(nrOfExamplesPerClass);
  195. GPMeanRightPart.set(0.0);
  196. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  197. choleskySolveLargeScale ( choleskyMatrix, y, GPMeanRightPart );
  198. // tTrainPrecise.stop();
  199. // std::cerr << "Precise time used for training class " << classNumber << ": " << tTrainPrecise.getLast() << std::endl;
  200. //toc tTrainPrecise
  201. time_t currentTime = clock();
  202. float tTrainPrecise = (float) (currentTime - tTrainPreciseStart);
  203. std::cerr << "start time: " << tTrainPreciseStart << std::endl;
  204. std::cerr << "current time: " << currentTime << std::endl;
  205. std::cerr << "Precise time used for GPMean training class " << classNumber << ": " << tTrainPrecise/CLOCKS_PER_SEC << std::endl;
  206. }
  207. void inline trainSVDD( const double & noise, const NICE::Matrix kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber)
  208. {
  209. /* Timer tTrainPrecise;
  210. tTrainPrecise.start(); */
  211. //tic tTrainPrecise
  212. time_t tTrainPreciseStart = clock();
  213. // tTrainPrecise.stop();
  214. // std::cerr << "Precise time used for training class " << classNumber << ": " << tTrainPrecise.getLast() << std::endl;
  215. //toc tTrainPrecise
  216. time_t currentTime = clock();
  217. float tTrainPrecise = (float) (currentTime - tTrainPreciseStart);
  218. //TODO!!!
  219. std::cerr << "start time: " << tTrainPreciseStart << std::endl;
  220. std::cerr << "current time: " << currentTime << std::endl;
  221. std::cerr << "Precise time used for SVDD training class " << classNumber << ": " << tTrainPrecise/CLOCKS_PER_SEC << std::endl;
  222. }
  223. // ------------- EVALUATION METHODS ---------------------
  224. void inline evaluateGPVarApprox(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples)
  225. {
  226. Timer tTestSingle;
  227. tTestSingle.start();
  228. NICE::Vector rightPart (kernelVector.size());
  229. for (int j = 0; j < kernelVector.size(); j++)
  230. {
  231. rightPart[j] = kernelVector[j] * matrixDInv[j];
  232. }
  233. double uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  234. tTestSingle.stop();
  235. timeForSingleExamples += tTestSingle.getLast();
  236. FullVector scores ( 2 );
  237. scores[0] = 0.0;
  238. scores[1] = 1.0 - uncertainty;
  239. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  240. }
  241. void inline evaluateGPVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Matrix & choleskyMatrix, ClassificationResult & r, double & timeForSingleExamples)
  242. {
  243. Timer tTestSingle;
  244. tTestSingle.start();
  245. NICE::Vector rightPart (kernelVector.size(),0.0);
  246. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  247. double uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  248. tTestSingle.stop();
  249. timeForSingleExamples += tTestSingle.getLast();
  250. FullVector scores ( 2 );
  251. scores[0] = 0.0;
  252. scores[1] = 1.0 - uncertainty;
  253. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  254. }
  255. void inline evaluateGPMeanApprox(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples)
  256. {
  257. Timer tTestSingle;
  258. tTestSingle.start();
  259. double mean = kernelVector.scalarProduct ( rightPart );
  260. tTestSingle.stop();
  261. timeForSingleExamples += tTestSingle.getLast();
  262. FullVector scores ( 2 );
  263. scores[0] = 0.0;
  264. scores[1] = mean;
  265. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  266. }
  267. void inline evaluateGPMean(const NICE::Vector & kernelVector, const NICE::Vector & GPMeanRightPart, ClassificationResult & r, double & timeForSingleExamples)
  268. {
  269. Timer tTestSingle;
  270. tTestSingle.start();
  271. double mean = kernelVector.scalarProduct ( GPMeanRightPart );
  272. tTestSingle.stop();
  273. timeForSingleExamples += tTestSingle.getLast();
  274. FullVector scores ( 2 );
  275. scores[0] = 0.0;
  276. scores[1] = mean;
  277. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  278. }
  279. void inline evaluateParzen(const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples)
  280. {
  281. Timer tTestSingle;
  282. tTestSingle.start();
  283. double score( kernelVector.Sum() / (double) kernelVector.size() ); //maybe we could directly call kernelVector.Mean()
  284. tTestSingle.stop();
  285. timeForSingleExamples += tTestSingle.getLast();
  286. FullVector scores ( 2 );
  287. scores[0] = 0.0;
  288. scores[1] = score;
  289. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  290. }
  291. void inline evaluateSVDD(const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples)
  292. {
  293. Timer tTestSingle;
  294. tTestSingle.start();
  295. double score (0.0);
  296. //TODO
  297. tTestSingle.stop();
  298. timeForSingleExamples += tTestSingle.getLast();
  299. FullVector scores ( 2 );
  300. scores[0] = 0.0;
  301. scores[1] = score;
  302. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  303. }
  304. /**
  305. test the basic functionality of fast-hik hyperparameter optimization
  306. */
  307. int main (int argc, char **argv)
  308. {
  309. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  310. Config conf ( argc, argv );
  311. string resultsfile = conf.gS("main", "results", "results.txt" );
  312. int nrOfExamplesPerClass = conf.gI("main", "nrOfExamplesPerClass", 50);
  313. nrOfExamplesPerClass = std::min(nrOfExamplesPerClass, 100); // we do not have more than 100 examples per class
  314. int indexOfFirstClass = conf.gI("main", "indexOfFirstClass", 0);
  315. indexOfFirstClass = std::max(indexOfFirstClass, 0); //we do not have less than 0 classes
  316. int indexOfLastClass = conf.gI("main", "indexOfLastClass", 999);
  317. indexOfLastClass = std::min(indexOfLastClass, 999); //we do not have more than 1000 classes
  318. int nrOfClassesToConcidere = (indexOfLastClass - indexOfLastClass)+1;
  319. //read the optimal parameters for the different methods
  320. // GP variance approximation
  321. string sigmaGPVarApproxFile = conf.gS("main", "sigmaGPVarApproxFile", "approxVarSigma.txt");
  322. string noiseGPVarApproxFile = conf.gS("main", "noiseGPVarApproxFile", "approxVarNoise.txt");
  323. // GP variance
  324. string sigmaGPVarFile = conf.gS("main", "sigmaGPVarFile", "approxVarSigma.txt");
  325. string noiseGPVarFile = conf.gS("main", "noiseGPVarFile", "approxVarNoise.txt");
  326. //GP mean approximation
  327. string sigmaGPMeanApproxFile = conf.gS("main", "sigmaGPMeanApproxFile", "approxVarSigma.txt");
  328. string noiseGPMeanApproxFile = conf.gS("main", "noiseGPMeanApproxFile", "approxVarNoise.txt");
  329. //GP mean
  330. string sigmaGPMeanFile = conf.gS("main", "sigmaGPMeanFile", "approxVarSigma.txt");
  331. string noiseGPMeanFile = conf.gS("main", "noiseGPMeanFile", "approxVarNoise.txt");
  332. //Parzen
  333. string sigmaParzenFile = conf.gS("main", "sigmaParzenFile", "approxVarSigma.txt");
  334. string noiseParzenFile = conf.gS("main", "noiseParzenFile", "approxVarNoise.txt");
  335. //SVDD
  336. string sigmaSVDDFile = conf.gS("main", "sigmaSVDDFile", "approxVarSigma.txt");
  337. string noiseSVDDFile = conf.gS("main", "noiseSVDDFile", "approxVarNoise.txt");
  338. // GP variance approximation
  339. NICE::Vector sigmaGPVarApproxParas(nrOfClassesToConcidere,0.0);
  340. NICE::Vector noiseGPVarApproxParas(nrOfClassesToConcidere,0.0);
  341. // GP variance
  342. NICE::Vector sigmaGPVarParas(nrOfClassesToConcidere,0.0);
  343. NICE::Vector noiseGPVarParas(nrOfClassesToConcidere,0.0);
  344. //GP mean approximation
  345. NICE::Vector sigmaGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  346. NICE::Vector noiseGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  347. //GP mean
  348. NICE::Vector sigmaGPMeanParas(nrOfClassesToConcidere,0.0);
  349. NICE::Vector noiseGPMeanParas(nrOfClassesToConcidere,0.0);
  350. //Parzen
  351. NICE::Vector sigmaParzenParas(nrOfClassesToConcidere,0.0);
  352. NICE::Vector noiseParzenParas(nrOfClassesToConcidere,0.0);
  353. //SVDD
  354. NICE::Vector sigmaSVDDParas(nrOfClassesToConcidere,0.0);
  355. NICE::Vector noiseSVDDParas(nrOfClassesToConcidere,0.0);
  356. // GP variance approximation
  357. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarApproxParas);
  358. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarApproxParas);
  359. // GP variance
  360. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarParas);
  361. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarParas);
  362. //GP mean approximation
  363. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanApproxParas);
  364. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanApproxParas);
  365. //GP mean
  366. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanParas);
  367. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanParas);
  368. //Parzen
  369. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaParzenParas);
  370. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseParzenParas);
  371. //SVDD
  372. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaSVDDParas);
  373. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseSVDDParas);
  374. // -------- optimal parameters read --------------
  375. std::vector<SparseVector> trainingData;
  376. NICE::Vector y;
  377. std::cerr << "Reading ImageNet data ..." << std::endl;
  378. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  379. string imageNetPath;
  380. if (imageNetLocal)
  381. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  382. else
  383. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  384. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  385. imageNetTrain.preloadData( "train", "training" );
  386. trainingData = imageNetTrain.getPreloadedData();
  387. y = imageNetTrain.getPreloadedLabels();
  388. std::cerr << "Reading of training data finished" << std::endl;
  389. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  390. std::cerr << "y.size(): " << y.size() << std::endl;
  391. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  392. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  393. imageNetTest.preloadData ( "val", "testing" );
  394. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  395. double OverallPerformanceGPVarApprox(0.0);
  396. double OverallPerformanceGPVar(0.0);
  397. double OverallPerformanceGPMeanApprox(0.0);
  398. double OverallPerformanceGPMean(0.0);
  399. double OverallPerformanceParzen(0.0);
  400. double OverallPerformanceSVDD(0.0);
  401. double kernelSigmaGPVarApprox;
  402. double kernelSigmaGPVar;
  403. double kernelSigmaGPMeanApprox;
  404. double kernelSigmaGPMean;
  405. double kernelSigmaParzen;
  406. double kernelSigmaSVDD;
  407. for (int cl = indexOfFirstClass; cl < indexOfLastClass; cl++)
  408. {
  409. std::cerr << "run for class " << cl << std::endl;
  410. int positiveClass = cl+1; //labels are from 1 to 1000, but our indices from 0 to 999
  411. // ------------------------------ TRAINING ------------------------------
  412. kernelSigmaGPVarApprox = sigmaGPVarApproxParas[cl];
  413. kernelSigmaGPVar = sigmaGPVarParas[cl];
  414. kernelSigmaGPMeanApprox = sigmaGPMeanApproxParas[cl];
  415. kernelSigmaGPMean = sigmaGPMeanParas[cl];
  416. kernelSigmaParzen = sigmaParzenParas[cl];
  417. kernelSigmaSVDD = sigmaSVDDParas[cl];
  418. Timer tTrain;
  419. tTrain.start();
  420. NICE::Matrix kernelMatrix(nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  421. //TODO in theory we have to compute a single kernel Matrix for every method, since every method may have its own optimal parameter
  422. // I'm sure, we can speed it up a bit and compute it only for every different parameter
  423. //nonetheless, it's not as nice as we originally thought (same matrix for every method)
  424. //NOTE since we're only interested in runtimes, we can ignore this (and still do some further code optimization...) //TODO
  425. /* //adding some noise, if necessary
  426. if (noiseParas[cl] != 0.0)
  427. {
  428. kernelMatrix.addIdentity(noiseParas[cl]);
  429. }
  430. else
  431. {
  432. //zero was already set
  433. } */
  434. //now sum up all entries of each row in the original kernel matrix
  435. double kernelScore(0.0);
  436. for (int i = cl*100; i < cl*100+nrOfExamplesPerClass; i++)
  437. {
  438. for (int j = i; j < cl*100+nrOfExamplesPerClass; j++)
  439. {
  440. kernelScore = measureDistance(trainingData[i],trainingData[j], kernelSigmaGPVarApprox);
  441. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  442. if (i != j)
  443. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  444. }
  445. }
  446. //train GP Var Approx
  447. NICE::Vector matrixDInv;
  448. trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl);
  449. //train GP Var
  450. NICE::Matrix GPVarCholesky;
  451. trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl);
  452. //train GP Mean Approx
  453. NICE::Vector GPMeanApproxRightPart;
  454. trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl);
  455. //train GP Mean
  456. NICE::Vector GPMeanRightPart;
  457. trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl);
  458. //train Parzen
  459. //nothing to do :)
  460. //train SVDD
  461. //TODO what do we need here?
  462. trainSVDD(noiseSVDDParas[cl], kernelMatrix, nrOfExamplesPerClass, cl);
  463. tTrain.stop();
  464. std::cerr << "Time used for training class " << cl << ": " << tTrain.getLast() << std::endl;
  465. std::cerr << "training done - now perform the evaluation" << std::endl;
  466. // ------------------------------ TESTING ------------------------------
  467. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  468. ClassificationResults resultsGPVarApprox;
  469. ClassificationResults resultsGPVar;
  470. ClassificationResults resultsGPMeanApprox;
  471. ClassificationResults resultsGPMean;
  472. ClassificationResults resultsParzen;
  473. ClassificationResults resultsSVDD;
  474. ProgressBar pb;
  475. Timer tTest;
  476. tTest.start();
  477. Timer tTestSingle;
  478. double timeForSingleExamplesGPVarApprox(0.0);
  479. double timeForSingleExamplesGPVar(0.0);
  480. double timeForSingleExamplesGPMeanApprox(0.0);
  481. double timeForSingleExamplesGPMean(0.0);
  482. double timeForSingleExamplesParzen(0.0);
  483. double timeForSingleExamplesSVDD(0.0);
  484. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  485. {
  486. pb.update ( imageNetTest.getNumPreloadedExamples() );
  487. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  488. //TODO: again we should use method-specific optimal parameters. If we're only interested in the runtimes, this doesn't matter
  489. double kernelSelf (measureDistance(svec,svec, kernelSigmaGPVarApprox) );
  490. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  491. for (int j = 0; j < nrOfExamplesPerClass; j++)
  492. {
  493. kernelVector[j] = measureDistance(trainingData[j+cl*100],svec, kernelSigmaGPVarApprox);
  494. }
  495. //evaluate GP Var Approx
  496. ClassificationResult rGPVarApprox;
  497. evaluateGPVarApprox(kernelVector, kernelSelf, matrixDInv, rGPVarApprox, timeForSingleExamplesGPVarApprox);
  498. //evaluate GP Var
  499. ClassificationResult rGPVar;
  500. evaluateGPVar(kernelVector, kernelSelf, GPVarCholesky, rGPVar, timeForSingleExamplesGPVar);
  501. //evaluate GP Mean Approx
  502. ClassificationResult rGPMeanApprox;
  503. evaluateGPMeanApprox(kernelVector, matrixDInv, rGPMeanApprox, timeForSingleExamplesGPMeanApprox);
  504. //evaluate GP Mean
  505. ClassificationResult rGPMean;
  506. evaluateGPMean(kernelVector, GPMeanRightPart, rGPMean, timeForSingleExamplesGPMean);
  507. //evaluate Parzen
  508. ClassificationResult rParzen;
  509. evaluateParzen(kernelVector, rParzen, timeForSingleExamplesParzen);
  510. //evaluate SVDD
  511. ClassificationResult rSVDD;
  512. evaluateSVDD(kernelVector, rSVDD, timeForSingleExamplesSVDD);
  513. // set ground truth label
  514. rGPVarApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  515. rGPVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  516. rGPMeanApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  517. rGPMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  518. rParzen.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  519. rSVDD.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  520. // std::cerr << "scores: " << std::endl;
  521. // scores >> std::cerr;
  522. // std::cerr << "gt: " << r.classno_groundtruth << " -- " << r.classno << std::endl;
  523. resultsGPVarApprox.push_back ( rGPVarApprox );
  524. resultsGPVar.push_back ( rGPVar );
  525. resultsGPMeanApprox.push_back ( rGPMeanApprox );
  526. resultsGPMean.push_back ( rGPMean );
  527. resultsParzen.push_back ( rParzen );
  528. resultsSVDD.push_back ( rSVDD );
  529. }
  530. tTest.stop();
  531. std::cerr << "Time used for evaluating class " << cl << ": " << tTest.getLast() << std::endl;
  532. timeForSingleExamplesGPVarApprox/= imageNetTest.getNumPreloadedExamples();
  533. timeForSingleExamplesGPVar/= imageNetTest.getNumPreloadedExamples();
  534. timeForSingleExamplesGPMeanApprox/= imageNetTest.getNumPreloadedExamples();
  535. timeForSingleExamplesGPMean/= imageNetTest.getNumPreloadedExamples();
  536. timeForSingleExamplesParzen/= imageNetTest.getNumPreloadedExamples();
  537. timeForSingleExamplesSVDD/= imageNetTest.getNumPreloadedExamples();
  538. std::cerr << "GPVarApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVarApprox << std::endl;
  539. std::cerr << "GPVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVar << std::endl;
  540. std::cerr << "GPMeanApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMeanApprox << std::endl;
  541. std::cerr << "GPMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMean << std::endl;
  542. std::cerr << "Parzen -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesParzen << std::endl;
  543. std::cerr << "SVDD -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesSVDD << std::endl;
  544. // std::cerr << "Writing results to " << resultsfile << std::endl;
  545. // results.writeWEKA ( resultsfile, 1 );
  546. double perfvalueGPVarApprox = resultsGPVarApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  547. double perfvalueGPVar = resultsGPVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  548. double perfvalueGPMeanApprox = resultsGPMeanApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  549. double perfvalueGPMean = resultsGPMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  550. double perfvalueParzen = resultsParzen.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  551. double perfvalueSVDD = resultsSVDD.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  552. std::cerr << "Performance GPVarApprox: " << perfvalueGPVarApprox << std::endl;
  553. std::cerr << "Performance GPVar: " << perfvalueGPVar << std::endl;
  554. std::cerr << "Performance GPMeanApprox: " << perfvalueGPMeanApprox << std::endl;
  555. std::cerr << "Performance GPMean: " << perfvalueGPMean << std::endl;
  556. std::cerr << "Performance Parzen: " << perfvalueParzen << std::endl;
  557. std::cerr << "Performance SVDD: " << perfvalueSVDD << std::endl;
  558. OverallPerformanceGPVarApprox += perfvalueGPVar;
  559. OverallPerformanceGPVar += perfvalueGPVarApprox;
  560. OverallPerformanceGPMeanApprox += perfvalueGPMeanApprox;
  561. OverallPerformanceGPMean += perfvalueGPMean;
  562. OverallPerformanceParzen += perfvalueParzen;
  563. OverallPerformanceSVDD += perfvalueSVDD;
  564. }
  565. OverallPerformanceGPVarApprox /= nrOfClassesToConcidere;
  566. OverallPerformanceGPVar /= nrOfClassesToConcidere;
  567. OverallPerformanceGPMeanApprox /= nrOfClassesToConcidere;
  568. OverallPerformanceGPMean /= nrOfClassesToConcidere;
  569. OverallPerformanceParzen /= nrOfClassesToConcidere;
  570. OverallPerformanceSVDD /= nrOfClassesToConcidere;
  571. std::cerr << "overall performance GPVarApprox: " << OverallPerformanceGPVarApprox << std::endl;
  572. std::cerr << "overall performance GPVar: " << OverallPerformanceGPVar << std::endl;
  573. std::cerr << "overall performance GPMeanApprox: " << OverallPerformanceGPMeanApprox << std::endl;
  574. std::cerr << "overall performance GPMean: " << OverallPerformanceGPMean << std::endl;
  575. std::cerr << "overall performance Parzen: " << OverallPerformanceParzen << std::endl;
  576. std::cerr << "overall performance SVDD: " << OverallPerformanceSVDD << std::endl;
  577. return 0;
  578. }