TestGPHIKPersistent.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. const bool verbose = true;
  21. CPPUNIT_TEST_SUITE_REGISTRATION( TestGPHIKPersistent );
  22. void TestGPHIKPersistent::setUp() {
  23. }
  24. void TestGPHIKPersistent::tearDown() {
  25. }
  26. void TestGPHIKPersistent::testPersistentMethods()
  27. {
  28. if (verboseStartEnd)
  29. std::cerr << "================== TestGPHIKPersistent::testPersistentMethods ===================== " << std::endl;
  30. NICE::Config conf;
  31. std::string trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
  32. NICE::GPHIKClassifier * classifier;
  33. //------------- read the training data --------------
  34. NICE::Matrix dataTrain;
  35. NICE::Vector yBinTrain;
  36. NICE::Vector yMultiTrain;
  37. std::ifstream ifsTrain ( trainData.c_str() , ios::in );
  38. if ( ifsTrain.good() )
  39. {
  40. ifsTrain >> dataTrain;
  41. ifsTrain >> yBinTrain;
  42. ifsTrain >> yMultiTrain;
  43. ifsTrain.close();
  44. }
  45. else
  46. {
  47. std::cerr << "Unable to read training data from file " << trainData << " -- aborting." << std::endl;
  48. CPPUNIT_ASSERT ( ifsTrain.good() );
  49. }
  50. //----------------- convert data to sparse data structures ---------
  51. std::vector< const NICE::SparseVector *> examplesTrain;
  52. examplesTrain.resize( dataTrain.rows() );
  53. std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
  54. for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
  55. {
  56. *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
  57. }
  58. // TRAIN CLASSIFIER FROM SCRATCH
  59. classifier = new GPHIKClassifier ( &conf );
  60. yBinTrain *= 2;
  61. yBinTrain -= 1;
  62. yBinTrain *= -1;
  63. std::cerr << yBinTrain << std::endl;
  64. std::cerr << "train classifier with artifially disturbed labels" << std::endl;
  65. classifier->train ( examplesTrain , yBinTrain);//yMultiTrain );
  66. // TEST STORING ABILITIES
  67. std::string s_destination_save ( "myClassifier.txt" );
  68. std::filebuf fbOut;
  69. fbOut.open ( s_destination_save.c_str(), ios::out );
  70. std::ostream os (&fbOut);
  71. //
  72. classifier->store( os );
  73. //
  74. fbOut.close();
  75. // TEST RESTORING ABILITIES
  76. NICE::GPHIKClassifier * classifierRestored = new GPHIKClassifier();
  77. std::string s_destination_load ( "myClassifier.txt" );
  78. std::filebuf fbIn;
  79. fbIn.open ( s_destination_load.c_str(), ios::in );
  80. std::istream is (&fbIn);
  81. //
  82. classifierRestored->restore( is );
  83. //
  84. fbIn.close();
  85. // TEST both classifiers to produce equal results
  86. //------------- read the test data --------------
  87. NICE::Matrix dataTest;
  88. NICE::Vector yBinTest;
  89. NICE::Vector yMultiTest;
  90. std::string testData = conf.gS( "main", "testData", "toyExampleTest.data" );
  91. std::ifstream ifsTest ( testData.c_str(), ios::in );
  92. if ( ifsTest.good() )
  93. {
  94. ifsTest >> dataTest;
  95. ifsTest >> yBinTest;
  96. ifsTest >> yMultiTest;
  97. ifsTest.close();
  98. }
  99. else
  100. {
  101. std::cerr << "Unable to read test data, aborting." << std::endl;
  102. CPPUNIT_ASSERT ( ifsTest.good() );
  103. }
  104. // ------------------------------------------
  105. // ------------- PREPARATION --------------
  106. // ------------------------------------------
  107. // determine classes known during training and corresponding mapping
  108. // thereby allow for non-continous class labels
  109. std::set<int> classesKnownTraining = classifier->getKnownClassNumbers();
  110. int noClassesKnownTraining ( classesKnownTraining.size() );
  111. std::map<int,int> mapClNoToIdxTrain;
  112. std::set<int>::const_iterator clTrIt = classesKnownTraining.begin();
  113. for ( int i=0; i < noClassesKnownTraining; i++, clTrIt++ )
  114. mapClNoToIdxTrain.insert ( std::pair<int,int> ( *clTrIt, i ) );
  115. // determine classes known during testing and corresponding mapping
  116. // thereby allow for non-continous class labels
  117. std::set<int> classesKnownTest;
  118. classesKnownTest.clear();
  119. // determine which classes we have in our label vector
  120. // -> MATLAB: myClasses = unique(y);
  121. for ( NICE::Vector::const_iterator it = yMultiTest.begin(); it != yMultiTest.end(); it++ )
  122. {
  123. if ( classesKnownTest.find ( *it ) == classesKnownTest.end() )
  124. {
  125. classesKnownTest.insert ( *it );
  126. }
  127. }
  128. int noClassesKnownTest ( classesKnownTest.size() );
  129. std::map<int,int> mapClNoToIdxTest;
  130. std::set<int>::const_iterator clTestIt = classesKnownTest.begin();
  131. for ( int i=0; i < noClassesKnownTest; i++, clTestIt++ )
  132. mapClNoToIdxTest.insert ( std::pair<int,int> ( *clTestIt, i ) );
  133. if ( verbose )
  134. {
  135. std::cout << "Train data mapping: " << std::endl;
  136. for ( std::map<int,int>::const_iterator clTrainIt = mapClNoToIdxTrain.begin(); clTrainIt != mapClNoToIdxTrain.end(); clTrainIt++ )
  137. {
  138. std::cout << " " << clTrainIt->first << " " << clTrainIt->second << std::endl;
  139. }
  140. std::cout << "Test data mapping: " << std::endl;
  141. for ( std::map<int,int>::const_iterator clTestIt = mapClNoToIdxTest.begin(); clTestIt != mapClNoToIdxTest.end(); clTestIt++ )
  142. {
  143. std::cout << " " << clTestIt->first << " " << clTestIt->second << std::endl;
  144. }
  145. }
  146. NICE::Matrix confusionMatrix ( noClassesKnownTraining, noClassesKnownTest, 0.0);
  147. NICE::Matrix confusionMatrixRestored ( noClassesKnownTraining, noClassesKnownTest, 0.0);
  148. int i_loopEnd ( (int)dataTest.rows() );
  149. for (int i = 0; i < i_loopEnd ; i++)
  150. {
  151. NICE::Vector example ( dataTest.getRow(i) );
  152. NICE::SparseVector scores;
  153. int result;
  154. // classify with trained classifier
  155. classifier->classify( &example, result, scores );
  156. confusionMatrix( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  157. // classify with restored classifier
  158. scores.clear();
  159. classifierRestored->classify( &example, result, scores );
  160. confusionMatrixRestored( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  161. }
  162. confusionMatrix.normalizeColumnsL1();
  163. double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
  164. confusionMatrixRestored.normalizeColumnsL1();
  165. double arrRestored ( confusionMatrixRestored.trace()/confusionMatrixRestored.cols() );
  166. if ( verbose )
  167. {
  168. std::cout << "confusionMatrix: " << confusionMatrix << std::endl;
  169. std::cout << "confusionMatrixRestored: " << confusionMatrixRestored << std::endl;
  170. std::cout << "arr: " << arr << std::endl;
  171. std::cout << "arrRestored: " << arrRestored << std::endl;
  172. }
  173. CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrRestored, 1e-8);
  174. // don't waste memory
  175. //TODO clean up of training data, also in TestGPHIKPersistent
  176. delete classifier;
  177. delete classifierRestored;
  178. for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
  179. {
  180. delete *exTrainIt;
  181. }
  182. if (verboseStartEnd)
  183. std::cerr << "================== TestGPHIKPersistent::testPersistentMethods done ===================== " << std::endl;
  184. }
  185. #endif