ソースを参照

added OOP like class structure for GPHIKClassifier

Alexander Freytag 11 年 前
コミット
360a93946f
5 ファイル変更139 行追加6 行削除
  1. 3 3
      matlab/GPHIK.cpp
  2. 73 0
      matlab/GPHIKClassifier.m
  3. 3 2
      matlab/classHandleMtoC.h
  4. 1 1
      matlab/testGPHIK.m
  5. 59 0
      matlab/testGPHIKClassifier.m

+ 3 - 3
matlab/GPHIK.cpp

@@ -693,7 +693,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
     }    
     
     
-    // Test    
+    // Test - evaluate classifier on whole test set  
     if ( !strcmp("test", cmd.c_str() ) )
     {        
         // Check parameters
@@ -894,7 +894,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
         return;
     }
     
-    // addExample    
+    // addMultipleExamples    
     if ( !strcmp("addMultipleExamples", cmd.c_str() ) )
     {
         // Check parameters
@@ -963,7 +963,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
     
   
     
-    // store the classifier    
+    // store the classifier  to an external file
     if ( !strcmp("store", cmd.c_str() ) || !strcmp("save", cmd.c_str() ) )
     {
         // Check parameters

+ 73 - 0
matlab/GPHIKClassifier.m

@@ -0,0 +1,73 @@
+% brief:    MATLAB class wrapper for the underlying Matlab-C++ Interface (GPHIK.cpp)
+% author:   Alexander Freytag
+% date:     07-01-2014 (dd-mm-yyyy)
+classdef GPHIKClassifier < handle
+    
+    properties (SetAccess = private, Hidden = true)
+        % Handle to the underlying C++ class instance
+        objectHandle; 
+    end
+    
+    methods
+        
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %%      Constructor / Destructor    %%
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
+        %% constructor - create object
+        function this = GPHIKClassifier(varargin)
+            this.objectHandle = GPHIK('new', varargin{:});
+        end
+        
+        %% destructor - delete object
+        function delete(this)
+            GPHIK('delete', this.objectHandle);
+        end
+
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %%       Classification stuff       %%
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
+        %% train - standard train - assumes initialized object
+        function varargout = train(this, varargin)
+            [varargout{1:nargout}] = GPHIK('train', this.objectHandle, varargin{:});
+        end
+        
+        %% classify
+        function varargout = classify(this, varargin)
+            [varargout{1:nargout}] = GPHIK('classify', this.objectHandle, varargin{:});
+        end 
+        
+        %% uncertainty - Uncertainty prediction
+        function varargout = uncertainty(this, varargin)
+            [varargout{1:nargout}] = GPHIK('uncertainty', this.objectHandle, varargin{:});
+        end        
+
+        %% test - evaluate classifier on whole test set
+        function varargout = test(this, varargin)
+            [varargout{1:nargout}] = GPHIK('test', this.objectHandle, varargin{:});
+        end
+        
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %%       Online Learnable methods   %%
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %% addExample
+        function varargout = addExample(this, varargin)
+            [varargout{1:nargout}] = GPHIK('addExample', this.objectHandle, varargin{:});
+        end 
+        %% addMultipleExamples
+        function varargout = addMultipleExamples(this, varargin)
+            [varargout{1:nargout}] = GPHIK('addMultipleExamples', this.objectHandle, varargin{:});
+        end
+        
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %%       Persistent methods         %%
+        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+        %% store - store the classifier to an external file
+        function varargout = store(this, varargin)
+            [varargout{1:nargout}] = GPHIK('store', this.objectHandle, varargin{:});
+        end
+        %% restore -  load classifier from external file 
+        function varargout = restore(this, varargin)
+            [varargout{1:nargout}] = GPHIK('restore', this.objectHandle, varargin{:});
+        end
+    end
+end

+ 3 - 2
matlab/classHandleMtoC.h

@@ -8,7 +8,8 @@
 #ifndef _NICE_CLASSHANDLEMTOCINCLUDE
 #define _NICE_CLASSHANDLEMTOCINCLUDE
 
-#include "mex.h"
+// STL includes
+#include <mex.h>
 #include <stdint.h>
 #include <iostream>
 #include <string>
@@ -18,7 +19,7 @@
 #define CLASS_HANDLE_SIGNATURE 0xFF00F0A3
 
   /** 
-  * @class FMKGPHyperparameterOptimization
+  * @class ClassHandle
   * @brief Generic class to pass C++ objects to matlab
   * @author Alexander Freytag
   */

+ 1 - 1
matlab/testGPHIK.m

@@ -1,4 +1,4 @@
-% brief:    Demo-program showing how to use the GPHIKClassifier Interface
+% brief:    Demo-program showing how to use the GPHIK Interface (without a class wrapper)
 % author:   Alexander Freytag
 % date:     07-01-2014 (dd-mm-yyyy)
 

+ 59 - 0
matlab/testGPHIKClassifier.m

@@ -0,0 +1,59 @@
+% brief:    Demo-program showing how to use the GPHIKClassifier Interface (including the class wrapper)
+% 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 = GPHIKClassifier ( 'verbose', 'false', ...
+    'optimization_method', 'none', 'varianceApproximation', 'approximate_rough',...
+    'nrOfEigenvaluesToConsiderForVarApprox',4,...
+    'uncertaintyPredictionForClassification', false ...
+    );
+
+% run train method
+myGPHIKClassifier.train( myData, myLabels );
+
+myDataTest = [ 0.3 0.4 0.3
+             ];
+myLabelsTest = [1];
+
+% run single classification call
+[ classNoEst, score, uncertainty] = myGPHIKClassifier.classify( myDataTest )
+% compute predictive variance
+uncertainty = myGPHIKClassifier.uncertainty( myDataTest )
+% run test method evaluating arr potentially using multiple examples
+[ arr, confMat, scores] = myGPHIKClassifier.test( myDataTest, myLabelsTest )
+
+% add a single new example
+newExample = [ 0.5 0.5 0.0
+             ];
+newLabel = [4];
+myGPHIKClassifier.addExample( newExample, newLabel);
+
+% add mutiple new examples
+newExamples = [ 0.3 0.3 0.4;
+                0.1, 0.2, 0.7
+             ];
+newLabels = [1,3];
+myGPHIKClassifier.addMultipleExamples( newExamples, newLabels );
+
+% perform evaluation again
+
+% run single classification call
+[ classNoEst, score, uncertainty] = myGPHIKClassifier.classify( myDataTest )
+% compute predictive variance
+uncertainty = myGPHIKClassifier.uncertainty( myDataTest )
+% run test method evaluating arr potentially using multiple examples
+[ arr, confMat, scores] = myGPHIKClassifier.test( myDataTest, myLabelsTest )
+
+% clean up and delete object
+myGPHIKClassifier.delete();