testImageNetBinaryBruteForce.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /**
  2. * @file testImageNetBinaryBruteForce.cpp
  3. * @brief perform ImageNet tests with binary tasks for OCC using GP mean and variance, sophisticated approximations of both, Parzen Density Estimation and SVDD
  4. * @author Alexander Lütz
  5. * @date 23-05-2012 (dd-mm-yyyy)
  6. */
  7. #include <ctime>
  8. #include <time.h>
  9. #include "core/basics/Config.h"
  10. #include "core/basics/Timer.h"
  11. #include "core/algebra/CholeskyRobust.h"
  12. #include "core/algebra/DiagonalMatrixApprox.h"
  13. #include "core/vector/Algorithms.h"
  14. #include "core/vector/SparseVectorT.h"
  15. #include "vislearning/cbaselib/ClassificationResults.h"
  16. #include "vislearning/baselib/ProgressBar.h"
  17. #include "vislearning/classifier/kernelclassifier/KCMinimumEnclosingBall.h"
  18. #include "fast-hik/tools.h"
  19. #include "fast-hik/MatFileIO.h"
  20. #include "fast-hik/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 (int 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 (int i = 0; i < indicesOfChosenExamples.size(); i++ )
  229. {
  230. for (int 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 (int 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 (int i = 0; i < indicesOfChosenExamples.size(); i++ )
  294. {
  295. for (int 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. void inline trainGPOptMean(NICE::Vector & rightPartGPOptMean, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  320. {
  321. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  322. // rightPartGPOptMean.resize(nrOfExamplesPerClass);
  323. NICE::Matrix kInv( nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0 );
  324. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  325. Timer tTrainPrecise;
  326. tTrainPrecise.start();
  327. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  328. {
  329. cr.robustCholInv ( kernelMatrix, kInv );
  330. //we initialize the D-Matrix with the approximation we use in other methods (row sums of kernel matrix)
  331. rightPartGPOptMean.resize(nrOfExamplesPerClass);
  332. rightPartGPOptMean.set(0.0);
  333. //compute D
  334. //start with adding some noise, if necessary
  335. if (noise != 0.0)
  336. rightPartGPOptMean.set(noise);
  337. else
  338. rightPartGPOptMean.set(0.0);
  339. // the approximation creates a diagonal matrix (which is easy to invert)
  340. // with entries equal the row sums of the original kernel matrix
  341. for (int i = 0; i < nrOfExamplesPerClass; i++)
  342. {
  343. for (int j = i; j < nrOfExamplesPerClass; j++)
  344. {
  345. rightPartGPOptMean[i] += kernelMatrix(i,j);
  346. if (i != j)
  347. rightPartGPOptMean[j] += kernelMatrix(i,j);
  348. }
  349. }
  350. //compute its inverse
  351. for (int i = 0; i < nrOfExamplesPerClass; i++)
  352. {
  353. rightPartGPOptMean[i] = 1.0 / rightPartGPOptMean[i];
  354. }
  355. // rightPartGPOptMean.set(0.0);
  356. //compute optimal diagonal matrix
  357. diagApprox.approx ( kernelMatrix, rightPartGPOptMean );
  358. }
  359. tTrainPrecise.stop();
  360. std::cerr << "Precise time used for GPOptMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  361. }
  362. void inline trainGPOptVar(NICE::Vector & DiagGPOptVar, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  363. {
  364. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  365. DiagGPOptVar.resize(nrOfExamplesPerClass);
  366. NICE::Matrix kInv( nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0 );
  367. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  368. Timer tTrainPrecise;
  369. tTrainPrecise.start();
  370. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  371. {
  372. cr.robustCholInv ( kernelMatrix, kInv );
  373. // DiagGPOptVar.set(0.0);
  374. //we initialize the D-Matrix with the approximation we use in other methods (row sums of kernel matrix)
  375. DiagGPOptVar.resize(nrOfExamplesPerClass);
  376. DiagGPOptVar.set(0.0);
  377. //compute D
  378. //start with adding some noise, if necessary
  379. if (noise != 0.0)
  380. DiagGPOptVar.set(noise);
  381. else
  382. DiagGPOptVar.set(0.0);
  383. // the approximation creates a diagonal matrix (which is easy to invert)
  384. // with entries equal the row sums of the original kernel matrix
  385. for (int i = 0; i < nrOfExamplesPerClass; i++)
  386. {
  387. for (int j = i; j < nrOfExamplesPerClass; j++)
  388. {
  389. DiagGPOptVar[i] += kernelMatrix(i,j);
  390. if (i != j)
  391. DiagGPOptVar[j] += kernelMatrix(i,j);
  392. }
  393. }
  394. //compute its inverse
  395. for (int i = 0; i < nrOfExamplesPerClass; i++)
  396. {
  397. DiagGPOptVar[i] = 1.0 / DiagGPOptVar[i];
  398. }
  399. //compute optimal diagonal matrix
  400. diagApprox.approx ( kernelMatrix, DiagGPOptVar );
  401. }
  402. tTrainPrecise.stop();
  403. std::cerr << "Precise time used for GPOptVar training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  404. }
  405. KCMinimumEnclosingBall *trainSVDD( const double & noise, const NICE::Matrix kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
  406. {
  407. Config conf;
  408. // set the outlier ratio (Paul optimized this paramter FIXME)
  409. conf.sD( "SVDD", "outlier_fraction", 0.1 );
  410. conf.sB( "SVDD", "verbose", false );
  411. KCMinimumEnclosingBall *svdd = new KCMinimumEnclosingBall ( &conf, NULL /* no kernel function */, "SVDD" /* config section */);
  412. KernelData kernelData ( &conf, kernelMatrix, "Kernel" , false /* update cholesky */ );
  413. Timer tTrainPrecise;
  414. tTrainPrecise.start();
  415. for (int run = 0; run < runsPerClassToAverageTraining; run++)
  416. {
  417. NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
  418. // KCMinimumEnclosingBall does not store the kernel data object, therefore, we are save with passing a local copy
  419. svdd->teach ( &kernelData, y );
  420. }
  421. tTrainPrecise.stop();
  422. std::cerr << "Precise time used for SVDD training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
  423. return svdd;
  424. }
  425. // ------------- EVALUATION METHODS ---------------------
  426. void inline evaluateGPVarApprox(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  427. {
  428. double uncertainty;
  429. Timer tTestSingle;
  430. tTestSingle.start();
  431. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  432. {
  433. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_* where D is our nice approximation of K
  434. NICE::Vector rightPart (kernelVector.size());
  435. for (int j = 0; j < kernelVector.size(); j++)
  436. {
  437. rightPart[j] = kernelVector[j] * matrixDInv[j];
  438. }
  439. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  440. }
  441. tTestSingle.stop();
  442. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  443. FullVector scores ( 2 );
  444. scores[0] = 0.0;
  445. scores[1] = 1.0 - uncertainty;
  446. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  447. }
  448. void inline evaluateGPVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Matrix & choleskyMatrix, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  449. {
  450. double uncertainty;
  451. Timer tTestSingle;
  452. tTestSingle.start();
  453. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  454. {
  455. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_*
  456. NICE::Vector rightPart (kernelVector.size(),0.0);
  457. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  458. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  459. }
  460. tTestSingle.stop();
  461. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  462. FullVector scores ( 2 );
  463. scores[0] = 0.0;
  464. scores[1] = 1.0 - uncertainty;
  465. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  466. }
  467. void inline evaluateGPMeanApprox(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  468. {
  469. double mean;
  470. Timer tTestSingle;
  471. tTestSingle.start();
  472. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  473. {
  474. // \mean = \k_*^T \cdot D^{-1} \cdot y where D is our nice approximation of K
  475. mean = kernelVector.scalarProduct ( rightPart );
  476. }
  477. tTestSingle.stop();
  478. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  479. FullVector scores ( 2 );
  480. scores[0] = 0.0;
  481. scores[1] = mean;
  482. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  483. }
  484. void inline evaluateGPMean(const NICE::Vector & kernelVector, const NICE::Vector & GPMeanRightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  485. {
  486. double mean;
  487. Timer tTestSingle;
  488. tTestSingle.start();
  489. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  490. {
  491. // \mean = \k_*^T \cdot K^{-1} \cdot y
  492. mean = kernelVector.scalarProduct ( GPMeanRightPart );
  493. }
  494. tTestSingle.stop();
  495. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  496. FullVector scores ( 2 );
  497. scores[0] = 0.0;
  498. scores[1] = mean;
  499. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  500. }
  501. 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)
  502. {
  503. double mean;
  504. //grep the entries corresponding to the active set
  505. NICE::Vector kernelVectorM;
  506. kernelVectorM.resize(nrOfRegressors);
  507. for (int i = 0; i < nrOfRegressors; i++)
  508. {
  509. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  510. }
  511. Timer tTestSingle;
  512. tTestSingle.start();
  513. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  514. {
  515. // \mean = \k_*^T \cdot K^{-1} \cdot y
  516. mean = kernelVectorM.scalarProduct ( GPSRMeanRightPart );
  517. }
  518. tTestSingle.stop();
  519. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  520. FullVector scores ( 2 );
  521. scores[0] = 0.0;
  522. scores[1] = mean;
  523. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  524. }
  525. 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)
  526. {
  527. double uncertainty;
  528. //grep the entries corresponding to the active set
  529. NICE::Vector kernelVectorM;
  530. kernelVectorM.resize(nrOfRegressors);
  531. for (int i = 0; i < nrOfRegressors; i++)
  532. {
  533. kernelVectorM[i] = kernelVector[indicesOfChosenExamples[i]];
  534. }
  535. Timer tTestSingle;
  536. tTestSingle.start();
  537. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  538. {
  539. NICE::Vector rightPart (nrOfRegressors,0.0);
  540. choleskySolveLargeScale ( choleskyMatrix, kernelVectorM, rightPart );
  541. uncertainty = noise*kernelVectorM.scalarProduct ( rightPart );
  542. }
  543. tTestSingle.stop();
  544. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  545. FullVector scores ( 2 );
  546. scores[0] = 0.0;
  547. scores[1] = 1.0 - uncertainty;
  548. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  549. }
  550. //this method is completely the same as evaluateGPMeanApprox, but for convenience, it is its own method
  551. void inline evaluateGPOptMean(const NICE::Vector & kernelVector, const NICE::Vector & rightPart, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  552. {
  553. double mean;
  554. Timer tTestSingle;
  555. tTestSingle.start();
  556. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  557. {
  558. // \mean = \k_*^T \cdot D^{-1} \cdot y where D is our nice approximation of K
  559. mean = kernelVector.scalarProduct ( rightPart );
  560. }
  561. tTestSingle.stop();
  562. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  563. FullVector scores ( 2 );
  564. scores[0] = 0.0;
  565. scores[1] = mean;
  566. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  567. }
  568. //this method is completely the same as evaluateGPVarApprox, but for convenience, it is its own method
  569. void inline evaluateGPOptVar(const NICE::Vector & kernelVector, const double & kernelSelf, const NICE::Vector & matrixDInv, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  570. {
  571. double uncertainty;
  572. Timer tTestSingle;
  573. tTestSingle.start();
  574. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  575. {
  576. // uncertainty = k{**} - \k_*^T \cdot D^{-1} \cdot k_* where D is our nice approximation of K
  577. NICE::Vector rightPart (kernelVector.size());
  578. for (int j = 0; j < kernelVector.size(); j++)
  579. {
  580. rightPart[j] = kernelVector[j] * matrixDInv[j];
  581. }
  582. uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  583. }
  584. tTestSingle.stop();
  585. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  586. FullVector scores ( 2 );
  587. scores[0] = 0.0;
  588. scores[1] = 1.0 - uncertainty;
  589. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  590. }
  591. void inline evaluateParzen(const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  592. {
  593. double score;
  594. Timer tTestSingle;
  595. tTestSingle.start();
  596. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  597. {
  598. //the Parzen score is nothing but the averaged similarity to every training sample
  599. score = kernelVector.Sum() / (double) kernelVector.size(); //maybe we could directly call kernelVector.Mean() here
  600. }
  601. tTestSingle.stop();
  602. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  603. FullVector scores ( 2 );
  604. scores[0] = 0.0;
  605. scores[1] = score;
  606. r = ClassificationResult ( scores[1]<0.5 ? 0 : 1, scores );
  607. }
  608. void inline evaluateSVDD( KCMinimumEnclosingBall *svdd, const NICE::Vector & kernelVector, ClassificationResult & r, double & timeForSingleExamples, const int & runsPerClassToAverageTesting)
  609. {
  610. Timer tTestSingle;
  611. tTestSingle.start();
  612. for (int run = 0; run < runsPerClassToAverageTesting; run++)
  613. {
  614. // In the following, we assume that we are using a Gaussian kernel
  615. r = svdd->classifyKernel ( kernelVector, 1.0 /* kernel self */ );
  616. }
  617. tTestSingle.stop();
  618. timeForSingleExamples += tTestSingle.getLast()/(double)runsPerClassToAverageTesting;
  619. }
  620. /**
  621. test the basic functionality of fast-hik hyperparameter optimization
  622. */
  623. int main (int argc, char **argv)
  624. {
  625. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  626. Config conf ( argc, argv );
  627. string resultsfile = conf.gS("main", "results", "results.txt" );
  628. int nrOfExamplesPerClass = conf.gI("main", "nrOfExamplesPerClass", 50);
  629. nrOfExamplesPerClass = std::min(nrOfExamplesPerClass, 100); // we do not have more than 100 examples per class
  630. //which classes to considere? we assume consecutive class numers
  631. int indexOfFirstClass = conf.gI("main", "indexOfFirstClass", 0);
  632. indexOfFirstClass = std::max(indexOfFirstClass, 0); //we do not have less than 0 classes
  633. int indexOfLastClass = conf.gI("main", "indexOfLastClass", 999);
  634. indexOfLastClass = std::min(indexOfLastClass, 999); //we do not have more than 1000 classes
  635. int nrOfClassesToConcidere = (indexOfLastClass - indexOfLastClass)+1;
  636. //repetitions for every class to achieve reliable time evalutions
  637. int runsPerClassToAverageTraining = conf.gI( "main", "runsPerClassToAverageTraining", 1 );
  638. int runsPerClassToAverageTesting = conf.gI( "main", "runsPerClassToAverageTesting", 1 );
  639. // share parameters among methods and classes?
  640. bool shareParameters = conf.gB("main" , "shareParameters", true);
  641. //which methods do we want to use?
  642. bool GPMeanApprox = conf.gB( "main", "GPMeanApprox", false);
  643. bool GPVarApprox = conf.gB( "main", "GPVarApprox", false);
  644. bool GPMean = conf.gB( "main", "GPMean", false);
  645. bool GPVar = conf.gB( "main", "GPVar", false);
  646. bool GPSRMean = conf.gB( "main", "GPSRMean", false);
  647. bool GPSRVar = conf.gB( "main", "GPSRVar", false);
  648. bool GPOptMean = conf.gB( "main", "GPOptMean", false);
  649. bool GPOptVar = conf.gB( "main", "GPOptVar", false);
  650. bool Parzen = conf.gB( "main", "Parzen", false);
  651. bool SVDD = conf.gB( "main", "SVDD", false);
  652. if (GPMeanApprox)
  653. std::cerr << "GPMeanApprox used" << std::endl;
  654. else
  655. std::cerr << "GPMeanApprox not used" << std::endl;
  656. if (GPVarApprox)
  657. std::cerr << "GPVarApprox used" << std::endl;
  658. else
  659. std::cerr << "GPVarApprox not used" << std::endl;
  660. if (GPMean)
  661. std::cerr << "GPMean used" << std::endl;
  662. else
  663. std::cerr << "GPMean not used" << std::endl;
  664. if (GPVar)
  665. std::cerr << "GPVar used" << std::endl;
  666. else
  667. std::cerr << "GPVar not used" << std::endl;
  668. if (GPSRMean)
  669. std::cerr << "GPSRMean used" << std::endl;
  670. else
  671. std::cerr << "GPSRMean not used" << std::endl;
  672. if (GPSRVar)
  673. std::cerr << "GPSRVar used" << std::endl;
  674. else
  675. std::cerr << "GPSRVar not used" << std::endl;
  676. if (GPOptMean)
  677. std::cerr << "GPOptMean used" << std::endl;
  678. else
  679. std::cerr << "GPOptMean not used" << std::endl;
  680. if (GPOptVar)
  681. std::cerr << "GPOptVar used" << std::endl;
  682. else
  683. std::cerr << "GPOptVar not used" << std::endl;
  684. if (Parzen)
  685. std::cerr << "Parzen used" << std::endl;
  686. else
  687. std::cerr << "Parzen not used" << std::endl;
  688. if (SVDD)
  689. std::cerr << "SVDD used" << std::endl;
  690. else
  691. std::cerr << "SVDD not used" << std::endl;
  692. // GP variance approximation
  693. NICE::Vector sigmaGPVarApproxParas(nrOfClassesToConcidere,0.0);
  694. NICE::Vector noiseGPVarApproxParas(nrOfClassesToConcidere,0.0);
  695. // GP variance
  696. NICE::Vector sigmaGPVarParas(nrOfClassesToConcidere,0.0);
  697. NICE::Vector noiseGPVarParas(nrOfClassesToConcidere,0.0);
  698. //GP mean approximation
  699. NICE::Vector sigmaGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  700. NICE::Vector noiseGPMeanApproxParas(nrOfClassesToConcidere,0.0);
  701. //GP mean
  702. NICE::Vector sigmaGPMeanParas(nrOfClassesToConcidere,0.0);
  703. NICE::Vector noiseGPMeanParas(nrOfClassesToConcidere,0.0);
  704. //GP SR mean
  705. NICE::Vector sigmaGPSRMeanParas(nrOfClassesToConcidere,0.0);
  706. NICE::Vector noiseGPSRMeanParas(nrOfClassesToConcidere,0.0);
  707. //GP SR var
  708. NICE::Vector sigmaGPSRVarParas(nrOfClassesToConcidere,0.0);
  709. NICE::Vector noiseGPSRVarParas(nrOfClassesToConcidere,0.0);
  710. //GP Opt mean
  711. NICE::Vector sigmaGPOptMeanParas(nrOfClassesToConcidere,0.0);
  712. NICE::Vector noiseGPOptMeanParas(nrOfClassesToConcidere,0.0);
  713. //GP Opt var
  714. NICE::Vector sigmaGPOptVarParas(nrOfClassesToConcidere,0.0);
  715. NICE::Vector noiseGPOptVarParas(nrOfClassesToConcidere,0.0);
  716. //Parzen
  717. NICE::Vector sigmaParzenParas(nrOfClassesToConcidere,0.0);
  718. NICE::Vector noiseParzenParas(nrOfClassesToConcidere,0.0);
  719. //SVDD
  720. NICE::Vector sigmaSVDDParas(nrOfClassesToConcidere,0.0);
  721. NICE::Vector noiseSVDDParas(nrOfClassesToConcidere,0.0);
  722. if (!shareParameters)
  723. {
  724. //read the optimal parameters for the different methods
  725. // GP variance approximation
  726. string sigmaGPVarApproxFile = conf.gS("main", "sigmaGPVarApproxFile", "approxVarSigma.txt");
  727. string noiseGPVarApproxFile = conf.gS("main", "noiseGPVarApproxFile", "approxVarNoise.txt");
  728. // GP variance
  729. string sigmaGPVarFile = conf.gS("main", "sigmaGPVarFile", "approxVarSigma.txt");
  730. string noiseGPVarFile = conf.gS("main", "noiseGPVarFile", "approxVarNoise.txt");
  731. //GP mean approximation
  732. string sigmaGPMeanApproxFile = conf.gS("main", "sigmaGPMeanApproxFile", "approxVarSigma.txt");
  733. string noiseGPMeanApproxFile = conf.gS("main", "noiseGPMeanApproxFile", "approxVarNoise.txt");
  734. //GP mean
  735. string sigmaGPMeanFile = conf.gS("main", "sigmaGPMeanFile", "approxVarSigma.txt");
  736. string noiseGPMeanFile = conf.gS("main", "noiseGPMeanFile", "approxVarNoise.txt");
  737. //Parzen
  738. string sigmaParzenFile = conf.gS("main", "sigmaParzenFile", "approxVarSigma.txt");
  739. string noiseParzenFile = conf.gS("main", "noiseParzenFile", "approxVarNoise.txt");
  740. //SVDD
  741. string sigmaSVDDFile = conf.gS("main", "sigmaSVDDFile", "approxVarSigma.txt");
  742. string noiseSVDDFile = conf.gS("main", "noiseSVDDFile", "approxVarNoise.txt");
  743. // GP variance approximation
  744. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarApproxParas);
  745. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarApproxParas);
  746. // GP variance
  747. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPVarParas);
  748. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPVarParas);
  749. //GP mean approximation
  750. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanApproxParas);
  751. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanApproxParas);
  752. //GP mean
  753. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPMeanParas);
  754. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPMeanParas);
  755. //GP SR mean
  756. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRMeanParas);
  757. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRMeanParas);
  758. //GP SR var
  759. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPSRVarParas);
  760. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPSRVarParas);
  761. //GP Opt mean
  762. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPOptMeanParas);
  763. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPOptMeanParas);
  764. //GP Opt var
  765. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaGPOptVarParas);
  766. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseGPOptVarParas);
  767. //Parzen
  768. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaParzenParas);
  769. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseParzenParas);
  770. //SVDD
  771. readParameters(sigmaGPVarApproxFile,nrOfClassesToConcidere, sigmaSVDDParas);
  772. readParameters(noiseGPVarApproxFile,nrOfClassesToConcidere, noiseSVDDParas);
  773. }
  774. else
  775. {
  776. //use static variables for all methods and classis
  777. double noise = conf.gD( "main", "noise", 0.01 );
  778. double sigma = conf.gD( "main", "sigma", 1.0 );
  779. sigmaGPVarApproxParas.set(sigma);
  780. noiseGPVarApproxParas.set(noise);
  781. // GP variance
  782. sigmaGPVarParas.set(sigma);
  783. noiseGPVarParas.set(noise);
  784. //GP mean approximation
  785. sigmaGPMeanApproxParas.set(sigma);
  786. noiseGPMeanApproxParas.set(noise);
  787. //GP mean
  788. sigmaGPMeanParas.set(sigma);
  789. noiseGPMeanParas.set(noise);
  790. //GP SR mean
  791. sigmaGPSRMeanParas.set(sigma);
  792. noiseGPSRMeanParas.set(noise);
  793. //GP SR var
  794. sigmaGPSRVarParas.set(sigma);
  795. noiseGPSRVarParas.set(noise);
  796. //GP Opt mean
  797. sigmaGPOptMeanParas.set(sigma);
  798. noiseGPOptMeanParas.set(noise);
  799. //GP Opt var
  800. sigmaGPOptVarParas.set(sigma);
  801. noiseGPOptVarParas.set(noise);
  802. //Parzen
  803. sigmaParzenParas.set(sigma);
  804. noiseParzenParas.set(noise);
  805. //SVDD
  806. sigmaSVDDParas.set(sigma);
  807. noiseSVDDParas.set(noise);
  808. }
  809. // -------- optimal parameters read --------------
  810. std::vector<SparseVector> trainingData;
  811. NICE::Vector y;
  812. std::cerr << "Reading ImageNet data ..." << std::endl;
  813. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  814. string imageNetPath;
  815. if (imageNetLocal)
  816. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  817. else
  818. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  819. ImageNetData imageNetTrain ( imageNetPath + "demo/" );
  820. imageNetTrain.preloadData( "train", "training" );
  821. trainingData = imageNetTrain.getPreloadedData();
  822. y = imageNetTrain.getPreloadedLabels();
  823. std::cerr << "Reading of training data finished" << std::endl;
  824. std::cerr << "trainingData.size(): " << trainingData.size() << std::endl;
  825. std::cerr << "y.size(): " << y.size() << std::endl;
  826. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  827. ImageNetData imageNetTest ( imageNetPath + "demo/" );
  828. imageNetTest.preloadData ( "val", "testing" );
  829. imageNetTest.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  830. double OverallPerformanceGPVarApprox(0.0);
  831. double OverallPerformanceGPVar(0.0);
  832. double OverallPerformanceGPMeanApprox(0.0);
  833. double OverallPerformanceGPMean(0.0);
  834. double OverallPerformanceGPSRMean(0.0);
  835. double OverallPerformanceGPSRVar(0.0);
  836. double OverallPerformanceGPOptMean(0.0);
  837. double OverallPerformanceGPOptVar(0.0);
  838. double OverallPerformanceParzen(0.0);
  839. double OverallPerformanceSVDD(0.0);
  840. double kernelSigmaGPVarApprox;
  841. double kernelSigmaGPVar;
  842. double kernelSigmaGPMeanApprox;
  843. double kernelSigmaGPMean;
  844. double kernelSigmaGPSRMean;
  845. double kernelSigmaGPSRVar;
  846. double kernelSigmaGPOptMean;
  847. double kernelSigmaGPOptVar;
  848. double kernelSigmaParzen;
  849. double kernelSigmaSVDD;
  850. for (int cl = indexOfFirstClass; cl <= indexOfLastClass; cl++)
  851. {
  852. std::cerr << "run for class " << cl << std::endl;
  853. int positiveClass = cl+1; //labels are from 1 to 1000, but our indices from 0 to 999
  854. // ------------------------------ TRAINING ------------------------------
  855. kernelSigmaGPVarApprox = sigmaGPVarApproxParas[cl];
  856. kernelSigmaGPVar = sigmaGPVarParas[cl];
  857. kernelSigmaGPMeanApprox = sigmaGPMeanApproxParas[cl];
  858. kernelSigmaGPMean = sigmaGPMeanParas[cl];
  859. kernelSigmaGPSRMean = sigmaGPSRMeanParas[cl];
  860. kernelSigmaGPSRVar = sigmaGPSRVarParas[cl];
  861. kernelSigmaGPOptMean = sigmaGPOptMeanParas[cl];
  862. kernelSigmaGPOptVar = sigmaGPOptVarParas[cl];
  863. kernelSigmaParzen = sigmaParzenParas[cl];
  864. kernelSigmaSVDD = sigmaSVDDParas[cl];
  865. Timer tTrain;
  866. tTrain.start();
  867. //compute the kernel matrix, which will be shared among all methods in this scenario
  868. NICE::Matrix kernelMatrix(nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
  869. //NOTE in theory we have to compute a single kernel Matrix for every method, since every method may have its own optimal parameter
  870. // I'm sure, we can speed it up a bit and compute it only for every different parameter
  871. //nonetheless, it's not as nice as we originally thought (same matrix for every method)
  872. //NOTE Nonetheless, since we're only interested in runtimes, we can ignore this
  873. //now sum up all entries of each row in the original kernel matrix
  874. double kernelScore(0.0);
  875. for (int i = cl*100; i < cl*100+nrOfExamplesPerClass; i++)
  876. {
  877. for (int j = i; j < cl*100+nrOfExamplesPerClass; j++)
  878. {
  879. kernelScore = measureDistance(trainingData[i],trainingData[j], kernelSigmaGPVarApprox);
  880. kernelMatrix(i-cl*100,j-cl*100) = kernelScore;
  881. if (i != j)
  882. kernelMatrix(j-cl*100,i-cl*100) = kernelScore;
  883. }
  884. }
  885. // now call the individual training methods
  886. //train GP Mean Approx
  887. NICE::Vector GPMeanApproxRightPart;
  888. if (GPMeanApprox)
  889. trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  890. //train GP Var Approx
  891. NICE::Vector matrixDInv;
  892. if (GPVarApprox)
  893. trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  894. //train GP Mean
  895. NICE::Vector GPMeanRightPart;
  896. if (GPMean)
  897. trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  898. //train GP Var
  899. NICE::Matrix GPVarCholesky;
  900. if (GPVar)
  901. trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  902. //train GP SR Mean
  903. NICE::Vector GPSRMeanRightPart;
  904. std::vector<int> indicesOfChosenExamplesGPSRMean;
  905. int nrOfRegressors = conf.gI( "GPSR", "nrOfRegressors", nrOfExamplesPerClass/2);
  906. nrOfRegressors = std::min( nrOfRegressors, nrOfExamplesPerClass );
  907. if (GPSRMean)
  908. trainGPSRMean(GPSRMeanRightPart, noiseGPSRMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  909. //train GP SR Var
  910. NICE::Matrix GPSRVarCholesky;
  911. std::vector<int> indicesOfChosenExamplesGPSRVar;
  912. if (GPSRVar)
  913. trainGPSRVar(GPSRVarCholesky, noiseGPSRVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining, nrOfRegressors, indicesOfChosenExamplesGPSRVar );
  914. //train GP Opt Mean
  915. NICE::Vector GPOptMeanRightPart;
  916. if (GPOptMean)
  917. trainGPOptMean(GPOptMeanRightPart, noiseGPOptMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  918. std::cerr << "GPOptMeanRightPart: " << std::endl; std::cerr << GPOptMeanRightPart << std::endl;
  919. //train GP Opt Var
  920. NICE::Vector DiagGPOptVar;
  921. if (GPOptVar)
  922. trainGPOptVar(DiagGPOptVar, noiseGPOptVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  923. std::cerr << "DiagGPOptVar: " << std::endl; std::cerr << DiagGPOptVar << std::endl;
  924. //train Parzen
  925. //nothing to do :)
  926. //train SVDD
  927. KCMinimumEnclosingBall *svdd;
  928. if (SVDD)
  929. svdd = trainSVDD(noiseSVDDParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
  930. tTrain.stop();
  931. std::cerr << "Time used for training class " << cl << ": " << tTrain.getLast() << std::endl;
  932. std::cerr << "training done - now perform the evaluation" << std::endl;
  933. // ------------------------------ TESTING ------------------------------
  934. std::cerr << "Classification step ... with " << imageNetTest.getNumPreloadedExamples() << " examples" << std::endl;
  935. ClassificationResults resultsGPVarApprox;
  936. ClassificationResults resultsGPVar;
  937. ClassificationResults resultsGPMeanApprox;
  938. ClassificationResults resultsGPMean;
  939. ClassificationResults resultsGPSRMean;
  940. ClassificationResults resultsGPSRVar;
  941. ClassificationResults resultsGPOptMean;
  942. ClassificationResults resultsGPOptVar;
  943. ClassificationResults resultsParzen;
  944. ClassificationResults resultsSVDD;
  945. ProgressBar pb;
  946. Timer tTest;
  947. tTest.start();
  948. Timer tTestSingle;
  949. double timeForSingleExamplesGPVarApprox(0.0);
  950. double timeForSingleExamplesGPVar(0.0);
  951. double timeForSingleExamplesGPMeanApprox(0.0);
  952. double timeForSingleExamplesGPMean(0.0);
  953. double timeForSingleExamplesGPSRMean(0.0);
  954. double timeForSingleExamplesGPSRVar(0.0);
  955. double timeForSingleExamplesGPOptMean(0.0);
  956. double timeForSingleExamplesGPOptVar(0.0);
  957. double timeForSingleExamplesParzen(0.0);
  958. double timeForSingleExamplesSVDD(0.0);
  959. for ( uint i = 0 ; i < (uint)imageNetTest.getNumPreloadedExamples(); i++ )
  960. {
  961. pb.update ( imageNetTest.getNumPreloadedExamples() );
  962. const SparseVector & svec = imageNetTest.getPreloadedExample ( i );
  963. //NOTE: again we should use method-specific optimal parameters. If we're only interested in the runtimes, this doesn't matter
  964. //compute (self) similarities
  965. double kernelSelf (measureDistance(svec,svec, kernelSigmaGPVarApprox) );
  966. NICE::Vector kernelVector (nrOfExamplesPerClass, 0.0);
  967. for (int j = 0; j < nrOfExamplesPerClass; j++)
  968. {
  969. kernelVector[j] = measureDistance(trainingData[j+cl*100],svec, kernelSigmaGPVarApprox);
  970. }
  971. //call the individual test-methods
  972. //evaluate GP Var Approx
  973. ClassificationResult rGPVarApprox;
  974. if (GPVarApprox)
  975. evaluateGPVarApprox( kernelVector, kernelSelf, matrixDInv, rGPVarApprox, timeForSingleExamplesGPVarApprox, runsPerClassToAverageTesting );
  976. //evaluate GP Var
  977. ClassificationResult rGPVar;
  978. if (GPVar)
  979. evaluateGPVar( kernelVector, kernelSelf, GPVarCholesky, rGPVar, timeForSingleExamplesGPVar, runsPerClassToAverageTesting );
  980. //evaluate GP Mean Approx
  981. ClassificationResult rGPMeanApprox;
  982. if (GPMeanApprox)
  983. evaluateGPMeanApprox( kernelVector, matrixDInv, rGPMeanApprox, timeForSingleExamplesGPMeanApprox, runsPerClassToAverageTesting );
  984. //evaluate GP Mean
  985. ClassificationResult rGPMean;
  986. if (GPMean)
  987. evaluateGPMean( kernelVector, GPMeanRightPart, rGPMean, timeForSingleExamplesGPMean, runsPerClassToAverageTesting );
  988. //evaluate GP SR Mean
  989. ClassificationResult rGPSRMean;
  990. if (GPSRMean)
  991. evaluateGPSRMean( kernelVector, GPSRMeanRightPart, rGPSRMean, timeForSingleExamplesGPSRMean, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRMean );
  992. //evaluate GP SR Var
  993. ClassificationResult rGPSRVar;
  994. if (GPSRVar)
  995. evaluateGPSRVar( kernelVector, GPSRVarCholesky, rGPSRVar, timeForSingleExamplesGPSRVar, runsPerClassToAverageTesting, nrOfRegressors, indicesOfChosenExamplesGPSRVar, noiseGPSRVarParas[cl] );
  996. //evaluate GP Opt Mean
  997. ClassificationResult rGPOptMean;
  998. if (GPOptMean)
  999. evaluateGPOptMean( kernelVector, GPOptMeanRightPart, rGPOptMean, timeForSingleExamplesGPOptMean, runsPerClassToAverageTesting );
  1000. //evaluate GP Opt Var
  1001. ClassificationResult rGPOptVar;
  1002. if (GPOptVar)
  1003. evaluateGPOptVar( kernelVector, kernelSelf, DiagGPOptVar, rGPOptVar, timeForSingleExamplesGPOptVar, runsPerClassToAverageTesting );
  1004. //evaluate Parzen
  1005. ClassificationResult rParzen;
  1006. if (Parzen)
  1007. evaluateParzen( kernelVector, rParzen, timeForSingleExamplesParzen, runsPerClassToAverageTesting );
  1008. //evaluate SVDD
  1009. ClassificationResult rSVDD;
  1010. if (SVDD)
  1011. evaluateSVDD( svdd, kernelVector, rSVDD, timeForSingleExamplesSVDD, runsPerClassToAverageTesting );
  1012. // set ground truth label
  1013. rGPVarApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1014. rGPVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1015. rGPMeanApprox.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1016. rGPMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1017. rGPSRMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1018. rGPSRVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1019. rGPOptMean.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1020. rGPOptVar.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1021. rParzen.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1022. rSVDD.classno_groundtruth = (((int)imageNetTest.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  1023. //remember the results for the evaluation lateron
  1024. resultsGPVarApprox.push_back ( rGPVarApprox );
  1025. resultsGPVar.push_back ( rGPVar );
  1026. resultsGPMeanApprox.push_back ( rGPMeanApprox );
  1027. resultsGPMean.push_back ( rGPMean );
  1028. resultsGPSRMean.push_back ( rGPSRMean );
  1029. resultsGPSRVar.push_back ( rGPSRVar );
  1030. resultsGPOptMean.push_back ( rGPOptMean );
  1031. resultsGPOptVar.push_back ( rGPOptVar );
  1032. resultsParzen.push_back ( rParzen );
  1033. resultsSVDD.push_back ( rSVDD );
  1034. }
  1035. tTest.stop();
  1036. std::cerr << "Time used for evaluating class " << cl << ": " << tTest.getLast() << std::endl;
  1037. timeForSingleExamplesGPVarApprox/= imageNetTest.getNumPreloadedExamples();
  1038. timeForSingleExamplesGPVar/= imageNetTest.getNumPreloadedExamples();
  1039. timeForSingleExamplesGPMeanApprox/= imageNetTest.getNumPreloadedExamples();
  1040. timeForSingleExamplesGPMean/= imageNetTest.getNumPreloadedExamples();
  1041. timeForSingleExamplesGPSRMean/= imageNetTest.getNumPreloadedExamples();
  1042. timeForSingleExamplesGPSRVar/= imageNetTest.getNumPreloadedExamples();
  1043. timeForSingleExamplesGPOptMean/= imageNetTest.getNumPreloadedExamples();
  1044. timeForSingleExamplesGPOptVar/= imageNetTest.getNumPreloadedExamples();
  1045. timeForSingleExamplesParzen/= imageNetTest.getNumPreloadedExamples();
  1046. timeForSingleExamplesSVDD/= imageNetTest.getNumPreloadedExamples();
  1047. std::cerr << "GPVarApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVarApprox << std::endl;
  1048. std::cerr << "GPVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPVar << std::endl;
  1049. std::cerr << "GPMeanApprox -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMeanApprox << std::endl;
  1050. std::cerr << "GPMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPMean << std::endl;
  1051. std::cerr << "GPSRMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRMean << std::endl;
  1052. std::cerr << "GPSRVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPSRVar << std::endl;
  1053. std::cerr << "GPOptMean -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPOptMean << std::endl;
  1054. std::cerr << "GPOptVar -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesGPOptVar << std::endl;
  1055. std::cerr << "Parzen -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesParzen << std::endl;
  1056. std::cerr << "SVDD -- time used for evaluation single elements of class " << cl << " : " << timeForSingleExamplesSVDD << std::endl;
  1057. // run the AUC-evaluation
  1058. double perfvalueGPVarApprox( 0.0 );
  1059. double perfvalueGPVar( 0.0 );
  1060. double perfvalueGPMeanApprox( 0.0 );
  1061. double perfvalueGPMean( 0.0 );
  1062. double perfvalueGPSRMean( 0.0 );
  1063. double perfvalueGPSRVar( 0.0 );
  1064. double perfvalueGPOptMean( 0.0 );
  1065. double perfvalueGPOptVar( 0.0 );
  1066. double perfvalueParzen( 0.0 );
  1067. double perfvalueSVDD( 0.0 );
  1068. if (GPVarApprox)
  1069. perfvalueGPVarApprox = resultsGPVarApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1070. if (GPVar)
  1071. perfvalueGPVar = resultsGPVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1072. if (GPMeanApprox)
  1073. perfvalueGPMeanApprox = resultsGPMeanApprox.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1074. if (GPMean)
  1075. perfvalueGPMean = resultsGPMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1076. if (GPSRMean)
  1077. perfvalueGPSRMean = resultsGPSRMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1078. if (GPSRVar)
  1079. perfvalueGPSRVar = resultsGPSRVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1080. if (GPOptMean)
  1081. perfvalueGPOptMean = resultsGPOptMean.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1082. if (GPOptVar)
  1083. perfvalueGPOptVar = resultsGPOptVar.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1084. if (Parzen)
  1085. perfvalueParzen = resultsParzen.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1086. if (SVDD)
  1087. perfvalueSVDD = resultsSVDD.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  1088. std::cerr << "Performance GPVarApprox: " << perfvalueGPVarApprox << std::endl;
  1089. std::cerr << "Performance GPVar: " << perfvalueGPVar << std::endl;
  1090. std::cerr << "Performance GPMeanApprox: " << perfvalueGPMeanApprox << std::endl;
  1091. std::cerr << "Performance GPMean: " << perfvalueGPMean << std::endl;
  1092. std::cerr << "Performance GPSRMean: " << perfvalueGPSRMean << std::endl;
  1093. std::cerr << "Performance GPSRVar: " << perfvalueGPSRVar << std::endl;
  1094. std::cerr << "Performance GPOptMean: " << perfvalueGPOptMean << std::endl;
  1095. std::cerr << "Performance GPOptVar: " << perfvalueGPOptVar << std::endl;
  1096. std::cerr << "Performance Parzen: " << perfvalueParzen << std::endl;
  1097. std::cerr << "Performance SVDD: " << perfvalueSVDD << std::endl;
  1098. OverallPerformanceGPVarApprox += perfvalueGPVar;
  1099. OverallPerformanceGPVar += perfvalueGPVarApprox;
  1100. OverallPerformanceGPMeanApprox += perfvalueGPMeanApprox;
  1101. OverallPerformanceGPMean += perfvalueGPMean;
  1102. OverallPerformanceGPSRMean += perfvalueGPSRMean;
  1103. OverallPerformanceGPSRVar += perfvalueGPSRVar;
  1104. OverallPerformanceGPOptMean += perfvalueGPOptMean;
  1105. OverallPerformanceGPOptVar += perfvalueGPOptVar;
  1106. OverallPerformanceParzen += perfvalueParzen;
  1107. OverallPerformanceSVDD += perfvalueSVDD;
  1108. // clean up memory used by SVDD
  1109. if (SVDD)
  1110. delete svdd;
  1111. }
  1112. OverallPerformanceGPVarApprox /= nrOfClassesToConcidere;
  1113. OverallPerformanceGPVar /= nrOfClassesToConcidere;
  1114. OverallPerformanceGPMeanApprox /= nrOfClassesToConcidere;
  1115. OverallPerformanceGPMean /= nrOfClassesToConcidere;
  1116. OverallPerformanceGPSRMean /= nrOfClassesToConcidere;
  1117. OverallPerformanceGPSRVar /= nrOfClassesToConcidere;
  1118. OverallPerformanceGPOptMean /= nrOfClassesToConcidere;
  1119. OverallPerformanceGPOptVar /= nrOfClassesToConcidere;
  1120. OverallPerformanceParzen /= nrOfClassesToConcidere;
  1121. OverallPerformanceSVDD /= nrOfClassesToConcidere;
  1122. std::cerr << "overall performance GPVarApprox: " << OverallPerformanceGPVarApprox << std::endl;
  1123. std::cerr << "overall performance GPVar: " << OverallPerformanceGPVar << std::endl;
  1124. std::cerr << "overall performance GPMeanApprox: " << OverallPerformanceGPMeanApprox << std::endl;
  1125. std::cerr << "overall performance GPMean: " << OverallPerformanceGPMean << std::endl;
  1126. std::cerr << "overall performance GPSRMean: " << OverallPerformanceGPSRMean << std::endl;
  1127. std::cerr << "overall performance GPSRVar: " << OverallPerformanceGPSRVar << std::endl;
  1128. std::cerr << "overall performance GPOptMean: " << OverallPerformanceGPOptMean << std::endl;
  1129. std::cerr << "overall performance GPOptVar: " << OverallPerformanceGPOptVar << std::endl;
  1130. std::cerr << "overall performance Parzen: " << OverallPerformanceParzen << std::endl;
  1131. std::cerr << "overall performance SVDD: " << OverallPerformanceSVDD << std::endl;
  1132. return 0;
  1133. }