TestFPCGPHIK.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "vislearning/classifier/fpclassifier/FPCGPHIK.h"
  13. //----------
  14. #include "TestFPCGPHIK.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( TestFPCGPHIK );
  21. void TestFPCGPHIK::setUp() {
  22. }
  23. void TestFPCGPHIK::tearDown() {
  24. }
  25. void myClassifierTest( FPCGPHIK & classifier, const Matrix & mX, const Vector & vY )
  26. {
  27. if (verboseStartEnd)
  28. std::cerr << "================== TestFPCGPHIK::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 << "================== TestFPCGPHIK::myClassifierTest done ===================== " << std::endl;
  68. }
  69. void myClassifierStoreRestoreTest( FPCGPHIK & classifier, const Matrix & mX, const Vector & vY )
  70. {
  71. if (verboseStartEnd)
  72. std::cerr << "================== TestFPCGPHIK::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. FPCGPHIK 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 << "================== TestFPCGPHIK::myClassifierStoreRestoreTest done ===================== " << std::endl;
  142. }
  143. void myClassifierILTest( FPCGPHIK & classifierRetrain, FPCGPHIK & classifierIL, const Matrix & mX, const Vector & vY )
  144. {
  145. if (verboseStartEnd)
  146. std::cerr << "================== TestFPCGPHIK::myClassifierILTest ===================== " << std::endl;
  147. Examples examples;
  148. if (verbose)
  149. std::cerr << "vY: " << vY << std::endl;
  150. for ( uint i = 0 ; i < vY.size() ; i++ )
  151. {
  152. if ( i % 4 == 1 )
  153. {
  154. Example example;
  155. example.svec = new SparseVector;
  156. example.svec->setDim(3);
  157. example.svec->set ( 0, mX(i,0) );
  158. example.svec->set ( 1, mX(i,1) );
  159. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  160. examples.push_back ( pair<int, Example> ( vY[i], example ) );
  161. }
  162. }
  163. if (verbose)
  164. std::cerr << "examples.size(): " << examples.size() << std::endl;
  165. FeaturePool fp; // will be ignored
  166. if ( verbose )
  167. std::cerr << "preparation done." << std::endl;
  168. if ( verbose )
  169. std::cerr << "learning ..." << std::endl;
  170. classifierIL.train ( fp, examples );
  171. //choose next example(s)
  172. Examples newExamples;
  173. for ( uint i = 0 ; i < vY.size() ; i++ )
  174. {
  175. if ( i % 4 == 3 )
  176. {
  177. Example example;
  178. example.svec = new SparseVector;
  179. example.svec->setDim(3);
  180. example.svec->set ( 0, mX(i,0) );
  181. example.svec->set ( 1, mX(i,1) );
  182. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  183. newExamples.push_back ( pair<int, Example> ( vY[i], example ) );
  184. }
  185. }
  186. // if ( verbose )
  187. std::cerr << std::endl << " =============== " << std::endl << "incremental learning ..." << std::endl;
  188. // add them to classifierIL
  189. // std::cerr << "We add several new examples" << std::endl;
  190. Timer t;
  191. t.start();
  192. // for (uint i = 0; i < newExamples.size(); i++)
  193. for (uint i = 0; i < 1; i++)
  194. {
  195. classifierIL.addExample( newExamples[i].second, newExamples[i].first);
  196. }
  197. t.stop();
  198. std::cerr << "Time used for incremental training: " << t.getLast() << std::endl;
  199. //add the new features to feature pool needed for batch training
  200. // for (uint i = 0; i < newExamples.size(); i++)
  201. for (uint i = 0; i < 2; i++)
  202. {
  203. examples.push_back( newExamples[i] );
  204. }
  205. std::cerr << std::endl << " =============== " << std::endl << "We train the second classifier from the scratch with the additional new example" << std::endl;
  206. t.start();
  207. classifierRetrain.train ( fp, examples );
  208. t.stop();
  209. std::cerr << "Time used for batch training: " << t.getLast() << std::endl;
  210. //evaluate both and compare the resulting scores
  211. // if ( verbose )
  212. std::cerr << "testing ..." << std::endl;
  213. for ( uint i = 0 ; i < vY.size() ; i++ )
  214. if ( i % 2 == 0 )
  215. {
  216. Example example;
  217. example.svec = new SparseVector;
  218. example.svec->setDim(3);
  219. example.svec->set ( 0, mX(i,0) );
  220. example.svec->set ( 1, mX(i,1) );
  221. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  222. ClassificationResult resultIL = classifierIL.classify ( example );
  223. ClassificationResult resultBatch = classifierRetrain.classify ( example );
  224. if (verbose)
  225. {
  226. std::cerr << "result of IL classifier: " << std::endl;
  227. resultIL.scores >> std::cerr;
  228. std::cerr << "result of batch classifier: " << std::endl;
  229. resultBatch.scores >> std::cerr;
  230. }
  231. //scores are of type FullVector
  232. //we use the [] operator, since there are no iterators given in FullVector.h
  233. bool equal(true);
  234. for (int i = 0; i< resultIL.scores.size(); i++)
  235. {
  236. if ( fabs(resultIL.scores[i] - resultBatch.scores[i]) > 10e-3)
  237. {
  238. equal = false;
  239. break;
  240. }
  241. }
  242. CPPUNIT_ASSERT_EQUAL ( equal, true );
  243. }
  244. examples.clean();
  245. if (verboseStartEnd)
  246. std::cerr << "================== TestFPCGPHIK::myClassifierILTest done ===================== " << std::endl;
  247. }
  248. void TestFPCGPHIK::testFPCGPHIK()
  249. {
  250. if (verboseStartEnd)
  251. std::cerr << "================== TestFPCGPHIK::testFPCGPHIK ===================== " << std::endl;
  252. Config conf;
  253. conf.sD( "FPCGPHIK", "noise", 0.01 );
  254. conf.sD( "FPCGPHIK", "parameter_lower_bound", 0.5 );
  255. conf.sD( "FPCGPHIK", "parameter_upper_bound", 3.5 );
  256. conf.sI( "FPCGPHIK", "uncertaintyPrediction", 1);
  257. // conf.sS( "FPCGPHIK", "optimization_method", "none");
  258. conf.sS( "FPCGPHIK", "optimization_method", "downhillsimplex");
  259. conf.sB( "FPCGPHIK", "uncertaintyPredictionForClassification", true);
  260. FPCGPHIK * classifier = new FPCGPHIK ( &conf );
  261. Matrix mX;
  262. Vector vY;
  263. Vector vY_multi;
  264. // ifstream ifs ("toyExample1.data", ios::in);
  265. // ifstream ifs ("toyExampleLargeScale.data", ios::in);
  266. ifstream ifs ("toyExampleLargeLargeScale.data", ios::in);
  267. CPPUNIT_ASSERT ( ifs.good() );
  268. ifs >> mX;
  269. ifs >> vY;
  270. ifs >> vY_multi;
  271. ifs.close();
  272. if (verbose)
  273. {
  274. std::cerr << "data loaded: mX" << std::endl;
  275. std::cerr << mX << std::endl;
  276. std::cerr << "vY: " << std::endl;
  277. std::cerr << vY << std::endl;
  278. std::cerr << "vY_multi: " << std::endl;
  279. std::cerr << vY_multi << std::endl;
  280. }
  281. if ( verbose )
  282. std::cerr << "Binary classification test " << std::endl;
  283. myClassifierTest ( *classifier, mX, vY );
  284. // ... we remove nothing here since we are only interested in store and restore :)
  285. myClassifierStoreRestoreTest ( *classifier, mX, vY );
  286. // ... remove previously computed things and start again, this time with incremental settings
  287. if (classifier != NULL)
  288. delete classifier;
  289. classifier = new FPCGPHIK ( &conf );
  290. FPCGPHIK * classifierBatch = new FPCGPHIK ( &conf );
  291. myClassifierILTest( *classifierBatch, *classifier, mX, vY );
  292. if (classifier != NULL)
  293. delete classifier;
  294. if (classifierBatch != NULL)
  295. delete classifierBatch;
  296. classifier = new FPCGPHIK ( &conf );
  297. classifierBatch = new FPCGPHIK ( &conf );
  298. if ( verbose )
  299. std::cerr << "Multi-class classification test " << std::endl;
  300. myClassifierTest ( *classifier, mX, vY_multi );
  301. // ... we remove nothing here since we are only interested and store and restore :)
  302. //
  303. // myClassifierStoreRestoreTest ( classifier, mX, vY_multi );
  304. // ... remove previously computed things and start again, this time with incremental settings
  305. if (classifier != NULL)
  306. delete classifier;
  307. if (classifierBatch != NULL)
  308. delete classifierBatch;
  309. classifier = new FPCGPHIK ( &conf, "GPHIKClassifier" /* section */);
  310. classifierBatch = new FPCGPHIK ( &conf, "GPHIKClassifier" /* section */ );
  311. myClassifierILTest( *classifierBatch, *classifier, mX, vY_multi );
  312. if (classifier != NULL)
  313. delete classifier;
  314. if (classifierBatch != NULL)
  315. delete classifierBatch;
  316. if (verboseStartEnd)
  317. std::cerr << "================== TestFPCGPHIK::testFPCGPHIK done ===================== " << std::endl;
  318. }
  319. void TestFPCGPHIK::testGPHIKVariance()
  320. {
  321. if (verboseStartEnd)
  322. std::cerr << "================== TestFPCGPHIK::testGPHIKVariance ===================== " << std::endl;
  323. double noise (0.01);
  324. Config conf;
  325. conf.sD( "GPHIKClassifier", "noise", noise );
  326. conf.sD( "GPHIKClassifier", "parameter_lower_bound", 1.0 );
  327. conf.sD( "GPHIKClassifier", "parameter_upper_bound", 1.0 );
  328. conf.sS( "GPHIKClassifier", "varianceApproximation", "approximate_rough");
  329. conf.sB( "GPHIKClassifier", "learn_balanced", true);
  330. conf.sB( "GPHIKClassifier", "uncertaintyPredictionForClassification", true);
  331. FPCGPHIK classifier ( &conf );
  332. Config confVarApproxQuant(conf);
  333. confVarApproxQuant.sB( "GPHIKClassifier", "use_quantization", true );
  334. FPCGPHIK classifierQuant ( &confVarApproxQuant );
  335. Config confVarApproxFine1(conf);
  336. confVarApproxFine1.sS( "GPHIKClassifier", "varianceApproximation", "approximate_fine");
  337. confVarApproxFine1.sI( "GPHIKClassifier", "nrOfEigenvaluesToConsiderForVarApprox", 1);
  338. FPCGPHIK classifierVarApproxFine1 ( &confVarApproxFine1 );
  339. Config confVarApproxFine2(conf);
  340. confVarApproxFine2.sS( "GPHIKClassifier", "varianceApproximation", "approximate_fine");
  341. confVarApproxFine2.sI( "GPHIKClassifier", "nrOfEigenvaluesToConsiderForVarApprox", 2);
  342. FPCGPHIK classifierVarApproxFine2 ( &confVarApproxFine2 );
  343. Config confExact(conf);
  344. confExact.sS( "GPHIKClassifier", "varianceApproximation", "exact");
  345. FPCGPHIK classifierVarExact ( &confExact );
  346. NICE::Matrix mX;
  347. NICE::Vector vY;
  348. NICE::Vector vY_multi;
  349. ifstream ifs ("toyExample2.data", ios::in);
  350. CPPUNIT_ASSERT ( ifs.good() );
  351. ifs >> mX;
  352. ifs >> vY;
  353. ifs >> vY_multi;
  354. ifs.close();
  355. if (verbose)
  356. {
  357. std::cerr << "data loaded: mX" << std::endl;
  358. std::cerr << mX << std::endl;
  359. std::cerr << "vY: " << std::endl;
  360. std::cerr << vY << std::endl;
  361. std::cerr << "vY_multi: " << std::endl;
  362. std::cerr << vY_multi << std::endl;
  363. }
  364. Examples examples;
  365. for ( uint i = 0 ; i < vY.size() ; i++ )
  366. if ( i % 2 == 0 )
  367. {
  368. Example example;
  369. example.svec = new SparseVector;
  370. example.svec->setDim(3);
  371. example.svec->set ( 0, mX(i,0) );
  372. example.svec->set ( 1, mX(i,1) );
  373. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  374. examples.push_back ( pair<int, Example> ( vY_multi[i], example ) );
  375. }
  376. FeaturePool fp; // will be ignored
  377. if ( verbose )
  378. std::cerr << "preparation for variance testing done." << std::endl;
  379. if ( verbose )
  380. std::cerr << "learning for variance testing ..." << std::endl;
  381. classifier.train ( fp, examples );
  382. classifierQuant.train ( fp, examples );
  383. classifierVarApproxFine1.train ( fp, examples );
  384. classifierVarApproxFine2.train ( fp, examples );
  385. classifierVarExact.train ( fp, examples );
  386. if ( verbose )
  387. std::cerr << "testing for variance testing ..." << std::endl;
  388. for ( uint i = 0 ; i < vY_multi.size() ; i++ )
  389. if ( i % 2 == 1 )
  390. {
  391. Example example;
  392. example.svec = new SparseVector;
  393. example.svec->setDim(3);
  394. example.svec->set ( 0, mX(i,0) );
  395. example.svec->set ( 1, mX(i,1) );
  396. example.svec->set ( 2, 1.0-mX(i,0)-mX(i,1) );
  397. ClassificationResult r = classifier.classify ( example );
  398. ClassificationResult rQuant = classifierQuant.classify ( example );
  399. ClassificationResult rVarApproxFine1 = classifierVarApproxFine1.classify ( example );
  400. ClassificationResult rVarApproxFine2 = classifierVarApproxFine2.classify ( example );
  401. ClassificationResult rExact = classifierVarExact.classify ( example );
  402. if (verbose)
  403. {
  404. std::cerr << "approxUnc: " << r.uncertainty << " approxUncQuant: " << rQuant.uncertainty<< " approxUncFine1: " << rVarApproxFine1.uncertainty << " approxUncFine2: " << rVarApproxFine2.uncertainty << " exactUnc: " << rExact.uncertainty << std::endl;
  405. }
  406. CPPUNIT_ASSERT ( r.uncertainty <= (1.0 + noise) ); //using the "standard" HIK, this is the upper bound
  407. CPPUNIT_ASSERT ( r.uncertainty > rVarApproxFine1.uncertainty);
  408. CPPUNIT_ASSERT ( rQuant.uncertainty > rVarApproxFine1.uncertainty);
  409. CPPUNIT_ASSERT ( rVarApproxFine1.uncertainty > rVarApproxFine2.uncertainty);
  410. CPPUNIT_ASSERT ( rVarApproxFine2.uncertainty > rExact.uncertainty);
  411. }
  412. examples.clean();
  413. if (verboseStartEnd)
  414. std::cerr << "================== TestFPCGPHIK::testGPHIKVariance done ===================== " << std::endl;
  415. }
  416. #endif