testImageNetBinaryBruteForce.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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, false /* tranpose first matrix*/, true /* 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, false /* tranpose first matrix*/, true /* 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 GPSRVar 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. //which methods do we want to use?
  514. bool GPMeanApprox = conf.gB( "main", "GPMeanApprox", false);
  515. bool GPVarApprox = conf.gB( "main", "GPVarApprox", false);
  516. bool GPMean = conf.gB( "main", "GPMean", false);
  517. bool GPVar = conf.gB( "main", "GPVar", false);
  518. bool GPSRMean = conf.gB( "main", "GPSRMean", false);
  519. bool GPSRVar = conf.gB( "main", "GPSRVar", false);
  520. bool Parzen = conf.gB( "main", "Parzen", false);
  521. bool SVDD = conf.gB( "main", "SVDD", false);
  522. if (GPMeanApprox)
  523. std::cerr << "GPMeanApprox used" << std::endl;
  524. else
  525. std::cerr << "GPMeanApprox not used" << std::endl;
  526. if (GPVarApprox)
  527. std::cerr << "GPVarApprox used" << std::endl;
  528. else
  529. std::cerr << "GPVarApprox not used" << std::endl;
  530. if (GPMean)
  531. std::cerr << "GPMean used" << std::endl;
  532. else
  533. std::cerr << "GPMean not used" << std::endl;
  534. if (GPVar)
  535. std::cerr << "GPVar used" << std::endl;
  536. else
  537. std::cerr << "GPVar not used" << std::endl;
  538. if (GPSRMean)
  539. std::cerr << "GPSRMean used" << std::endl;
  540. else
  541. std::cerr << "GPSRMean not used" << std::endl;
  542. if (GPSRVar)
  543. std::cerr << "GPSRVar used" << std::endl;
  544. else
  545. std::cerr << "GPSRVar not used" << std::endl;
  546. if (Parzen)
  547. std::cerr << "Parzen used" << std::endl;
  548. else
  549. std::cerr << "Parzen not used" << std::endl;
  550. if (SVDD)
  551. std::cerr << "SVDD used" << std::endl;
  552. else
  553. std::cerr << "SVDD not used" << std::endl;
  554. // GP variance approximation
  555. NICE::Vector sigmaGPVarApproxParas(nrOfClassesToConcidere,0.0);
  556. NICE::Vector noiseGPVarApproxParas(nrOfClassesToConcidere,0.0);
  557. // GP variance
  558. NICE::Vector sigmaGPVarParas(nrOfClassesToConcidere,0.0);
  559. NICE::Vector noiseGPVarParas(nrOfClassesToConcidere,0.0);
  560. //GP mean approximation
  561. NICE::Vector sigmaGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  562. NICE::Vector noiseGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  563. //GP mean
  564. NICE::Vector sigmaGPMeanParas(nrOfClassesToConcidere,0.0);
  565. NICE::Vector noiseGPMeanParas(nrOfClassesToConcidere,0.0);
  566. //GP SR mean
  567. NICE::Vector sigmaGPSRMeanParas(nrOfClassesToConcidere,0.0);
  568. NICE::Vector noiseGPSRMeanParas(nrOfClassesToConcidere,0.0);
  569. //GP SR var
  570. NICE::Vector sigmaGPSRVarParas(nrOfClassesToConcidere,0.0);
  571. NICE::Vector noiseGPSRVarParas(nrOfClassesToConcidere,0.0);
  572. //Parzen
  573. NICE::Vector sigmaParzenParas(nrOfClassesToConcidere,0.0);
  574. NICE::Vector noiseParzenParas(nrOfClassesToConcidere,0.0);
  575. //SVDD
  576. NICE::Vector sigmaSVDDParas(nrOfClassesToConcidere,0.0);
  577. NICE::Vector noiseSVDDParas(nrOfClassesToConcidere,0.0);
  578. if (!shareParameters)
  579. {
  580. //read the optimal parameters for the different methods
  581. // GP variance approximation
  582. string sigmaGPVarApproxFile = conf.gS("main", "sigmaGPVarApproxFile", "approxVarSigma.txt");
  583. string noiseGPVarApproxFile = conf.gS("main", "noiseGPVarApproxFile", "approxVarNoise.txt");
  584. // GP variance
  585. string sigmaGPVarFile = conf.gS("main", "sigmaGPVarFile", "approxVarSigma.txt");
  586. string noiseGPVarFile = conf.gS("main", "noiseGPVarFile", "approxVarNoise.txt");
  587. //GP mean approximation
  588. string sigmaGPMeanApproxFile = conf.gS("main", "sigmaGPMeanApproxFile", "approxVarSigma.txt");
  589. string noiseGPMeanApproxFile = conf.gS("main", "noiseGPMeanApproxFile", "approxVarNoise.txt");
  590. //GP mean
  591. string sigmaGPMeanFile = conf.gS("main", "sigmaGPMeanFile", "approxVarSigma.txt");
  592. string noiseGPMeanFile = conf.gS("main", "noiseGPMeanFile", "approxVarNoise.txt");
  593. //Parzen
  594. string sigmaParzenFile = conf.gS("main", "sigmaParzenFile", "approxVarSigma.txt");
  595. string noiseParzenFile = conf.gS("main", "noiseParzenFile", "approxVarNoise.txt");
  596. //SVDD
  597. string sigmaSVDDFile = conf.gS("main", "sigmaSVDDFile", "approxVarSigma.txt");
  598. string noiseSVDDFile = conf.gS("main", "noiseSVDDFile", "approxVarNoise.txt");
  599. // GP variance approximation
  600. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarApproxParas);
  601. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarApproxParas);
  602. // GP variance
  603. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarParas);
  604. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarParas);
  605. //GP mean approximation
  606. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanApproxParas);
  607. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanApproxParas);
  608. //GP mean
  609. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanParas);
  610. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanParas);
  611. //GP SR mean
  612. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRMeanParas);
  613. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRMeanParas);
  614. //GP SR var
  615. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRVarParas);
  616. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRVarParas);
  617. //Parzen
  618. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaParzenParas);
  619. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseParzenParas);
  620. //SVDD
  621. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaSVDDParas);
  622. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseSVDDParas);
  623. }
  624. else
  625. {
  626. //use static variables for all methods and classis
  627. double noise = conf.gD( "main", "noise", 0.01 );
  628. double sigma = conf.gD( "main", "sigma", 1.0 );
  629. sigmaGPVarApproxParas.set(sigma);
  630. noiseGPVarApproxParas.set(noise);
  631. // GP variance
  632. sigmaGPVarParas.set(sigma);
  633. noiseGPVarParas.set(noise);
  634. //GP mean approximation
  635. sigmaGPMeanApproxParas.set(sigma);
  636. noiseGPMeanApproxParas.set(noise);
  637. //GP mean
  638. sigmaGPMeanParas.set(sigma);
  639. noiseGPMeanParas.set(noise);
  640. //GP SR mean
  641. sigmaGPSRMeanParas.set(sigma);
  642. noiseGPSRMeanParas.set(noise);
  643. //GP SR var
  644. sigmaGPSRVarParas.set(sigma);
  645. noiseGPSRVarParas.set(noise);
  646. //Parzen
  647. sigmaParzenParas.set(sigma);
  648. noiseParzenParas.set(noise);
  649. //SVDD
  650. sigmaSVDDParas.set(sigma);
  651. noiseSVDDParas.set(noise);
  652. }
  653. // -------- optimal parameters read --------------
  654. std::vector<SparseVector> trainingData;
  655. NICE::Vector y;
  656. std::cerr << "Reading ImageNet data ..." << std::endl;
  657. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  658. string imageNetPath;
  659. if (imageNetLocal)
  660. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  661. else
  662. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  663. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  664. imageNetTrain.preloadData( "train", "training" );
  665. trainingData = imageNetTrain.getPreloadedData();
  666. y = imageNetTrain.getPreloadedLabels();
  667. std::cerr << "Reading of training data finished" << std::endl;
  668. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  669. std::cerr << "y.size(): " << y.size() << std::endl;
  670. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  671. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  672. imageNetTest.preloadData ( "val", "testing" );
  673. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  674. double OverallPerformanceGPVarApprox(0.0);
  675. double OverallPerformanceGPVar(0.0);
  676. double OverallPerformanceGPMeanApprox(0.0);
  677. double OverallPerformanceGPMean(0.0);
  678. double OverallPerformanceGPSRMean(0.0);
  679. double OverallPerformanceGPSRVar(0.0);
  680. double OverallPerformanceParzen(0.0);
  681. double OverallPerformanceSVDD(0.0);
  682. double kernelSigmaGPVarApprox;
  683. double kernelSigmaGPVar;
  684. double kernelSigmaGPMeanApprox;
  685. double kernelSigmaGPMean;
  686. double kernelSigmaGPSRMean;
  687. double kernelSigmaGPSRVar;
  688. double kernelSigmaParzen;
  689. double kernelSigmaSVDD;
  690. for (int cl = indexOfFirstClass; cl <= indexOfLastClass; cl++)
  691. {
  692. std::cerr << "run for class " << cl << std::endl;
  693. int positiveClass = cl+1; //labels are from 1 to 1000, but our indices from 0 to 999
  694. // ------------------------------ TRAINING ------------------------------
  695. kernelSigmaGPVarApprox = sigmaGPVarApproxParas[cl];
  696. kernelSigmaGPVar = sigmaGPVarParas[cl];
  697. kernelSigmaGPMeanApprox = sigmaGPMeanApproxParas[cl];
  698. kernelSigmaGPMean = sigmaGPMeanParas[cl];
  699. kernelSigmaGPMean = sigmaGPSRMeanParas[cl];
  700. kernelSigmaGPSRVar = sigmaGPSRVarParas[cl];
  701. kernelSigmaParzen = sigmaParzenParas[cl];
  702. kernelSigmaSVDD = sigmaSVDDParas[cl];
  703. Timer tTrain;
  704. tTrain.start();
  705. //compute the kernel matrix, which will be shared among all methods in this scenario
  706. NICE::Matrix kernelMatrix(nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  707. //NOTE in theory we have to compute a single kernel Matrix for every method, since every method may have its own optimal parameter
  708. // I'm sure, we can speed it up a bit and compute it only for every different parameter
  709. //nonetheless, it's not as nice as we originally thought (same matrix for every method)
  710. //NOTE Nonetheless, since we're only interested in runtimes, we can ignore this
  711. //now sum up all entries of each row in the original kernel matrix
  712. double kernelScore(0.0);
  713. for (int i = cl*100; i < cl*100+nrOfExamplesPerClass; i++)
  714. {
  715. for (int j = i; j < cl*100+nrOfExamplesPerClass; j++)
  716. {
  717. kernelScore = measureDistance(trainingData[i],trainingData[j], kernelSigmaGPVarApprox);
  718. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  719. if (i != j)
  720. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  721. }
  722. }
  723. // now call the individual training methods
  724. //train GP Var Approx
  725. NICE::Vector matrixDInv;
  726. if (GPVarApprox)
  727. trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  728. //train GP Var
  729. NICE::Matrix GPVarCholesky;
  730. if (GPVar)
  731. trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  732. //train GP Mean Approx
  733. NICE::Vector GPMeanApproxRightPart;
  734. if (GPMeanApprox)
  735. trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  736. //train GP Mean
  737. NICE::Vector GPMeanRightPart;
  738. if (GPMean)
  739. trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  740. //train GP SR Mean
  741. NICE::Vector GPSRMeanRightPart;
  742. std::vector<int> indicesOfChosenExamplesGPSRMean;
  743. int nrOfRegressors = conf.gI( "GPSR", "nrOfRegressors", nrOfExamplesPerClass/2);
  744. nrOfRegressors = std::min( nrOfRegressors, nrOfExamplesPerClass );
  745. if (GPSRMean)
  746. trainGPSRMean(GPSRMeanRightPart, noiseGPSRMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  747. //train GP SR Var
  748. NICE::Matrix GPSRVarCholesky;
  749. std::vector<int> indicesOfChosenExamplesGPSRVar;
  750. if (GPSRVar)
  751. trainGPSRVar(GPSRVarCholesky, noiseGPSRVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRVar );
  752. //train Parzen
  753. //nothing to do :)
  754. //train SVDD
  755. KCMinimumEnclosingBall *svdd;
  756. if (SVDD)
  757. svdd = trainSVDD(noiseSVDDParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  758. tTrain.stop();
  759. std::cerr << "Time used for training class " << cl << ": " << tTrain.getLast() << std::endl;
  760. std::cerr << "training done - now perform the evaluation" << std::endl;
  761. // ------------------------------ TESTING ------------------------------
  762. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  763. ClassificationResults resultsGPVarApprox;
  764. ClassificationResults resultsGPVar;
  765. ClassificationResults resultsGPMeanApprox;
  766. ClassificationResults resultsGPMean;
  767. ClassificationResults resultsGPSRMean;
  768. ClassificationResults resultsGPSRVar;
  769. ClassificationResults resultsParzen;
  770. ClassificationResults resultsSVDD;
  771. ProgressBar pb;
  772. Timer tTest;
  773. tTest.start();
  774. Timer tTestSingle;
  775. double timeForSingleExamplesGPVarApprox(0.0);
  776. double timeForSingleExamplesGPVar(0.0);
  777. double timeForSingleExamplesGPMeanApprox(0.0);
  778. double timeForSingleExamplesGPMean(0.0);
  779. double timeForSingleExamplesGPSRMean(0.0);
  780. double timeForSingleExamplesGPSRVar(0.0);
  781. double timeForSingleExamplesParzen(0.0);
  782. double timeForSingleExamplesSVDD(0.0);
  783. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  784. {
  785. pb.update ( imageNetTest.getNumPreloadedExamples() );
  786. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  787. //NOTE: again we should use method-specific optimal parameters. If we're only interested in the runtimes, this doesn't matter
  788. //compute (self) similarities
  789. double kernelSelf (measureDistance(svec,svec, kernelSigmaGPVarApprox) );
  790. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  791. for (int j = 0; j < nrOfExamplesPerClass; j++)
  792. {
  793. kernelVector[j] = measureDistance(trainingData[j+cl*100],svec, kernelSigmaGPVarApprox);
  794. }
  795. //call the individual test-methods
  796. //evaluate GP Var Approx
  797. ClassificationResult rGPVarApprox;
  798. if (GPVarApprox)
  799. evaluateGPVarApprox( kernelVector, kernelSelf, matrixDInv, rGPVarApprox, timeForSingleExamplesGPVarApprox, runsPerClassToAverageTesting );
  800. //evaluate GP Var
  801. ClassificationResult rGPVar;
  802. if (GPVar)
  803. evaluateGPVar( kernelVector, kernelSelf, GPVarCholesky, rGPVar, timeForSingleExamplesGPVar, runsPerClassToAverageTesting );
  804. //evaluate GP Mean Approx
  805. ClassificationResult rGPMeanApprox;
  806. if (GPMeanApprox)
  807. evaluateGPMeanApprox( kernelVector, matrixDInv, rGPMeanApprox, timeForSingleExamplesGPMeanApprox, runsPerClassToAverageTesting );
  808. //evaluate GP Mean
  809. ClassificationResult rGPMean;
  810. if (GPMean)
  811. evaluateGPMean( kernelVector, GPMeanRightPart, rGPMean, timeForSingleExamplesGPMean, runsPerClassToAverageTesting );
  812. //evaluate GP SR Mean
  813. ClassificationResult rGPSRMean;
  814. if (GPSRMean)
  815. evaluateGPSRMean( kernelVector, GPSRMeanRightPart, rGPSRMean, timeForSingleExamplesGPSRMean, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  816. //evaluate GP SR Var
  817. ClassificationResult rGPSRVar;
  818. if (GPSRVar)
  819. evaluateGPSRVar( kernelVector, GPSRVarCholesky, rGPSRVar, timeForSingleExamplesGPSRVar, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRVar, noiseGPSRVarParas[cl] );
  820. //evaluate Parzen
  821. ClassificationResult rParzen;
  822. if (Parzen)
  823. evaluateParzen( kernelVector, rParzen, timeForSingleExamplesParzen, runsPerClassToAverageTesting );
  824. //evaluate SVDD
  825. ClassificationResult rSVDD;
  826. if (SVDD)
  827. evaluateSVDD( svdd, kernelVector, rSVDD, timeForSingleExamplesSVDD, runsPerClassToAverageTesting );
  828. // set ground truth label
  829. rGPVarApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  830. rGPVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  831. rGPMeanApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  832. rGPMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  833. rGPSRMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  834. rGPSRVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  835. rParzen.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  836. rSVDD.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  837. //remember the results for the evaluation lateron
  838. resultsGPVarApprox.push_back ( rGPVarApprox );
  839. resultsGPVar.push_back ( rGPVar );
  840. resultsGPMeanApprox.push_back ( rGPMeanApprox );
  841. resultsGPMean.push_back ( rGPMean );
  842. resultsGPSRMean.push_back ( rGPSRMean );
  843. resultsGPSRVar.push_back ( rGPSRVar );
  844. resultsParzen.push_back ( rParzen );
  845. resultsSVDD.push_back ( rSVDD );
  846. }
  847. tTest.stop();
  848. std::cerr << "Time used for evaluating class " << cl << ": " << tTest.getLast() << std::endl;
  849. timeForSingleExamplesGPVarApprox/= imageNetTest.getNumPreloadedExamples();
  850. timeForSingleExamplesGPVar/= imageNetTest.getNumPreloadedExamples();
  851. timeForSingleExamplesGPMeanApprox/= imageNetTest.getNumPreloadedExamples();
  852. timeForSingleExamplesGPMean/= imageNetTest.getNumPreloadedExamples();
  853. timeForSingleExamplesGPSRMean/= imageNetTest.getNumPreloadedExamples();
  854. timeForSingleExamplesGPSRVar/= imageNetTest.getNumPreloadedExamples();
  855. timeForSingleExamplesParzen/= imageNetTest.getNumPreloadedExamples();
  856. timeForSingleExamplesSVDD/= imageNetTest.getNumPreloadedExamples();
  857. std::cerr << "GPVarApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVarApprox << std::endl;
  858. std::cerr << "GPVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVar << std::endl;
  859. std::cerr << "GPMeanApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMeanApprox << std::endl;
  860. std::cerr << "GPMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMean << std::endl;
  861. std::cerr << "GPSRMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRMean << std::endl;
  862. std::cerr << "GPSRVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRVar << std::endl;
  863. std::cerr << "Parzen -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesParzen << std::endl;
  864. std::cerr << "SVDD -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesSVDD << std::endl;
  865. // run the AUC-evaluation
  866. double perfvalueGPVarApprox( 0.0 );
  867. double perfvalueGPVar( 0.0 );
  868. double perfvalueGPMeanApprox( 0.0 );
  869. double perfvalueGPMean( 0.0 );
  870. double perfvalueGPSRMean( 0.0 );
  871. double perfvalueGPSRVar( 0.0 );
  872. double perfvalueParzen( 0.0 );
  873. double perfvalueSVDD( 0.0 );
  874. if (GPVarApprox)
  875. perfvalueGPVarApprox = resultsGPVarApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  876. if (GPVar)
  877. perfvalueGPVar = resultsGPVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  878. if (GPMeanApprox)
  879. perfvalueGPMeanApprox = resultsGPMeanApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  880. if (GPMean)
  881. perfvalueGPMean = resultsGPMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  882. if (GPSRMean)
  883. perfvalueGPSRMean = resultsGPSRMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  884. if (GPSRVar)
  885. perfvalueGPSRVar = resultsGPSRVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  886. if (Parzen)
  887. perfvalueParzen = resultsParzen.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  888. if (SVDD)
  889. perfvalueSVDD = resultsSVDD.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  890. std::cerr << "Performance GPVarApprox: " << perfvalueGPVarApprox << std::endl;
  891. std::cerr << "Performance GPVar: " << perfvalueGPVar << std::endl;
  892. std::cerr << "Performance GPMeanApprox: " << perfvalueGPMeanApprox << std::endl;
  893. std::cerr << "Performance GPMean: " << perfvalueGPMean << std::endl;
  894. std::cerr << "Performance GPSRMean: " << perfvalueGPSRMean << std::endl;
  895. std::cerr << "Performance GPSRVar: " << perfvalueGPSRVar << std::endl;
  896. std::cerr << "Performance Parzen: " << perfvalueParzen << std::endl;
  897. std::cerr << "Performance SVDD: " << perfvalueSVDD << std::endl;
  898. OverallPerformanceGPVarApprox += perfvalueGPVar;
  899. OverallPerformanceGPVar += perfvalueGPVarApprox;
  900. OverallPerformanceGPMeanApprox += perfvalueGPMeanApprox;
  901. OverallPerformanceGPMean += perfvalueGPMean;
  902. OverallPerformanceGPSRMean += perfvalueGPSRMean;
  903. OverallPerformanceGPSRVar += perfvalueGPSRVar;
  904. OverallPerformanceParzen += perfvalueParzen;
  905. OverallPerformanceSVDD += perfvalueSVDD;
  906. // clean up memory used by SVDD
  907. if (SVDD)
  908. delete svdd;
  909. }
  910. OverallPerformanceGPVarApprox /= nrOfClassesToConcidere;
  911. OverallPerformanceGPVar /= nrOfClassesToConcidere;
  912. OverallPerformanceGPMeanApprox /= nrOfClassesToConcidere;
  913. OverallPerformanceGPMean /= nrOfClassesToConcidere;
  914. OverallPerformanceGPSRMean /= nrOfClassesToConcidere;
  915. OverallPerformanceGPSRVar /= nrOfClassesToConcidere;
  916. OverallPerformanceParzen /= nrOfClassesToConcidere;
  917. OverallPerformanceSVDD /= nrOfClassesToConcidere;
  918. std::cerr << "overall performance GPVarApprox: " << OverallPerformanceGPVarApprox << std::endl;
  919. std::cerr << "overall performance GPVar: " << OverallPerformanceGPVar << std::endl;
  920. std::cerr << "overall performance GPMeanApprox: " << OverallPerformanceGPMeanApprox << std::endl;
  921. std::cerr << "overall performance GPMean: " << OverallPerformanceGPMean << std::endl;
  922. std::cerr << "overall performance GPSRMean: " << OverallPerformanceGPSRMean << std::endl;
  923. std::cerr << "overall performance GPSRVar: " << OverallPerformanceGPSRVar << std::endl;
  924. std::cerr << "overall performance Parzen: " << OverallPerformanceParzen << std::endl;
  925. std::cerr << "overall performance SVDD: " << OverallPerformanceSVDD << std::endl;
  926. return 0;
  927. }