testImageNetBinaryBruteForce.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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/algebra/DiagonalMatrixApprox.h"
  13. #include "core/vector/Algorithms.h"
  14. #include "core/vector/SparseVectorT.h"
  15. #include "vislearning/cbaselib/ClassificationResults.h"
  16. #include "vislearning/baselib/ProgressBar.h"
  17. #include "vislearning/classifier/kernelclassifier/KCMinimumEnclosingBall.h"
  18. #include "vislearning/matlabAccess/MatFileIO.h"
  19. #include "vislearning/matlabAccess/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 trainGPMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  92. {
  93. Timer tTrainPrecise;
  94. tTrainPrecise.start();
  95. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  96. {
  97. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  98. NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  99. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
  100. cr.robustChol ( kernelMatrix, choleskyMatrix );
  101. GPMeanRightPart.resize(nrOfExamplesPerClass);
  102. GPMeanRightPart.set(0.0);
  103. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  104. // pre-compute K^{-1} \cdot y, which is the same for every new test sample
  105. choleskySolveLargeScale ( choleskyMatrix, y, GPMeanRightPart );
  106. }
  107. tTrainPrecise.stop();
  108. std::cerr << "Precise time used for GPMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  109. }
  110. void inline trainGPVar(NICE::Matrix & choleskyMatrix, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  111. {
  112. Timer tTrainPrecise;
  113. tTrainPrecise.start();
  114. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  115. {
  116. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  117. choleskyMatrix.resize(nrOfExamplesPerClass, nrOfExamplesPerClass);
  118. choleskyMatrix.set(0.0);
  119. //compute the cholesky decomposition of K in order to compute K^{-1} \cdot k_* for new test samples
  120. cr.robustChol ( kernelMatrix, choleskyMatrix );
  121. }
  122. tTrainPrecise.stop();
  123. std::cerr << "Precise time used for GPVar training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  124. }
  125. void inline trainGPMeanApprox(NICE::Vector & GPMeanApproxRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  126. {
  127. Timer tTrainPrecise;
  128. tTrainPrecise.start();
  129. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  130. {
  131. NICE::Vector matrixDInv(nrOfExamplesPerClass,0.0);
  132. //compute D
  133. //start with adding some noise, if necessary
  134. if (noise != 0.0)
  135. matrixDInv.set(noise);
  136. else
  137. matrixDInv.set(0.0);
  138. // the approximation creates a diagonal matrix (which is easy to invert)
  139. // with entries equal the row sums of the original kernel matrix
  140. for (int i = 0; i < nrOfExamplesPerClass; i++)
  141. {
  142. for (int j = i; j < nrOfExamplesPerClass; j++)
  143. {
  144. matrixDInv[i] += kernelMatrix(i,j);
  145. if (i != j)
  146. matrixDInv[j] += kernelMatrix(i,j);
  147. }
  148. }
  149. //compute its inverse (and multiply every element with the label vector, which contains only one-entries and therefore be skipped...)
  150. GPMeanApproxRightPart.resize(nrOfExamplesPerClass);
  151. for (int i = 0; i < nrOfExamplesPerClass; i++)
  152. {
  153. GPMeanApproxRightPart[i] = 1.0 / matrixDInv[i];
  154. }
  155. }
  156. tTrainPrecise.stop();
  157. std::cerr << "Precise time used for GPMeanApprox training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  158. }
  159. void inline trainGPVarApprox(NICE::Vector & matrixDInv, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  160. {
  161. std::cerr << "nrOfExamplesPerClass : " << nrOfExamplesPerClass << std::endl;
  162. Timer tTrainPreciseTimer;
  163. tTrainPreciseTimer.start();
  164. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  165. {
  166. matrixDInv.resize(nrOfExamplesPerClass);
  167. matrixDInv.set(0.0);
  168. //compute D
  169. //start with adding some noise, if necessary
  170. if (noise != 0.0)
  171. matrixDInv.set(noise);
  172. else
  173. matrixDInv.set(0.0);
  174. // the approximation creates a diagonal matrix (which is easy to invert)
  175. // with entries equal the row sums of the original kernel matrix
  176. for (int i = 0; i < nrOfExamplesPerClass; i++)
  177. {
  178. for (int j = i; j < nrOfExamplesPerClass; j++)
  179. {
  180. matrixDInv[i] += kernelMatrix(i,j);
  181. if (i != j)
  182. matrixDInv[j] += kernelMatrix(i,j);
  183. }
  184. }
  185. //compute its inverse
  186. for (int i = 0; i < nrOfExamplesPerClass; i++)
  187. {
  188. matrixDInv[i] = 1.0 / matrixDInv[i];
  189. }
  190. }
  191. tTrainPreciseTimer.stop();
  192. std::cerr << "Precise time used for GPVarApprox training class " << classNumber << ": " << tTrainPreciseTimer.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 (uint 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 (uint i = 0; i < indicesOfChosenExamples.size(); i++ )
  228. {
  229. for (uint 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 (uint 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 (uint i = 0; i < indicesOfChosenExamples.size(); i++ )
  293. {
  294. for (uint 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. // GP FITC approx
  319. void inline trainGPFITCMean(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 )
  320. {
  321. std::vector<int> examplesToChoose;
  322. indicesOfChosenExamples.clear();
  323. //add all examples for possible choice
  324. for (int i = 0; i < nrOfExamplesPerClass; i++)
  325. {
  326. examplesToChoose.push_back(i);
  327. }
  328. //now chose randomly some examples as active subset
  329. int index;
  330. for (int i = 0; i < std::min(nrOfRegressors,nrOfExamplesPerClass); i++)
  331. {
  332. index = rand() % examplesToChoose.size();
  333. indicesOfChosenExamples.push_back(examplesToChoose[index]);
  334. examplesToChoose.erase(examplesToChoose.begin() + index);
  335. }
  336. NICE::Vector diagK (nrOfExamplesPerClass, 0.0);
  337. //set every element
  338. for (int i = 0; i < nrOfExamplesPerClass; i++ )
  339. {
  340. diagK(i) = kernelMatrix(i,i);
  341. }
  342. NICE::Matrix Ku (indicesOfChosenExamples.size(), nrOfExamplesPerClass, 0.0);
  343. int rowCnt(0);
  344. //set every row
  345. for (uint i = 0; i < indicesOfChosenExamples.size(); i++, rowCnt++ )
  346. {
  347. //set every element of this row
  348. NICE::Vector col = kernelMatrix.getRow(indicesOfChosenExamples[i]);
  349. for (int j = 0; j < nrOfExamplesPerClass; j++)
  350. {
  351. Ku(rowCnt,j) = col(j);
  352. }
  353. }
  354. //we could speed this up if we would order the indices
  355. NICE::Matrix Kuu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  356. double tmp(0.0);
  357. for (uint i = 0; i < indicesOfChosenExamples.size(); i++ )
  358. {
  359. for (uint j = i; j < indicesOfChosenExamples.size(); j++ )
  360. {
  361. tmp = kernelMatrix(indicesOfChosenExamples[i], indicesOfChosenExamples[j]);
  362. Kuu(i,j) = tmp;
  363. if (i != j)
  364. Kuu(j,i) = tmp;
  365. }
  366. }
  367. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  368. Timer tTrainPrecise;
  369. tTrainPrecise.start();
  370. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  371. {
  372. // NICE::Vector projectedLabels;
  373. // projectedLabels.multiply(Kmn,y);
  374. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  375. NICE::Matrix Luu (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  376. cr.robustChol ( Kuu, Luu );
  377. NICE::Matrix V (Ku);
  378. choleskySolveMatrixLargeScale( Luu, V);
  379. NICE::Vector dg (diagK);
  380. NICE::Vector sumV (diagK.size(),0.0);
  381. for (uint i=0; i<V.cols(); i++)
  382. {
  383. for (uint j=0; j<V.rows(); j++)
  384. {
  385. sumV(i) += V(j,i)*V(j,i);
  386. }
  387. sumV(i) += noise;
  388. }
  389. dg += sumV;
  390. for (uint i=0; i<V.cols(); i++)
  391. {
  392. for (uint j=0; j<V.rows(); j++)
  393. {
  394. V(j,i) /= sqrt(dg(i));
  395. }
  396. }
  397. NICE::Matrix Lu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  398. NICE::Matrix tmpVV (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  399. tmpVV.multiply(V,V,false,true);
  400. tmpVV.addIdentity(1.0);
  401. cr.robustChol ( tmpVV, Lu );
  402. NICE::Vector r (dg);
  403. for (uint i=0; i<r.size(); i++)
  404. {
  405. r(i) = 1.0/sqrt(r(i));
  406. }
  407. NICE::Vector be (indicesOfChosenExamples.size(), 0.0);
  408. choleskySolveLargeScale (Lu, V*r, be);
  409. choleskySolveLargeScale (Lu.transpose(), be, be);
  410. GPMeanRightPart.resize(indicesOfChosenExamples.size());
  411. GPMeanRightPart.set(0.0);
  412. choleskySolveLargeScale ( Luu.transpose(), be, GPMeanRightPart );
  413. }
  414. tTrainPrecise.stop();
  415. std::cerr << "Precise time used for GPFITCMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  416. }
  417. // GP FITC approx
  418. void inline trainGPFITCVar(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 )
  419. {
  420. std::vector<int> examplesToChoose;
  421. indicesOfChosenExamples.clear();
  422. //add all examples for possible choice
  423. for (int i = 0; i < nrOfExamplesPerClass; i++)
  424. {
  425. examplesToChoose.push_back(i);
  426. }
  427. //now chose randomly some examples as active subset
  428. int index;
  429. for (int i = 0; i < std::min(nrOfRegressors,nrOfExamplesPerClass); i++)
  430. {
  431. index = rand() % examplesToChoose.size();
  432. indicesOfChosenExamples.push_back(examplesToChoose[index]);
  433. examplesToChoose.erase(examplesToChoose.begin() + index);
  434. }
  435. NICE::Vector diagK (nrOfExamplesPerClass, 0.0);
  436. //set every element
  437. for (int i = 0; i < nrOfExamplesPerClass; i++ )
  438. {
  439. diagK(i) = kernelMatrix(i,i);
  440. }
  441. NICE::Matrix Ku (indicesOfChosenExamples.size(), nrOfExamplesPerClass, 0.0);
  442. int rowCnt(0);
  443. //set every row
  444. for (uint i = 0; i < indicesOfChosenExamples.size(); i++, rowCnt++ )
  445. {
  446. //set every element of this row
  447. NICE::Vector col = kernelMatrix.getRow(indicesOfChosenExamples[i]);
  448. for (int j = 0; j < nrOfExamplesPerClass; j++)
  449. {
  450. Ku(rowCnt,j) = col(j);
  451. }
  452. }
  453. //we could speed this up if we would order the indices
  454. NICE::Matrix Kuu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  455. double tmp(0.0);
  456. for (uint i = 0; i < indicesOfChosenExamples.size(); i++ )
  457. {
  458. for (uint j = i; j < indicesOfChosenExamples.size(); j++ )
  459. {
  460. tmp = kernelMatrix(indicesOfChosenExamples[i], indicesOfChosenExamples[j]);
  461. Kuu(i,j) = tmp;
  462. if (i != j)
  463. Kuu(j,i) = tmp;
  464. }
  465. }
  466. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  467. Timer tTrainPrecise;
  468. tTrainPrecise.start();
  469. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  470. {
  471. // NICE::Vector projectedLabels;
  472. // projectedLabels.multiply(Kmn,y);
  473. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  474. NICE::Matrix Luu (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  475. cr.robustChol ( Kuu, Luu );
  476. NICE::Matrix V (Ku);
  477. choleskySolveMatrixLargeScale( Luu, V);
  478. NICE::Vector dg (diagK);
  479. NICE::Vector sumV (diagK.size(),0.0);
  480. for (uint i=0; i<V.cols(); i++)
  481. {
  482. for (uint j=0; j<V.rows(); j++)
  483. {
  484. sumV(i) += V(j,i)*V(j,i);
  485. }
  486. sumV(i) += noise;
  487. }
  488. dg += sumV;
  489. for (uint i=0; i<V.cols(); i++)
  490. {
  491. for (uint j=0; j<V.rows(); j++)
  492. {
  493. V(j,i) /= sqrt(dg(i));
  494. }
  495. }
  496. NICE::Matrix Lu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  497. NICE::Matrix tmpVV (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  498. tmpVV.multiply(V,V,false,true);
  499. tmpVV.addIdentity(1.0);
  500. cr.robustChol ( tmpVV, Lu );
  501. NICE::Matrix iKuu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  502. iKuu.addIdentity(1.0);
  503. choleskySolveMatrixLargeScale ( Luu.transpose(), iKuu );
  504. choleskySolveMatrixLargeScale ( Luu, iKuu );
  505. NICE::Matrix LuLuu (indicesOfChosenExamples.size(), indicesOfChosenExamples.size(), 0.0);
  506. LuLuu.multiply(Lu,Luu);
  507. choleskyMatrix.setIdentity();
  508. choleskySolveMatrixLargeScale ( LuLuu.transpose(), choleskyMatrix);
  509. choleskySolveMatrixLargeScale ( LuLuu, choleskyMatrix);
  510. choleskyMatrix -= iKuu;
  511. }
  512. tTrainPrecise.stop();
  513. std::cerr << "Precise time used for GPFITCVar training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  514. }
  515. void inline trainGPOptMean(NICE::Vector & rightPartGPOptMean, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  516. {
  517. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  518. // rightPartGPOptMean.resize(nrOfExamplesPerClass);
  519. NICE::Matrix kInv( nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0 );
  520. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  521. Timer tTrainPrecise;
  522. tTrainPrecise.start();
  523. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  524. {
  525. cr.robustCholInv ( kernelMatrix, kInv );
  526. //we initialize the D-Matrix with the approximation we use in other methods (row sums of kernel matrix)
  527. rightPartGPOptMean.resize(nrOfExamplesPerClass);
  528. rightPartGPOptMean.set(0.0);
  529. //compute D
  530. //start with adding some noise, if necessary
  531. if (noise != 0.0)
  532. rightPartGPOptMean.set(noise);
  533. else
  534. rightPartGPOptMean.set(0.0);
  535. // the approximation creates a diagonal matrix (which is easy to invert)
  536. // with entries equal the row sums of the original kernel matrix
  537. for (int i = 0; i < nrOfExamplesPerClass; i++)
  538. {
  539. for (int j = i; j < nrOfExamplesPerClass; j++)
  540. {
  541. rightPartGPOptMean[i] += kernelMatrix(i,j);
  542. if (i != j)
  543. rightPartGPOptMean[j] += kernelMatrix(i,j);
  544. }
  545. }
  546. //compute its inverse
  547. for (int i = 0; i < nrOfExamplesPerClass; i++)
  548. {
  549. rightPartGPOptMean[i] = 1.0 / rightPartGPOptMean[i];
  550. }
  551. // rightPartGPOptMean.set(0.0);
  552. //compute optimal diagonal matrix
  553. diagApprox.approx ( kernelMatrix, rightPartGPOptMean );
  554. }
  555. tTrainPrecise.stop();
  556. std::cerr << "Precise time used for GPOptMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  557. }
  558. void inline trainGPOptVar(NICE::Vector & DiagGPOptVar, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  559. {
  560. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  561. DiagGPOptVar.resize(nrOfExamplesPerClass);
  562. NICE::Matrix kInv( nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0 );
  563. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  564. Timer tTrainPrecise;
  565. tTrainPrecise.start();
  566. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  567. {
  568. cr.robustCholInv ( kernelMatrix, kInv );
  569. // DiagGPOptVar.set(0.0);
  570. //we initialize the D-Matrix with the approximation we use in other methods (row sums of kernel matrix)
  571. DiagGPOptVar.resize(nrOfExamplesPerClass);
  572. DiagGPOptVar.set(0.0);
  573. //compute D
  574. //start with adding some noise, if necessary
  575. if (noise != 0.0)
  576. DiagGPOptVar.set(noise);
  577. else
  578. DiagGPOptVar.set(0.0);
  579. // the approximation creates a diagonal matrix (which is easy to invert)
  580. // with entries equal the row sums of the original kernel matrix
  581. for (int i = 0; i < nrOfExamplesPerClass; i++)
  582. {
  583. for (int j = i; j < nrOfExamplesPerClass; j++)
  584. {
  585. DiagGPOptVar[i] += kernelMatrix(i,j);
  586. if (i != j)
  587. DiagGPOptVar[j] += kernelMatrix(i,j);
  588. }
  589. }
  590. //compute its inverse
  591. for (int i = 0; i < nrOfExamplesPerClass; i++)
  592. {
  593. DiagGPOptVar[i] = 1.0 / DiagGPOptVar[i];
  594. }
  595. //compute optimal diagonal matrix
  596. diagApprox.approx ( kernelMatrix, DiagGPOptVar );
  597. }
  598. tTrainPrecise.stop();
  599. std::cerr << "Precise time used for GPOptVar training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  600. }
  601. KCMinimumEnclosingBall *trainSVDD( const double & noise, const NICE::Matrix kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  602. {
  603. Config conf;
  604. // set the outlier ratio (Paul optimized this paramter FIXME)
  605. conf.sD( "SVDD", "outlier_fraction", 0.1 );
  606. conf.sB( "SVDD", "verbose", false );
  607. KCMinimumEnclosingBall *svdd = new KCMinimumEnclosingBall ( &conf, NULL /* no kernel function */, "SVDD" /* config section */);
  608. KernelData kernelData ( &conf, kernelMatrix, "Kernel" , false /* update cholesky */ );
  609. Timer tTrainPrecise;
  610. tTrainPrecise.start();
  611. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  612. {
  613. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  614. // KCMinimumEnclosingBall does not store the kernel data object, therefore, we are save with passing a local copy
  615. svdd->teach ( &kernelData, y );
  616. }
  617. tTrainPrecise.stop();
  618. std::cerr << "Precise time used for SVDD training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  619. return svdd;
  620. }
  621. // ------------- EVALUATION METHODS ---------------------
  622. void inline evaluateGPVarApprox(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  623. {
  624. double uncertainty;
  625. Timer tTestSingle;
  626. tTestSingle.start();
  627. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  628. {
  629. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_* where D is our nice approximation of K
  630. NICE::Vector rightPart (kernelVector.size());
  631. for (uint j = 0; j < kernelVector.size(); j++)
  632. {
  633. rightPart[j] = kernelVector[j] * matrixDInv[j];
  634. }
  635. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  636. }
  637. tTestSingle.stop();
  638. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  639. FullVector scores ( 2 );
  640. scores[0] = 0.0;
  641. scores[1] = 1.0 - uncertainty;
  642. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  643. }
  644. void inline evaluateGPVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Matrix & choleskyMatrix, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  645. {
  646. double uncertainty;
  647. Timer tTestSingle;
  648. tTestSingle.start();
  649. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  650. {
  651. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_*
  652. NICE::Vector rightPart (kernelVector.size(),0.0);
  653. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  654. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  655. }
  656. tTestSingle.stop();
  657. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  658. FullVector scores ( 2 );
  659. scores[0] = 0.0;
  660. scores[1] = 1.0 - uncertainty;
  661. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  662. }
  663. void inline evaluateGPMeanApprox(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  664. {
  665. double mean;
  666. Timer tTestSingle;
  667. tTestSingle.start();
  668. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  669. {
  670. // \mean = \k_*^T \cdot D^{-1} \cdot y where D is our nice approximation of K
  671. mean = kernelVector.scalarProduct ( rightPart );
  672. }
  673. tTestSingle.stop();
  674. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  675. FullVector scores ( 2 );
  676. scores[0] = 0.0;
  677. scores[1] = mean;
  678. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  679. }
  680. void inline evaluateGPMean(const NICE::Vector & kernelVector, const NICE::Vector & GPMeanRightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  681. {
  682. double mean;
  683. Timer tTestSingle;
  684. tTestSingle.start();
  685. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  686. {
  687. // \mean = \k_*^T \cdot K^{-1} \cdot y
  688. mean = kernelVector.scalarProduct ( GPMeanRightPart );
  689. }
  690. tTestSingle.stop();
  691. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  692. FullVector scores ( 2 );
  693. scores[0] = 0.0;
  694. scores[1] = mean;
  695. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  696. }
  697. 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)
  698. {
  699. double mean;
  700. //grep the entries corresponding to the active set
  701. NICE::Vector kernelVectorM;
  702. kernelVectorM.resize(nrOfRegressors);
  703. for (int i = 0; i < nrOfRegressors; i++)
  704. {
  705. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  706. }
  707. Timer tTestSingle;
  708. tTestSingle.start();
  709. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  710. {
  711. // \mean = \k_*^T \cdot K^{-1} \cdot y
  712. mean = kernelVectorM.scalarProduct ( GPSRMeanRightPart );
  713. }
  714. tTestSingle.stop();
  715. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  716. FullVector scores ( 2 );
  717. scores[0] = 0.0;
  718. scores[1] = mean;
  719. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  720. }
  721. 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)
  722. {
  723. double uncertainty;
  724. //grep the entries corresponding to the active set
  725. NICE::Vector kernelVectorM;
  726. kernelVectorM.resize(nrOfRegressors);
  727. for (int i = 0; i < nrOfRegressors; i++)
  728. {
  729. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  730. }
  731. Timer tTestSingle;
  732. tTestSingle.start();
  733. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  734. {
  735. NICE::Vector rightPart (nrOfRegressors,0.0);
  736. choleskySolveLargeScale ( choleskyMatrix, kernelVectorM, rightPart );
  737. uncertainty = noise*kernelVectorM.scalarProduct ( rightPart );
  738. }
  739. tTestSingle.stop();
  740. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  741. FullVector scores ( 2 );
  742. scores[0] = 0.0;
  743. scores[1] = 1.0 - uncertainty;
  744. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  745. }
  746. void inline evaluateGPFITCMean(const NICE::Vector & kernelVector, const NICE::Vector & GPFITCMeanRightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting, const int & nrOfRegressors, const std::vector<int> & indicesOfChosenExamples)
  747. {
  748. double mean;
  749. //grep the entries corresponding to the active set
  750. NICE::Vector kernelVectorM;
  751. kernelVectorM.resize(nrOfRegressors);
  752. for (int i = 0; i < nrOfRegressors; i++)
  753. {
  754. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  755. }
  756. Timer tTestSingle;
  757. tTestSingle.start();
  758. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  759. {
  760. // \mean = \k_*^T \cdot K^{-1} \cdot y
  761. mean = kernelVectorM.scalarProduct ( GPFITCMeanRightPart );
  762. }
  763. tTestSingle.stop();
  764. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  765. FullVector scores ( 2 );
  766. scores[0] = 0.0;
  767. scores[1] = mean;
  768. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  769. }
  770. void inline evaluateGPFITCVar(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)
  771. {
  772. double uncertainty;
  773. //grep the entries corresponding to the active set
  774. NICE::Vector kernelVectorM;
  775. kernelVectorM.resize(nrOfRegressors);
  776. for (int i = 0; i < nrOfRegressors; i++)
  777. {
  778. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  779. }
  780. Timer tTestSingle;
  781. tTestSingle.start();
  782. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  783. {
  784. NICE::Vector tmp (nrOfRegressors,0.0);
  785. tmp = choleskyMatrix*kernelVectorM;
  786. tmp *= kernelVectorM;
  787. uncertainty = 1.0 + tmp.Sum();
  788. }
  789. tTestSingle.stop();
  790. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  791. FullVector scores ( 2 );
  792. scores[0] = 0.0;
  793. scores[1] = 1.0 - uncertainty;
  794. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  795. }
  796. //this method is completely the same as evaluateGPMeanApprox, but for convenience, it is its own method
  797. void inline evaluateGPOptMean(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  798. {
  799. double mean;
  800. Timer tTestSingle;
  801. tTestSingle.start();
  802. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  803. {
  804. // \mean = \k_*^T \cdot D^{-1} \cdot y where D is our nice approximation of K
  805. mean = kernelVector.scalarProduct ( rightPart );
  806. }
  807. tTestSingle.stop();
  808. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  809. FullVector scores ( 2 );
  810. scores[0] = 0.0;
  811. scores[1] = mean;
  812. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  813. }
  814. //this method is completely the same as evaluateGPVarApprox, but for convenience, it is its own method
  815. void inline evaluateGPOptVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  816. {
  817. double uncertainty;
  818. Timer tTestSingle;
  819. tTestSingle.start();
  820. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  821. {
  822. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_* where D is our nice approximation of K
  823. NICE::Vector rightPart (kernelVector.size());
  824. for (uint j = 0; j < kernelVector.size(); j++)
  825. {
  826. rightPart[j] = kernelVector[j] * matrixDInv[j];
  827. }
  828. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  829. }
  830. tTestSingle.stop();
  831. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  832. FullVector scores ( 2 );
  833. scores[0] = 0.0;
  834. scores[1] = 1.0 - uncertainty;
  835. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  836. }
  837. void inline evaluateParzen(const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  838. {
  839. double score;
  840. Timer tTestSingle;
  841. tTestSingle.start();
  842. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  843. {
  844. //the Parzen score is nothing but the averaged similarity to every training sample
  845. score = kernelVector.Sum() / (double) kernelVector.size(); //maybe we could directly call kernelVector.Mean() here
  846. }
  847. tTestSingle.stop();
  848. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  849. FullVector scores ( 2 );
  850. scores[0] = 0.0;
  851. scores[1] = score;
  852. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  853. }
  854. void inline evaluateSVDD( KCMinimumEnclosingBall *svdd, const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  855. {
  856. Timer tTestSingle;
  857. tTestSingle.start();
  858. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  859. {
  860. // In the following, we assume that we are using a Gaussian kernel
  861. r = svdd->classifyKernel ( kernelVector, 1.0 /* kernel self */ );
  862. }
  863. tTestSingle.stop();
  864. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  865. }
  866. /**
  867. test the basic functionality of fast-hik hyperparameter optimization
  868. */
  869. int main (int argc, char **argv)
  870. {
  871. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  872. Config conf ( argc, argv );
  873. string resultsfile = conf.gS("main", "results", "results.txt" );
  874. int nrOfExamplesPerClass = conf.gI("main", "nrOfExamplesPerClass", 50);
  875. nrOfExamplesPerClass = std::min(nrOfExamplesPerClass, 100); // we do not have more than 100 examples per class
  876. //which classes to considere? we assume consecutive class numers
  877. int indexOfFirstClass = conf.gI("main", "indexOfFirstClass", 0);
  878. indexOfFirstClass = std::max(indexOfFirstClass, 0); //we do not have less than 0 classes
  879. int indexOfLastClass = conf.gI("main", "indexOfLastClass", 999);
  880. indexOfLastClass = std::min(indexOfLastClass, 999); //we do not have more than 1000 classes
  881. int nrOfClassesToConcidere = (indexOfLastClass - indexOfLastClass)+1;
  882. //repetitions for every class to achieve reliable time evalutions
  883. int runsPerClassToAverageTraining = conf.gI( "main", "runsPerClassToAverageTraining", 1 );
  884. int runsPerClassToAverageTesting = conf.gI( "main", "runsPerClassToAverageTesting", 1 );
  885. // share parameters among methods and classes?
  886. bool shareParameters = conf.gB("main" , "shareParameters", true);
  887. //which methods do we want to use?
  888. bool GPMeanApprox = conf.gB( "main", "GPMeanApprox", false);
  889. bool GPVarApprox = conf.gB( "main", "GPVarApprox", false);
  890. bool GPMean = conf.gB( "main", "GPMean", false);
  891. bool GPVar = conf.gB( "main", "GPVar", false);
  892. bool GPSRMean = conf.gB( "main", "GPSRMean", false);
  893. bool GPSRVar = conf.gB( "main", "GPSRVar", false);
  894. bool GPFITCMean = conf.gB( "main", "GPFITCMean", false);
  895. bool GPFITCVar = conf.gB( "main", "GPFITCVar", false);
  896. bool GPOptMean = conf.gB( "main", "GPOptMean", false);
  897. bool GPOptVar = conf.gB( "main", "GPOptVar", false);
  898. bool Parzen = conf.gB( "main", "Parzen", false);
  899. bool SVDD = conf.gB( "main", "SVDD", false);
  900. if (GPMeanApprox)
  901. std::cerr << "GPMeanApprox used" << std::endl;
  902. else
  903. std::cerr << "GPMeanApprox not used" << std::endl;
  904. if (GPVarApprox)
  905. std::cerr << "GPVarApprox used" << std::endl;
  906. else
  907. std::cerr << "GPVarApprox not used" << std::endl;
  908. if (GPMean)
  909. std::cerr << "GPMean used" << std::endl;
  910. else
  911. std::cerr << "GPMean not used" << std::endl;
  912. if (GPVar)
  913. std::cerr << "GPVar used" << std::endl;
  914. else
  915. std::cerr << "GPVar not used" << std::endl;
  916. if (GPSRMean)
  917. std::cerr << "GPSRMean used" << std::endl;
  918. else
  919. std::cerr << "GPSRMean not used" << std::endl;
  920. if (GPSRVar)
  921. std::cerr << "GPSRVar used" << std::endl;
  922. else
  923. std::cerr << "GPSRVar not used" << std::endl;
  924. if (GPFITCMean)
  925. std::cerr << "GPFITCMean used" << std::endl;
  926. else
  927. std::cerr << "GPFITCMean not used" << std::endl;
  928. if (GPFITCVar)
  929. std::cerr << "GPFITCVar used" << std::endl;
  930. else
  931. std::cerr << "GPFITCVar not used" << std::endl;
  932. if (GPOptMean)
  933. std::cerr << "GPOptMean used" << std::endl;
  934. else
  935. std::cerr << "GPOptMean not used" << std::endl;
  936. if (GPOptVar)
  937. std::cerr << "GPOptVar used" << std::endl;
  938. else
  939. std::cerr << "GPOptVar not used" << std::endl;
  940. if (Parzen)
  941. std::cerr << "Parzen used" << std::endl;
  942. else
  943. std::cerr << "Parzen not used" << std::endl;
  944. if (SVDD)
  945. std::cerr << "SVDD used" << std::endl;
  946. else
  947. std::cerr << "SVDD not used" << std::endl;
  948. // GP variance approximation
  949. NICE::Vector sigmaGPVarApproxParas(nrOfClassesToConcidere,0.0);
  950. NICE::Vector noiseGPVarApproxParas(nrOfClassesToConcidere,0.0);
  951. // GP variance
  952. NICE::Vector sigmaGPVarParas(nrOfClassesToConcidere,0.0);
  953. NICE::Vector noiseGPVarParas(nrOfClassesToConcidere,0.0);
  954. //GP mean approximation
  955. NICE::Vector sigmaGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  956. NICE::Vector noiseGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  957. //GP mean
  958. NICE::Vector sigmaGPMeanParas(nrOfClassesToConcidere,0.0);
  959. NICE::Vector noiseGPMeanParas(nrOfClassesToConcidere,0.0);
  960. //GP SR mean
  961. NICE::Vector sigmaGPSRMeanParas(nrOfClassesToConcidere,0.0);
  962. NICE::Vector noiseGPSRMeanParas(nrOfClassesToConcidere,0.0);
  963. //GP SR var
  964. NICE::Vector sigmaGPSRVarParas(nrOfClassesToConcidere,0.0);
  965. NICE::Vector noiseGPSRVarParas(nrOfClassesToConcidere,0.0);
  966. //GP FITC mean
  967. NICE::Vector sigmaGPFITCMeanParas(nrOfClassesToConcidere,0.0);
  968. NICE::Vector noiseGPFITCMeanParas(nrOfClassesToConcidere,0.0);
  969. //GP FITC var
  970. NICE::Vector sigmaGPFITCVarParas(nrOfClassesToConcidere,0.0);
  971. NICE::Vector noiseGPFITCVarParas(nrOfClassesToConcidere,0.0);
  972. //GP Opt mean
  973. NICE::Vector sigmaGPOptMeanParas(nrOfClassesToConcidere,0.0);
  974. NICE::Vector noiseGPOptMeanParas(nrOfClassesToConcidere,0.0);
  975. //GP Opt var
  976. NICE::Vector sigmaGPOptVarParas(nrOfClassesToConcidere,0.0);
  977. NICE::Vector noiseGPOptVarParas(nrOfClassesToConcidere,0.0);
  978. //Parzen
  979. NICE::Vector sigmaParzenParas(nrOfClassesToConcidere,0.0);
  980. NICE::Vector noiseParzenParas(nrOfClassesToConcidere,0.0);
  981. //SVDD
  982. NICE::Vector sigmaSVDDParas(nrOfClassesToConcidere,0.0);
  983. NICE::Vector noiseSVDDParas(nrOfClassesToConcidere,0.0);
  984. if (!shareParameters)
  985. {
  986. //read the optimal parameters for the different methods
  987. // GP variance approximation
  988. string sigmaGPVarApproxFile = conf.gS("main", "sigmaGPVarApproxFile", "approxVarSigma.txt");
  989. string noiseGPVarApproxFile = conf.gS("main", "noiseGPVarApproxFile", "approxVarNoise.txt");
  990. // GP variance
  991. string sigmaGPVarFile = conf.gS("main", "sigmaGPVarFile", "approxVarSigma.txt");
  992. string noiseGPVarFile = conf.gS("main", "noiseGPVarFile", "approxVarNoise.txt");
  993. //GP mean approximation
  994. string sigmaGPMeanApproxFile = conf.gS("main", "sigmaGPMeanApproxFile", "approxVarSigma.txt");
  995. string noiseGPMeanApproxFile = conf.gS("main", "noiseGPMeanApproxFile", "approxVarNoise.txt");
  996. //GP mean
  997. string sigmaGPMeanFile = conf.gS("main", "sigmaGPMeanFile", "approxVarSigma.txt");
  998. string noiseGPMeanFile = conf.gS("main", "noiseGPMeanFile", "approxVarNoise.txt");
  999. //Parzen
  1000. string sigmaParzenFile = conf.gS("main", "sigmaParzenFile", "approxVarSigma.txt");
  1001. string noiseParzenFile = conf.gS("main", "noiseParzenFile", "approxVarNoise.txt");
  1002. //SVDD
  1003. string sigmaSVDDFile = conf.gS("main", "sigmaSVDDFile", "approxVarSigma.txt");
  1004. string noiseSVDDFile = conf.gS("main", "noiseSVDDFile", "approxVarNoise.txt");
  1005. // GP variance approximation
  1006. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarApproxParas);
  1007. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarApproxParas);
  1008. // GP variance
  1009. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarParas);
  1010. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarParas);
  1011. //GP mean approximation
  1012. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanApproxParas);
  1013. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanApproxParas);
  1014. //GP mean
  1015. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanParas);
  1016. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanParas);
  1017. //GP SR mean
  1018. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRMeanParas);
  1019. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRMeanParas);
  1020. //GP SR var
  1021. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRVarParas);
  1022. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRVarParas);
  1023. //GP FITC mean
  1024. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPFITCMeanParas);
  1025. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPFITCMeanParas);
  1026. //GP FITC var
  1027. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPFITCVarParas);
  1028. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPFITCVarParas);
  1029. //GP Opt mean
  1030. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPOptMeanParas);
  1031. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPOptMeanParas);
  1032. //GP Opt var
  1033. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPOptVarParas);
  1034. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPOptVarParas);
  1035. //Parzen
  1036. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaParzenParas);
  1037. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseParzenParas);
  1038. //SVDD
  1039. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaSVDDParas);
  1040. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseSVDDParas);
  1041. }
  1042. else
  1043. {
  1044. //use static variables for all methods and classis
  1045. double noise = conf.gD( "main", "noise", 0.01 );
  1046. double sigma = conf.gD( "main", "sigma", 1.0 );
  1047. sigmaGPVarApproxParas.set(sigma);
  1048. noiseGPVarApproxParas.set(noise);
  1049. // GP variance
  1050. sigmaGPVarParas.set(sigma);
  1051. noiseGPVarParas.set(noise);
  1052. //GP mean approximation
  1053. sigmaGPMeanApproxParas.set(sigma);
  1054. noiseGPMeanApproxParas.set(noise);
  1055. //GP mean
  1056. sigmaGPMeanParas.set(sigma);
  1057. noiseGPMeanParas.set(noise);
  1058. //GP SR mean
  1059. sigmaGPSRMeanParas.set(sigma);
  1060. noiseGPSRMeanParas.set(noise);
  1061. //GP SR var
  1062. sigmaGPSRVarParas.set(sigma);
  1063. noiseGPSRVarParas.set(noise);
  1064. //GP FITC mean
  1065. sigmaGPFITCMeanParas.set(sigma);
  1066. noiseGPFITCMeanParas.set(noise);
  1067. //GP FITC var
  1068. sigmaGPFITCVarParas.set(sigma);
  1069. noiseGPFITCVarParas.set(noise);
  1070. //GP Opt mean
  1071. sigmaGPOptMeanParas.set(sigma);
  1072. noiseGPOptMeanParas.set(noise);
  1073. //GP Opt var
  1074. sigmaGPOptVarParas.set(sigma);
  1075. noiseGPOptVarParas.set(noise);
  1076. //Parzen
  1077. sigmaParzenParas.set(sigma);
  1078. noiseParzenParas.set(noise);
  1079. //SVDD
  1080. sigmaSVDDParas.set(sigma);
  1081. noiseSVDDParas.set(noise);
  1082. }
  1083. // -------- optimal parameters read --------------
  1084. std::vector<SparseVector> trainingData;
  1085. NICE::Vector y;
  1086. std::cerr << "Reading ImageNet data ..." << std::endl;
  1087. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  1088. string imageNetPath;
  1089. if (imageNetLocal)
  1090. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  1091. else
  1092. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  1093. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  1094. imageNetTrain.preloadData( "train", "training" );
  1095. trainingData = imageNetTrain.getPreloadedData();
  1096. y = imageNetTrain.getPreloadedLabels();
  1097. std::cerr << "Reading of training data finished" << std::endl;
  1098. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  1099. std::cerr << "y.size(): " << y.size() << std::endl;
  1100. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  1101. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  1102. imageNetTest.preloadData ( "val", "testing" );
  1103. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  1104. double OverallPerformanceGPVarApprox(0.0);
  1105. double OverallPerformanceGPVar(0.0);
  1106. double OverallPerformanceGPMeanApprox(0.0);
  1107. double OverallPerformanceGPMean(0.0);
  1108. double OverallPerformanceGPSRMean(0.0);
  1109. double OverallPerformanceGPSRVar(0.0);
  1110. double OverallPerformanceGPFITCMean(0.0);
  1111. double OverallPerformanceGPFITCVar(0.0);
  1112. double OverallPerformanceGPOptMean(0.0);
  1113. double OverallPerformanceGPOptVar(0.0);
  1114. double OverallPerformanceParzen(0.0);
  1115. double OverallPerformanceSVDD(0.0);
  1116. double kernelSigmaGPVarApprox;
  1117. double kernelSigmaGPVar;
  1118. double kernelSigmaGPMeanApprox;
  1119. double kernelSigmaGPMean;
  1120. double kernelSigmaGPSRMean;
  1121. double kernelSigmaGPSRVar;
  1122. double kernelSigmaGPFITCMean;
  1123. double kernelSigmaGPFITCVar;
  1124. double kernelSigmaGPOptMean;
  1125. double kernelSigmaGPOptVar;
  1126. double kernelSigmaParzen;
  1127. double kernelSigmaSVDD;
  1128. for (int cl = indexOfFirstClass; cl <= indexOfLastClass; cl++)
  1129. {
  1130. std::cerr << "run for class " << cl << std::endl;
  1131. int positiveClass = cl+1; //labels are from 1 to 1000, but our indices from 0 to 999
  1132. // ------------------------------ TRAINING ------------------------------
  1133. kernelSigmaGPVarApprox = sigmaGPVarApproxParas[cl];
  1134. kernelSigmaGPVar = sigmaGPVarParas[cl];
  1135. kernelSigmaGPMeanApprox = sigmaGPMeanApproxParas[cl];
  1136. kernelSigmaGPMean = sigmaGPMeanParas[cl];
  1137. kernelSigmaGPSRMean = sigmaGPSRMeanParas[cl];
  1138. kernelSigmaGPSRVar = sigmaGPSRVarParas[cl];
  1139. kernelSigmaGPFITCMean = sigmaGPFITCMeanParas[cl];
  1140. kernelSigmaGPFITCVar = sigmaGPFITCVarParas[cl];
  1141. kernelSigmaGPOptMean = sigmaGPOptMeanParas[cl];
  1142. kernelSigmaGPOptVar = sigmaGPOptVarParas[cl];
  1143. kernelSigmaParzen = sigmaParzenParas[cl];
  1144. kernelSigmaSVDD = sigmaSVDDParas[cl];
  1145. Timer tTrain;
  1146. tTrain.start();
  1147. //compute the kernel matrix, which will be shared among all methods in this scenario
  1148. NICE::Matrix kernelMatrix(nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  1149. //NOTE in theory we have to compute a single kernel Matrix for every method, since every method may have its own optimal parameter
  1150. // I'm sure, we can speed it up a bit and compute it only for every different parameter
  1151. //nonetheless, it's not as nice as we originally thought (same matrix for every method)
  1152. //NOTE Nonetheless, since we're only interested in runtimes, we can ignore this
  1153. //now sum up all entries of each row in the original kernel matrix
  1154. double kernelScore(0.0);
  1155. for (int i = cl*100; i < cl*100+nrOfExamplesPerClass; i++)
  1156. {
  1157. for (int j = i; j < cl*100+nrOfExamplesPerClass; j++)
  1158. {
  1159. kernelScore = measureDistance(trainingData[i],trainingData[j], kernelSigmaGPVarApprox);
  1160. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  1161. if (i != j)
  1162. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  1163. }
  1164. }
  1165. // now call the individual training methods
  1166. //train GP Mean Approx
  1167. NICE::Vector GPMeanApproxRightPart;
  1168. if (GPMeanApprox)
  1169. trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1170. //train GP Var Approx
  1171. NICE::Vector matrixDInv;
  1172. if (GPVarApprox)
  1173. trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1174. //train GP Mean
  1175. NICE::Vector GPMeanRightPart;
  1176. if (GPMean)
  1177. trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1178. //train GP Var
  1179. NICE::Matrix GPVarCholesky;
  1180. if (GPVar)
  1181. trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1182. int nrOfRegressors (0);
  1183. //train GP SR Mean
  1184. NICE::Vector GPSRMeanRightPart;
  1185. std::vector<int> indicesOfChosenExamplesGPSRMean;
  1186. nrOfRegressors = conf.gI( "GPSR", "nrOfRegressors", nrOfExamplesPerClass/2);
  1187. nrOfRegressors = std::min( nrOfRegressors, nrOfExamplesPerClass );
  1188. if (GPSRMean)
  1189. trainGPSRMean(GPSRMeanRightPart, noiseGPSRMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  1190. //train GP SR Var
  1191. NICE::Matrix GPSRVarCholesky;
  1192. std::vector<int> indicesOfChosenExamplesGPSRVar;
  1193. if (GPSRVar)
  1194. trainGPSRVar(GPSRVarCholesky, noiseGPSRVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRVar );
  1195. //train GP FITC Mean
  1196. NICE::Vector GPFITCMeanRightPart;
  1197. std::vector<int> indicesOfChosenExamplesGPFITCMean;
  1198. nrOfRegressors = conf.gI( "GPFITC", "nrOfRegressors", nrOfExamplesPerClass/5);
  1199. nrOfRegressors = std::min( nrOfRegressors, nrOfExamplesPerClass );
  1200. if (GPFITCMean)
  1201. trainGPFITCMean(GPFITCMeanRightPart, noiseGPFITCMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPFITCMean );
  1202. //train GP FITC Var
  1203. NICE::Matrix GPFITCVarCholesky;
  1204. std::vector<int> indicesOfChosenExamplesGPFITCVar;
  1205. if (GPFITCVar)
  1206. trainGPFITCVar(GPFITCVarCholesky, noiseGPFITCVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPFITCVar );
  1207. //train GP Opt Mean
  1208. NICE::Vector GPOptMeanRightPart;
  1209. if (GPOptMean)
  1210. trainGPOptMean(GPOptMeanRightPart, noiseGPOptMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1211. std::cerr << "GPOptMeanRightPart: " << std::endl; std::cerr << GPOptMeanRightPart << std::endl;
  1212. //train GP Opt Var
  1213. NICE::Vector DiagGPOptVar;
  1214. if (GPOptVar)
  1215. trainGPOptVar(DiagGPOptVar, noiseGPOptVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1216. std::cerr << "DiagGPOptVar: " << std::endl; std::cerr << DiagGPOptVar << std::endl;
  1217. //train Parzen
  1218. //nothing to do :)
  1219. //train SVDD
  1220. KCMinimumEnclosingBall *svdd;
  1221. if (SVDD)
  1222. svdd = trainSVDD(noiseSVDDParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  1223. tTrain.stop();
  1224. std::cerr << "Time used for training class " << cl << ": " << tTrain.getLast() << std::endl;
  1225. std::cerr << "training done - now perform the evaluation" << std::endl;
  1226. // ------------------------------ TESTING ------------------------------
  1227. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  1228. ClassificationResults resultsGPVarApprox;
  1229. ClassificationResults resultsGPVar;
  1230. ClassificationResults resultsGPMeanApprox;
  1231. ClassificationResults resultsGPMean;
  1232. ClassificationResults resultsGPSRMean;
  1233. ClassificationResults resultsGPSRVar;
  1234. ClassificationResults resultsGPFITCMean;
  1235. ClassificationResults resultsGPFITCVar;
  1236. ClassificationResults resultsGPOptMean;
  1237. ClassificationResults resultsGPOptVar;
  1238. ClassificationResults resultsParzen;
  1239. ClassificationResults resultsSVDD;
  1240. ProgressBar pb;
  1241. Timer tTest;
  1242. tTest.start();
  1243. Timer tTestSingle;
  1244. double timeForSingleExamplesGPVarApprox(0.0);
  1245. double timeForSingleExamplesGPVar(0.0);
  1246. double timeForSingleExamplesGPMeanApprox(0.0);
  1247. double timeForSingleExamplesGPMean(0.0);
  1248. double timeForSingleExamplesGPSRMean(0.0);
  1249. double timeForSingleExamplesGPSRVar(0.0);
  1250. double timeForSingleExamplesGPFITCMean(0.0);
  1251. double timeForSingleExamplesGPFITCVar(0.0);
  1252. double timeForSingleExamplesGPOptMean(0.0);
  1253. double timeForSingleExamplesGPOptVar(0.0);
  1254. double timeForSingleExamplesParzen(0.0);
  1255. double timeForSingleExamplesSVDD(0.0);
  1256. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  1257. {
  1258. pb.update ( imageNetTest.getNumPreloadedExamples() );
  1259. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  1260. //NOTE: again we should use method-specific optimal parameters. If we're only interested in the runtimes, this doesn't matter
  1261. //compute (self) similarities
  1262. double kernelSelf (measureDistance(svec,svec, kernelSigmaGPVarApprox) );
  1263. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  1264. for (int j = 0; j < nrOfExamplesPerClass; j++)
  1265. {
  1266. kernelVector[j] = measureDistance(trainingData[j+cl*100],svec, kernelSigmaGPVarApprox);
  1267. }
  1268. //call the individual test-methods
  1269. //evaluate GP Var Approx
  1270. ClassificationResult rGPVarApprox;
  1271. if (GPVarApprox)
  1272. evaluateGPVarApprox( kernelVector, kernelSelf, matrixDInv, rGPVarApprox, timeForSingleExamplesGPVarApprox, runsPerClassToAverageTesting );
  1273. //evaluate GP Var
  1274. ClassificationResult rGPVar;
  1275. if (GPVar)
  1276. evaluateGPVar( kernelVector, kernelSelf, GPVarCholesky, rGPVar, timeForSingleExamplesGPVar, runsPerClassToAverageTesting );
  1277. //evaluate GP Mean Approx
  1278. ClassificationResult rGPMeanApprox;
  1279. if (GPMeanApprox)
  1280. evaluateGPMeanApprox( kernelVector, matrixDInv, rGPMeanApprox, timeForSingleExamplesGPMeanApprox, runsPerClassToAverageTesting );
  1281. //evaluate GP Mean
  1282. ClassificationResult rGPMean;
  1283. if (GPMean)
  1284. evaluateGPMean( kernelVector, GPMeanRightPart, rGPMean, timeForSingleExamplesGPMean, runsPerClassToAverageTesting );
  1285. //evaluate GP SR Mean
  1286. ClassificationResult rGPSRMean;
  1287. if (GPSRMean)
  1288. evaluateGPSRMean( kernelVector, GPSRMeanRightPart, rGPSRMean, timeForSingleExamplesGPSRMean, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  1289. //evaluate GP SR Var
  1290. ClassificationResult rGPSRVar;
  1291. if (GPSRVar)
  1292. evaluateGPSRVar( kernelVector, GPSRVarCholesky, rGPSRVar, timeForSingleExamplesGPSRVar, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRVar, noiseGPSRVarParas[cl] );
  1293. //evaluate GP FITC Mean
  1294. ClassificationResult rGPFITCMean;
  1295. if (GPFITCMean)
  1296. evaluateGPFITCMean( kernelVector, GPFITCMeanRightPart, rGPFITCMean, timeForSingleExamplesGPFITCMean, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPFITCMean );
  1297. //evaluate GP FITC Var
  1298. ClassificationResult rGPFITCVar;
  1299. if (GPFITCVar)
  1300. evaluateGPFITCVar( kernelVector, GPFITCVarCholesky, rGPFITCVar, timeForSingleExamplesGPFITCVar, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPFITCVar, noiseGPFITCVarParas[cl] );
  1301. //evaluate GP Opt Mean
  1302. ClassificationResult rGPOptMean;
  1303. if (GPOptMean)
  1304. evaluateGPOptMean( kernelVector, GPOptMeanRightPart, rGPOptMean, timeForSingleExamplesGPOptMean, runsPerClassToAverageTesting );
  1305. //evaluate GP Opt Var
  1306. ClassificationResult rGPOptVar;
  1307. if (GPOptVar)
  1308. evaluateGPOptVar( kernelVector, kernelSelf, DiagGPOptVar, rGPOptVar, timeForSingleExamplesGPOptVar, runsPerClassToAverageTesting );
  1309. //evaluate Parzen
  1310. ClassificationResult rParzen;
  1311. if (Parzen)
  1312. evaluateParzen( kernelVector, rParzen, timeForSingleExamplesParzen, runsPerClassToAverageTesting );
  1313. //evaluate SVDD
  1314. ClassificationResult rSVDD;
  1315. if (SVDD)
  1316. evaluateSVDD( svdd, kernelVector, rSVDD, timeForSingleExamplesSVDD, runsPerClassToAverageTesting );
  1317. // set ground truth label
  1318. rGPVarApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1319. rGPVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1320. rGPMeanApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1321. rGPMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1322. rGPSRMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1323. rGPSRVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1324. rGPFITCMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1325. rGPFITCVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1326. rGPOptMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1327. rGPOptVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1328. rParzen.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1329. rSVDD.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1330. //remember the results for the evaluation lateron
  1331. resultsGPVarApprox.push_back ( rGPVarApprox );
  1332. resultsGPVar.push_back ( rGPVar );
  1333. resultsGPMeanApprox.push_back ( rGPMeanApprox );
  1334. resultsGPMean.push_back ( rGPMean );
  1335. resultsGPSRMean.push_back ( rGPSRMean );
  1336. resultsGPSRVar.push_back ( rGPSRVar );
  1337. resultsGPFITCMean.push_back ( rGPFITCMean );
  1338. resultsGPFITCVar.push_back ( rGPFITCVar );
  1339. resultsGPOptMean.push_back ( rGPOptMean );
  1340. resultsGPOptVar.push_back ( rGPOptVar );
  1341. resultsParzen.push_back ( rParzen );
  1342. resultsSVDD.push_back ( rSVDD );
  1343. }
  1344. tTest.stop();
  1345. std::cerr << "Time used for evaluating class " << cl << ": " << tTest.getLast() << std::endl;
  1346. timeForSingleExamplesGPVarApprox/= imageNetTest.getNumPreloadedExamples();
  1347. timeForSingleExamplesGPVar/= imageNetTest.getNumPreloadedExamples();
  1348. timeForSingleExamplesGPMeanApprox/= imageNetTest.getNumPreloadedExamples();
  1349. timeForSingleExamplesGPMean/= imageNetTest.getNumPreloadedExamples();
  1350. timeForSingleExamplesGPSRMean/= imageNetTest.getNumPreloadedExamples();
  1351. timeForSingleExamplesGPSRVar/= imageNetTest.getNumPreloadedExamples();
  1352. timeForSingleExamplesGPFITCMean/= imageNetTest.getNumPreloadedExamples();
  1353. timeForSingleExamplesGPFITCVar/= imageNetTest.getNumPreloadedExamples();
  1354. timeForSingleExamplesGPOptMean/= imageNetTest.getNumPreloadedExamples();
  1355. timeForSingleExamplesGPOptVar/= imageNetTest.getNumPreloadedExamples();
  1356. timeForSingleExamplesParzen/= imageNetTest.getNumPreloadedExamples();
  1357. timeForSingleExamplesSVDD/= imageNetTest.getNumPreloadedExamples();
  1358. std::cerr << "GPVarApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVarApprox << std::endl;
  1359. std::cerr << "GPVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVar << std::endl;
  1360. std::cerr << "GPMeanApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMeanApprox << std::endl;
  1361. std::cerr << "GPMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMean << std::endl;
  1362. std::cerr << "GPSRMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRMean << std::endl;
  1363. std::cerr << "GPSRVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRVar << std::endl;
  1364. std::cerr << "GPFITCMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPFITCMean << std::endl;
  1365. std::cerr << "GPFITCVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPFITCVar << std::endl;
  1366. std::cerr << "GPOptMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPOptMean << std::endl;
  1367. std::cerr << "GPOptVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPOptVar << std::endl;
  1368. std::cerr << "Parzen -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesParzen << std::endl;
  1369. std::cerr << "SVDD -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesSVDD << std::endl;
  1370. // run the AUC-evaluation
  1371. double perfvalueGPVarApprox( 0.0 );
  1372. double perfvalueGPVar( 0.0 );
  1373. double perfvalueGPMeanApprox( 0.0 );
  1374. double perfvalueGPMean( 0.0 );
  1375. double perfvalueGPSRMean( 0.0 );
  1376. double perfvalueGPSRVar( 0.0 );
  1377. double perfvalueGPFITCMean( 0.0 );
  1378. double perfvalueGPFITCVar( 0.0 );
  1379. double perfvalueGPOptMean( 0.0 );
  1380. double perfvalueGPOptVar( 0.0 );
  1381. double perfvalueParzen( 0.0 );
  1382. double perfvalueSVDD( 0.0 );
  1383. if (GPVarApprox)
  1384. perfvalueGPVarApprox = resultsGPVarApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1385. if (GPVar)
  1386. perfvalueGPVar = resultsGPVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1387. if (GPMeanApprox)
  1388. perfvalueGPMeanApprox = resultsGPMeanApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1389. if (GPMean)
  1390. perfvalueGPMean = resultsGPMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1391. if (GPSRMean)
  1392. perfvalueGPSRMean = resultsGPSRMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1393. if (GPSRVar)
  1394. perfvalueGPSRVar = resultsGPSRVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1395. if (GPFITCMean)
  1396. perfvalueGPFITCMean = resultsGPFITCMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1397. if (GPFITCVar)
  1398. perfvalueGPFITCVar = resultsGPFITCVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1399. if (GPOptMean)
  1400. perfvalueGPOptMean = resultsGPOptMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1401. if (GPOptVar)
  1402. perfvalueGPOptVar = resultsGPOptVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1403. if (Parzen)
  1404. perfvalueParzen = resultsParzen.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1405. if (SVDD)
  1406. perfvalueSVDD = resultsSVDD.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1407. std::cerr << "Performance GPVarApprox: " << perfvalueGPVarApprox << std::endl;
  1408. std::cerr << "Performance GPVar: " << perfvalueGPVar << std::endl;
  1409. std::cerr << "Performance GPMeanApprox: " << perfvalueGPMeanApprox << std::endl;
  1410. std::cerr << "Performance GPMean: " << perfvalueGPMean << std::endl;
  1411. std::cerr << "Performance GPSRMean: " << perfvalueGPSRMean << std::endl;
  1412. std::cerr << "Performance GPSRVar: " << perfvalueGPSRVar << std::endl;
  1413. std::cerr << "Performance GPFITCMean: " << perfvalueGPFITCMean << std::endl;
  1414. std::cerr << "Performance GPFITCVar: " << perfvalueGPFITCVar << std::endl;
  1415. std::cerr << "Performance GPOptMean: " << perfvalueGPOptMean << std::endl;
  1416. std::cerr << "Performance GPOptVar: " << perfvalueGPOptVar << std::endl;
  1417. std::cerr << "Performance Parzen: " << perfvalueParzen << std::endl;
  1418. std::cerr << "Performance SVDD: " << perfvalueSVDD << std::endl;
  1419. OverallPerformanceGPVarApprox += perfvalueGPVar;
  1420. OverallPerformanceGPVar += perfvalueGPVarApprox;
  1421. OverallPerformanceGPMeanApprox += perfvalueGPMeanApprox;
  1422. OverallPerformanceGPMean += perfvalueGPMean;
  1423. OverallPerformanceGPSRMean += perfvalueGPSRMean;
  1424. OverallPerformanceGPSRVar += perfvalueGPSRVar;
  1425. OverallPerformanceGPFITCMean += perfvalueGPFITCMean;
  1426. OverallPerformanceGPFITCVar += perfvalueGPFITCVar;
  1427. OverallPerformanceGPOptMean += perfvalueGPOptMean;
  1428. OverallPerformanceGPOptVar += perfvalueGPOptVar;
  1429. OverallPerformanceParzen += perfvalueParzen;
  1430. OverallPerformanceSVDD += perfvalueSVDD;
  1431. // clean up memory used by SVDD
  1432. if (SVDD)
  1433. delete svdd;
  1434. }
  1435. OverallPerformanceGPVarApprox /= nrOfClassesToConcidere;
  1436. OverallPerformanceGPVar /= nrOfClassesToConcidere;
  1437. OverallPerformanceGPMeanApprox /= nrOfClassesToConcidere;
  1438. OverallPerformanceGPMean /= nrOfClassesToConcidere;
  1439. OverallPerformanceGPSRMean /= nrOfClassesToConcidere;
  1440. OverallPerformanceGPSRVar /= nrOfClassesToConcidere;
  1441. OverallPerformanceGPFITCMean /= nrOfClassesToConcidere;
  1442. OverallPerformanceGPFITCVar /= nrOfClassesToConcidere;
  1443. OverallPerformanceGPOptMean /= nrOfClassesToConcidere;
  1444. OverallPerformanceGPOptVar /= nrOfClassesToConcidere;
  1445. OverallPerformanceParzen /= nrOfClassesToConcidere;
  1446. OverallPerformanceSVDD /= nrOfClassesToConcidere;
  1447. std::cerr << "overall performance GPVarApprox: " << OverallPerformanceGPVarApprox << std::endl;
  1448. std::cerr << "overall performance GPVar: " << OverallPerformanceGPVar << std::endl;
  1449. std::cerr << "overall performance GPMeanApprox: " << OverallPerformanceGPMeanApprox << std::endl;
  1450. std::cerr << "overall performance GPMean: " << OverallPerformanceGPMean << std::endl;
  1451. std::cerr << "overall performance GPSRMean: " << OverallPerformanceGPSRMean << std::endl;
  1452. std::cerr << "overall performance GPSRVar: " << OverallPerformanceGPSRVar << std::endl;
  1453. std::cerr << "overall performance GPFITCMean: " << OverallPerformanceGPFITCMean << std::endl;
  1454. std::cerr << "overall performance GPFITCVar: " << OverallPerformanceGPFITCVar << std::endl;
  1455. std::cerr << "overall performance GPOptMean: " << OverallPerformanceGPOptMean << std::endl;
  1456. std::cerr << "overall performance GPOptVar: " << OverallPerformanceGPOptVar << std::endl;
  1457. std::cerr << "overall performance Parzen: " << OverallPerformanceParzen << std::endl;
  1458. std::cerr << "overall performance SVDD: " << OverallPerformanceSVDD << std::endl;
  1459. return 0;
  1460. }