TestFeatureMatrixT.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include <core/matlabAccess/MatFileIO.h>
  5. #include <gp-hik-core/tools.h>
  6. #include "TestFeatureMatrixT.h"
  7. const bool verbose = false;
  8. const bool verboseStartEnd = true;
  9. const uint n = 15;
  10. const uint d = 3;
  11. const double sparse_prob = 0.8;
  12. using namespace NICE;
  13. using namespace std;
  14. CPPUNIT_TEST_SUITE_REGISTRATION( TestFeatureMatrixT );
  15. void TestFeatureMatrixT::setUp() {
  16. }
  17. void TestFeatureMatrixT::tearDown() {
  18. }
  19. void TestFeatureMatrixT::testSetup()
  20. {
  21. if (verboseStartEnd)
  22. std::cerr << "================== TestFeatureMatrixT::testSetup ===================== " << std::endl;
  23. std::vector< std::vector<double> > dataMatrix;
  24. generateRandomFeatures ( d, n, dataMatrix );
  25. uint nrZeros(0);
  26. for ( uint i = 0 ; i < d; i++ )
  27. {
  28. for ( uint k = 0; k < n; k++ )
  29. if ( drand48() < sparse_prob )
  30. {
  31. dataMatrix[i][k] = 0.0;
  32. nrZeros++;
  33. }
  34. }
  35. if ( verbose ) {
  36. cerr << "data matrix: " << endl;
  37. printMatrix ( dataMatrix );
  38. cerr << endl;
  39. }
  40. transposeVectorOfVectors(dataMatrix);
  41. NICE::FeatureMatrixT<double> fm(dataMatrix);
  42. if ( (n*d)>0)
  43. {
  44. if (verbose)
  45. std::cerr << "fm.computeSparsityRatio(): " << fm.computeSparsityRatio() << " (double)nrZeros/(double)(n*d): " << (double)nrZeros/(double)(n*d) << std::endl;
  46. CPPUNIT_ASSERT_DOUBLES_EQUAL(fm.computeSparsityRatio(), (double)nrZeros/(double)(n*d), 1e-8);
  47. }
  48. transposeVectorOfVectors(dataMatrix);
  49. std::vector<std::vector<uint> > permutations;
  50. if (verbose)
  51. std::cerr << "now try to set_features" << std::endl;
  52. fm.set_features(dataMatrix, permutations);
  53. if ( (n*d)>0)
  54. {
  55. if (verbose)
  56. std::cerr << "fm.computeSparsityRatio(): " << fm.computeSparsityRatio() << " (double)nrZeros/(double)(n*d): " << (double)nrZeros/(double)(n*d) << std::endl;
  57. CPPUNIT_ASSERT_DOUBLES_EQUAL(fm.computeSparsityRatio(), (double)nrZeros/(double)(n*d), 1e-8);
  58. }
  59. NICE::MatrixT<double> matNICE;
  60. fm.computeNonSparseMatrix(matNICE);
  61. if (verbose)
  62. {
  63. std::cerr << "converted NICE-Matrix" << std::endl;
  64. std::cerr << matNICE << std::endl;
  65. }
  66. std::vector<std::vector<double> > matSTD;
  67. fm.computeNonSparseMatrix(matSTD);
  68. if (verbose)
  69. {
  70. std::cerr << "converted std-Matrix" << std::endl;
  71. printMatrix(matSTD);
  72. }
  73. if (verboseStartEnd)
  74. std::cerr << "================== TestFeatureMatrixT::testSetup done ===================== " << std::endl;
  75. }
  76. void TestFeatureMatrixT::testMatlabIO()
  77. {
  78. if (verboseStartEnd)
  79. std::cerr << "================== TestFeatureMatrixT::testMatlabIO ===================== " << std::endl;
  80. #ifndef NICE_USELIB_MATIO
  81. std::cerr << "MatIO library not included -- TestFeatureMatrixT::testMatlabIO not possible" << std::endl;
  82. #else //#ifdef NICE_USELIB_MATIO
  83. NICE::MatFileIO matfileIOA = MatFileIO("./sparse3x3matrixA.mat",MAT_ACC_RDONLY);
  84. sparse_t sparseA;
  85. matfileIOA.getSparseVariableViaName(sparseA,"A");
  86. NICE::FeatureMatrixT<double> fmA(sparseA);//, 3);
  87. if ( verbose )
  88. {
  89. fmA.print(std::cerr);
  90. }
  91. if (verbose)
  92. std::cerr << "fmA.get_n(): " << fmA.get_n() << " fmA.get_d(): " << fmA.get_d() << std::endl;
  93. NICE::MatFileIO matfileIOM = MatFileIO("./sparse20x30matrixM.mat",MAT_ACC_RDONLY);
  94. sparse_t sparseM;
  95. matfileIOM.getSparseVariableViaName(sparseM,"M");
  96. NICE::FeatureMatrixT<double> fmM(sparseM);//, 20);
  97. if ( verbose )
  98. {
  99. fmM.print(std::cerr);
  100. }
  101. if (verbose)
  102. std::cerr << "fmM.get_n(): " << fmM.get_n() << " fmM.get_d(): " << fmM.get_d() << std::endl;
  103. NICE::MatrixT<double> matNICE;
  104. fmM.computeNonSparseMatrix(matNICE, true);
  105. if (verbose)
  106. {
  107. std::cerr << "converted NICE-Matrix" << std::endl;
  108. std::cerr << matNICE << std::endl;
  109. }
  110. std::vector<std::vector<double> > matSTD;
  111. fmM.computeNonSparseMatrix(matSTD, true);
  112. if (verbose)
  113. {
  114. std::cerr << "converted std-Matrix" << std::endl;
  115. printMatrix(matSTD);
  116. }
  117. // std::string filename = "/home/dbv/bilder/imagenet/devkit-1.0/demo/demo.train.mat";
  118. // std::string dataMatrixMatlab = "training_instance_matrix";
  119. //
  120. // //tic
  121. // time_t readSparseMatlabMatrix_start = clock();
  122. //
  123. // std::cerr << "try to read " << filename << std::endl;
  124. // MatFileIO matfileIO = MatFileIO(filename,MAT_ACC_RDONLY);
  125. // std::cerr << "matfileIO successfully done"<< std::endl;
  126. //
  127. // sparse_t sparse;
  128. // matfileIO.getSparseVariableViaName(sparse,dataMatrixMatlab);
  129. //
  130. // //toc
  131. // float time_readSparseMatlabMatrix = (float) (clock() - readSparseMatlabMatrix_start);
  132. // std::cerr << "Time for reading the sparse Matlab Matrix: " << time_readSparseMatlabMatrix/CLOCKS_PER_SEC << " s" << std::endl;
  133. //
  134. // std::cerr << "sparse-struct read, now try to give it to our FeatureMatrixT" << std::endl;
  135. //
  136. // //tic
  137. // time_t readSparseIntoFM_start = clock();
  138. //
  139. // NICE::FeatureMatrixT<double> fm(sparse);
  140. //
  141. // //toc
  142. // float time_readSparseIntoFM = (float) (clock() - readSparseIntoFM_start);
  143. // std::cerr << "Time for parsing the sparse Matrix into our FeatureMatrixT-struct: " << time_readSparseIntoFM/CLOCKS_PER_SEC << " s" << std::endl;
  144. //
  145. //
  146. // std::cerr << "fm.get_n(): " << fm.get_n() << " fm.get_d(): " << fm.get_d() << std::endl;
  147. // std::cerr << "fm.computeSparsityRatio() of Imagenet: " << fm.computeSparsityRatio() << std::endl;
  148. #endif //#ifdef NICE_USELIB_MATIO
  149. if (verboseStartEnd)
  150. std::cerr << "================== TestFeatureMatrixT::testMatlabIO done ===================== " << std::endl;
  151. }
  152. #endif