TestGMM.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include "TestGMM.h"
  5. #include <core/basics/Config.h>
  6. #include "vislearning/math/distances/genericDistance.h"
  7. const bool verboseStartEnd = true;
  8. const bool verbose = false;
  9. const std::string distanceType = "euclidean";
  10. using namespace OBJREC;
  11. using namespace NICE;
  12. using namespace std;
  13. CPPUNIT_TEST_SUITE_REGISTRATION( TestGMM );
  14. void TestGMM::setUp() {
  15. }
  16. void TestGMM::tearDown() {
  17. }
  18. void TestGMM::testGMMClustering()
  19. {
  20. if (verboseStartEnd)
  21. std::cerr << "================== TestGMM::testGMMClustering ===================== " << std::endl;
  22. Config * conf = new Config;
  23. std::string confSection ( "GMM" );
  24. conf->sI( confSection, "i_numOfGaussians", 2 );
  25. conf->sI( confSection, "maxIterations", 200 );
  26. OBJREC::GMM gmm ( conf, confSection );
  27. //create some artificial data
  28. NICE::VVector features;
  29. NICE::Vector x1 (2); x1[0] = 1; x1[1] = 1; features.push_back(x1);
  30. NICE::Vector x2 (2); x2[0] = 4; x2[1] = 1; features.push_back(x2);
  31. NICE::Vector x3 (2); x3[0] = 2; x3[1] = 4; features.push_back(x3);
  32. NICE::Vector x4 (2); x4[0] = 10; x4[1] = 3; features.push_back(x4);
  33. NICE::Vector x5 (2); x5[0] = 8; x5[1] = 3; features.push_back(x5);
  34. NICE::Vector x6 (2); x6[0] = 4; x6[1] = 3; features.push_back(x6);
  35. NICE::Vector x7 (2); x7[0] = 3; x7[1] = 2; features.push_back(x7);
  36. NICE::Vector x8 (2); x8[0] = 1; x8[1] = 3; features.push_back(x8);
  37. NICE::Vector x9 (2); x9[0] = 9; x9[1] = 2; features.push_back(x9);
  38. //cluster data
  39. NICE::VVector prototypes;
  40. std::vector<double> weights;
  41. std::vector<int> assignment;
  42. gmm.cluster ( features, prototypes, weights, assignment );
  43. //check whether the results fits the ground truth
  44. //NOTE
  45. // adapted from TestKMeadian:
  46. // If no random initialization is activated, we initially grab x2 and x8.
  47. // After 3 iterations, we should have converged and obtain x5 and x7.
  48. //
  49. //TODO define proper test example for a GMM!
  50. //
  51. // NICE::VectorDistance<double> * distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  52. //
  53. // if ( verbose )
  54. // {
  55. std::cerr << " x9: " << x9 << " cl1: " << prototypes[0] << std::endl;
  56. std::cerr << " x7: " << x7 << " cl2: " << prototypes[1] << std::endl;
  57. // }
  58. //
  59. // double distX9Cl1 ( distancefunction->calculate( x9, prototypes[0] ) );
  60. // double distX7Cl2 ( distancefunction->calculate( x7, prototypes[1] ) );
  61. //
  62. // CPPUNIT_ASSERT_DOUBLES_EQUAL( distX9Cl1, 0.0, 1e-8);
  63. // CPPUNIT_ASSERT_DOUBLES_EQUAL( distX7Cl2, 0.0, 1e-8);
  64. //
  65. // std::cerr << " successfull " << std::endl;
  66. //don't waste memory
  67. delete conf;
  68. CPPUNIT_ASSERT(true);
  69. if (verboseStartEnd)
  70. std::cerr << "================== TestGMM::testGMMClustering done ===================== " << std::endl;
  71. }
  72. void TestGMM::testGMMPersistent()
  73. {
  74. if (verboseStartEnd)
  75. std::cerr << "================== TestGMM::testGMMPersistent ===================== " << std::endl;
  76. Config * conf = new Config;
  77. std::string confSection ( "GMM" );
  78. conf->sI( confSection, "i_numOfGaussians", 2 );
  79. conf->sI( confSection, "maxIterations", 200 );
  80. OBJREC::GMM gmm;
  81. gmm.initFromConfig( conf, confSection );
  82. // TEST STORING ABILITIES
  83. if ( verbose )
  84. std::cerr << " TEST STORING ABILITIES FOR KMEDIAN" << std::endl;
  85. std::string s_destination_save ( "myGMM.txt" );
  86. std::filebuf fbOut;
  87. fbOut.open ( s_destination_save.c_str(), ios::out );
  88. std::ostream os (&fbOut);
  89. //
  90. gmm.store( os );
  91. //
  92. fbOut.close();
  93. // TEST RESTORING ABILITIES
  94. if ( verbose )
  95. std::cerr << " TEST RESTORING ABILITIES FOR KMEDIAN" << std::endl;
  96. OBJREC::GMM gmmRestore;
  97. std::string s_destination_load ( "myGMM.txt" );
  98. std::filebuf fbIn;
  99. fbIn.open ( s_destination_load.c_str(), ios::in );
  100. std::istream is (&fbIn);
  101. //
  102. gmmRestore.restore( is );
  103. //
  104. fbIn.close();
  105. // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e.,
  106. // it returned an object identical to the stored one.
  107. // However, if we reached this point, at least something went right, so we should be happy...
  108. //don't waste memory
  109. delete conf;
  110. CPPUNIT_ASSERT(true);
  111. if (verboseStartEnd)
  112. std::cerr << "================== TestGMM::testGMMPersistent done ===================== " << std::endl;
  113. }
  114. #endif