TestGPHIKPersistent.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file TestGPHIKPersistent.cpp
  3. * @brief CppUnit-Testcase to verify that GPHIKClassifier methods herited from Persistent (store and restore) work as desired.
  4. * @author Alexander Freytag
  5. * @date 21-12-2013
  6. */
  7. #ifdef NICE_USELIB_CPPUNIT
  8. // STL includes
  9. #include <iostream>
  10. #include <vector>
  11. // NICE-core includes
  12. #include <core/basics/Config.h>
  13. #include <core/basics/Timer.h>
  14. // gp-hik-core includes
  15. #include "gp-hik-core/GPHIKClassifier.h"
  16. #include "TestGPHIKPersistent.h"
  17. using namespace std; //C basics
  18. using namespace NICE; // nice-core
  19. const bool verboseStartEnd = true;
  20. CPPUNIT_TEST_SUITE_REGISTRATION( TestGPHIKPersistent );
  21. void TestGPHIKPersistent::setUp() {
  22. }
  23. void TestGPHIKPersistent::tearDown() {
  24. }
  25. void TestGPHIKPersistent::testPersistentMethods()
  26. {
  27. if (verboseStartEnd)
  28. std::cerr << "================== TestGPHIKPersistent::testPersistentMethods ===================== " << std::endl;
  29. NICE::Config conf;
  30. std::string trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
  31. NICE::GPHIKClassifier * classifier;
  32. //------------- read the training data --------------
  33. NICE::Matrix dataTrain;
  34. NICE::Vector yBinTrain;
  35. NICE::Vector yMultiTrain;
  36. std::ifstream ifsTrain ( trainData.c_str() , ios::in );
  37. if ( ifsTrain.good() )
  38. {
  39. ifsTrain >> dataTrain;
  40. ifsTrain >> yBinTrain;
  41. ifsTrain >> yMultiTrain;
  42. ifsTrain.close();
  43. }
  44. else
  45. {
  46. std::cerr << "Unable to read training data from file " << trainData << " -- aborting." << std::endl;
  47. CPPUNIT_ASSERT ( ifsTrain.good() );
  48. }
  49. //----------------- convert data to sparse data structures ---------
  50. std::vector< const NICE::SparseVector *> examplesTrain;
  51. examplesTrain.resize( dataTrain.rows() );
  52. std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
  53. for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
  54. {
  55. *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
  56. }
  57. // TRAIN CLASSIFIER FROM SCRATCH
  58. classifier = new GPHIKClassifier ( &conf );
  59. classifier->train ( examplesTrain , yMultiTrain );
  60. // TEST STORING ABILITIES
  61. std::string s_destination_save ( "myClassifier.txt" );
  62. std::filebuf fbOut;
  63. fbOut.open ( s_destination_save.c_str(), ios::out );
  64. std::ostream os (&fbOut);
  65. //
  66. classifier->store( os );
  67. //
  68. fbOut.close();
  69. // TEST RESTORING ABILITIES
  70. NICE::GPHIKClassifier * classifierRestored = new GPHIKClassifier;
  71. std::string s_destination_load ( "myClassifier.txt" );
  72. std::filebuf fbIn;
  73. fbIn.open ( s_destination_load.c_str(), ios::in );
  74. std::istream is (&fbIn);
  75. //
  76. classifierRestored->restore( is );
  77. //
  78. fbIn.close();
  79. // TEST both classifiers to produce equal results
  80. //------------- read the test data --------------
  81. NICE::Matrix dataTest;
  82. NICE::Vector yBinTest;
  83. NICE::Vector yMultiTest;
  84. std::string testData = conf.gS( "main", "testData", "toyExampleTest.data" );
  85. std::ifstream ifsTest ( testData.c_str(), ios::in );
  86. if ( ifsTest.good() )
  87. {
  88. ifsTest >> dataTest;
  89. ifsTest >> yBinTest;
  90. ifsTest >> yMultiTest;
  91. ifsTest.close();
  92. }
  93. else
  94. {
  95. std::cerr << "Unable to read test data, aborting." << std::endl;
  96. CPPUNIT_ASSERT ( ifsTest.good() );
  97. }
  98. // ------------------------------------------
  99. // ------------- PREPARATION --------------
  100. // ------------------------------------------
  101. // determine classes known during training and corresponding mapping
  102. // thereby allow for non-continous class labels
  103. std::set<int> classesKnownTraining = classifier->getKnownClassNumbers();
  104. int noClassesKnownTraining ( classesKnownTraining.size() );
  105. std::map<int,int> mapClNoToIdxTrain;
  106. std::set<int>::const_iterator clTrIt = classesKnownTraining.begin();
  107. for ( int i=0; i < noClassesKnownTraining; i++, clTrIt++ )
  108. mapClNoToIdxTrain.insert ( std::pair<int,int> ( *clTrIt, i ) );
  109. // determine classes known during testing and corresponding mapping
  110. // thereby allow for non-continous class labels
  111. std::set<int> classesKnownTest;
  112. classesKnownTest.clear();
  113. // determine which classes we have in our label vector
  114. // -> MATLAB: myClasses = unique(y);
  115. for ( NICE::Vector::const_iterator it = yMultiTest.begin(); it != yMultiTest.end(); it++ )
  116. {
  117. if ( classesKnownTest.find ( *it ) == classesKnownTest.end() )
  118. {
  119. classesKnownTest.insert ( *it );
  120. }
  121. }
  122. int noClassesKnownTest ( classesKnownTest.size() );
  123. std::map<int,int> mapClNoToIdxTest;
  124. std::set<int>::const_iterator clTestIt = classesKnownTest.begin();
  125. for ( int i=0; i < noClassesKnownTest; i++, clTestIt++ )
  126. mapClNoToIdxTest.insert ( std::pair<int,int> ( *clTestIt, i ) );
  127. NICE::Matrix confusionMatrix ( noClassesKnownTraining, noClassesKnownTest, 0.0);
  128. NICE::Matrix confusionMatrixRestored ( noClassesKnownTraining, noClassesKnownTest, 0.0);
  129. int i_loopEnd ( (int)dataTest.rows() );
  130. for (int i = 0; i < i_loopEnd ; i++)
  131. {
  132. NICE::Vector example ( dataTest.getRow(i) );
  133. NICE::SparseVector scores;
  134. int result;
  135. // classify with trained classifier
  136. classifier->classify( &example, result, scores );
  137. confusionMatrix( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  138. // classify with restored classifier
  139. scores.clear();
  140. classifierRestored->classify( &example, result, scores );
  141. confusionMatrixRestored( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  142. }
  143. confusionMatrix.normalizeColumnsL1();
  144. double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
  145. confusionMatrixRestored.normalizeColumnsL1();
  146. double arrRestored ( confusionMatrixRestored.trace()/confusionMatrixRestored.cols() );
  147. CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrRestored, 1e-8);
  148. // don't waste memory
  149. //TODO clean up of training data, also in TestGPHIKPersistent
  150. delete classifier;
  151. delete classifierRestored;
  152. for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
  153. {
  154. delete *exTrainIt;
  155. }
  156. if (verboseStartEnd)
  157. std::cerr << "================== TestGPHIKPersistent::testPersistentMethods done ===================== " << std::endl;
  158. }
  159. #endif