liblinear_train.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function svmmodel = liblinear_train ( labels, feat, settings )
  2. %
  3. % BRIEF
  4. % A simple wrapper to provide training of 1-vs-all-classification for LIBLINEAR. No
  5. % further settings are adjustable currently.
  6. %
  7. % INPUT
  8. % labels -- multi-class labels (#sample x 1)
  9. % feat -- features for training images (#samples x # dimensions)
  10. % settings -- struct for configuring the svm model training, e.g., via
  11. % 'b_verbose', 'f_svm_C', ...
  12. %
  13. % OUTPUT:
  14. % svmmodel -- cell ( #classes x 1 ), every model entry is obtained via
  15. % svmtrain of the corresponding 1-vs-all-problem
  16. %
  17. % date: 30-04-2014 ( dd-mm-yyyy )
  18. % author: Alexander Freytag
  19. if ( nargin < 3 )
  20. settings = [];
  21. end
  22. libsvm_options = '';
  23. % outputs for training
  24. if ( ~ getFieldWithDefault ( settings, 'b_verbose', false ) )
  25. libsvm_options = sprintf('%s -q', libsvm_options);
  26. end
  27. % cost parameter
  28. f_svm_C = getFieldWithDefault ( settings, 'f_svm_C', 1);
  29. libsvm_options = sprintf('%s -c %f', libsvm_options, f_svm_C);
  30. % do we want to use an offset for the hyperplane?
  31. if ( getFieldWithDefault ( settings, 'b_addOffset', false) )
  32. libsvm_options = sprintf('%s -B 1', libsvm_options);
  33. end
  34. % which solver to use
  35. % copied from the liblinear manual:
  36. % for multi-class classification
  37. % 0 -- L2-regularized logistic regression (primal)
  38. % 1 -- L2-regularized L2-loss support vector classification (dual)
  39. % 2 -- L2-regularized L2-loss support vector classification (primal)
  40. % 3 -- L2-regularized L1-loss support vector classification (dual)
  41. % 4 -- support vector classification by Crammer and Singer
  42. % 5 -- L1-regularized L2-loss support vector classification
  43. % 6 -- L1-regularized logistic regression
  44. % 7 -- L2-regularized logistic regression (dual)
  45. i_svmSolver = getFieldWithDefault ( settings, 'i_svmSolver', 1);
  46. libsvm_options = sprintf('%s -s %d', libsvm_options, i_svmSolver);
  47. % increase penalty for positive samples according to invers ratio of
  48. % their number, i.e., if 1/3 is ratio of positive to negative samples, then
  49. % impact of positives is 3 the times of negatives
  50. %
  51. b_weightBalancing = getFieldWithDefault ( settings, 'b_weightBalancing', false);
  52. uniqueLabels = unique ( labels );
  53. i_numClasses = size ( uniqueLabels,1);
  54. %# train one-against-all models
  55. if ( ~b_weightBalancing)
  56. svmmodel = train( labels, feat, libsvm_options );
  57. else
  58. svmmodel = cell( i_numClasses,1);
  59. for k=1:length(i_classesToRun)
  60. yBin = 2*double( labels == uniqueLabels( k ) )-1;
  61. fraction = double(sum(yBin==1))/double(numel(yBin));
  62. libsvm_optionsLocal = sprintf('%s -w1 %f', libsvm_options, 1.0/fraction);
  63. svmmodel{ k } = train( yBin, feat, libsvm_optionsLocal );
  64. %store the unique class label for later evaluations.
  65. svmmodel{ k }.uniqueLabel = uniqueLabels( k );
  66. end
  67. end
  68. end