testImageNetBinaryBruteForce.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 "vislearning/classifier/kernelclassifier/KCMinimumEnclosingBall.h"
  17. #include "fast-hik/tools.h"
  18. #include "fast-hik/MatFileIO.h"
  19. #include "fast-hik/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. // --------------- INPUT METHOD ----------------------
  70. void readParameters(string & filename, const int & size, NICE::Vector & parameterVector)
  71. {
  72. //we read the parameters which are given from a Matlab-Script (each line contains a single number, which is the optimal parameter for this class)
  73. parameterVector.resize(size);
  74. parameterVector.set(0.0);
  75. ifstream is(filename.c_str());
  76. if ( !is.good() )
  77. fthrow(IOException, "Unable to read parameters.");
  78. //
  79. string tmp;
  80. int cnt(0);
  81. while (! is.eof())
  82. {
  83. is >> tmp;
  84. parameterVector[cnt] = atof(tmp.c_str());
  85. cnt++;
  86. }
  87. //
  88. is.close();
  89. }
  90. //------------------- TRAINING METHODS --------------------
  91. void inline trainGPVarApprox(NICE::Vector & matrixDInv, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  92. {
  93. std::cerr << "nrOfExamplesPerClass : " << nrOfExamplesPerClass << std::endl;
  94. Timer tTrainPreciseTimer;
  95. tTrainPreciseTimer.start();
  96. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  97. {
  98. matrixDInv.resize(nrOfExamplesPerClass);
  99. matrixDInv.set(0.0);
  100. //compute D
  101. //start with adding some noise, if necessary
  102. if (noise != 0.0)
  103. matrixDInv.set(noise);
  104. else
  105. matrixDInv.set(0.0);
  106. // the approximation creates a diagonal matrix (which is easy to invert)
  107. // with entries equal the row sums of the original kernel matrix
  108. for (int i = 0; i < nrOfExamplesPerClass; i++)
  109. {
  110. for (int j = i; j < nrOfExamplesPerClass; j++)
  111. {
  112. matrixDInv[i] += kernelMatrix(i,j);
  113. if (i != j)
  114. matrixDInv[j] += kernelMatrix(i,j);
  115. }
  116. }
  117. //compute its inverse
  118. for (int i = 0; i < nrOfExamplesPerClass; i++)
  119. {
  120. matrixDInv[i] = 1.0 / matrixDInv[i];
  121. }
  122. }
  123. tTrainPreciseTimer.stop();
  124. std::cerr << "Precise time used for GPVarApprox training class " << classNumber << ": " << tTrainPreciseTimer.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  125. }
  126. void inline trainGPVar(NICE::Matrix & choleskyMatrix, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  127. {
  128. Timer tTrainPrecise;
  129. tTrainPrecise.start();
  130. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  131. {
  132. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  133. choleskyMatrix.resize(nrOfExamplesPerClass, nrOfExamplesPerClass);
  134. choleskyMatrix.set(0.0);
  135. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot k_* for new test samples
  136. cr.robustChol ( kernelMatrix, choleskyMatrix );
  137. }
  138. tTrainPrecise.stop();
  139. std::cerr << "Precise time used for GPVar training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  140. }
  141. void inline trainGPMeanApprox(NICE::Vector & GPMeanApproxRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  142. {
  143. Timer tTrainPrecise;
  144. tTrainPrecise.start();
  145. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  146. {
  147. NICE::Vector matrixDInv(nrOfExamplesPerClass,0.0);
  148. //compute D
  149. //start with adding some noise, if necessary
  150. if (noise != 0.0)
  151. matrixDInv.set(noise);
  152. else
  153. matrixDInv.set(0.0);
  154. // the approximation creates a diagonal matrix (which is easy to invert)
  155. // with entries equal the row sums of the original kernel matrix
  156. for (int i = 0; i < nrOfExamplesPerClass; i++)
  157. {
  158. for (int j = i; j < nrOfExamplesPerClass; j++)
  159. {
  160. matrixDInv[i] += kernelMatrix(i,j);
  161. if (i != j)
  162. matrixDInv[j] += kernelMatrix(i,j);
  163. }
  164. }
  165. //compute its inverse (and multiply every element with the label vector, which contains only one-entries and therefore be skipped...)
  166. GPMeanApproxRightPart.resize(nrOfExamplesPerClass);
  167. for (int i = 0; i < nrOfExamplesPerClass; i++)
  168. {
  169. GPMeanApproxRightPart[i] = 1.0 / matrixDInv[i];
  170. }
  171. }
  172. tTrainPrecise.stop();
  173. std::cerr << "Precise time used for GPMeanApprox training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  174. }
  175. void inline trainGPMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  176. {
  177. Timer tTrainPrecise;
  178. tTrainPrecise.start();
  179. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  180. {
  181. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  182. NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  183. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
  184. cr.robustChol ( kernelMatrix, choleskyMatrix );
  185. GPMeanRightPart.resize(nrOfExamplesPerClass);
  186. GPMeanRightPart.set(0.0);
  187. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  188. // pre-compute K^{-1} \cdot y, which is the same for every new test sample
  189. choleskySolveLargeScale ( choleskyMatrix, y, GPMeanRightPart );
  190. }
  191. tTrainPrecise.stop();
  192. std::cerr << "Precise time used for GPMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  193. }
  194. // GP subset of regressors
  195. void inline trainGPSRMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining, const int & nrOfRegressors, std::vector<int> & indicesOfChosenExamples )
  196. {
  197. std::vector<int> examplesToChoose;
  198. indicesOfChosenExamples.clear();
  199. //add all examples for possible choice
  200. for (int i = 0; i < nrOfExamplesPerClass; i++)
  201. {
  202. examplesToChoose.push_back(i);
  203. }
  204. //now chose randomly some examples as active subset
  205. int index;
  206. for (int i = 0; i < std::min(nrOfRegressors,nrOfExamplesPerClass); i++)
  207. {
  208. index = rand() % examplesToChoose.size();
  209. indicesOfChosenExamples.push_back(examplesToChoose[index]);
  210. examplesToChoose.erase(examplesToChoose.begin() + index);
  211. }
  212. NICE::Matrix Kmn (indicesOfChosenExamples.size(), nrOfExamplesPerClass, 0.0);
  213. int rowCnt(0);
  214. //set every row
  215. for (int i = 0; i < indicesOfChosenExamples.size(); i++, rowCnt++ )
  216. {
  217. //set every element of this row
  218. NICE::Vector col = kernelMatrix.getRow(indicesOfChosenExamples[i]);
  219. for (int j = 0; j < nrOfExamplesPerClass; j++)
  220. {
  221. Kmn(rowCnt,j) = col(j);
  222. }
  223. }
  224. //we could speed this up if we would order the indices
  225. NICE::Matrix Kmm (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  226. double tmp(0.0);
  227. for (int i = 0; i < indicesOfChosenExamples.size(); i++ )
  228. {
  229. for (int j = i; j < indicesOfChosenExamples.size(); j++ )
  230. {
  231. tmp = kernelMatrix(indicesOfChosenExamples[i], indicesOfChosenExamples[j]);
  232. Kmm(i,j) = tmp;
  233. if (i != j)
  234. Kmm(j,i) = tmp;
  235. }
  236. }
  237. Timer tTrainPrecise;
  238. tTrainPrecise.start();
  239. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  240. {
  241. NICE::Matrix innerMatrix;
  242. innerMatrix.multiply(Kmn, Kmn, true /* tranpose first matrix*/, false /* transpose second matrix*/);
  243. innerMatrix.addScaledMatrix( noise, Kmm );
  244. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  245. NICE::Vector projectedLabels;
  246. projectedLabels.multiply(Kmn,y);
  247. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  248. NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  249. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
  250. cr.robustChol ( innerMatrix, choleskyMatrix );
  251. GPMeanRightPart.resize(indicesOfChosenExamples.size());
  252. GPMeanRightPart.set(0.0);
  253. // pre-compute K^{-1} \cdot y, which is the same for every new test sample
  254. choleskySolveLargeScale ( choleskyMatrix, projectedLabels, GPMeanRightPart );
  255. }
  256. tTrainPrecise.stop();
  257. std::cerr << "Precise time used for GPSRMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  258. }
  259. // GP subset of regressors
  260. void inline trainGPSRVar(NICE::Matrix choleskyMatrix, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining, const int & nrOfRegressors, std::vector<int> & indicesOfChosenExamples )
  261. {
  262. std::vector<int> examplesToChoose;
  263. indicesOfChosenExamples.clear();
  264. //add all examples for possible choice
  265. for (int i = 0; i < nrOfExamplesPerClass; i++)
  266. {
  267. examplesToChoose.push_back(i);
  268. }
  269. //now chose randomly some examples as active subset
  270. int index;
  271. for (int i = 0; i < std::min(nrOfRegressors,nrOfExamplesPerClass); i++)
  272. {
  273. index = rand() % examplesToChoose.size();
  274. indicesOfChosenExamples.push_back(examplesToChoose[index]);
  275. examplesToChoose.erase(examplesToChoose.begin() + index);
  276. }
  277. NICE::Matrix Kmn (indicesOfChosenExamples.size(), nrOfExamplesPerClass, 0.0);
  278. int rowCnt(0);
  279. //set every row
  280. for (int i = 0; i < indicesOfChosenExamples.size(); i++, rowCnt++ )
  281. {
  282. //set every element of this row
  283. NICE::Vector col = kernelMatrix.getRow(indicesOfChosenExamples[i]);
  284. for (int j = 0; j < nrOfExamplesPerClass; j++)
  285. {
  286. Kmn(rowCnt,j) = col(j);
  287. }
  288. }
  289. //we could speed this up if we would order the indices
  290. NICE::Matrix Kmm (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  291. double tmp(0.0);
  292. for (int i = 0; i < indicesOfChosenExamples.size(); i++ )
  293. {
  294. for (int j = i; j < indicesOfChosenExamples.size(); j++ )
  295. {
  296. tmp = kernelMatrix(indicesOfChosenExamples[i], indicesOfChosenExamples[j]);
  297. Kmm(i,j) = tmp;
  298. if (i != j)
  299. Kmm(j,i) = tmp;
  300. }
  301. }
  302. Timer tTrainPrecise;
  303. tTrainPrecise.start();
  304. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  305. {
  306. NICE::Matrix innerMatrix;
  307. innerMatrix.multiply(Kmn, Kmn, true /* tranpose first matrix*/, false /* transpose second matrix*/);
  308. innerMatrix.addScaledMatrix( noise, Kmm );
  309. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  310. choleskyMatrix.resize( nrOfExamplesPerClass, nrOfExamplesPerClass );
  311. choleskyMatrix.set( 0.0 );
  312. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
  313. cr.robustChol ( innerMatrix, choleskyMatrix );
  314. }
  315. tTrainPrecise.stop();
  316. std::cerr << "Precise time used for GPSRMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  317. }
  318. KCMinimumEnclosingBall *trainSVDD( const double & noise, const NICE::Matrix kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  319. {
  320. Config conf;
  321. // set the outlier ratio (Paul optimized this paramter FIXME)
  322. conf.sD( "SVDD", "outlier_fraction", 0.1 );
  323. conf.sB( "SVDD", "verbose", false );
  324. KCMinimumEnclosingBall *svdd = new KCMinimumEnclosingBall ( &conf, NULL /* no kernel function */, "SVDD" /* config section */);
  325. KernelData kernelData ( &conf, kernelMatrix, "Kernel" , false /* update cholesky */ );
  326. Timer tTrainPrecise;
  327. tTrainPrecise.start();
  328. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  329. {
  330. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  331. // KCMinimumEnclosingBall does not store the kernel data object, therefore, we are save with passing a local copy
  332. svdd->teach ( &kernelData, y );
  333. }
  334. tTrainPrecise.stop();
  335. std::cerr << "Precise time used for SVDD training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  336. return svdd;
  337. }
  338. // ------------- EVALUATION METHODS ---------------------
  339. void inline evaluateGPVarApprox(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  340. {
  341. double uncertainty;
  342. Timer tTestSingle;
  343. tTestSingle.start();
  344. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  345. {
  346. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_* where D is our nice approximation of K
  347. NICE::Vector rightPart (kernelVector.size());
  348. for (int j = 0; j < kernelVector.size(); j++)
  349. {
  350. rightPart[j] = kernelVector[j] * matrixDInv[j];
  351. }
  352. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  353. }
  354. tTestSingle.stop();
  355. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  356. FullVector scores ( 2 );
  357. scores[0] = 0.0;
  358. scores[1] = 1.0 - uncertainty;
  359. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  360. }
  361. void inline evaluateGPVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Matrix & choleskyMatrix, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  362. {
  363. double uncertainty;
  364. Timer tTestSingle;
  365. tTestSingle.start();
  366. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  367. {
  368. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_*
  369. NICE::Vector rightPart (kernelVector.size(),0.0);
  370. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  371. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  372. }
  373. tTestSingle.stop();
  374. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  375. FullVector scores ( 2 );
  376. scores[0] = 0.0;
  377. scores[1] = 1.0 - uncertainty;
  378. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  379. }
  380. void inline evaluateGPMeanApprox(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  381. {
  382. double mean;
  383. Timer tTestSingle;
  384. tTestSingle.start();
  385. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  386. {
  387. // \mean = \k_*^T \cdot D^{-1} \cdot y where D is our nice approximation of K
  388. mean = kernelVector.scalarProduct ( rightPart );
  389. }
  390. tTestSingle.stop();
  391. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  392. FullVector scores ( 2 );
  393. scores[0] = 0.0;
  394. scores[1] = mean;
  395. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  396. }
  397. void inline evaluateGPMean(const NICE::Vector & kernelVector, const NICE::Vector & GPMeanRightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  398. {
  399. double mean;
  400. Timer tTestSingle;
  401. tTestSingle.start();
  402. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  403. {
  404. // \mean = \k_*^T \cdot K^{-1} \cdot y
  405. mean = kernelVector.scalarProduct ( GPMeanRightPart );
  406. }
  407. tTestSingle.stop();
  408. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  409. FullVector scores ( 2 );
  410. scores[0] = 0.0;
  411. scores[1] = mean;
  412. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  413. }
  414. void inline evaluateGPSRMean(const NICE::Vector & kernelVector, const NICE::Vector & GPSRMeanRightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting, const int & nrOfRegressors, const std::vector<int> & indicesOfChosenExamples)
  415. {
  416. double mean;
  417. //grep the entries corresponding to the active set
  418. NICE::Vector kernelVectorM;
  419. kernelVectorM.resize(nrOfRegressors);
  420. for (int i = 0; i < nrOfRegressors; i++)
  421. {
  422. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  423. }
  424. Timer tTestSingle;
  425. tTestSingle.start();
  426. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  427. {
  428. // \mean = \k_*^T \cdot K^{-1} \cdot y
  429. mean = kernelVectorM.scalarProduct ( GPSRMeanRightPart );
  430. }
  431. tTestSingle.stop();
  432. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  433. FullVector scores ( 2 );
  434. scores[0] = 0.0;
  435. scores[1] = mean;
  436. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  437. }
  438. void inline evaluateGPSRVar(const NICE::Vector & kernelVector, const NICE::Matrix & choleskyMatrix, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting, const int & nrOfRegressors, std::vector<int> & indicesOfChosenExamples, const double & noise)
  439. {
  440. double uncertainty;
  441. //grep the entries corresponding to the active set
  442. NICE::Vector kernelVectorM;
  443. kernelVectorM.resize(nrOfRegressors);
  444. for (int i = 0; i < nrOfRegressors; i++)
  445. {
  446. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  447. }
  448. Timer tTestSingle;
  449. tTestSingle.start();
  450. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  451. {
  452. NICE::Vector rightPart (nrOfRegressors,0.0);
  453. choleskySolveLargeScale ( choleskyMatrix, kernelVectorM, rightPart );
  454. uncertainty = noise*kernelVectorM.scalarProduct ( rightPart );
  455. }
  456. tTestSingle.stop();
  457. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  458. FullVector scores ( 2 );
  459. scores[0] = 0.0;
  460. scores[1] = 1.0 - uncertainty;
  461. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  462. }
  463. void inline evaluateParzen(const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  464. {
  465. double score;
  466. Timer tTestSingle;
  467. tTestSingle.start();
  468. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  469. {
  470. //the Parzen score is nothing but the averaged similarity to every training sample
  471. score = kernelVector.Sum() / (double) kernelVector.size(); //maybe we could directly call kernelVector.Mean() here
  472. }
  473. tTestSingle.stop();
  474. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  475. FullVector scores ( 2 );
  476. scores[0] = 0.0;
  477. scores[1] = score;
  478. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  479. }
  480. void inline evaluateSVDD( KCMinimumEnclosingBall *svdd, const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  481. {
  482. Timer tTestSingle;
  483. tTestSingle.start();
  484. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  485. {
  486. // In the following, we assume that we are using a Gaussian kernel
  487. r = svdd->classifyKernel ( kernelVector, 1.0 /* kernel self */ );
  488. }
  489. tTestSingle.stop();
  490. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  491. }
  492. /**
  493. test the basic functionality of fast-hik hyperparameter optimization
  494. */
  495. int main (int argc, char **argv)
  496. {
  497. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  498. Config conf ( argc, argv );
  499. string resultsfile = conf.gS("main", "results", "results.txt" );
  500. int nrOfExamplesPerClass = conf.gI("main", "nrOfExamplesPerClass", 50);
  501. nrOfExamplesPerClass = std::min(nrOfExamplesPerClass, 100); // we do not have more than 100 examples per class
  502. //which classes to considere? we assume consecutive class numers
  503. int indexOfFirstClass = conf.gI("main", "indexOfFirstClass", 0);
  504. indexOfFirstClass = std::max(indexOfFirstClass, 0); //we do not have less than 0 classes
  505. int indexOfLastClass = conf.gI("main", "indexOfLastClass", 999);
  506. indexOfLastClass = std::min(indexOfLastClass, 999); //we do not have more than 1000 classes
  507. int nrOfClassesToConcidere = (indexOfLastClass - indexOfLastClass)+1;
  508. //repetitions for every class to achieve reliable time evalutions
  509. int runsPerClassToAverageTraining = conf.gI( "main", "runsPerClassToAverageTraining", 1 );
  510. int runsPerClassToAverageTesting = conf.gI( "main", "runsPerClassToAverageTesting", 1 );
  511. // share parameters among methods and classes?
  512. bool shareParameters = conf.gB("main" , "shareParameters", true);
  513. bool GPMeanApprox = conf.gB( "main", "GPMeanApprox", false);
  514. bool GPVarApprox = conf.gB( "main", "GPVarApprox", false);
  515. bool GPMean = conf.gB( "main", "GPMean", false);
  516. bool GPVar = conf.gB( "main", "GPVar", false);
  517. bool GPSRMean = conf.gB( "main", "GPSRMean", false);
  518. bool GPSRVar = conf.gB( "main", "GPSRVar", false);
  519. bool Parzen = conf.gB( "main", "Parzen", false);
  520. bool SVDD = conf.gB( "main", "SVDD", false);
  521. // GP variance approximation
  522. NICE::Vector sigmaGPVarApproxParas(nrOfClassesToConcidere,0.0);
  523. NICE::Vector noiseGPVarApproxParas(nrOfClassesToConcidere,0.0);
  524. // GP variance
  525. NICE::Vector sigmaGPVarParas(nrOfClassesToConcidere,0.0);
  526. NICE::Vector noiseGPVarParas(nrOfClassesToConcidere,0.0);
  527. //GP mean approximation
  528. NICE::Vector sigmaGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  529. NICE::Vector noiseGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  530. //GP mean
  531. NICE::Vector sigmaGPMeanParas(nrOfClassesToConcidere,0.0);
  532. NICE::Vector noiseGPMeanParas(nrOfClassesToConcidere,0.0);
  533. //GP SR mean
  534. NICE::Vector sigmaGPSRMeanParas(nrOfClassesToConcidere,0.0);
  535. NICE::Vector noiseGPSRMeanParas(nrOfClassesToConcidere,0.0);
  536. //GP SR var
  537. NICE::Vector sigmaGPSRVarParas(nrOfClassesToConcidere,0.0);
  538. NICE::Vector noiseGPSRVarParas(nrOfClassesToConcidere,0.0);
  539. //Parzen
  540. NICE::Vector sigmaParzenParas(nrOfClassesToConcidere,0.0);
  541. NICE::Vector noiseParzenParas(nrOfClassesToConcidere,0.0);
  542. //SVDD
  543. NICE::Vector sigmaSVDDParas(nrOfClassesToConcidere,0.0);
  544. NICE::Vector noiseSVDDParas(nrOfClassesToConcidere,0.0);
  545. if (!shareParameters)
  546. {
  547. //read the optimal parameters for the different methods
  548. // GP variance approximation
  549. string sigmaGPVarApproxFile = conf.gS("main", "sigmaGPVarApproxFile", "approxVarSigma.txt");
  550. string noiseGPVarApproxFile = conf.gS("main", "noiseGPVarApproxFile", "approxVarNoise.txt");
  551. // GP variance
  552. string sigmaGPVarFile = conf.gS("main", "sigmaGPVarFile", "approxVarSigma.txt");
  553. string noiseGPVarFile = conf.gS("main", "noiseGPVarFile", "approxVarNoise.txt");
  554. //GP mean approximation
  555. string sigmaGPMeanApproxFile = conf.gS("main", "sigmaGPMeanApproxFile", "approxVarSigma.txt");
  556. string noiseGPMeanApproxFile = conf.gS("main", "noiseGPMeanApproxFile", "approxVarNoise.txt");
  557. //GP mean
  558. string sigmaGPMeanFile = conf.gS("main", "sigmaGPMeanFile", "approxVarSigma.txt");
  559. string noiseGPMeanFile = conf.gS("main", "noiseGPMeanFile", "approxVarNoise.txt");
  560. //Parzen
  561. string sigmaParzenFile = conf.gS("main", "sigmaParzenFile", "approxVarSigma.txt");
  562. string noiseParzenFile = conf.gS("main", "noiseParzenFile", "approxVarNoise.txt");
  563. //SVDD
  564. string sigmaSVDDFile = conf.gS("main", "sigmaSVDDFile", "approxVarSigma.txt");
  565. string noiseSVDDFile = conf.gS("main", "noiseSVDDFile", "approxVarNoise.txt");
  566. // GP variance approximation
  567. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarApproxParas);
  568. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarApproxParas);
  569. // GP variance
  570. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarParas);
  571. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarParas);
  572. //GP mean approximation
  573. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanApproxParas);
  574. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanApproxParas);
  575. //GP mean
  576. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanParas);
  577. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanParas);
  578. //GP SR mean
  579. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRMeanParas);
  580. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRMeanParas);
  581. //GP SR var
  582. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRVarParas);
  583. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRVarParas);
  584. //Parzen
  585. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaParzenParas);
  586. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseParzenParas);
  587. //SVDD
  588. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaSVDDParas);
  589. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseSVDDParas);
  590. }
  591. else
  592. {
  593. //use static variables for all methods and classis
  594. double noise = conf.gD( "main", "noise", 0.01 );
  595. double sigma = conf.gD( "main", "sigma", 1.0 );
  596. sigmaGPVarApproxParas.set(sigma);
  597. noiseGPVarApproxParas.set(noise);
  598. // GP variance
  599. sigmaGPVarParas.set(sigma);
  600. noiseGPVarParas.set(noise);
  601. //GP mean approximation
  602. sigmaGPMeanApproxParas.set(sigma);
  603. noiseGPMeanApproxParas.set(noise);
  604. //GP mean
  605. sigmaGPMeanParas.set(sigma);
  606. noiseGPMeanParas.set(noise);
  607. //GP SR mean
  608. sigmaGPSRMeanParas.set(sigma);
  609. noiseGPSRMeanParas.set(noise);
  610. //GP SR var
  611. sigmaGPSRVarParas.set(sigma);
  612. noiseGPSRVarParas.set(noise);
  613. //Parzen
  614. sigmaParzenParas.set(sigma);
  615. noiseParzenParas.set(noise);
  616. //SVDD
  617. sigmaSVDDParas.set(sigma);
  618. noiseSVDDParas.set(noise);
  619. }
  620. // -------- optimal parameters read --------------
  621. std::vector<SparseVector> trainingData;
  622. NICE::Vector y;
  623. std::cerr << "Reading ImageNet data ..." << std::endl;
  624. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  625. string imageNetPath;
  626. if (imageNetLocal)
  627. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  628. else
  629. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  630. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  631. imageNetTrain.preloadData( "train", "training" );
  632. trainingData = imageNetTrain.getPreloadedData();
  633. y = imageNetTrain.getPreloadedLabels();
  634. std::cerr << "Reading of training data finished" << std::endl;
  635. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  636. std::cerr << "y.size(): " << y.size() << std::endl;
  637. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  638. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  639. imageNetTest.preloadData ( "val", "testing" );
  640. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  641. double OverallPerformanceGPVarApprox(0.0);
  642. double OverallPerformanceGPVar(0.0);
  643. double OverallPerformanceGPMeanApprox(0.0);
  644. double OverallPerformanceGPMean(0.0);
  645. double OverallPerformanceGPSRMean(0.0);
  646. double OverallPerformanceGPSRVar(0.0);
  647. double OverallPerformanceParzen(0.0);
  648. double OverallPerformanceSVDD(0.0);
  649. double kernelSigmaGPVarApprox;
  650. double kernelSigmaGPVar;
  651. double kernelSigmaGPMeanApprox;
  652. double kernelSigmaGPMean;
  653. double kernelSigmaGPSRMean;
  654. double kernelSigmaGPSRVar;
  655. double kernelSigmaParzen;
  656. double kernelSigmaSVDD;
  657. for (int cl = indexOfFirstClass; cl <= indexOfLastClass; cl++)
  658. {
  659. std::cerr << "run for class " << cl << std::endl;
  660. int positiveClass = cl+1; //labels are from 1 to 1000, but our indices from 0 to 999
  661. // ------------------------------ TRAINING ------------------------------
  662. kernelSigmaGPVarApprox = sigmaGPVarApproxParas[cl];
  663. kernelSigmaGPVar = sigmaGPVarParas[cl];
  664. kernelSigmaGPMeanApprox = sigmaGPMeanApproxParas[cl];
  665. kernelSigmaGPMean = sigmaGPMeanParas[cl];
  666. kernelSigmaGPMean = sigmaGPSRMeanParas[cl];
  667. kernelSigmaGPSRVar = sigmaGPSRVarParas[cl];
  668. kernelSigmaParzen = sigmaParzenParas[cl];
  669. kernelSigmaSVDD = sigmaSVDDParas[cl];
  670. Timer tTrain;
  671. tTrain.start();
  672. //compute the kernel matrix, which will be shared among all methods in this scenario
  673. NICE::Matrix kernelMatrix(nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  674. //NOTE in theory we have to compute a single kernel Matrix for every method, since every method may have its own optimal parameter
  675. // I'm sure, we can speed it up a bit and compute it only for every different parameter
  676. //nonetheless, it's not as nice as we originally thought (same matrix for every method)
  677. //NOTE Nonetheless, since we're only interested in runtimes, we can ignore this
  678. //now sum up all entries of each row in the original kernel matrix
  679. double kernelScore(0.0);
  680. for (int i = cl*100; i < cl*100+nrOfExamplesPerClass; i++)
  681. {
  682. for (int j = i; j < cl*100+nrOfExamplesPerClass; j++)
  683. {
  684. kernelScore = measureDistance(trainingData[i],trainingData[j], kernelSigmaGPVarApprox);
  685. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  686. if (i != j)
  687. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  688. }
  689. }
  690. // now call the individual training methods
  691. //train GP Var Approx
  692. NICE::Vector matrixDInv;
  693. if (GPVarApprox)
  694. trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  695. //train GP Var
  696. NICE::Matrix GPVarCholesky;
  697. if (GPVar)
  698. trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  699. //train GP Mean Approx
  700. NICE::Vector GPMeanApproxRightPart;
  701. if (GPMeanApprox)
  702. trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  703. //train GP Mean
  704. NICE::Vector GPMeanRightPart;
  705. if (GPMean)
  706. trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  707. //train GP SR Mean
  708. NICE::Vector GPSRMeanRightPart;
  709. std::vector<int> indicesOfChosenExamplesGPSRMean;
  710. int nrOfRegressors = conf.gI( "GPSR", "nrOfRegressors", nrOfExamplesPerClass/2);
  711. nrOfRegressors = std::min( nrOfRegressors, nrOfExamplesPerClass );
  712. if (GPSRMean)
  713. trainGPSRMean(GPSRMeanRightPart, noiseGPSRMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  714. //train GP SR Var
  715. NICE::Matrix GPSRVarCholesky;
  716. std::vector<int> indicesOfChosenExamplesGPSRVar;
  717. if (GPSRVar)
  718. trainGPSRVar(GPSRVarCholesky, noiseGPSRVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRVar );
  719. //train Parzen
  720. //nothing to do :)
  721. //train SVDD
  722. KCMinimumEnclosingBall *svdd;
  723. if (SVDD)
  724. svdd = trainSVDD(noiseSVDDParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  725. tTrain.stop();
  726. std::cerr << "Time used for training class " << cl << ": " << tTrain.getLast() << std::endl;
  727. std::cerr << "training done - now perform the evaluation" << std::endl;
  728. // ------------------------------ TESTING ------------------------------
  729. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  730. ClassificationResults resultsGPVarApprox;
  731. ClassificationResults resultsGPVar;
  732. ClassificationResults resultsGPMeanApprox;
  733. ClassificationResults resultsGPMean;
  734. ClassificationResults resultsGPSRMean;
  735. ClassificationResults resultsGPSRVar;
  736. ClassificationResults resultsParzen;
  737. ClassificationResults resultsSVDD;
  738. ProgressBar pb;
  739. Timer tTest;
  740. tTest.start();
  741. Timer tTestSingle;
  742. double timeForSingleExamplesGPVarApprox(0.0);
  743. double timeForSingleExamplesGPVar(0.0);
  744. double timeForSingleExamplesGPMeanApprox(0.0);
  745. double timeForSingleExamplesGPMean(0.0);
  746. double timeForSingleExamplesGPSRMean(0.0);
  747. double timeForSingleExamplesGPSRVar(0.0);
  748. double timeForSingleExamplesParzen(0.0);
  749. double timeForSingleExamplesSVDD(0.0);
  750. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  751. {
  752. pb.update ( imageNetTest.getNumPreloadedExamples() );
  753. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  754. //NOTE: again we should use method-specific optimal parameters. If we're only interested in the runtimes, this doesn't matter
  755. //compute (self) similarities
  756. double kernelSelf (measureDistance(svec,svec, kernelSigmaGPVarApprox) );
  757. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  758. for (int j = 0; j < nrOfExamplesPerClass; j++)
  759. {
  760. kernelVector[j] = measureDistance(trainingData[j+cl*100],svec, kernelSigmaGPVarApprox);
  761. }
  762. //call the individual test-methods
  763. //evaluate GP Var Approx
  764. ClassificationResult rGPVarApprox;
  765. if (GPVarApprox)
  766. evaluateGPVarApprox( kernelVector, kernelSelf, matrixDInv, rGPVarApprox, timeForSingleExamplesGPVarApprox, runsPerClassToAverageTesting );
  767. //evaluate GP Var
  768. ClassificationResult rGPVar;
  769. if (GPVar)
  770. evaluateGPVar( kernelVector, kernelSelf, GPVarCholesky, rGPVar, timeForSingleExamplesGPVar, runsPerClassToAverageTesting );
  771. //evaluate GP Mean Approx
  772. ClassificationResult rGPMeanApprox;
  773. if (GPMeanApprox)
  774. evaluateGPMeanApprox( kernelVector, matrixDInv, rGPMeanApprox, timeForSingleExamplesGPMeanApprox, runsPerClassToAverageTesting );
  775. //evaluate GP Mean
  776. ClassificationResult rGPMean;
  777. if (GPMean)
  778. evaluateGPMean( kernelVector, GPMeanRightPart, rGPMean, timeForSingleExamplesGPMean, runsPerClassToAverageTesting );
  779. //evaluate GP SR Mean
  780. ClassificationResult rGPSRMean;
  781. if (GPSRMean)
  782. evaluateGPSRMean( kernelVector, GPSRMeanRightPart, rGPSRMean, timeForSingleExamplesGPSRMean, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  783. //evaluate GP SR Var
  784. ClassificationResult rGPSRVar;
  785. if (GPSRVar)
  786. evaluateGPSRVar( kernelVector, GPSRVarCholesky, rGPSRVar, timeForSingleExamplesGPSRVar, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRVar, noiseGPSRVarParas[cl] );
  787. //evaluate Parzen
  788. ClassificationResult rParzen;
  789. if (Parzen)
  790. evaluateParzen( kernelVector, rParzen, timeForSingleExamplesParzen, runsPerClassToAverageTesting );
  791. //evaluate SVDD
  792. ClassificationResult rSVDD;
  793. if (SVDD)
  794. evaluateSVDD( svdd, kernelVector, rSVDD, timeForSingleExamplesSVDD, runsPerClassToAverageTesting );
  795. // set ground truth label
  796. rGPVarApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  797. rGPVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  798. rGPMeanApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  799. rGPMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  800. rGPSRMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  801. rGPSRVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  802. rParzen.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  803. rSVDD.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  804. //remember the results for the evaluation lateron
  805. resultsGPVarApprox.push_back ( rGPVarApprox );
  806. resultsGPVar.push_back ( rGPVar );
  807. resultsGPMeanApprox.push_back ( rGPMeanApprox );
  808. resultsGPMean.push_back ( rGPMean );
  809. resultsGPSRMean.push_back ( rGPSRMean );
  810. resultsGPSRVar.push_back ( rGPSRVar );
  811. resultsParzen.push_back ( rParzen );
  812. resultsSVDD.push_back ( rSVDD );
  813. }
  814. tTest.stop();
  815. std::cerr << "Time used for evaluating class " << cl << ": " << tTest.getLast() << std::endl;
  816. timeForSingleExamplesGPVarApprox/= imageNetTest.getNumPreloadedExamples();
  817. timeForSingleExamplesGPVar/= imageNetTest.getNumPreloadedExamples();
  818. timeForSingleExamplesGPMeanApprox/= imageNetTest.getNumPreloadedExamples();
  819. timeForSingleExamplesGPMean/= imageNetTest.getNumPreloadedExamples();
  820. timeForSingleExamplesGPSRMean/= imageNetTest.getNumPreloadedExamples();
  821. timeForSingleExamplesGPSRVar/= imageNetTest.getNumPreloadedExamples();
  822. timeForSingleExamplesParzen/= imageNetTest.getNumPreloadedExamples();
  823. timeForSingleExamplesSVDD/= imageNetTest.getNumPreloadedExamples();
  824. std::cerr << "GPVarApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVarApprox << std::endl;
  825. std::cerr << "GPVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVar << std::endl;
  826. std::cerr << "GPMeanApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMeanApprox << std::endl;
  827. std::cerr << "GPMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMean << std::endl;
  828. std::cerr << "GPSRMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRMean << std::endl;
  829. std::cerr << "GPSRVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRVar << std::endl;
  830. std::cerr << "Parzen -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesParzen << std::endl;
  831. std::cerr << "SVDD -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesSVDD << std::endl;
  832. // run the AUC-evaluation
  833. double perfvalueGPVarApprox( 0.0 );
  834. double perfvalueGPVar( 0.0 );
  835. double perfvalueGPMeanApprox( 0.0 );
  836. double perfvalueGPMean( 0.0 );
  837. double perfvalueGPSRMean( 0.0 );
  838. double perfvalueGPSRVar( 0.0 );
  839. double perfvalueParzen( 0.0 );
  840. double perfvalueSVDD( 0.0 );
  841. if (GPVarApprox)
  842. perfvalueGPVarApprox = resultsGPVarApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  843. if (GPVar)
  844. perfvalueGPVar = resultsGPVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  845. if (GPMeanApprox)
  846. perfvalueGPMeanApprox = resultsGPMeanApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  847. if (GPMean)
  848. perfvalueGPMean = resultsGPMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  849. if (GPSRMean)
  850. perfvalueGPSRMean = resultsGPSRMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  851. if (GPSRVar)
  852. perfvalueGPSRVar = resultsGPSRVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  853. if (Parzen)
  854. perfvalueParzen = resultsParzen.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  855. if (SVDD)
  856. perfvalueSVDD = resultsSVDD.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  857. std::cerr << "Performance GPVarApprox: " << perfvalueGPVarApprox << std::endl;
  858. std::cerr << "Performance GPVar: " << perfvalueGPVar << std::endl;
  859. std::cerr << "Performance GPMeanApprox: " << perfvalueGPMeanApprox << std::endl;
  860. std::cerr << "Performance GPMean: " << perfvalueGPMean << std::endl;
  861. std::cerr << "Performance GPSRMean: " << perfvalueGPSRMean << std::endl;
  862. std::cerr << "Performance GPSRVar: " << perfvalueGPSRVar << std::endl;
  863. std::cerr << "Performance Parzen: " << perfvalueParzen << std::endl;
  864. std::cerr << "Performance SVDD: " << perfvalueSVDD << std::endl;
  865. OverallPerformanceGPVarApprox += perfvalueGPVar;
  866. OverallPerformanceGPVar += perfvalueGPVarApprox;
  867. OverallPerformanceGPMeanApprox += perfvalueGPMeanApprox;
  868. OverallPerformanceGPMean += perfvalueGPMean;
  869. OverallPerformanceGPSRMean += perfvalueGPSRMean;
  870. OverallPerformanceGPSRVar += perfvalueGPSRVar;
  871. OverallPerformanceParzen += perfvalueParzen;
  872. OverallPerformanceSVDD += perfvalueSVDD;
  873. // clean up memory used by SVDD
  874. delete svdd;
  875. }
  876. OverallPerformanceGPVarApprox /= nrOfClassesToConcidere;
  877. OverallPerformanceGPVar /= nrOfClassesToConcidere;
  878. OverallPerformanceGPMeanApprox /= nrOfClassesToConcidere;
  879. OverallPerformanceGPMean /= nrOfClassesToConcidere;
  880. OverallPerformanceGPSRMean /= nrOfClassesToConcidere;
  881. OverallPerformanceGPSRVar /= nrOfClassesToConcidere;
  882. OverallPerformanceParzen /= nrOfClassesToConcidere;
  883. OverallPerformanceSVDD /= nrOfClassesToConcidere;
  884. std::cerr << "overall performance GPVarApprox: " << OverallPerformanceGPVarApprox << std::endl;
  885. std::cerr << "overall performance GPVar: " << OverallPerformanceGPVar << std::endl;
  886. std::cerr << "overall performance GPMeanApprox: " << OverallPerformanceGPMeanApprox << std::endl;
  887. std::cerr << "overall performance GPMean: " << OverallPerformanceGPMean << std::endl;
  888. std::cerr << "overall performance GPSRMean: " << OverallPerformanceGPSRMean << std::endl;
  889. std::cerr << "overall performance GPSRVar: " << OverallPerformanceGPSRVar << std::endl;
  890. std::cerr << "overall performance Parzen: " << OverallPerformanceParzen << std::endl;
  891. std::cerr << "overall performance SVDD: " << OverallPerformanceSVDD << std::endl;
  892. return 0;
  893. }