浏览代码

initial commit

Alexander Freytag 9 年之前
当前提交
69c436c122
共有 4 个文件被更改,包括 237 次插入0 次删除
  1. 80 0
      initWorkspaceLibSVM.m
  2. 49 0
      libsvm_test.m
  3. 82 0
      libsvm_train.m
  4. 26 0
      misc/getFieldWithDefault.m

+ 80 - 0
initWorkspaceLibSVM.m

@@ -0,0 +1,80 @@
+function initWorkspaceLibSVM
+% function initWorkspaceLibSVM
+% 
+% BRIEF
+%   Add local subfolders and 3rd party libraries to Matlabs work space.
+%
+%   Exemplary call from external position:
+%        LIBLINEARWRAPDIR = '/place/to/this/repository/';
+%        currentDir = pwd;
+%        cd ( LIBLINEARWRAPDIR );
+%        initWorkspaceLibSVM;
+%        cd ( currentDir );
+%    
+% 
+% Author: Alexander Freytag
+
+    %% setup paths in user-specific manner
+
+
+    % currently we do not have any dependencies, but if we would have some,
+    % we would add them here  
+    
+    %% add paths which come with this repository
+    
+    if strcmp( getenv('USER'), 'freytag')
+        LIBSMVDIR    = '/home/freytag/code/3rdParty/libsvm/matlab/';   
+    else
+        fprintf('Unknown user %s and unknown default settings', getenv('USER') ); 
+    end    
+    
+    % add main path
+    b_recursive             = false; 
+    b_overwrite             = true;
+    s_pathMain              = fullfile(pwd);
+    addPathSafely ( s_pathMain, b_recursive, b_overwrite )
+    clear ( 's_pathMain' );    
+    
+    % for addFieldWithDefault.m
+    b_recursive             = true; 
+    b_overwrite             = true;
+    s_pathMisc              = fullfile(pwd, 'misc');
+    addPathSafely ( s_pathMisc, b_recursive, b_overwrite )
+    clear ( 's_pathMisc' );        
+        
+    %% 3rd party, untouched
+    
+    if ( isempty(LIBSMVDIR) )
+        fprintf('InitPatchDiscovery-WARNING - no LIBSMVDIR dir found on your machine. Code is available at https://github.com/cjlin1/libsvm/ \n');
+    else
+        b_recursive             = true; 
+        b_overwrite             = true;
+        addPathSafely ( LIBSMVDIR, b_recursive, b_overwrite );        
+    end   
+    
+    %% clean up
+    
+    clear( 'LIBSMVDIR' );
+end
+
+
+function addPathSafely ( s_path, b_recursive, b_overwrite )
+    if ( ~isempty(strfind(path, [s_path , pathsep])) )
+        if ( b_overwrite )
+            if ( b_recursive )
+                rmpath( genpath( s_path ) );
+            else
+                rmpath( s_path );
+            end
+        else
+            fprintf('initWSLibSVM - %s already in your path but overwriting de-activated.\n', s_path);
+            return;
+        end
+    end
+    
+    if ( b_recursive )
+        addpath( genpath( s_path ) );
+    else
+        addpath( s_path );
+    end
+end

+ 49 - 0
libsvm_test.m

@@ -0,0 +1,49 @@
+function [predicted_label, accuracy, scores] =libsvm_test ( labels_test, feat_test, svmmodel, settings )
+%
+% BRIEF
+%   A simple wrapper to provide testing of 1-vs-all-classification for LIBSVM. 
+%   No further settings are adjustable currently.
+% 
+% INPUT
+%  labels_test   -- multi-class labels (#samples x 1)
+%  feat_test     -- features for test images (#samples x # dimensions)
+%  svmmodel      -- cell ( #classes x 1 ), every model entry is obtained via
+%                   svmtrain of the corresponding 1-vs-all-problem
+%  settings      -- struct for configuring the svm classification, e.g., via
+%                   'b_verbose' ...
+% 
+% OUTPUT:
+%    predicted_label ( note: in range [ymin, ymax], consequetively ordered.
+%    Don't miss to map it to the original labels!
+% 
+%
+% date: 28-04-2014 ( dd-mm-yyyy )
+% author: Alexander Freytag
+
+    if ( nargin < 4 ) 
+        settings = [];
+    end
+    
+    libsvm_options = '';
+    
+    % outputs for training
+    if ( ~ getFieldWithDefault ( settings, 'b_verbose', false ) )
+        libsvm_options = sprintf('%s -q', libsvm_options);
+    end    
+  
+    i_numClasses = size ( svmmodel,1);
+    i_numSamples = size( labels_test,1);
+    
+    scores = zeros( i_numSamples, i_numClasses );
+	
+    % classify with every one-against-all model    
+    for k=1:i_numClasses
+        yBin              = 2*double( labels_test == svmmodel{k}.uniqueLabel )-1;
+        [~,~,scores(:,k)] = svmpredict( yBin, feat_test, svmmodel{k}.model, libsvm_options );
+    end 
+    
+    %# predict the class with the highest score
+    [~,predicted_label] = max(scores,[],2);
+    % accuracy
+    accuracy = sum(predicted_label == labels_test) ./ numel(labels_test) ;   
+end

+ 82 - 0
libsvm_train.m

@@ -0,0 +1,82 @@
+function svmmodel = libsvm_train ( labels, feat, settings )
+%
+% BRIEF
+%   A simple wrapper to provide training of 1-vs-all-classification for LIBSVM. No
+%   further settings are adjustable currently.
+% 
+% INPUT
+%  labels   -- multi-class labels (#sample x 1)
+%  feat     -- features for training images (#samples x # dimensions)
+%  settings -- struct for configuring the svm model training, e.g., via
+%              'b_verbose', 's_svm_type', 's_kernel_type', 'f_svm_C', ...
+% 
+% OUTPUT:
+%  svmmodel -- cell ( #classes x 1 ), every model entry is obtained via
+%              svmtrain of the corresponding 1-vs-all-problem
+%
+% date: 28-04-2014 ( dd-mm-yyyy )
+% author: Alexander Freytag
+
+    if ( nargin < 3 ) 
+        settings = [];
+    end
+%     
+    
+    libsvm_options = '';
+    
+    % outputs for training
+    if ( ~ getFieldWithDefault ( settings, 'b_verbose', false ) )
+        libsvm_options = sprintf('%s -q', libsvm_options);
+    end
+    
+   
+    % 	-s svm_type : set type of SVM (default 0)
+	% 	0 -- C-SVC		(multi-class classification)
+	% 	1 -- nu-SVC		(multi-class classification)
+	% 	2 -- one-class SVM
+	% 	3 -- epsilon-SVR	(regression)
+	% 	4 -- nu-SVR		(regression)    
+    s_svm_type = getFieldWithDefault ( settings, 's_svm_type', '0');
+    libsvm_options = sprintf('%s -s %s', libsvm_options, s_svm_type);    
+
+    %   -t kernel_type : set type of kernel function (default 2)
+    % 	0 -- linear: u'*v
+    % 	1 -- polynomial: (gamma*u'*v + coef0)^degree
+    % 	2 -- radial basis function: exp(-gamma*|u-v|^2)
+    % 	3 -- sigmoid: tanh(gamma*u'*v + coef0)        
+    s_kernel_type = getFieldWithDefault ( settings, 's_kernel_type', '2');
+    libsvm_options = sprintf('%s -t %s', libsvm_options, s_kernel_type);
+
+    % cost parameter (default 1)
+    f_svm_C = getFieldWithDefault ( settings, 'f_svm_C', 1);
+    libsvm_options = sprintf('%s -c %f', libsvm_options, f_svm_C);    
+    
+    % increase penalty for positive samples according to invers ratio of
+    % their number, i.e., if 1/3 is ratio of positive to negative samples, then
+    % impact of positives is 3 the times of negatives
+    % 
+    b_weightBalancing = getFieldWithDefault ( settings, 'b_weightBalancing', false);
+    
+    
+  
+    uniqueLabels = unique ( labels );
+    i_numClasses = size ( uniqueLabels,1);
+	
+    % train one-against-all models
+    svmmodel = cell( i_numClasses,1);
+    for k=1:i_numClasses
+        yBin        = 2*double( labels == uniqueLabels(k) )-1;
+        
+        if ( b_weightBalancing )
+            fraction = double(sum(yBin==1))/double(numel(yBin));
+            libsvm_optionsLocal = sprintf('%s -w1 %f -w-1 1', libsvm_options, 1.0/fraction);
+            svmmodel{k}.model = svmtrain( yBin, feat, libsvm_optionsLocal );
+        else
+            svmmodel{k}.model = svmtrain( yBin, feat, libsvm_options );
+        end
+        
+        %store the unique class label for later evaluations.
+        svmmodel{k}.uniqueLabel = uniqueLabels(k);        
+    end 
+  
+end

+ 26 - 0
misc/getFieldWithDefault.m

@@ -0,0 +1,26 @@
+function myOut = getFieldWithDefault ( myStruct, myField, myDefault )
+% function myOut = getFieldWithDefault ( myStruct, myField, myDefault )
+% 
+%  BRIEF:
+%    Get the content of a named field of a struct if existing, or return a
+%    specified default value instead
+%    ...inspired by NICE::Config.gI ('section','name', default )
+% 
+%  INPUT:
+%    myStruct  -- a struct
+%    myField   -- string with desired field name
+%    myDefault -- default value to use if field is non-existing or empty
+% 
+%  OUTPUT:
+%    myOut     -- content of field or default value
+% 
+% 
+% author: Alexander Freytag
+% date  : 04-03-2014 ( dd-mm-yyyy )
+
+    if ( ~isempty(myStruct) && isfield(myStruct, myField) && ~isempty( myStruct.( myField ) ))
+        myOut = myStruct.( myField );
+    else
+        myOut = myDefault;
+    end
+end