TestGPHIKClassifier.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include <iostream>
  5. #include <fstream>
  6. //----------
  7. #include <core/basics/Timer.h>
  8. //----------
  9. #include <vislearning/cbaselib/ClassificationResults.h>
  10. #include <vislearning/classifier/kernelclassifier/KCGPRegOneVsAll.h>
  11. //----------
  12. #include "gp-hik-exp/GPHIKClassifierNICE.h"
  13. //----------
  14. #include "TestGPHIKClassifier.h"
  15. const bool verbose = false;
  16. const bool verboseStartEnd = true;
  17. using namespace OBJREC;
  18. using namespace NICE;
  19. using namespace std;
  20. CPPUNIT_TEST_SUITE_REGISTRATION( TestGPHIKClassifier );
  21. void TestGPHIKClassifier::setUp() {
  22. }
  23. void TestGPHIKClassifier::tearDown() {
  24. }
  25. void myClassifierTest( GPHIKClassifierNICE & classifier, const Matrix & mX, const Vector & vY )
  26. {
  27. if (verboseStartEnd)
  28. std::cerr << "================== TestGPHIKClassifier::myClassifierTest ===================== " << std::endl;
  29. Examples examples;
  30. for ( uint i = 0 ; i < vY.size() ; i++ )
  31. if ( i % 2 == 1 )
  32. {
  33. Example example;
  34. example.svec = new SparseVector;
  35. example.svec->setDim(3);
  36. example.svec->set ( 0, mX(i,0) );
  37. example.svec->set ( 1, mX(i,1) );
  38. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  39. examples.push_back ( pair<int, Example> ( vY[i], example ) );
  40. }
  41. FeaturePool fp; // will be ignored
  42. if ( verbose )
  43. std::cerr << "preparation done." << std::endl;
  44. if ( verbose )
  45. std::cerr << "learning ..." << std::endl;
  46. classifier.train ( fp, examples );
  47. if ( verbose )
  48. std::cerr << "testing ..." << std::endl;
  49. for ( uint i = 0 ; i < vY.size() ; i++ )
  50. if ( i % 2 == 0 )
  51. {
  52. Example example;
  53. example.svec = new SparseVector;
  54. example.svec->setDim(3);
  55. example.svec->set ( 0, mX(i,0) );
  56. example.svec->set ( 1, mX(i,1) );
  57. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  58. ClassificationResult r = classifier.classify ( example );
  59. if (verbose)
  60. {
  61. r.scores >> std::cerr;
  62. std::cerr << "predicted uncertainty: " << r.uncertainty << std::endl;
  63. }
  64. }
  65. examples.clean();
  66. if (verboseStartEnd)
  67. std::cerr << "================== TestGPHIKClassifier::myClassifierTest done ===================== " << std::endl;
  68. }
  69. void myClassifierStoreRestoreTest( GPHIKClassifierNICE & classifier, const Matrix & mX, const Vector & vY )
  70. {
  71. if (verboseStartEnd)
  72. std::cerr << "================== TestGPHIKClassifier::myClassifierStoreRestoreTest ===================== " << std::endl;
  73. Examples examples;
  74. for ( uint i = 0 ; i < vY.size() ; i++ )
  75. if ( i % 2 == 1 )
  76. {
  77. Example example;
  78. example.svec = new SparseVector;
  79. example.svec->setDim(3);
  80. example.svec->set ( 0, mX(i,0) );
  81. example.svec->set ( 1, mX(i,1) );
  82. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  83. examples.push_back ( pair<int, Example> ( vY[i], example ) );
  84. }
  85. FeaturePool fp; // will be ignored
  86. if ( verbose )
  87. std::cerr << "preparation done." << std::endl;
  88. if ( verbose )
  89. std::cerr << "learning ..." << std::endl;
  90. classifier.train ( fp, examples );
  91. if ( verbose )
  92. std::cerr << "storing ..." << std::endl;
  93. //test the store-functionality
  94. string destination("/tmp/GPHIK_store.txt");
  95. std::filebuf fb;
  96. fb.open (destination.c_str(),ios::out);
  97. std::ostream os(&fb);
  98. //
  99. classifier.store(os);
  100. //
  101. fb.close();
  102. if ( verbose )
  103. std::cerr << "loading ..." << std::endl;
  104. Config confTmp;
  105. GPHIKClassifierNICE classifierRestored(&confTmp);
  106. std::filebuf fbIn;
  107. fbIn.open (destination.c_str(),ios::in);
  108. std::istream is(&fbIn);
  109. //
  110. classifierRestored.restore(is);
  111. //
  112. fbIn.close();
  113. if ( verbose )
  114. std::cerr << "testing ..." << std::endl;
  115. for ( uint i = 0 ; i < vY.size() ; i++ )
  116. if ( i % 2 == 0 )
  117. {
  118. Example example;
  119. example.svec = new SparseVector;
  120. example.svec->setDim(3);
  121. example.svec->set ( 0, mX(i,0) );
  122. example.svec->set ( 1, mX(i,1) );
  123. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  124. ClassificationResult rOrig = classifier.classify ( example );
  125. ClassificationResult rRestored = classifierRestored.classify ( example );
  126. //scores are of type FullVector
  127. //we use the [] operator, since there are no iterators given in FullVector.h
  128. bool equal(true);
  129. for (int i = 0; i< rOrig.scores.size(); i++)
  130. {
  131. if ( fabs(rOrig.scores[i] - rRestored.scores[i]) > 10-6)
  132. {
  133. equal = false;
  134. break;
  135. }
  136. }
  137. CPPUNIT_ASSERT_EQUAL ( equal, true );
  138. }
  139. examples.clean();
  140. if (verboseStartEnd)
  141. std::cerr << "================== TestGPHIKClassifier::myClassifierStoreRestoreTest done ===================== " << std::endl;
  142. }
  143. void myClassifierILTest( OBJREC::GPHIKClassifierNICE & classifierRetrain,
  144. OBJREC::GPHIKClassifierNICE & classifierIL,
  145. const Matrix & mX,
  146. const Vector & vY
  147. )
  148. {
  149. if (verboseStartEnd)
  150. std::cerr << "================== TestGPHIKClassifier::myClassifierILTest ===================== " << std::endl;
  151. Examples examples;
  152. if (verbose)
  153. std::cerr << "vY: " << vY << std::endl;
  154. for ( uint i = 0 ; i < vY.size() ; i++ )
  155. {
  156. if ( i % 4 == 1 )
  157. {
  158. Example example;
  159. example.svec = new SparseVector;
  160. example.svec->setDim(3);
  161. example.svec->set ( 0, mX(i,0) );
  162. example.svec->set ( 1, mX(i,1) );
  163. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  164. examples.push_back ( pair<int, Example> ( vY[i], example ) );
  165. }
  166. }
  167. if (verbose)
  168. std::cerr << "examples.size(): " << examples.size() << std::endl;
  169. FeaturePool fp; // will be ignored
  170. if ( verbose )
  171. std::cerr << "preparation done." << std::endl;
  172. if ( verbose )
  173. std::cerr << "learning ..." << std::endl;
  174. Timer t;
  175. t.start();
  176. classifierIL.train ( fp, examples );
  177. t.stop();
  178. std::cerr << "Time used for initial training: " << t.getLast() << std::endl;
  179. //choose next example(s)
  180. int i_numExamplesToAdd ( 2 );
  181. Examples newExamples;
  182. for ( uint i = 0 ; i < vY.size() ; i++ )
  183. {
  184. if ( i % 4 == 3 )
  185. {
  186. Example example;
  187. example.svec = new SparseVector();
  188. example.svec->setDim(3);
  189. example.svec->set ( 0, mX(i,0) );
  190. example.svec->set ( 1, mX(i,1) );
  191. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  192. newExamples.push_back ( std::pair<int, Example> ( vY[i], example ) );
  193. }
  194. if ( newExamples.size() == i_numExamplesToAdd )
  195. break;
  196. }
  197. i_numExamplesToAdd = std::min ( i_numExamplesToAdd, (int) newExamples.size() );
  198. //add the new features to feature pool needed for batch training
  199. for (uint i = 0; i < i_numExamplesToAdd; i++)
  200. {
  201. examples.push_back( newExamples[i] );
  202. }
  203. std::cerr << std::endl << " =============== " << std::endl << "We train the second classifier from scratch with the additional new example" << std::endl;
  204. t.start();
  205. classifierRetrain.train ( fp, examples );
  206. t.stop();
  207. std::cerr << "Time used for batch training: " << t.getLast() << std::endl;
  208. if ( verbose )
  209. std::cerr << std::endl << " =============== " << std::endl << "incremental learning ..." << std::endl;
  210. // add them to classifierIL
  211. t.start();
  212. if ( verbose )
  213. std::cerr << "We add " << i_numExamplesToAdd << " new examples" << std::endl;
  214. if ( i_numExamplesToAdd > 1 )
  215. classifierIL.addMultipleExamples( newExamples );
  216. else if ( i_numExamplesToAdd == 1 )
  217. classifierIL.addExample( newExamples[0].second, newExamples[0].first);
  218. else
  219. {
  220. //nothing to do
  221. }
  222. t.stop();
  223. std::cerr << "Time used for incremental training: " << t.getLast() << std::endl;
  224. //evaluate both and compare the resulting scores
  225. if ( verbose )
  226. std::cerr << "testing ..." << std::endl;
  227. for ( uint i = 0 ; i < vY.size() ; i++ )
  228. if ( i % 2 == 0 )
  229. {
  230. Example example;
  231. example.svec = new SparseVector;
  232. example.svec->setDim(3);
  233. example.svec->set ( 0, mX(i,0) );
  234. example.svec->set ( 1, mX(i,1) );
  235. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  236. ClassificationResult resultIL = classifierIL.classify ( example );
  237. ClassificationResult resultBatch = classifierRetrain.classify ( example );
  238. if (verbose)
  239. {
  240. std::cerr << "result of IL classifier: " << std::endl;
  241. resultIL.scores >> std::cerr;
  242. std::cerr << "result of batch classifier: " << std::endl;
  243. resultBatch.scores >> std::cerr;
  244. }
  245. //scores are of type FullVector
  246. //we use the [] operator, since there are no iterators given in FullVector.h
  247. bool equal(true);
  248. for (int i = 0; i< resultIL.scores.size(); i++)
  249. {
  250. if ( fabs(resultIL.scores[i] - resultBatch.scores[i]) > 10e-3)
  251. {
  252. std::cerr << " resultIL.scores[i]: " << resultIL.scores[i] << " resultBatch.scores[i]: " << resultBatch.scores[i] << std::endl;
  253. equal = false;
  254. break;
  255. }
  256. }
  257. CPPUNIT_ASSERT_EQUAL ( equal, true );
  258. }
  259. examples.clean();
  260. if (verboseStartEnd)
  261. std::cerr << "================== TestGPHIKClassifier::myClassifierILTest done ===================== " << std::endl;
  262. }
  263. void TestGPHIKClassifier::testGPHIKClassifier()
  264. {
  265. if (verboseStartEnd)
  266. std::cerr << "================== TestGPHIKClassifier::testGPHIKClassifier ===================== " << std::endl;
  267. NICE::Config conf;
  268. conf.sD( "GPHIKClassifier", "noise", 0.01 );
  269. conf.sD( "GPHIKClassifier", "parameter_lower_bound", 0.5 );
  270. conf.sD( "GPHIKClassifier", "parameter_upper_bound", 3.5 );
  271. // conf.sS( "GPHIKClassifier", "optimization_method", "none");
  272. // conf.sD( "GPHIKClassifier", "performOptimizationAfterIncrement", false );
  273. conf.sS( "GPHIKClassifier", "optimization_method", "downhillsimplex");
  274. conf.sD( "GPHIKClassifier", "performOptimizationAfterIncrement", true );
  275. conf.sB( "GPHIKClassifier", "uncertaintyPredictionForClassification", false);
  276. OBJREC::GPHIKClassifierNICE * classifier = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  277. NICE::Matrix mX;
  278. NICE::Vector vY;
  279. NICE::Vector vY_multi;
  280. // ifstream ifs ("toyExample1.data", ios::in);
  281. // ifstream ifs ("toyExampleLargeScale.data", ios::in);
  282. ifstream ifs ("toyExampleLargeLargeScale.data", ios::in);
  283. CPPUNIT_ASSERT ( ifs.good() );
  284. ifs >> mX;
  285. ifs >> vY;
  286. ifs >> vY_multi;
  287. ifs.close();
  288. if (verbose)
  289. {
  290. std::cerr << "data loaded: mX" << std::endl;
  291. std::cerr << mX << std::endl;
  292. std::cerr << "vY: " << std::endl;
  293. std::cerr << vY << std::endl;
  294. std::cerr << "vY_multi: " << std::endl;
  295. std::cerr << vY_multi << std::endl;
  296. }
  297. if ( verbose )
  298. std::cerr << "Binary classification test " << std::endl;
  299. myClassifierTest ( *classifier, mX, vY );
  300. // ... we remove nothing here since we are only interested in store and restore :)
  301. myClassifierStoreRestoreTest ( *classifier, mX, vY );
  302. // ... remove previously computed things and start again, this time with incremental settings
  303. if (classifier != NULL)
  304. delete classifier;
  305. classifier = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  306. OBJREC::GPHIKClassifierNICE * classifierBatch = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  307. myClassifierILTest( *classifierBatch, *classifier, mX, vY );
  308. if (classifier != NULL)
  309. delete classifier;
  310. if (classifierBatch != NULL)
  311. delete classifierBatch;
  312. classifier = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  313. classifierBatch = new OBJREC::GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  314. if ( verbose )
  315. std::cerr << "Multi-class classification test " << std::endl;
  316. myClassifierTest ( *classifier, mX, vY_multi );
  317. // ... we remove nothing here since we are only interested and store and restore :)
  318. //
  319. // myClassifierStoreRestoreTest ( classifier, mX, vY_multi );
  320. // ... remove previously computed things and start again, this time with incremental settings
  321. if (classifier != NULL)
  322. delete classifier;
  323. if (classifierBatch != NULL)
  324. delete classifierBatch;
  325. classifier = new GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  326. classifierBatch = new GPHIKClassifierNICE ( &conf, "GPHIKClassifier" );
  327. myClassifierILTest( *classifierBatch, *classifier, mX, vY_multi );
  328. if (classifier != NULL)
  329. delete classifier;
  330. if (classifierBatch != NULL)
  331. delete classifierBatch;
  332. if (verboseStartEnd)
  333. std::cerr << "================== TestGPHIKClassifier::testGPHIKClassifier done ===================== " << std::endl;
  334. }
  335. void TestGPHIKClassifier::testGPHIKVariance()
  336. {
  337. if (verboseStartEnd)
  338. std::cerr << "================== TestGPHIKClassifier::testGPHIKVariance ===================== " << std::endl;
  339. double noise (0.01);
  340. Config conf;
  341. conf.sD( "GPHIKClassifier", "noise", noise );
  342. conf.sD( "GPHIKClassifier", "parameter_lower_bound", 1.0 );
  343. conf.sD( "GPHIKClassifier", "parameter_upper_bound", 1.0 );
  344. conf.sS( "GPHIKClassifier", "varianceApproximation", "approximate_rough");
  345. conf.sB( "GPHIKClassifier", "learn_balanced", true);
  346. conf.sB( "GPHIKClassifier", "uncertaintyPredictionForClassification", true);
  347. GPHIKClassifierNICE classifier ( &conf );
  348. Config confVarApproxQuant(conf);
  349. confVarApproxQuant.sB( "GPHIKClassifier", "use_quantization", true );
  350. GPHIKClassifierNICE classifierQuant ( &confVarApproxQuant );
  351. Config confVarApproxFine1(conf);
  352. confVarApproxFine1.sS( "GPHIKClassifier", "varianceApproximation", "approximate_fine");
  353. confVarApproxFine1.sI( "GPHIKClassifier", "nrOfEigenvaluesToConsiderForVarApprox", 1);
  354. GPHIKClassifierNICE classifierVarApproxFine1 ( &confVarApproxFine1 );
  355. Config confVarApproxFine2(conf);
  356. confVarApproxFine2.sS( "GPHIKClassifier", "varianceApproximation", "approximate_fine");
  357. confVarApproxFine2.sI( "GPHIKClassifier", "nrOfEigenvaluesToConsiderForVarApprox", 2);
  358. GPHIKClassifierNICE classifierVarApproxFine2 ( &confVarApproxFine2 );
  359. Config confExact(conf);
  360. confExact.sS( "GPHIKClassifier", "varianceApproximation", "exact");
  361. GPHIKClassifierNICE classifierVarExact ( &confExact );
  362. NICE::Matrix mX;
  363. NICE::Vector vY;
  364. NICE::Vector vY_multi;
  365. ifstream ifs ("toyExample2.data", ios::in);
  366. CPPUNIT_ASSERT ( ifs.good() );
  367. ifs >> mX;
  368. ifs >> vY;
  369. ifs >> vY_multi;
  370. ifs.close();
  371. if (verbose)
  372. {
  373. std::cerr << "data loaded: mX" << std::endl;
  374. std::cerr << mX << std::endl;
  375. std::cerr << "vY: " << std::endl;
  376. std::cerr << vY << std::endl;
  377. std::cerr << "vY_multi: " << std::endl;
  378. std::cerr << vY_multi << std::endl;
  379. }
  380. Examples examples;
  381. for ( uint i = 0 ; i < vY.size() ; i++ )
  382. if ( i % 2 == 0 )
  383. {
  384. Example example;
  385. example.svec = new SparseVector;
  386. example.svec->setDim(3);
  387. example.svec->set ( 0, mX(i,0) );
  388. example.svec->set ( 1, mX(i,1) );
  389. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  390. examples.push_back ( pair<int, Example> ( vY_multi[i], example ) );
  391. }
  392. FeaturePool fp; // will be ignored
  393. if ( verbose )
  394. std::cerr << "preparation for variance testing done." << std::endl;
  395. if ( verbose )
  396. std::cerr << "learning for variance testing ..." << std::endl;
  397. classifier.train ( fp, examples );
  398. classifierQuant.train ( fp, examples );
  399. classifierVarApproxFine1.train ( fp, examples );
  400. classifierVarApproxFine2.train ( fp, examples );
  401. classifierVarExact.train ( fp, examples );
  402. if ( verbose )
  403. std::cerr << "testing for variance testing ..." << std::endl;
  404. for ( uint i = 0 ; i < vY_multi.size() ; i++ )
  405. if ( i % 2 == 1 )
  406. {
  407. Example example;
  408. example.svec = new SparseVector;
  409. example.svec->setDim(3);
  410. example.svec->set ( 0, mX(i,0) );
  411. example.svec->set ( 1, mX(i,1) );
  412. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  413. ClassificationResult r = classifier.classify ( example );
  414. ClassificationResult rQuant = classifierQuant.classify ( example );
  415. ClassificationResult rVarApproxFine1 = classifierVarApproxFine1.classify ( example );
  416. ClassificationResult rVarApproxFine2 = classifierVarApproxFine2.classify ( example );
  417. ClassificationResult rExact = classifierVarExact.classify ( example );
  418. // if (verbose)
  419. // {
  420. std::cerr << "approxUnc: " << r.uncertainty << " approxUncQuant: " << rQuant.uncertainty<< " approxUncFine1: " << rVarApproxFine1.uncertainty << " approxUncFine2: " << rVarApproxFine2.uncertainty << " exactUnc: " << rExact.uncertainty << std::endl;
  421. // }
  422. CPPUNIT_ASSERT ( r.uncertainty <= (1.0 + noise) ); //using the "standard" HIK, this is the upper bound
  423. CPPUNIT_ASSERT ( r.uncertainty > rVarApproxFine1.uncertainty);
  424. CPPUNIT_ASSERT ( rQuant.uncertainty > rVarApproxFine1.uncertainty);
  425. CPPUNIT_ASSERT ( rVarApproxFine1.uncertainty > rVarApproxFine2.uncertainty);
  426. CPPUNIT_ASSERT ( rVarApproxFine2.uncertainty > rExact.uncertainty);
  427. }
  428. examples.clean();
  429. if (verboseStartEnd)
  430. std::cerr << "================== TestGPHIKClassifier::testGPHIKVariance done ===================== " << std::endl;
  431. }
  432. #endif