TestGPHIKPersistent.cpp 7.7 KB

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