testImageNetBinaryBruteForce.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. #ifdef NICE_USELIB_MATIO
  8. #include <ctime>
  9. #include <time.h>
  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. #endif