#ifdef NICE_USELIB_CPPUNIT #include #include #include "TestGMM.h" #include #include "vislearning/math/distances/genericDistance.h" const bool verboseStartEnd = true; const bool verbose = false; const std::string distanceType = "euclidean"; using namespace OBJREC; using namespace NICE; using namespace std; CPPUNIT_TEST_SUITE_REGISTRATION( TestGMM ); void TestGMM::setUp() { } void TestGMM::tearDown() { } void TestGMM::testGMMClustering() { if (verboseStartEnd) std::cerr << "================== TestGMM::testGMMClustering ===================== " << std::endl; Config * conf = new Config; std::string confSection ( "GMM" ); conf->sI( confSection, "i_numOfGaussians", 2 ); conf->sI( confSection, "maxIterations", 200 ); OBJREC::GMM gmm ( conf, confSection ); //create some artificial data NICE::VVector features; NICE::Vector x1 (2); x1[0] = 1; x1[1] = 1; features.push_back(x1); NICE::Vector x2 (2); x2[0] = 4; x2[1] = 1; features.push_back(x2); NICE::Vector x3 (2); x3[0] = 2; x3[1] = 4; features.push_back(x3); NICE::Vector x4 (2); x4[0] = 10; x4[1] = 3; features.push_back(x4); NICE::Vector x5 (2); x5[0] = 8; x5[1] = 3; features.push_back(x5); NICE::Vector x6 (2); x6[0] = 4; x6[1] = 3; features.push_back(x6); NICE::Vector x7 (2); x7[0] = 3; x7[1] = 2; features.push_back(x7); NICE::Vector x8 (2); x8[0] = 1; x8[1] = 3; features.push_back(x8); NICE::Vector x9 (2); x9[0] = 9; x9[1] = 2; features.push_back(x9); //cluster data NICE::VVector prototypes; std::vector weights; std::vector assignment; gmm.cluster ( features, prototypes, weights, assignment ); //check whether the results fits the ground truth //NOTE // adapted from TestKMeadian: // If no random initialization is activated, we initially grab x2 and x8. // After 3 iterations, we should have converged and obtain x5 and x7. // //TODO define proper test example for a GMM! // // NICE::VectorDistance * distancefunction = GenericDistanceSelection::selectDistance(distanceType); // // if ( verbose ) // { std::cerr << " x9: " << x9 << " cl1: " << prototypes[0] << std::endl; std::cerr << " x7: " << x7 << " cl2: " << prototypes[1] << std::endl; // } // // double distX9Cl1 ( distancefunction->calculate( x9, prototypes[0] ) ); // double distX7Cl2 ( distancefunction->calculate( x7, prototypes[1] ) ); // // CPPUNIT_ASSERT_DOUBLES_EQUAL( distX9Cl1, 0.0, 1e-8); // CPPUNIT_ASSERT_DOUBLES_EQUAL( distX7Cl2, 0.0, 1e-8); // // std::cerr << " successfull " << std::endl; //don't waste memory delete conf; CPPUNIT_ASSERT(true); if (verboseStartEnd) std::cerr << "================== TestGMM::testGMMClustering done ===================== " << std::endl; } void TestGMM::testGMMPersistent() { if (verboseStartEnd) std::cerr << "================== TestGMM::testGMMPersistent ===================== " << std::endl; Config * conf = new Config; std::string confSection ( "GMM" ); conf->sI( confSection, "i_numOfGaussians", 2 ); conf->sI( confSection, "maxIterations", 200 ); OBJREC::GMM gmm; gmm.initFromConfig( conf, confSection ); // TEST STORING ABILITIES if ( verbose ) std::cerr << " TEST STORING ABILITIES FOR KMEDIAN" << std::endl; std::string s_destination_save ( "myGMM.txt" ); std::filebuf fbOut; fbOut.open ( s_destination_save.c_str(), ios::out ); std::ostream os (&fbOut); // gmm.store( os ); // fbOut.close(); // TEST RESTORING ABILITIES if ( verbose ) std::cerr << " TEST RESTORING ABILITIES FOR KMEDIAN" << std::endl; OBJREC::GMM gmmRestore; std::string s_destination_load ( "myGMM.txt" ); std::filebuf fbIn; fbIn.open ( s_destination_load.c_str(), ios::in ); std::istream is (&fbIn); // gmmRestore.restore( is ); // fbIn.close(); // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e., // it returned an object identical to the stored one. // However, if we reached this point, at least something went right, so we should be happy... //don't waste memory delete conf; CPPUNIT_ASSERT(true); if (verboseStartEnd) std::cerr << "================== TestGMM::testGMMPersistent done ===================== " << std::endl; } #endif