Browse Source

improved multicore integration

Christoph Kaeding 9 years ago
parent
commit
95404a724d
4 changed files with 20 additions and 151 deletions
  1. 0 67
      liblinear_test_multicore.m
  2. 10 1
      liblinear_train.m
  3. 0 82
      liblinear_train_multicore.m
  4. 10 1
      liblinear_train_regression.m

+ 0 - 67
liblinear_test_multicore.m

@@ -1,67 +0,0 @@
-function [predicted_label, accuracy, scores] =liblinear_test_multicore ( labels_test, feat_test, svmmodel, settings )
-%
-% BRIEF
-%   A simple wrapper to provide testing of 1-vs-all-classification for LIBLINEAR. 
-%   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:
-% 
-%
-% date: 30-04-2014 ( dd-mm-yyyy )
-% last modified: 22-10-2015
-% author: Alexander Freytag, Chirstoph Käding
-
-    if ( nargin < 4 ) 
-        settings = [];
-    end
-    
-    libsvm_options = '';
-    
-    % outputs for training
-    if ( ~ getFieldWithDefault ( settings, 'b_verbose', false ) )
-        libsvm_options = sprintf('%s -q', libsvm_options);
-    end    
-  
-    % add multithreading
-    i_numThreads = getFieldWithDefault ( settings, 'i_numThreads', 2);
-    libsvm_options = sprintf('%s -n %d', libsvm_options, i_numThreads);    
-    
-    i_numClasses = size ( svmmodel,1);
-    i_numSamples = size( labels_test,1);
-    
-    b_weightBalancing = getFieldWithDefault ( settings, 'b_weightBalancing', false);
-    
-    if ( ~b_weightBalancing )
-        [predicted_label,~,scores] = predict( labels_test, feat_test, svmmodel, libsvm_options );
-    else
-        scores = zeros( i_numSamples, i_numClasses );
-        
-        % classify with everyone-against-all model    
-        for k=1:i_numClasses
-            yBin              = 2*double( labels_test == svmmodel{k}.uniqueLabel )-1;
-            [~,~,scores(:,k)] = predict( yBin, feat_test, svmmodel{k}, libsvm_options );
-            
-            %Q: Why the sign of predicted labels and decision values are sometimes reversed?
-            %Please see the answer in LIBSVM faq.
-            %To correctly obtain decision values, you need to check the array
-            %label
-            %in the model.
-            scores(:,k)       = scores(:,k) .* repmat( svmmodel{k}.Label(1), [i_numSamples,1] ) ;
-        end 
-
-        %# predict the class with the highest score
-        [~,predicted_label] = max(scores,[],2);        
-    end
-
-    % accuracy
-    accuracy = sum(predicted_label == labels_test) ./ numel(labels_test) ;   
-end
-

+ 10 - 1
liblinear_train.m

@@ -15,7 +15,8 @@ function svmmodel = liblinear_train ( labels, feat, settings )
 %              svmtrain of the corresponding 1-vs-all-problem
 %
 % date: 30-04-2014 ( dd-mm-yyyy )
-% author: Alexander Freytag
+% last modified: 22-10-2015
+% author: Alexander Freytag, Christoph Käding
 
     if ( nargin < 3 ) 
         settings = [];
@@ -38,6 +39,14 @@ function svmmodel = liblinear_train ( labels, feat, settings )
         libsvm_options = sprintf('%s -B 1', libsvm_options);    
     end
     
+    % add multithreading
+    % NOTE: - requires liblinear-multicore
+    %       - supports only -s 0, -s 2, or -s 11 (so far)
+    i_numThreads = getFieldWithDefault ( settings, 'i_numThreads', 1);
+    if i_numThreads > 1
+        libsvm_options = sprintf('%s -n %d', libsvm_options, i_numThreads);
+    end
+        
     % which solver to use
     % copied from the liblinear manual:
 %       for multi-class classification

+ 0 - 82
liblinear_train_multicore.m

@@ -1,82 +0,0 @@
-function svmmodel = liblinear_train_multicore ( labels, feat, settings )
-%
-% BRIEF
-%   A simple wrapper to provide training of 1-vs-all-classification for LIBLINEAR. 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', 'f_svm_C', ...
-% 
-% OUTPUT:
-%  svmmodel -- cell ( #classes x 1 ), every model entry is obtained via
-%              svmtrain of the corresponding 1-vs-all-problem
-%
-% date: 30-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
-    
-    % cost parameter
-    f_svm_C = getFieldWithDefault ( settings, 'f_svm_C', 1);
-    libsvm_options = sprintf('%s -c %f', libsvm_options, f_svm_C);    
-    
-    % do we want to use an offset for the hyperplane?
-    if ( getFieldWithDefault ( settings, 'b_addOffset', false) )
-        libsvm_options = sprintf('%s -B 1', libsvm_options);    
-    end
-    
-    % which solver to use
-    % copied from the liblinear manual:
-%       for multi-class classification
-%          0 -- L2-regularized logistic regression (primal)
-%          2 -- L2-regularized L2-loss support vector classification (primal)
-%          11 -- l2-loss SVR
-    i_svmSolver = getFieldWithDefault ( settings, 'i_svmSolver', 2);
-    i_numThreads = getFieldWithDefault ( settings, 'i_numThreads', 2);
-    libsvm_options = sprintf('%s -s %d -n %d', libsvm_options, i_svmSolver, i_numThreads);    
-
-    
-    % 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
-    
-    if ( ~b_weightBalancing)    
-        svmmodel = train( labels, feat, libsvm_options );
-    else
-        svmmodel = cell( i_numClasses,1);
-        for k=1:i_numClasses
-            yBin        = 2*double( labels == uniqueLabels( k ) )-1;
-            
-            fraction = double(sum(yBin==1))/double(numel(yBin));
-            libsvm_optionsLocal = sprintf('%s -w1 %f', libsvm_options, 1.0/fraction);
-            svmmodel{ k } = train( yBin, feat, libsvm_optionsLocal );
-            
-            %store the unique class label for later evaluations.
-            svmmodel{ k }.uniqueLabel = uniqueLabels( k );
-        end         
-    end
-    
-end

+ 10 - 1
liblinear_train_regression.m

@@ -14,7 +14,8 @@ function svmmodel = liblinear_train_regression ( labels, feat, settings )
 %  svmmodel -- resulting model
 %
 % date: 30-04-2014 ( dd-mm-yyyy )
-% author: Alexander Freytag
+% last modified: 22-10-2015
+% author: Alexander Freytag, Christoph Käding
 
     if ( nargin < 3 ) 
         settings = [];
@@ -37,6 +38,14 @@ function svmmodel = liblinear_train_regression ( labels, feat, settings )
         libsvm_options = sprintf('%s -B 1', libsvm_options);    
     end
     
+    % add multithreading
+    % NOTE: - requires liblinear-multicore
+    %       - supports only -s 0, -s 2, or -s 11 (so far)
+    i_numThreads = getFieldWithDefault ( settings, 'i_numThreads', 1);
+    if i_numThreads > 1
+        libsvm_options = sprintf('%s -n %d', libsvm_options, i_numThreads);
+    end
+    
     % which solver to use
     % copied from the liblinear manual:
 %        for regression