TestVectorFeature.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include <iostream>
  5. #include <fstream>
  6. //----------
  7. #include "TestVectorFeature.h"
  8. #include "vislearning/cbaselib/FeaturePool.h"
  9. #include "../VectorFeature.h"
  10. const bool verbose = true;
  11. const bool verboseStartEnd = true;
  12. using namespace OBJREC;
  13. using namespace NICE;
  14. using namespace std;
  15. CPPUNIT_TEST_SUITE_REGISTRATION( TestVectorFeature );
  16. void TestVectorFeature::setUp() {
  17. }
  18. void TestVectorFeature::tearDown() {
  19. }
  20. void TestVectorFeature::testVectorFeature()
  21. {
  22. if (verboseStartEnd)
  23. std::cerr << "================== TestVectorFeature::testVectorFeature ===================== " << std::endl;
  24. Matrix mX;
  25. Vector vY;
  26. Vector vY_multi;
  27. ifstream ifs ("toyExample1.data", ios::in);
  28. // ifstream ifs ("toyExampleLargeScale.data", ios::in);
  29. // ifstream ifs ("toyExampleLargeLargeScale.data", ios::in);
  30. CPPUNIT_ASSERT ( ifs.good() );
  31. ifs >> mX;
  32. ifs >> vY;
  33. ifs >> vY_multi;
  34. ifs.close();
  35. if (verbose)
  36. {
  37. std::cerr << "data loaded: mX" << std::endl;
  38. std::cerr << mX << std::endl;
  39. std::cerr << "vY: " << std::endl;
  40. std::cerr << vY << std::endl;
  41. std::cerr << "vY_multi: " << std::endl;
  42. std::cerr << vY_multi << std::endl;
  43. }
  44. int iNumFeatureDimension = mX.cols();
  45. FeaturePool fp;
  46. VectorFeature *pVecFeature = new VectorFeature(iNumFeatureDimension);
  47. pVecFeature->explode(fp);
  48. // memory layout needs to be transposed into rows x column: features x samples
  49. // features must lay next to each other in memory, so that each feature vector can
  50. // be adressed by a starting pointer and the number of feature dimensions to come.
  51. Matrix mX_transposed = mX.transpose();
  52. Examples examples;
  53. bool bSuccess = Examples::wrapExamplesAroundFeatureMatrix(mX_transposed, vY, examples);
  54. CPPUNIT_ASSERT( bSuccess );
  55. CPPUNIT_ASSERT( examples.size() == mX.rows() );
  56. for(int i=0; i< examples.size(); i++)
  57. {
  58. Example &t_Example = examples[i].second;
  59. NICE::Vector t_FeatVector;
  60. fp.calcFeatureVector(t_Example, t_FeatVector);
  61. std::cerr << "Example " << i << " Features: " <<t_FeatVector << std::endl;
  62. for(int f=0; f< iNumFeatureDimension;f++)
  63. {
  64. double t_f1 = t_FeatVector[f];
  65. double t_f2 = mX(i,f);
  66. CPPUNIT_ASSERT_DOUBLES_EQUAL( t_f1, t_f2, 0.001f );
  67. }
  68. }
  69. examples.clean();
  70. delete pVecFeature;
  71. if (verboseStartEnd)
  72. std::cerr << "================== TestVectorFeature::TestVectorFeature done ===================== " << std::endl;
  73. }
  74. #endif