Browse Source

1-vs-all gphikraw stable and supported

Alexander Freytag 9 years ago
parent
commit
8201b1ec2d

+ 23 - 8
GPHIKRawClassifier.cpp

@@ -222,7 +222,7 @@ void GPHIKRawClassifier::initFromConfig(const Config *_conf,
   {
     int numBins = _conf->gI ( _confSection, "num_bins", 100 );
     if ( this->b_verbose )
-      std::cerr << "FMKGPHyperparameterOptimization: quantization initialized with " << numBins << " bins." << std::endl;
+      std::cerr << "GPHIKRawClassifier: quantization initialized with " << numBins << " bins." << std::endl;
 
 
     std::string s_quantType = _conf->gS( _confSection, "s_quantType", "1d-aequi-0-1" );
@@ -407,8 +407,11 @@ void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
   {
     uint class1 = *(this->knownClasses.begin());
     uint class2 = *(this->knownClasses.rbegin());
-    uint class_for_which_we_have_a_score = _scores.begin()->first;
-    uint class_for_which_we_dont_have_a_score = (class1 == class_for_which_we_have_a_score ? class2 : class1);
+
+    // since we erased the binary label vector corresponding to the smaller class number,
+    // we only have scores for the larger class number
+    uint class_for_which_we_have_a_score          = class2;
+    uint class_for_which_we_dont_have_a_score     = class1;
 
     _scores[class_for_which_we_dont_have_a_score] = - _scores[class_for_which_we_have_a_score];
 
@@ -532,8 +535,11 @@ void GPHIKRawClassifier::classify ( const NICE::SparseVector * _xstar,
   {
     uint class1 = *(this->knownClasses.begin());
     uint class2 = *(this->knownClasses.rbegin());
-    uint class_for_which_we_have_a_score = ( class1 < class2  ? class2 : class1 );
-    uint class_for_which_we_dont_have_a_score = ( class1 < class2  ? class1 : class2);
+
+    // since we erased the binary label vector corresponding to the smaller class number,
+    // we only have scores for the larger class number
+    uint class_for_which_we_have_a_score          = class2;
+    uint class_for_which_we_dont_have_a_score     = class1;
     
     _scores[class_for_which_we_dont_have_a_score] = - _scores[class_for_which_we_have_a_score];
 
@@ -610,9 +616,18 @@ void GPHIKRawClassifier::train ( const std::vector< const NICE::SparseVector *>
   // handle special binary case
   if ( knownClasses.size() == 2 )
   {
-    std::map<uint, NICE::Vector>::iterator it = binLabels.begin();
-    it++;
-    binLabels.erase( binLabels.begin(), it );
+      // we erase the binary label vector which corresponds to the smaller class number as positive class
+      uint clNoSmall = *(this->knownClasses.begin());
+      std::map<uint, NICE::Vector>::iterator it = binLabels.begin();
+      it++;
+      if ( binLabels.begin()->first == clNoSmall )
+      {
+        binLabels.erase( binLabels.begin(), it );
+      }
+      else
+      {
+        binLabels.erase( it, binLabels.end() );
+      }
   }
 
   this->train ( _examples, binLabels );

+ 18 - 0
matlab/ConverterMatlabToNICE.cpp

@@ -233,4 +233,22 @@ bool MatlabConversion::convertMatlabToBool( const mxArray *matlabBool )
   bool* ptr = (bool*) mxGetData( matlabBool );
   return ptr[0];
 }
+
+bool MatlabConversion::isSparseDataAMatrix( const mxArray *array_ptr )
+{
+     mwSize   i_numExamples, i_numDim;
+
+     // dimenions of the matrix -> feature dimension and number of examples
+     i_numExamples = mxGetM( array_ptr );
+     i_numDim = mxGetN( array_ptr );
+
+     if ( ( i_numExamples == 1) || ( i_numDim == 1) )
+     {
+         return false;
+     }
+     else
+     {
+         return true;
+     }
+ }
 #endif

+ 8 - 0
matlab/ConverterMatlabToNICE.h

@@ -92,6 +92,14 @@ namespace NICE {
      **/    
     bool convertMatlabToBool( const mxArray *matlabBool );
 
+    /**
+     * @brief Checks whether a given sparse data structure is a matrix (or a vector instead)
+     *
+     * @param array_ptr Sparse MxD Matlab matrix
+     * @return bool. false of either M or D equals to 1
+     **/
+    bool isSparseDataAMatrix( const mxArray *array_ptr );
+
 } //ns MatlabConversion
 
 }

+ 1 - 1
matlab/GPHIKRawClassifierMex.cpp

@@ -294,7 +294,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 
         if ( mxIsSparse( prhs[2] ) )
         {
-          if ( isMatrix )
+          if ( MatlabConversion::isSparseDataAMatrix( prhs[2] ) )
           {
             //----------------- conversion -------------
             std::vector< const NICE::SparseVector *> examplesTest;