IL_AL_Binary_GPBaseline.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /**
  2. * @file IL_AL_Binary_GPBaseline.cpp
  3. * @brief Incrementally train the GP HIK classifier using the predictive variance and its approximations to select new samples, perform binary tests. We do not use the fast-hik implementations but perform the computations manually
  4. * @author Alexander Freytag
  5. * @date 11-06-2012
  6. */
  7. #include <vector>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <set>
  11. #include <core/basics/Config.h>
  12. #include <core/basics/StringTools.h>
  13. #include <core/basics/Timer.h>
  14. #include <core/algebra/CholeskyRobust.h>
  15. #include <core/vector/Algorithms.h>
  16. #include <core/vector/SparseVectorT.h>
  17. #include <core/vector/VectorT.h>
  18. //----------
  19. #include <vislearning/baselib/ProgressBar.h>
  20. #include <vislearning/baselib/Globals.h>
  21. #include <vislearning/classifier/kernelclassifier/KCGPRegOneVsAll.h>
  22. #include <vislearning/cbaselib/MultiDataset.h>
  23. #include <vislearning/cbaselib/LabeledSet.h>
  24. #include <vislearning/cbaselib/ClassificationResults.h>
  25. #include <vislearning/cbaselib/Example.h>
  26. #include <vislearning/math/kernels/KernelData.h>
  27. //----------
  28. #include "gp-hik-exp/progs/datatools.h"
  29. //
  30. using namespace std;
  31. using namespace NICE;
  32. using namespace OBJREC;
  33. enum verbose_level {NONE = 0, LOW = 1, MEDIUM = 2, EVERYTHING = 3};
  34. enum QueryStrategy{
  35. RANDOM = 0,
  36. GPMEAN,
  37. GPPREDVAR,
  38. GPHEURISTIC
  39. };
  40. std::string convertInt(int number)
  41. {
  42. stringstream ss;//create a stringstream
  43. ss << number;//add number to the stream
  44. return ss.str();//return a string with the contents of the stream
  45. }
  46. double measureMinimumDistance ( const NICE::SparseVector & a, const NICE::SparseVector & b)
  47. {
  48. double sum(0.0);
  49. NICE::SparseVector::const_iterator aIt = a.begin();
  50. NICE::SparseVector::const_iterator bIt = b.begin();
  51. while ( (aIt != a.end()) && (bIt != b.end()) )
  52. {
  53. if (aIt->first == bIt->first)
  54. {
  55. sum += std::min( aIt->second, bIt->second );
  56. aIt++;
  57. bIt++;
  58. }
  59. else if ( aIt->first < bIt->first)
  60. {
  61. //minimum is zero
  62. aIt++;
  63. }
  64. else
  65. {
  66. //minimum is zero
  67. bIt++;
  68. }
  69. }
  70. //we do not have to compute the remaining values for the second iterator, since the other one is since in the corresponding dimensions
  71. return sum;
  72. }
  73. /**
  74. Computes from randomly or deterministically choosen trainimages kernelmatrizes and evaluates their performance, using ROI-optimization
  75. */
  76. int main ( int argc, char **argv )
  77. {
  78. std::cout.precision ( 10 );
  79. std::cerr.precision ( 10 );
  80. NICE::Config conf ( argc, argv );
  81. int trainExPerClass = conf.gI ( "GP_IL", "trainExPerClass", 10 );
  82. int incrementalAddSize = conf.gI("GP_IL", "incrementalAddSize", 1);
  83. int nrOfIncrements = conf.gI("GP_IL", "nrOfIncrements", 9);
  84. int num_runs = conf.gI ( "GP_IL", "num_runs", 10 );
  85. bool do_classification = conf.gB ( "GP_IL", "do_classification", true );
  86. double noise = conf.gD("GPHIKClassifier", "noise", 0.01);
  87. double squaredNoise = pow( noise, 2);
  88. int minClass = conf.gI( "main", "minClass", 0);
  89. int maxClass = conf.gI( "main", "maxClass", 15);
  90. string queryStrategyString = conf.gS( "main", "queryStrategy", "random");
  91. QueryStrategy queryStrategy;
  92. if (queryStrategyString.compare("gpMean") == 0)
  93. {
  94. queryStrategy = GPMEAN;
  95. }
  96. else if (queryStrategyString.compare("gpPredVar") == 0)
  97. {
  98. queryStrategy = GPPREDVAR;
  99. }
  100. else if (queryStrategyString.compare("gpHeuristic") == 0)
  101. {
  102. queryStrategy = GPHEURISTIC;
  103. }
  104. else
  105. {
  106. queryStrategy = RANDOM;
  107. }
  108. int verbose_int = conf.gI ( "GP_IL", "verbose", 0 );
  109. verbose_level verbose ( NONE );
  110. switch ( verbose_int )
  111. {
  112. case 0:
  113. verbose = NONE;
  114. break;
  115. case 1:
  116. verbose = LOW;
  117. break;
  118. case 2:
  119. verbose = MEDIUM;
  120. break;
  121. case 3:
  122. verbose = EVERYTHING;
  123. break;
  124. }
  125. std::string locationOfPermutations = conf.gS( "main", "locationOfPermutations", "/home/luetz/data/images/caltech-101/" );
  126. std::string classselection_train = conf.gS( "main", "classselection_train", "*" );
  127. std::string classselection_test = conf.gS( "main", "classselection_test", "*" );
  128. std::string examples_train = conf.gS( "main", "examples_train", "seq * 100" );
  129. std::string examples_test = conf.gS( "main", "examples_test", "seq * 50" );
  130. /* initialize random seed: */
  131. srand ( time ( NULL ) ); //with 0 for reproductive results
  132. // srand ( 0 ); //with 0 for reproductive results
  133. for (int currentClass = minClass; currentClass <= maxClass; currentClass++)
  134. {
  135. std::cerr << "start binary experiments for class " << currentClass << std::endl;
  136. // =========================== INIT ===========================
  137. std::vector<std::vector<double> > recognitions_rates(nrOfIncrements+1);
  138. std::vector<std::vector<double> > AUC_scores(nrOfIncrements+1);
  139. std::vector<std::vector<float> > classification_times(nrOfIncrements+1);
  140. std::vector<std::vector<float> > IL_training_times(nrOfIncrements);
  141. for ( int run = 0; run < num_runs; run++ )
  142. {
  143. std::cerr << "run: " << run << std::endl;
  144. NICE::Config confCurrentRun ( conf );
  145. confCurrentRun.sS( "train"+convertInt(run), "dataset", locationOfPermutations+"run"+convertInt(run)+".train" );
  146. confCurrentRun.sS( "train"+convertInt(run), "classselection_train", classselection_train );
  147. confCurrentRun.sS( "train"+convertInt(run), "examples_train", examples_train );
  148. confCurrentRun.sS( "test"+convertInt(run), "dataset", locationOfPermutations+"run"+convertInt(run)+".test" );
  149. confCurrentRun.sS( "test"+convertInt(run), "classselection_test", classselection_test );
  150. confCurrentRun.sS( "train"+convertInt(run), "examples_test", examples_test );
  151. //15-scenes settings
  152. std::string ext = confCurrentRun.gS("main", "ext", ".txt");
  153. std::cerr << "Using cache extension: " << ext << std::endl;
  154. OBJREC::MultiDataset md ( &confCurrentRun );
  155. std::cerr << "now read the dataset" << std::endl;
  156. // read training set
  157. vector< NICE::Vector > trainDataOrig;
  158. Vector y;
  159. string trainRun ( "train" + convertInt( run ) );
  160. std::cerr << "look for " << trainRun << std::endl;
  161. const LabeledSet *train = md[ trainRun ]; //previously, we only selected "train", no we select the permutation for this run
  162. //we just store the filenames to have a look which image we picked in every step
  163. std::vector<std::string> filenamesTraining;
  164. readData< std::vector< NICE::Vector >, NICE::Vector > ( confCurrentRun, *train, trainDataOrig, y, filenamesTraining, ext );
  165. std::cerr << "label vector after reading: " << y << std::endl;
  166. bool firstPositivePrinted( false );
  167. //assure the binary setting
  168. for ( uint i = 0; i < y.size(); i++ )
  169. {
  170. if ( y[i] == currentClass)
  171. {
  172. if ( !firstPositivePrinted )
  173. {
  174. std::cerr << "first positive example: " << filenamesTraining[i] << std::endl;
  175. firstPositivePrinted = true;
  176. }
  177. y[i] = 1;
  178. }
  179. else
  180. y[i] = 0;//-1;
  181. }
  182. std::cerr << "resulting binary label vector:" << y << std::endl;
  183. std::set<int> classesAvailable;
  184. classesAvailable.insert( 0 ); //we have a single negative class
  185. classesAvailable.insert( 1 ); //and we have a single positive class
  186. std::map<int,int> nrExamplesPerClassInDataset; //simply count how many examples for every class are available
  187. std::map<int,std::vector<int> > examplesPerClassInDataset; //as well as their corresponding indices in the dataset
  188. //initialize this storage
  189. for (std::set<int>::const_iterator it = classesAvailable.begin(); it != classesAvailable.end(); it++)
  190. {
  191. nrExamplesPerClassInDataset.insert(std::pair<int,int>(*it,0));
  192. examplesPerClassInDataset.insert(std::pair<int,std::vector<int> >(*it,std::vector<int>(0)));
  193. }
  194. //store the indices of the examples
  195. for ( uint i = 0; i < y.size(); i++ )
  196. {
  197. (examplesPerClassInDataset.find( y[i] )->second).push_back(i);
  198. }
  199. //and count how many examples are in every class
  200. for (std::map<int,std::vector<int> >::const_iterator it = examplesPerClassInDataset.begin(); it != examplesPerClassInDataset.end(); it++)
  201. {
  202. nrExamplesPerClassInDataset.find(it->first)->second = it->second.size();
  203. }
  204. //simple output to tell how many examples we have for every class
  205. for ( std::map<int,int>::const_iterator it = nrExamplesPerClassInDataset.begin(); it != nrExamplesPerClassInDataset.end(); it++)
  206. {
  207. cerr << it->first << ": " << it->second << endl;
  208. }
  209. Examples examples;
  210. //count how many examples of every class we have while actively selecting new examples
  211. //NOTE works only if we have subsequent class numbers
  212. NICE::Vector pickedExamplesPerClass( classesAvailable.size(), trainExPerClass);
  213. std::map<int,std::vector<int> > examplesPerClassInDatasetTmp (examplesPerClassInDataset);
  214. //chose examples for every class used for training
  215. //we will always use the first examples from each class, since the dataset comes already randomly ordered
  216. for (std::set<int>::const_iterator clIt = classesAvailable.begin(); clIt != classesAvailable.end(); clIt++)
  217. {
  218. std::map<int,std::vector<int> >::iterator exIt = examplesPerClassInDatasetTmp.find(*clIt);
  219. std::cerr << "pick training examples for class " << *clIt << std::endl;
  220. for (int i = 0; i < trainExPerClass; i++)
  221. {
  222. std::cerr << "i: " << i << std::endl;
  223. int exampleIndex ( 0 ); //old: rand() % ( exIt->second.size() ) );
  224. std::cerr << "pick example " << exIt->second[exampleIndex] << " - " << y[exIt->second[exampleIndex] ] << " -- " << filenamesTraining[exIt->second[exampleIndex]] << std::endl;
  225. Example example;
  226. NICE::Vector & xTrain = trainDataOrig[exIt->second[exampleIndex]];
  227. example.svec = new SparseVector(xTrain);
  228. //let's take this example and its corresponding label (which should be *clIt)
  229. examples.push_back ( pair<int, Example> ( y[exIt->second[exampleIndex] ], example ) );
  230. //
  231. exIt->second.erase(exIt->second.begin()+exampleIndex);
  232. }
  233. }
  234. std::vector<std::string> filenamesUnlabeled;
  235. filenamesUnlabeled.clear();
  236. //which examples are left to be actively chosen lateron?
  237. std::vector<int> unlabeledExamples( y.size() - trainExPerClass*classesAvailable.size() );
  238. int exCnt( 0 );
  239. for (std::set<int>::const_iterator clIt = classesAvailable.begin(); clIt != classesAvailable.end(); clIt++ )
  240. {
  241. std::map<int,std::vector<int> >::iterator exIt = examplesPerClassInDatasetTmp.find(*clIt);
  242. //list all examples of this specific class
  243. for (std::vector<int>::const_iterator it = exIt->second.begin(); it != exIt->second.end(); it++)
  244. {
  245. unlabeledExamples[exCnt] = *it;
  246. exCnt++;
  247. filenamesUnlabeled.push_back( filenamesTraining[*it] );
  248. }
  249. }
  250. //brute force GP regression graining
  251. Timer t;
  252. t.start();
  253. NICE::Matrix kernelMatrix (examples.size(), examples.size(), 0.0);
  254. //and set zero to minus one for the internal GP computations for expected mean
  255. NICE::Vector yBinGP ( examples.size(), -1 );
  256. //now compute the kernelScores for every element
  257. double kernelScore(0.0);
  258. for ( uint i = 0; i < examples.size(); i++ )
  259. {
  260. for ( uint j = i; j < examples.size(); j++ )
  261. {
  262. kernelScore = measureMinimumDistance(* examples[i].second.svec, * examples[j].second.svec);
  263. kernelMatrix(i,j) = kernelScore;
  264. if (i != j)
  265. kernelMatrix(j,i) = kernelScore;
  266. }
  267. if ( examples[i].first == 1)
  268. yBinGP[i] = 1;
  269. }
  270. //adding some noise, if necessary
  271. if ( squaredNoise != 0.0 )
  272. {
  273. kernelMatrix.addIdentity( noise );
  274. }
  275. else
  276. {
  277. //zero was already set
  278. }
  279. std::cerr << "noise: " << noise << std::endl;
  280. std::cerr << "kernelMatrix: " << kernelMatrix << std::endl;
  281. //compute its inverse
  282. //noise is already added :)
  283. CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
  284. NICE::Matrix choleskyMatrix ( examples.size(), examples.size(), 0.0 );
  285. cr.robustChol ( kernelMatrix, choleskyMatrix );
  286. NICE::Vector GPrightPart ( examples.size() );
  287. choleskySolveLargeScale ( choleskyMatrix, yBinGP, GPrightPart );
  288. std::cerr << "choleskyMatrix: " << choleskyMatrix << std::endl;
  289. t.stop();
  290. cerr << "Time used for initial training: " << t.getLast() << endl;
  291. int nrOfClassesUsed = classesAvailable.size();
  292. // ------------------ TESTING
  293. string testRun ( "test" + convertInt( run ) );
  294. const LabeledSet *test = md[ testRun ]; //previously, we only selected "test", now we select the permutation for this run
  295. VVector testData;
  296. Vector yTest;
  297. readData< VVector, Vector > ( confCurrentRun, *test, testData, yTest, ext );
  298. NICE::Matrix confusionMatrix ( 2, 2 );
  299. confusionMatrix.set ( 0.0 );
  300. time_t start_time = clock();
  301. std::vector<int> chosen_examples_per_class ( nrOfClassesUsed );
  302. std::cerr << "Current statistic about picked examples per class: " << pickedExamplesPerClass << std::endl;
  303. if ( do_classification )
  304. {
  305. ClassificationResults results;
  306. for ( uint i = 0 ; i < testData.size(); i++ )
  307. {
  308. const Vector & xstar = testData[i];
  309. SparseVector xstar_sparse ( xstar );
  310. //compute similarities
  311. NICE::Vector kernelVector ( examples.size(), 0.0 );
  312. for ( uint j = 0; j < examples.size(); j++ )
  313. {
  314. kernelVector[j] = measureMinimumDistance( * examples[j].second.svec, xstar_sparse );
  315. }
  316. //compute the resulting score
  317. double score = kernelVector.scalarProduct( GPrightPart );
  318. //this is the standard score-object needed for the evaluation
  319. FullVector scores ( 2 );
  320. scores[0] = -1.0*score;
  321. scores[1] = score;
  322. ClassificationResult result ( scores.maxElement(), scores );
  323. result.classno_groundtruth = ( yTest[i] == 1 ) ? 1 : 0;
  324. result.classno = ( score >= 0.0 ) ? 1 : 0;
  325. confusionMatrix ( result.classno_groundtruth , result.classno ) ++;
  326. results.push_back( result );
  327. }
  328. float time_classification = ( float ) ( clock() - start_time ) ;
  329. if ( verbose >= LOW )
  330. cerr << "Time for Classification with " << nrOfClassesUsed*trainExPerClass << " training-examples: " << time_classification / CLOCKS_PER_SEC << " [s]" << endl;
  331. ( classification_times[0] ).push_back ( time_classification / CLOCKS_PER_SEC );
  332. confusionMatrix.normalizeRowsL1();
  333. std::cerr << confusionMatrix;
  334. double avg_recognition_rate = 0.0;
  335. for ( int i = 0 ; i < ( int ) confusionMatrix.rows(); i++ )
  336. {
  337. avg_recognition_rate += confusionMatrix ( i, i );
  338. }
  339. avg_recognition_rate /= confusionMatrix.rows();
  340. std::cerr << "class: " << currentClass << " run: " << run << " avg recognition rate: " << avg_recognition_rate*100 << " % -- " << examples.size() << " training examples used" << std::endl;
  341. recognitions_rates[0].push_back ( avg_recognition_rate*100 );
  342. std::cerr << "number of classified examples: " << results.size() << std::endl;
  343. std::cerr << "perform auc evaluation "<< std::endl;
  344. double aucScore = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  345. std::cerr << "class: " << currentClass << " run: " << run << " AUC-score: " << aucScore << " % -- " << examples.size() << " training examples used" << std::endl << std::endl;
  346. AUC_scores[0].push_back ( aucScore*100 );
  347. }
  348. //Now start the Incremental-Learning-Part
  349. for (int incrementationStep = 0; incrementationStep < nrOfIncrements; incrementationStep++)
  350. {
  351. //simply count how many possible example we have
  352. int nrOfPossibleExamples( unlabeledExamples.size() );
  353. if (queryStrategy == RANDOM)
  354. {
  355. std::cerr << "print chosen examples: " << std::endl;
  356. for (int i = 0; i < incrementalAddSize; i++)
  357. {
  358. int exampleIndex ( rand() % ( unlabeledExamples.size() ) );
  359. Example newExample;
  360. NICE::Vector & xTrain = trainDataOrig[ unlabeledExamples[exampleIndex] ];
  361. newExample.svec = new SparseVector( xTrain );
  362. int label( y[ unlabeledExamples[exampleIndex] ] );
  363. examples.push_back ( pair<int, Example> ( label, newExample ) );
  364. unlabeledExamples.erase( unlabeledExamples.begin()+exampleIndex );
  365. std::cerr << exampleIndex+1 << " / " << incrementalAddSize << " : " << filenamesUnlabeled[ exampleIndex ] << std::endl;
  366. filenamesUnlabeled.erase( filenamesUnlabeled.begin()+exampleIndex );
  367. pickedExamplesPerClass[label]++;
  368. }
  369. }// end computation for RANDOM
  370. else if ( (queryStrategy == GPMEAN) || (queryStrategy == GPPREDVAR) || (queryStrategy == GPHEURISTIC) )
  371. {
  372. //compute uncertainty values for all examples according to the query strategy
  373. std::vector<std::pair<int,double> > scores;
  374. scores.clear();
  375. time_t unc_pred_start_time = clock();
  376. for (uint exIndex = 0; exIndex < unlabeledExamples.size(); exIndex++)
  377. {
  378. NICE::Vector & xTrain = trainDataOrig[ unlabeledExamples[exIndex] ];
  379. SparseVector xTrainSparse ( xTrain );
  380. //compute similarities
  381. NICE::Vector kernelVector ( examples.size(), 0.0);
  382. for ( uint j = 0; j < examples.size(); j++ )
  383. {
  384. kernelVector[j] = measureMinimumDistance( * examples[j].second.svec, xTrainSparse );
  385. }
  386. if (queryStrategy == GPMEAN)
  387. {
  388. //compute the resulting score
  389. double score = kernelVector.scalarProduct( GPrightPart );
  390. scores.push_back( std::pair<int,double> ( exIndex, fabs(score) ) );
  391. }
  392. else if (queryStrategy == GPPREDVAR)
  393. {
  394. double kernelSelf ( measureMinimumDistance( xTrainSparse, xTrainSparse) );
  395. NICE::Vector rightPart (examples.size());
  396. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  397. double uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  398. scores.push_back( std::pair<int,double> ( exIndex, uncertainty) );
  399. }
  400. else if (queryStrategy == GPHEURISTIC)
  401. {
  402. double kernelSelf ( measureMinimumDistance( xTrainSparse, xTrainSparse) );
  403. NICE::Vector rightPart (examples.size());
  404. choleskySolveLargeScale ( choleskyMatrix, kernelVector, rightPart );
  405. //uncertainty
  406. double uncertainty = kernelSelf - kernelVector.scalarProduct ( rightPart );
  407. //mean
  408. double score = kernelVector.scalarProduct( GPrightPart );
  409. //compute the resulting score
  410. scores.push_back( std::pair<int,double> ( exIndex, fabs(score) / sqrt( squaredNoise + uncertainty ) ) );
  411. }
  412. }
  413. float time_score_computation = ( float ) ( clock() - unc_pred_start_time ) ;
  414. //pick the ones with best score
  415. //we could speed this up using a more sophisticated search method
  416. if (queryStrategy == GPPREDVAR) //take the maximum of the scores for the predictive variance
  417. {
  418. std::set<int> chosenExamplesForThisRun;
  419. chosenExamplesForThisRun.clear();
  420. for (int i = 0; i < incrementalAddSize; i++)
  421. {
  422. std::vector<std::pair<int,double> >::iterator bestExample = scores.begin();
  423. std::vector<std::pair<int,double> >::iterator worstExample = scores.begin();
  424. for (std::vector<std::pair<int,double> >::iterator jIt = scores.begin(); jIt !=scores.end(); jIt++)
  425. {
  426. if (jIt->second > bestExample->second)
  427. bestExample = jIt;
  428. if (jIt->second < worstExample->second)
  429. worstExample = jIt;
  430. }
  431. std::cerr << "i: " << i << " bestExample: " << bestExample->second << " worstExample: " << worstExample->second << std::endl;
  432. Example newExample;
  433. NICE::Vector & xTrain = trainDataOrig[ unlabeledExamples[bestExample->first] ];
  434. newExample.svec = new SparseVector( xTrain );
  435. //actually this is the ACTIVE LEARNING step (query a label)
  436. int label( y[ unlabeledExamples[bestExample->first] ] );
  437. examples.push_back ( pair<int, Example> ( label, newExample ) );
  438. //remember the index, to safely remove this example afterwards from unlabeledExamples
  439. chosenExamplesForThisRun.insert(bestExample->first);
  440. scores.erase(bestExample);
  441. pickedExamplesPerClass[label]++;
  442. }
  443. // std::cerr << "print chosen examples: " << std::endl;
  444. /* int tmpCnt(0);
  445. for (std::set<int>::const_iterator it = chosenExamplesForThisRun.begin(); it != chosenExamplesForThisRun.end(); it++, tmpCnt++)
  446. {
  447. std::cerr << tmpCnt+1 << " / " << incrementalAddSize << " : " << filenamesUnlabeled[ *it ] << std::endl;
  448. } */
  449. //delete the queried examples from the set of unlabeled ones
  450. //do this in an decreasing order in terms of indices to ensure valid access
  451. for (std::set<int>::const_reverse_iterator it = chosenExamplesForThisRun.rbegin(); it != chosenExamplesForThisRun.rend(); it++)
  452. {
  453. unlabeledExamples.erase( unlabeledExamples.begin()+(*it) );
  454. }
  455. }
  456. else //take the minimum of the scores for the heuristic and the gp mean (minimum margin)
  457. {
  458. std::set<int> chosenExamplesForThisRun;
  459. chosenExamplesForThisRun.clear();
  460. for (int i = 0; i < incrementalAddSize; i++)
  461. {
  462. std::vector<std::pair<int,double> >::iterator bestExample = scores.begin();
  463. std::vector<std::pair<int,double> >::iterator worstExample = scores.begin();
  464. for (std::vector<std::pair<int,double> >::iterator jIt = scores.begin(); jIt !=scores.end(); jIt++)
  465. {
  466. if (jIt->second < bestExample->second)
  467. bestExample = jIt;
  468. if (jIt->second > worstExample->second)
  469. worstExample = jIt;
  470. }
  471. std::cerr << "i: " << i << " bestExample: " << bestExample->second << " worstExample: " << worstExample->second << std::endl;
  472. Example newExample;
  473. NICE::Vector & xTrain = trainDataOrig[ unlabeledExamples[bestExample->first] ];
  474. newExample.svec = new SparseVector( xTrain );
  475. //actually this is the ACTIVE LEARNING step (query a label)
  476. int label( y[ unlabeledExamples[bestExample->first] ] );
  477. examples.push_back ( pair<int, Example> ( label, newExample ) );
  478. //remember the index, to safely remove this example afterwards from unlabeledExamples
  479. chosenExamplesForThisRun.insert(bestExample->first);
  480. scores.erase(bestExample);
  481. pickedExamplesPerClass[label]++;
  482. }
  483. //delete the queried example from the set of unlabeled ones
  484. //do this in an decreasing order in terms of indices to ensure valid access
  485. for (std::set<int>::const_reverse_iterator it = chosenExamplesForThisRun.rbegin(); it != chosenExamplesForThisRun.rend(); it++)
  486. {
  487. unlabeledExamples.erase( unlabeledExamples.begin()+(*it) );
  488. }
  489. }
  490. std::cerr << "Time used to compute query-scores for " << nrOfPossibleExamples << " examples: " << time_score_computation / CLOCKS_PER_SEC << " [s]" << std::endl;
  491. } // end computation for GPMEAN, GPPREDVAR, or GPHEURISTIC
  492. std::cerr << "Current statistic about picked examples per class: " << pickedExamplesPerClass << std::endl;
  493. //again: brute force GP regression graining
  494. Timer t;
  495. t.start();
  496. NICE::Matrix kernelMatrix (examples.size(), examples.size(), 0.0);
  497. //and set zero to minus one for the internal GP computations for expected mean
  498. NICE::Vector yBinGP ( examples.size(), -1 );
  499. //now compute the kernelScores for every element
  500. double kernelScore(0.0);
  501. for ( uint i = 0; i < examples.size(); i++ )
  502. {
  503. for ( uint j = i; j < examples.size(); j++ )
  504. {
  505. kernelScore = measureMinimumDistance(* examples[i].second.svec, * examples[j].second.svec);
  506. kernelMatrix(i,j) = kernelScore;
  507. if (i != j)
  508. kernelMatrix(j,i) = kernelScore;
  509. }
  510. if ( examples[i].first == 1)
  511. yBinGP[i] = 1;
  512. }
  513. //adding some noise, if necessary
  514. if ( squaredNoise != 0.0 )
  515. {
  516. kernelMatrix.addIdentity( squaredNoise );
  517. }
  518. else
  519. {
  520. //zero was already set
  521. }
  522. //compute its inverse
  523. //noise is already added :)
  524. //update the cholesky decomposition
  525. choleskyMatrix.resize ( examples.size(), examples.size() );
  526. choleskyMatrix.set( 0.0 );
  527. cr.robustChol ( kernelMatrix, choleskyMatrix );
  528. //and update the right part needed for the posterior mean
  529. GPrightPart.resize ( examples.size() );
  530. GPrightPart.set( 0.0 );
  531. choleskySolveLargeScale ( choleskyMatrix, yBinGP, GPrightPart );
  532. t.stop();
  533. std::cerr << "Time for IL-adding of " << incrementalAddSize << " examples to already " << nrOfClassesUsed*trainExPerClass+incrementalAddSize*incrementationStep << " training-examples: " << t.getLast() << " [s]" << std::endl;
  534. IL_training_times[incrementationStep].push_back( t.getLast() );
  535. //do the classification for evaluating the benefit of new examples
  536. if ( do_classification )
  537. {
  538. time_t start_time = clock();
  539. ClassificationResults results;
  540. confusionMatrix.set( 0.0 );
  541. for ( uint i = 0 ; i < testData.size(); i++ )
  542. {
  543. const Vector & xstar = testData[i];
  544. SparseVector xstar_sparse ( xstar );
  545. //compute similarities
  546. NICE::Vector kernelVector ( examples.size(), 0.0 );
  547. for ( uint j = 0; j < examples.size(); j++ )
  548. {
  549. kernelVector[j] = measureMinimumDistance( * examples[j].second.svec, xstar_sparse );
  550. }
  551. //compute the resulting score
  552. double score = kernelVector.scalarProduct( GPrightPart );
  553. //this is the standard score-object needed for the evaluation
  554. FullVector scores ( 2 );
  555. scores[0] = -1.0*score;
  556. scores[1] = score;
  557. ClassificationResult result ( scores.maxElement(), scores );
  558. result.classno_groundtruth = ( yTest[i] == 1 ) ? 1 : 0;
  559. result.classno = ( score >= 0.0 ) ? 1 : 0;
  560. results.push_back( result );
  561. confusionMatrix ( result.classno_groundtruth , result.classno ) ++;
  562. }
  563. float time_classification = ( float ) ( clock() - start_time ) ;
  564. if ( verbose >= LOW )
  565. std::cerr << "Time for Classification with " << nrOfClassesUsed*trainExPerClass+incrementalAddSize*(incrementationStep+1) << " training-examples: " << time_classification / CLOCKS_PER_SEC << " [s]" << std::endl;
  566. ( classification_times[incrementationStep+1] ).push_back ( time_classification / CLOCKS_PER_SEC );
  567. confusionMatrix.normalizeRowsL1();
  568. std::cerr << confusionMatrix;
  569. double avg_recognition_rate ( 0.0 );
  570. for ( int i = 0 ; i < ( int ) confusionMatrix.rows(); i++ )
  571. {
  572. avg_recognition_rate += confusionMatrix ( i, i );
  573. }
  574. avg_recognition_rate /= confusionMatrix.rows();
  575. std::cerr << "class: " << currentClass << " run: " << run << " avg recognition rate: " << avg_recognition_rate*100 << " % -- " << nrOfClassesUsed*trainExPerClass+incrementalAddSize*(incrementationStep+1) << " training examples used" << std::endl;
  576. recognitions_rates[incrementationStep+1].push_back ( avg_recognition_rate*100 );
  577. double score = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  578. std::cerr << "class: " << currentClass << " run: " << run << " AUC-score: " << score << " % -- " << nrOfClassesUsed*trainExPerClass+incrementalAddSize*(incrementationStep+1) << " training examples used" << std::endl << std::endl;
  579. AUC_scores[incrementationStep+1].push_back ( score*100 );
  580. } //classification after IL adding */
  581. } //IL adding of different classes
  582. std::cerr << "Final statistic about picked examples per class: " << pickedExamplesPerClass << std::endl;
  583. //don't waste memory!
  584. for ( uint tmp = 0; tmp < examples.size(); tmp++ )
  585. {
  586. delete examples[tmp].second.svec;
  587. examples[tmp].second.svec = NULL;
  588. }
  589. }//runs
  590. // ================= EVALUATION =========================
  591. int nrOfClassesUsed ( 2 ); //binary setting
  592. if ( do_classification )
  593. {
  594. std::cerr << "========================" << std::endl;
  595. std::cerr << " final evaluation for class: " << currentClass << std::endl;
  596. std::cerr << "content of classification_times: " << std::endl;
  597. for ( std::vector<std::vector<float> >::const_iterator it = classification_times.begin(); it != classification_times.end(); it++ )
  598. {
  599. for ( std::vector<float> ::const_iterator jt = ( *it ).begin(); jt != ( *it ).end(); jt++ )
  600. {
  601. std::cerr << *jt << " ";
  602. }
  603. std::cerr << std::endl;
  604. }
  605. std::vector<float> mean_classification_times;
  606. std::vector<float> std_dev_classification_times;
  607. for ( std::vector<std::vector<float> >::const_iterator it = classification_times.begin(); it != classification_times.end(); it++ )
  608. {
  609. float mean_classification_time ( 0.0 );
  610. for ( std::vector<float>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  611. {
  612. mean_classification_time += *itRun;
  613. }
  614. mean_classification_time /= it->size();
  615. mean_classification_times.push_back ( mean_classification_time );
  616. double std_dev_classification_time ( 0.0 );
  617. for ( std::vector<float>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  618. {
  619. std_dev_classification_time += pow ( *itRun - mean_classification_time, 2 );
  620. }
  621. std_dev_classification_time /= it->size();
  622. std_dev_classification_time = sqrt ( std_dev_classification_time );
  623. std_dev_classification_times.push_back ( std_dev_classification_time );
  624. }
  625. int datasize ( nrOfClassesUsed*trainExPerClass );
  626. for ( uint i = 0; i < mean_classification_times.size(); i++)
  627. {
  628. std::cerr << "size: " << datasize << " mean classification time: " << mean_classification_times[i] << " std_dev classification time: " << std_dev_classification_times[i] << std::endl;
  629. datasize += incrementalAddSize ;
  630. }
  631. }
  632. else
  633. {
  634. std::cerr << "========================" << std::endl;
  635. std::cerr << "No classification done therefor no classification times available." << std::endl;
  636. }
  637. std::cerr << "========================" << std::endl;
  638. std::cerr << "content of IL_training_times for class : "<< currentClass << std::endl;
  639. for ( std::vector<std::vector<float> >::const_iterator it = IL_training_times.begin(); it != IL_training_times.end(); it++ )
  640. {
  641. for ( std::vector<float> ::const_iterator jt = ( *it ).begin(); jt != ( *it ).end(); jt++ )
  642. {
  643. std::cerr << *jt << " ";
  644. }
  645. std::cerr << std::endl;
  646. }
  647. std::vector<float> mean_IL_training_times;
  648. std::vector<float> std_dev_IL_training_times;
  649. for ( std::vector<std::vector<float> >::const_iterator it = IL_training_times.begin(); it != IL_training_times.end(); it++ )
  650. {
  651. float mean_IL_training_time ( 0.0 );
  652. for ( std::vector<float>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  653. {
  654. mean_IL_training_time += *itRun;
  655. }
  656. mean_IL_training_time /= it->size();
  657. mean_IL_training_times.push_back ( mean_IL_training_time );
  658. double std_dev_IL_training_time ( 0.0 );
  659. for ( std::vector<float>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  660. {
  661. std_dev_IL_training_time += pow ( *itRun - mean_IL_training_time, 2 );
  662. }
  663. std_dev_IL_training_time /= it->size();
  664. std_dev_IL_training_time = sqrt ( std_dev_IL_training_time );
  665. std_dev_IL_training_times.push_back ( std_dev_IL_training_time );
  666. }
  667. int datasize ( nrOfClassesUsed*trainExPerClass );
  668. for ( uint i = 0; i < mean_IL_training_times.size(); i++)
  669. {
  670. cerr << "size: " << datasize << " and adding " << incrementalAddSize << " mean IL_training time: " << mean_IL_training_times[i] << " std_dev IL_training time: " << std_dev_IL_training_times[i] << endl;
  671. datasize += incrementalAddSize ;
  672. }
  673. if ( do_classification )
  674. {
  675. std::cerr << "========================" << std::endl;
  676. std::cerr << "content of recognition_rates for class : "<< currentClass << std::endl;
  677. for ( std::vector<std::vector<double> >::const_iterator it = recognitions_rates.begin(); it != recognitions_rates.end(); it++ )
  678. {
  679. for ( std::vector<double> ::const_iterator jt = ( *it ).begin(); jt != ( *it ).end(); jt++ )
  680. {
  681. std::cerr << *jt << " ";
  682. }
  683. std::cerr << std::endl;
  684. }
  685. std::cerr << "calculating final recognition_rates for class : "<< currentClass << std::endl;
  686. std::vector<double> mean_recs;
  687. std::vector<double> std_dev_recs;
  688. for (std::vector<std::vector<double> >::const_iterator it = recognitions_rates.begin(); it != recognitions_rates.end(); it++ )
  689. {
  690. double mean_rec ( 0.0 );
  691. for ( std::vector<double>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  692. {
  693. mean_rec += *itRun;
  694. }
  695. mean_rec /= it->size();
  696. mean_recs.push_back ( mean_rec );
  697. double std_dev_rec ( 0.0 );
  698. for ( std::vector<double>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  699. {
  700. std_dev_rec += pow ( *itRun - mean_rec, 2 );
  701. }
  702. std_dev_rec /= it->size();
  703. std_dev_rec = sqrt ( std_dev_rec );
  704. std_dev_recs.push_back ( std_dev_rec );
  705. }
  706. int datasize ( nrOfClassesUsed*trainExPerClass );
  707. for ( uint i = 0; i < recognitions_rates.size(); i++)
  708. {
  709. std::cerr << "size: " << datasize << " mean_IL: " << mean_recs[i] << " std_dev_IL: " << std_dev_recs[i] << std::endl;
  710. datasize += incrementalAddSize ;
  711. }
  712. std::cerr << "========================" << std::endl;
  713. std::cerr << "content of AUC_scores for class : "<< currentClass << std::endl;
  714. for ( std::vector<std::vector<double> >::const_iterator it = AUC_scores.begin(); it != AUC_scores.end(); it++ )
  715. {
  716. for ( std::vector<double> ::const_iterator jt = ( *it ).begin(); jt != ( *it ).end(); jt++ )
  717. {
  718. std::cerr << *jt << " ";
  719. }
  720. std::cerr << std::endl;
  721. }
  722. std::cerr << "calculating final AUC_scores for class : "<< currentClass << std::endl;
  723. std::vector<double> mean_aucs;
  724. std::vector<double> std_dev_aucs;
  725. for (std::vector<std::vector<double> >::const_iterator it = AUC_scores.begin(); it != AUC_scores.end(); it++ )
  726. {
  727. double mean_auc ( 0.0 );
  728. for ( std::vector<double>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  729. {
  730. mean_auc += *itRun;
  731. }
  732. mean_auc /= it->size();
  733. mean_aucs.push_back ( mean_auc );
  734. double std_dev_auc ( 0.0 );
  735. for ( std::vector<double>::const_iterator itRun = it->begin(); itRun != it->end(); itRun++ )
  736. {
  737. std_dev_auc += pow ( *itRun - mean_auc, 2 );
  738. }
  739. std_dev_auc /= it->size();
  740. std_dev_auc = sqrt ( std_dev_auc );
  741. std_dev_aucs.push_back ( std_dev_auc );
  742. }
  743. datasize = nrOfClassesUsed*trainExPerClass;
  744. for ( uint i = 0; i < recognitions_rates.size(); i++)
  745. {
  746. std::cerr << "size: " << datasize << " mean_IL: " << mean_aucs[i] << " std_dev_IL: " << std_dev_aucs[i] << std::endl;
  747. datasize += incrementalAddSize ;
  748. }
  749. }
  750. else
  751. {
  752. std::cerr << "========================" << std::endl;
  753. std::cerr << "No classification done therefor no classification times available." << std::endl;
  754. }
  755. } //for int currentClass...
  756. return 0;
  757. }