testImageNetBinaryBruteForce.cpp 72 KB

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