Browse Source

added Matlab demo file for GPHIK, handling of variable setting nicer

Alexander Freytag 11 years ago
parent
commit
573b6a0a54
2 changed files with 178 additions and 101 deletions
  1. 119 101
      matlab/GPHIK.cpp
  2. 59 0
      matlab/testGPHIK.m

+ 119 - 101
matlab/GPHIK.cpp

@@ -1,7 +1,7 @@
 /** 
 /** 
 * @file GPHIK.cpp
 * @file GPHIK.cpp
 * @author Alexander Freytag
 * @author Alexander Freytag
-* @date 07-01-2014 (dd-m-yyyy)
+* @date 07-01-2014 (dd-mm-yyyy)
 * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and  storing/re-storing.
 * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and  storing/re-storing.
 */
 */
 
 
@@ -339,24 +339,124 @@ NICE::Config parseParameters(const mxArray *prhs[], int nrhs)
   for( int i=i_start; i < nrhs; i+=2 )
   for( int i=i_start; i < nrhs; i+=2 )
   {
   {
     std::string variable = convertMatlabToString(prhs[i]);
     std::string variable = convertMatlabToString(prhs[i]);
-    if(variable == "ils_verbose")
+    
+    /////////////////////////////////////////
+    // READ STANDARD BOOLEAN VARIABLES
+    /////////////////////////////////////////
+    if( (variable == "verboseTime") || (variable == "verbose") ||
+        (variable == "optimize_noise") || (variable == "uncertaintyPredictionForClassification") ||
+        (variable == "use_quantization") || (variable == "ils_verbose")
+      )
     {
     {
-      string value = convertMatlabToString(prhs[i+1]);
-      if(value != "true" && value != "false")
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'ils_verbose\'. \'true\' or \'false\' expected.");
-      if(value == "true")
-        conf.sB("GPHIKClassifier", variable, true);
+      if ( mxIsChar( prhs[i+1] ) )
+      {
+        string value = convertMatlabToString( prhs[i+1] );
+        if ( (value != "true") && (value != "false") )
+        {
+          std::string errorMsg = "Unexpected parameter value for \'" +  variable + "\'. In string modus, \'true\' or \'false\' expected.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
+        }
+        
+        if( value == "true" )
+          conf.sB("GPHIKClassifier", variable, true);
+        else
+          conf.sB("GPHIKClassifier", variable, false);
+      }
+      else if ( mxIsLogical( prhs[i+1] ) )
+      {
+        bool value = convertMatlabToBool( prhs[i+1] );
+        conf.sB("GPHIKClassifier", variable, value);
+      }
       else
       else
-        conf.sB("GPHIKClassifier", variable, false);
+      {
+          std::string errorMsg = "Unexpected parameter value for \'" +  variable + "\'. \'true\', \'false\', or logical expected.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );        
+      }
     }
     }
-
-    if(variable == "ils_max_iterations")
+    
+    /////////////////////////////////////////
+    // READ STANDARD INT VARIABLES
+    /////////////////////////////////////////
+    if ( (variable == "nrOfEigenvaluesToConsiderForVarApprox")
+       )
     {
     {
-      int value = convertMatlabToInt32(prhs[i+1]);
-      if(value < 1)
-        mexErrMsgIdAndTxt("mexnice:error","Expected parameter value larger than 0 for \'ils_max_iterations\'.");
-      conf.sI("GPHIKClassifier", variable, value);
+      if ( mxIsDouble( prhs[i+1] ) )
+      {
+        double value = convertMatlabToDouble(prhs[i+1]);
+        conf.sI("GPHIKClassifier", variable, (int) value);        
+      }
+      else if ( mxIsInt32( prhs[i+1] ) )
+      {
+        int value = convertMatlabToInt32(prhs[i+1]);
+        conf.sI("GPHIKClassifier", variable, value);          
+      }
+      else
+      {
+          std::string errorMsg = "Unexpected parameter value for \'" +  variable + "\'. Int32 or Double expected.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );         
+      }     
     }
     }
+    
+    /////////////////////////////////////////
+    // READ STRICT POSITIVE INT VARIABLES
+    /////////////////////////////////////////
+    if ( (variable == "num_bins") || (variable == "ils_max_iterations")
+       )
+    {
+      if ( mxIsDouble( prhs[i+1] ) )
+      {
+        double value = convertMatlabToDouble(prhs[i+1]);
+        if( value < 1 )
+        {
+          std::string errorMsg = "Expected parameter value larger than 0 for \'" +  variable + "\'.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );     
+        }
+        conf.sI("GPHIKClassifier", variable, (int) value);        
+      }
+      else if ( mxIsInt32( prhs[i+1] ) )
+      {
+        int value = convertMatlabToInt32(prhs[i+1]);
+        if( value < 1 )
+        {
+          std::string errorMsg = "Expected parameter value larger than 0 for \'" +  variable + "\'.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );     
+        }        
+        conf.sI("GPHIKClassifier", variable, value);          
+      }
+      else
+      {
+          std::string errorMsg = "Unexpected parameter value for \'" +  variable + "\'. Int32 or Double expected.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );         
+      }     
+    }
+    
+    /////////////////////////////////////////
+    // READ POSITIVE DOUBLE VARIABLES
+    /////////////////////////////////////////
+    if ( (variable == "ils_min_delta") || (variable == "ils_min_residual") ||
+         (variable == "noise")
+       )
+    {
+      if ( mxIsDouble( prhs[i+1] ) )
+      {
+        double value = convertMatlabToDouble(prhs[i+1]);
+        if( value < 0.0 )
+        {
+          std::string errorMsg = "Expected parameter value larger than 0 for \'" +  variable + "\'.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );     
+        }
+        conf.sD("GPHIKClassifier", variable, value);        
+      }
+      else
+      {
+          std::string errorMsg = "Unexpected parameter value for \'" +  variable + "\'. Double expected.";
+          mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );         
+      }     
+    }    
+    
+    /////////////////////////////////////////
+    // READ REMAINING SPECIFIC VARIABLES
+    /////////////////////////////////////////  
 
 
     if(variable == "ils_method")
     if(variable == "ils_method")
     {
     {
@@ -366,22 +466,6 @@ NICE::Config parseParameters(const mxArray *prhs[], int nrhs)
         conf.sS("GPHIKClassifier", variable, value);
         conf.sS("GPHIKClassifier", variable, value);
     }
     }
 
 
-    if(variable == "ils_min_delta")
-    {
-      double value = convertMatlabToDouble(prhs[i+1]);
-      if(value < 0.0)
-        mexErrMsgIdAndTxt("mexnice:error","Expected parameter value larger than 0 for \'ils_min_delta\'.");
-      conf.sD("GPHIKClassifier", variable, value);
-    }
-
-    if(variable == "ils_min_residual")
-    {
-      double value = convertMatlabToDouble(prhs[i+1]);
-      if(value < 0.0)
-        mexErrMsgIdAndTxt("mexnice:error","Expected parameter value larger than 0 for \'ils_min_residual\'.");
-      conf.sD("GPHIKClassifier", variable, value);
-    }
-
 
 
     if(variable == "optimization_method")
     if(variable == "optimization_method")
     {
     {
@@ -391,75 +475,15 @@ NICE::Config parseParameters(const mxArray *prhs[], int nrhs)
         conf.sS("GPHIKClassifier", variable, value);
         conf.sS("GPHIKClassifier", variable, value);
     }
     }
 
 
-    if(variable == "use_quantization")
-    {
-      string value = convertMatlabToString(prhs[i+1]);
-      if(value != "true" && value != "false")
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'use_quantization\'. \'true\' or \'false\' expected.");
-      if(value == "true")
-        conf.sB("GPHIKClassifier", variable, true);
-      else
-        conf.sB("GPHIKClassifier", variable, false);
-    }
-
-    if(variable == "num_bins")
-    {
-      int value = convertMatlabToInt32(prhs[i+1]);
-      if(value < 1)
-        mexErrMsgIdAndTxt("mexnice:error","Expected parameter value larger than 0 for \'num_bins\'.");
-      conf.sI("GPHIKClassifier", variable, value);
-    }
-
     if(variable == "transform")
     if(variable == "transform")
     {
     {
-      string value = convertMatlabToString(prhs[i+1]);
+      string value = convertMatlabToString( prhs[i+1] );
       if(value != "absexp" && value != "exp" && value != "MKL" && value != "WeightedDim")
       if(value != "absexp" && value != "exp" && value != "MKL" && value != "WeightedDim")
         mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'transform\'. \'absexp\', \'exp\' , \'MKL\' or \'WeightedDim\' expected.");
         mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'transform\'. \'absexp\', \'exp\' , \'MKL\' or \'WeightedDim\' expected.");
         conf.sS("GPHIKClassifier", variable, value);
         conf.sS("GPHIKClassifier", variable, value);
     }
     }
 
 
-    if(variable == "verboseTime")
-    {
-      string value = convertMatlabToString(prhs[i+1]);
-      if(value != "true" && value != "false")
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'verboseTime\'. \'true\' or \'false\' expected.");
-      if(value == "true")
-        conf.sB("GPHIKClassifier", variable, true);
-      else
-        conf.sB("GPHIKClassifier", variable, false);
-    }
-
-    if(variable == "verbose")
-    {
-      string value = convertMatlabToString(prhs[i+1]);
-      if(value != "true" && value != "false")
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'verbose\'. \'true\' or \'false\' expected.");
-      if(value == "true")
-        conf.sB("GPHIKClassifier", variable, true);
-      else
-        conf.sB("GPHIKClassifier", variable, false);
-    }
-
-    if(variable == "noise")
-    {
-      double value = convertMatlabToDouble(prhs[i+1]);
-      if(value < 0.0)
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value larger than 0 for \'noise\'.");
-      conf.sD("GPHIKClassifier", variable, value);
-    }
-
-
-    if(variable == "optimize_noise")
-    {
-      string value = convertMatlabToString(prhs[i+1]);
-      if(value != "true" && value != "false")
-        mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'optimize_noise\'. \'true\' or \'false\' expected.");
-      if(value == "true")
-        conf.sB("GPHIKClassifier", variable, true);
-      else
-        conf.sB("GPHIKClassifier", variable, false);
-    }
-    
+  
     if(variable == "varianceApproximation")
     if(variable == "varianceApproximation")
     {
     {
       string value = convertMatlabToString(prhs[i+1]);
       string value = convertMatlabToString(prhs[i+1]);
@@ -468,11 +492,7 @@ NICE::Config parseParameters(const mxArray *prhs[], int nrhs)
         conf.sS("GPHIKClassifier", variable, value);
         conf.sS("GPHIKClassifier", variable, value);
     }
     }
     
     
-    if(variable == "nrOfEigenvaluesToConsiderForVarApprox")
-    {
-      double value = convertMatlabToDouble(prhs[i+1]);
-      conf.sI("GPHIKClassifier", variable, (int) value);
-    }    
+
     
     
   }
   }
 
 
@@ -504,7 +524,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         NICE::Config conf = parseParameters(prhs+1,nrhs-1);
         NICE::Config conf = parseParameters(prhs+1,nrhs-1);
         
         
         // create class instance
         // create class instance
-        NICE::GPHIKClassifier * classifier = new NICE::GPHIKClassifier ( &conf );
+        NICE::GPHIKClassifier * classifier = new NICE::GPHIKClassifier ( &conf, "GPHIKClassifier" /*sectionName in config*/ );
         
         
          
          
         // handle to the C++ instance
         // handle to the C++ instance
@@ -806,10 +826,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 
 
 
 
         confusionMatrix.normalizeColumnsL1();
         confusionMatrix.normalizeColumnsL1();
-        //std::cerr << confusionMatrix << std::endl;
 
 
-        double recRate = confusionMatrix.trace()/confusionMatrix.rows();
-        //std::cerr << "average recognition rate: " << recRate << std::endl;
+        double recRate = confusionMatrix.trace()/confusionMatrix.cols();
 
 
         
         
         plhs[0] = mxCreateDoubleScalar( recRate );
         plhs[0] = mxCreateDoubleScalar( recRate );

+ 59 - 0
matlab/testGPHIK.m

@@ -0,0 +1,59 @@
+% brief:    Demo-program showing how to use the GPHIKClassifier Interface
+% author:   Alexander Freytag
+% date:     07-01-2014 (dd-mm-yyyy)
+
+myData = [ 0.2 0.3 0.5;
+           0.3 0.2 0.5;
+           0.9 0.0 0.1;
+           0.8 0.1 0.1;
+           0.1 0.1 0.8;
+           0.1 0.0 0.9
+          ];
+myLabels = [1,1,2,2,3,3];
+
+
+% init new GPHIKClassifier object
+myGPHIKClassifier = GPHIK ( 'new', 'verbose', 'false', ...
+    'optimization_method', 'none', 'varianceApproximation', 'approximate_rough',...
+    'nrOfEigenvaluesToConsiderForVarApprox',4,...
+    'uncertaintyPredictionForClassification', false ...
+    );
+
+% run train method
+GPHIK ( 'train', myGPHIKClassifier, myData, myLabels);
+
+myDataTest = [ 0.3 0.4 0.3
+             ];
+myLabelsTest = [1];
+
+% run single classification call
+[ classNoEst, score, uncertainty] = GPHIK ( 'classify', myGPHIKClassifier, myDataTest )
+% compute predictive variance
+uncertainty = GPHIK ( 'uncertainty', myGPHIKClassifier, myDataTest )
+% run test method evaluating arr potentially using multiple examples
+[ arr, confMat, scores] = GPHIK ( 'test', myGPHIKClassifier, myDataTest, myLabelsTest )
+
+% add a single new example
+newExample = [ 0.5 0.5 0.0
+             ];
+newLabel = [4];
+GPHIK ( 'addExample', myGPHIKClassifier, newExample, newLabel);
+
+% add mutiple new examples
+newExamples = [ 0.3 0.3 0.4;
+                0.1, 0.2, 0.7
+             ];
+newLabels = [1,3];
+GPHIK ( 'addMultipleExamples', myGPHIKClassifier, newExamples, newLabels );
+
+% perform evaluation again
+
+% run single classification call
+[ classNoEst, score, uncertainty] = GPHIK ( 'classify', myGPHIKClassifier, myDataTest )
+% compute predictive variance
+uncertainty = GPHIK ( 'uncertainty', myGPHIKClassifier, myDataTest )
+% run test method evaluating arr potentially using multiple examples
+[ arr, confMat, scores] = GPHIK ( 'test', myGPHIKClassifier, myDataTest, myLabelsTest )
+
+% clean up and delete object
+GPHIK ( 'delete',myGPHIKClassifier);