TestGPHIKPersistent.cpp 7.5 KB

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