testImageNetBinaryBruteForce.cpp 71 KB

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