TestFeatureMatrixT.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. int 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<int> > 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. NICE::MatFileIO matfileIOA = MatFileIO("./sparse3x3matrixA.mat",MAT_ACC_RDONLY);
  81. sparse_t sparseA;
  82. matfileIOA.getSparseVariableViaName(sparseA,"A");
  83. NICE::FeatureMatrixT<double> fmA(sparseA);//, 3);
  84. if ( verbose )
  85. {
  86. fmA.print(std::cerr);
  87. }
  88. if (verbose)
  89. std::cerr << "fmA.get_n(): " << fmA.get_n() << " fmA.get_d(): " << fmA.get_d() << std::endl;
  90. NICE::MatFileIO matfileIOM = MatFileIO("./sparse20x30matrixM.mat",MAT_ACC_RDONLY);
  91. sparse_t sparseM;
  92. matfileIOM.getSparseVariableViaName(sparseM,"M");
  93. NICE::FeatureMatrixT<double> fmM(sparseM);//, 20);
  94. if ( verbose )
  95. {
  96. fmM.print(std::cerr);
  97. }
  98. if (verbose)
  99. std::cerr << "fmM.get_n(): " << fmM.get_n() << " fmM.get_d(): " << fmM.get_d() << std::endl;
  100. NICE::MatrixT<double> matNICE;
  101. fmM.computeNonSparseMatrix(matNICE, true);
  102. if (verbose)
  103. {
  104. std::cerr << "converted NICE-Matrix" << std::endl;
  105. std::cerr << matNICE << std::endl;
  106. }
  107. std::vector<std::vector<double> > matSTD;
  108. fmM.computeNonSparseMatrix(matSTD, true);
  109. if (verbose)
  110. {
  111. std::cerr << "converted std-Matrix" << std::endl;
  112. printMatrix(matSTD);
  113. }
  114. // std::string filename = "/home/dbv/bilder/imagenet/devkit-1.0/demo/demo.train.mat";
  115. // std::string dataMatrixMatlab = "training_instance_matrix";
  116. //
  117. // //tic
  118. // time_t readSparseMatlabMatrix_start = clock();
  119. //
  120. // std::cerr << "try to read " << filename << std::endl;
  121. // MatFileIO matfileIO = MatFileIO(filename,MAT_ACC_RDONLY);
  122. // std::cerr << "matfileIO successfully done"<< std::endl;
  123. //
  124. // sparse_t sparse;
  125. // matfileIO.getSparseVariableViaName(sparse,dataMatrixMatlab);
  126. //
  127. // //toc
  128. // float time_readSparseMatlabMatrix = (float) (clock() - readSparseMatlabMatrix_start);
  129. // std::cerr << "Time for reading the sparse Matlab Matrix: " << time_readSparseMatlabMatrix/CLOCKS_PER_SEC << " s" << std::endl;
  130. //
  131. // std::cerr << "sparse-struct read, now try to give it to our FeatureMatrixT" << std::endl;
  132. //
  133. // //tic
  134. // time_t readSparseIntoFM_start = clock();
  135. //
  136. // NICE::FeatureMatrixT<double> fm(sparse);
  137. //
  138. // //toc
  139. // float time_readSparseIntoFM = (float) (clock() - readSparseIntoFM_start);
  140. // std::cerr << "Time for parsing the sparse Matrix into our FeatureMatrixT-struct: " << time_readSparseIntoFM/CLOCKS_PER_SEC << " s" << std::endl;
  141. //
  142. //
  143. // std::cerr << "fm.get_n(): " << fm.get_n() << " fm.get_d(): " << fm.get_d() << std::endl;
  144. // std::cerr << "fm.computeSparsityRatio() of Imagenet: " << fm.computeSparsityRatio() << std::endl;
  145. if (verboseStartEnd)
  146. std::cerr << "================== TestFeatureMatrixT::testMatlabIO done===================== " << std::endl;
  147. }
  148. #endif