toyExample.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @file toyExample.cpp
  3. * @brief Demo-Program to show how to call some methods of the GPHIKClassifier class
  4. * @author Alexander Freytag
  5. * @date 19-10-2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. #include <vector>
  10. // NICE-core includes
  11. #include <core/basics/Config.h>
  12. #include <core/basics/Timer.h>
  13. #include <core/vector/MatrixT.h>
  14. #include <core/vector/VectorT.h>
  15. // gp-hik-core includes
  16. #include "gp-hik-core/GPHIKClassifier.h"
  17. using namespace std; //C basics
  18. using namespace NICE; // nice-core
  19. int main (int argc, char* argv[])
  20. {
  21. Config conf ( argc, argv );
  22. std::string trainData = conf.gS( "main", "trainData", "progs/toyExampleSmallScaleTrain.data" );
  23. bool b_debug = conf.gB( "main", "debug", false );
  24. //------------- read the training data --------------
  25. NICE::Matrix dataTrain;
  26. NICE::Vector yBinTrain;
  27. NICE::Vector yMultiTrain;
  28. if ( b_debug )
  29. {
  30. dataTrain.resize(6,3);
  31. dataTrain.set(0);
  32. dataTrain(0,0) = 0.2; dataTrain(0,1) = 0.3; dataTrain(0,2) = 0.5;
  33. dataTrain(1,0) = 0.3; dataTrain(1,1) = 0.2; dataTrain(1,2) = 0.5;
  34. dataTrain(2,0) = 0.9; dataTrain(2,1) = 0.0; dataTrain(2,2) = 0.1;
  35. dataTrain(3,0) = 0.8; dataTrain(3,1) = 0.1; dataTrain(3,2) = 0.1;
  36. dataTrain(4,0) = 0.1; dataTrain(4,1) = 0.1; dataTrain(4,2) = 0.8;
  37. dataTrain(5,0) = 0.1; dataTrain(5,1) = 0.0; dataTrain(5,2) = 0.9;
  38. yMultiTrain.resize(6);
  39. yMultiTrain[0] = 1; yMultiTrain[1] = 1;
  40. yMultiTrain[2] = 2; yMultiTrain[3] = 2;
  41. yMultiTrain[4] = 3; yMultiTrain[5] = 3;
  42. }
  43. else
  44. {
  45. std::ifstream ifsTrain ( trainData.c_str() , ios::in );
  46. if (ifsTrain.good() )
  47. {
  48. ifsTrain >> dataTrain;
  49. ifsTrain >> yBinTrain;
  50. ifsTrain >> yMultiTrain;
  51. ifsTrain.close();
  52. }
  53. else
  54. {
  55. std::cerr << "Unable to read training data, aborting." << std::endl;
  56. return -1;
  57. }
  58. }
  59. //----------------- convert data to sparse data structures ---------
  60. std::vector< NICE::SparseVector *> examplesTrain;
  61. examplesTrain.resize( dataTrain.rows() );
  62. std::vector< NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
  63. for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
  64. {
  65. *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
  66. }
  67. std::cerr << "Number of training examples: " << examplesTrain.size() << std::endl;
  68. //----------------- train our classifier -------------
  69. // conf.sB("GPHIKClassifier", "verbose", false);
  70. GPHIKClassifier * classifier = new GPHIKClassifier ( &conf );
  71. classifier->train ( examplesTrain , yMultiTrain );
  72. // ------------------------------------------
  73. // ------------- CLASSIFICATION --------------
  74. // ------------------------------------------
  75. //------------- read the test data --------------
  76. NICE::Matrix dataTest;
  77. NICE::Vector yBinTest;
  78. NICE::Vector yMultiTest;
  79. if ( b_debug )
  80. {
  81. dataTest.resize(1,3);
  82. dataTest.set(0);
  83. dataTest(0,0) = 0.3; dataTest(0,1) = 0.4; dataTest(0,2) = 0.3;
  84. yMultiTest.resize(1);
  85. yMultiTest[0] = 1;
  86. }
  87. else
  88. {
  89. std::string testData = conf.gS( "main", "testData", "progs/toyExampleTest.data" );
  90. std::ifstream ifsTest ( testData.c_str(), ios::in );
  91. if (ifsTest.good() )
  92. {
  93. ifsTest >> dataTest;
  94. ifsTest >> yBinTest;
  95. ifsTest >> yMultiTest;
  96. ifsTest.close();
  97. }
  98. else
  99. {
  100. std::cerr << "Unable to read test data, aborting." << std::endl;
  101. return -1;
  102. }
  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. NICE::Matrix confusionMatrix( noClassesKnownTraining, noClassesKnownTest, 0.0);
  134. NICE::Timer t;
  135. double testTime (0.0);
  136. double uncertainty;
  137. int i_loopEnd ( (int)dataTest.rows() );
  138. for (int i = 0; i < i_loopEnd ; i++)
  139. {
  140. NICE::Vector example ( dataTest.getRow(i) );
  141. NICE::SparseVector scores;
  142. int result;
  143. // and classify
  144. t.start();
  145. classifier->classify( &example, result, scores );
  146. t.stop();
  147. testTime += t.getLast();
  148. std::cerr << " scores.size(): " << scores.size() << std::endl;
  149. scores.store(std::cerr);
  150. if ( b_debug )
  151. {
  152. classifier->predictUncertainty( &example, uncertainty );
  153. std::cerr << " uncertainty: " << uncertainty << std::endl;
  154. }
  155. confusionMatrix( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  156. }
  157. std::cerr << "Time for testing: " << testTime << std::endl;
  158. confusionMatrix.normalizeColumnsL1();
  159. std::cerr << confusionMatrix << std::endl;
  160. std::cerr << "average recognition rate: " << confusionMatrix.trace()/confusionMatrix.cols() << std::endl;
  161. return 0;
  162. }