TestGPHIKPersistent.cpp 7.8 KB

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