Преглед на файлове

index handling in matlab, unittest

Erik Rodner преди 9 години
родител
ревизия
74449cb4f2
променени са 5 файла, в които са добавени 63 реда и са изтрити 12 реда
  1. 2 2
      matlab/ConverterMatlabToNICE.cpp
  2. 4 4
      matlab/ConverterNICEToMatlab.cpp
  3. 3 3
      matlab/Makefile
  4. 11 3
      tests/TestFastHIK.cpp
  5. 43 0
      tests/TestGPHIKOnlineLearnable.cpp

+ 2 - 2
matlab/ConverterMatlabToNICE.cpp

@@ -115,7 +115,7 @@ NICE::SparseVector MatlabConversion::convertSparseVectorToNice(
         {
           //note: no complex data supported her
             double value ( pr[total++] );
-            if ( b_adaptIndexMtoC ) 
+            if ( b_adaptIndexMtoC )
                 svec.insert( std::pair<uint, double>( row+1,  value ) );
             else
                 svec.insert( std::pair<uint, double>( row,  value ) );
@@ -130,7 +130,7 @@ NICE::SparseVector MatlabConversion::convertSparseVectorToNice(
     {
         //note: no complex data supported her
         double value ( pr[total++] );
-        if ( b_adaptIndexMtoC ) 
+        if ( b_adaptIndexMtoC )
             svec.insert( std::pair<uint, double>( ir[colNonZero]+1, value  ) );
         else
             svec.insert( std::pair<uint, double>( ir[colNonZero], value  ) );

+ 4 - 4
matlab/ConverterNICEToMatlab.cpp

@@ -10,10 +10,10 @@ mxArray* MatlabConversion::convertSparseVectorFromNice( const NICE::SparseVector
 {
     mxArray * matlabSparseVec;
     
-    if ( b_adaptIndexCtoM ) 
-       matlabSparseVec = mxCreateSparse( niceSvec.getDim() -1 /*m*/, 1/*n*/, niceSvec.size() -1 /*nzmax*/, mxREAL);
+    if ( b_adaptIndexCtoM )
+        matlabSparseVec = mxCreateSparse( niceSvec.getDim()-1 /*m*/, 1/*n*/, niceSvec.size() /*nzmax*/, mxREAL);
     else
-      matlabSparseVec = mxCreateSparse( niceSvec.getDim() /*m*/, 1/*n*/, niceSvec.size() /*nzmax*/, mxREAL);
+        matlabSparseVec = mxCreateSparse( niceSvec.getDim() /*m*/, 1/*n*/, niceSvec.size() /*nzmax*/, mxREAL);
     
     
     // To make the returned sparse mxArray useful, you must initialize the pr, ir, jc, and (if it exists) pi arrays.    
@@ -36,7 +36,7 @@ mxArray* MatlabConversion::convertSparseVectorFromNice( const NICE::SparseVector
     for ( NICE::SparseVector::const_iterator myIt = niceSvec.begin(); myIt != niceSvec.end(); myIt++, cnt++ )
     {
         // set index
-        if ( b_adaptIndexCtoM ) 
+        if ( b_adaptIndexCtoM )
             ir[cnt] = myIt->first-1;
         else
             ir[cnt] = myIt->first;

+ 3 - 3
matlab/Makefile

@@ -1,5 +1,5 @@
-MEX=/home/matlab/7.14/bin/mex
-#MEX=/home/matlab/8.2/academic/bin/mex
+#MEX=/home/matlab/7.14/bin/mex
+MEX=/home/matlab/8.2/academic/bin/mex
 NICEFLAGS1=$(shell pkg-config libgp-hik-core --cflags --libs)
 NICEFLAGS=$(subst -fopenmp,,$(NICEFLAGS1))
 
@@ -15,4 +15,4 @@ regression:
 	${MEX} ${NICEFLAGS} -largeArrayDims GPHIKRegressionMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
 
 unittest:        
-	${MEX} ${NICEFLAGS} -largeArrayDims testMatlabConversionFunctionsMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp
+	${MEX} ${NICEFLAGS} -largeArrayDims testMatlabConversionFunctionsMex.cpp ConverterMatlabToNICE.cpp ConverterNICEToMatlab.cpp

+ 11 - 3
tests/TestFastHIK.cpp

@@ -25,8 +25,8 @@ const bool verbose = true;
 const bool verboseStartEnd = true;
 // this test seems to be broken
 const bool solveLinWithoutRand = false;
-const uint n = 500;//1500;//1500;//10;
-const uint d = 50;//200;//2;
+const uint n = 1500;//1500;//1500;//10;
+const uint d = 100;//200;//2;
 const uint numBins = 11;//1001;//1001;
 const uint solveLinMaxIterations = 1000;
 const double sparse_prob = 0.6;
@@ -218,6 +218,13 @@ void TestFastHIK::testKernelMultiplication()
   CPPUNIT_ASSERT_DOUBLES_EQUAL((alpha-alpha_slow).normL1(), 0.0, 1e-8);
   CPPUNIT_ASSERT_DOUBLES_EQUAL((alpha_raw-alpha_slow).normL1(), 0.0, 1e-8);
 
+
+  Vector gmk_diag;
+  Vector gmk_raw_diag;
+  gmk.getDiagonalElements(gmk_diag);
+  gmk_raw.getDiagonalElements(gmk_raw_diag);
+  CPPUNIT_ASSERT_DOUBLES_EQUAL((gmk_diag - gmk_raw_diag).normL1(), 0.0, 1e-8);
+
   // test the case, where we first transform and then use the multiply stuff
   NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.2 );
 
@@ -776,7 +783,7 @@ void TestFastHIK::testLinSolve()
     if ( verbose )
       std::cerr << "solveLin without randomization" << std::endl;
 
-    fmk.solveLin(y, alpha, q, pf, false, 30);
+    fmk.solveLin(y, alpha, q, pf, false, 30 /* max iterations */);
     Vector K_alpha;
     K_alpha.multiply(gK, alpha);
 
@@ -803,6 +810,7 @@ void TestFastHIK::testLinSolve()
   if ( verbose )
   {
     std::cerr << "now assert that K_alphaRandomized == y" << std::endl;
+    // TODO: normL1 sometimes delivered a negative number here
     std::cerr << "(K_alphaRandomized-y).normL1(): " << (K_alphaRandomized - y).normL1() << std::endl;
   }
 

+ 43 - 0
tests/TestGPHIKOnlineLearnable.cpp

@@ -184,6 +184,47 @@ void compareClassifierOutputs ( const NICE::GPHIKClassifier * classifier,
   }  
 }
 
+void compareClassifierOutputsRaw ( const NICE::GPHIKClassifier * classifier,
+                                const NICE::GPHIKRawClassifier * classifierRaw,
+                                const NICE::Matrix & data
+                              )
+{
+  int i_loopEnd  ( (int)data.rows() );
+
+  for (int i = 0; i < i_loopEnd ; i++)
+  {
+    NICE::Vector example ( data.getRow(i) );
+
+    NICE::SparseVector scores;
+    uint result;
+
+    // classify with incrementally trained classifier
+    classifier->classify( &example, result, scores );
+
+
+    NICE::SparseVector scoresRaw;
+    uint resultRaw;
+    NICE::SparseVector example_sparse(example);
+    classifierRaw->classify( &example_sparse, resultRaw, scoresRaw );
+
+
+    bool equal(true);
+    NICE::SparseVector::const_iterator itScores        = scores.begin();
+    NICE::SparseVector::const_iterator itScoresScratch = scoresRaw.begin();
+    for ( ; itScores != scores.end(); itScores++, itScoresScratch++)
+    {
+      if ( fabs( itScores->second - itScores->second ) > 10e-6)
+      {
+        std::cerr << " itScores->second: " << itScores->second << " itScores->second: " << itScores->second << std::endl;
+        equal = false;
+        break;
+      }
+    }
+
+    CPPUNIT_ASSERT_EQUAL ( equal, true );
+  }
+}
+
 void TestGPHIKOnlineLearnable::testOnlineLearningStartEmpty()
 {
   if (verboseStartEnd)
@@ -429,6 +470,8 @@ void TestGPHIKOnlineLearnable::testOnlineLearningOCCtoBinary()
   
   CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrScratch, 1e-8);
   CPPUNIT_ASSERT_DOUBLES_EQUAL( arrScratch, arrScratchRaw, 1e-8);
+
+  compareClassifierOutputsRaw(classifier, classifierScratchRaw, dataTest);
   
   // don't waste memory