libsvm_train.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function svmmodel = libsvm_train ( labels, feat, settings )
  2. %
  3. % BRIEF
  4. % A simple wrapper to provide training of 1-vs-all-classification for LIBSVM. 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', 's_svm_type', 's_kernel_type', 'f_svm_C',
  12. % 'f_rbf_gamma', 'b_svm_shrinking' ...
  13. %
  14. % OUTPUT:
  15. % svmmodel -- cell ( #classes x 1 ), every model entry is obtained via
  16. % svmtrain of the corresponding 1-vs-all-problem
  17. %
  18. % date: 28-04-2014 ( dd-mm-yyyy )
  19. % author: Alexander Freytag
  20. if ( nargin < 3 )
  21. settings = [];
  22. end
  23. %
  24. libsvm_options = '';
  25. % outputs for training
  26. if ( ~ getFieldWithDefault ( settings, 'b_verbose', false ) )
  27. libsvm_options = sprintf('%s -q', libsvm_options);
  28. end
  29. % -s svm_type : set type of SVM (default 0)
  30. % 0 -- C-SVC (multi-class classification)
  31. % 1 -- nu-SVC (multi-class classification)
  32. % 2 -- one-class SVM
  33. % 3 -- epsilon-SVR (regression)
  34. % 4 -- nu-SVR (regression)
  35. s_svm_type = getFieldWithDefault ( settings, 's_svm_type', '0');
  36. libsvm_options = sprintf('%s -s %s', libsvm_options, s_svm_type);
  37. % -t kernel_type : set type of kernel function (default 2)
  38. % 0 -- linear: u'*v
  39. % 1 -- polynomial: (gamma*u'*v + coef0)^degree
  40. % 2 -- radial basis function: exp(-gamma*|u-v|^2)
  41. % 3 -- sigmoid: tanh(gamma*u'*v + coef0)
  42. s_kernel_type = getFieldWithDefault ( settings, 's_kernel_type', '2');
  43. libsvm_options = sprintf('%s -t %s', libsvm_options, s_kernel_type);
  44. % cost parameter (default 1)
  45. f_svm_C = getFieldWithDefault ( settings, 'f_svm_C', 1);
  46. libsvm_options = sprintf('%s -c %f', libsvm_options, f_svm_C);
  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. % RBF bandwidth parameter (default 1)
  53. f_rbf_gamma = getFieldWithDefault ( settings, 'f_rbf_gamma', 1);
  54. libsvm_options = sprintf('%s -g %f', libsvm_options, f_rbf_gamma);
  55. % do we want to use the shrinking option? (default: no)
  56. b_svm_shrinking = getFieldWithDefault ( settings, 'b_svm_shrinking', false);
  57. libsvm_options = sprintf('%s -h %d', libsvm_options, b_svm_shrinking);
  58. uniqueLabels = unique ( labels );
  59. i_numClasses = size ( uniqueLabels,1);
  60. % train one-against-all models
  61. svmmodel = cell( i_numClasses,1);
  62. for k=1:i_numClasses
  63. yBin = 2*double( labels == uniqueLabels(k) )-1;
  64. if ( b_weightBalancing )
  65. fraction = double(sum(yBin==1))/double(sum(yBin==-1));
  66. libsvm_optionsLocal = sprintf('%s -w1 %f -w-1 1', libsvm_options, 1.0/fraction);
  67. svmmodel{k}.model = svmtrain( yBin, feat, libsvm_optionsLocal );
  68. else
  69. svmmodel{k}.model = svmtrain( yBin, feat, libsvm_options );
  70. end
  71. %store the unique class label for later evaluations.
  72. svmmodel{k}.uniqueLabel = uniqueLabels(k);
  73. end
  74. end