TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
classif_module.m
Go to the documentation of this file.
1 function [estimated, scores, estimated_training, scores_training] = classif_module(xTrain, xVal, xTest, labels_train, labels_val, parameters)
2 %classification module,
3 %Mohammad Soleymani, 1 October 2012, m.soleymani@imperial.ac.uk
4 %IN:
5 %xTrain = training samples, if non sequnetional, each row is one sample, each
6 %column is a feature
7 %xTest = test samples
8 %labels_train = training ground truth labels_train
9 %prameters gridSearch 0 = no grid search for svm , 1 = grid search for svm
10 % OUT:
11 %estimated = estimated labels_train on the test set
12 %scores = confidence scores on the estimations
13 % estimated_training : classfied labels_train on training set
14 
15 
16 if nargin<6
17  parameters.grid_search = 1;
18  parameters.priorP = ones(1,parameters.nbClasses)/parameters.nbClasses;
19 end
20 
21 if ~isfield(parameters, 'priorP')
22  parameters.priorP = ones(1,parameters.nbClasses)/parameters.nbClasses;
23 end
24 
25 labels_unique = unique(labels_train);
26 if ~isfield(parameters,'nbClasses')
27  parameters.nbClasses = length(labels_unique);
28 end
29 
30 
31 
32 
33 switch(parameters.classifier)
34  case 'SVMRbf'
35  Cs = 2.^[-5:5];
36  Gammas = 2.^[-5:5];
37  if parameters.grid_search
38  perf_metr_m = zeros(length(Cs),length(Gammas));
39  perf_metr_sd = zeros(length(Cs),length(Gammas));
40  parameters.nbFolds = 5;
41  folds = kfold_gen(labels_train,parameters);
42  paramSV = '';
43  if length(labels_unique)==parameters.nbClasses
44  for k = 1:parameters.nbClasses
45  paramSV = [paramSV '-w' num2str(labels_unique(k)) ' ' num2str(length(labels_train)/sum(labels_train==labels_unique(k))) ' ' ];
46  end
47  else
48  warning('Training set labels do not contains all the labels')
49  end
50  for iC = 1:length(Cs)
51  for iGamma = 1:length(Gammas)
52  svm_prms = ['-s 0 -t 2 -d 3 -c ' num2str(Cs(iC)) ' -h 0 -b 1 -g ' num2str(Gammas(iGamma)) ' ' paramSV];
53  lbls_fld = ones(size(labels_train));
54  nbTrials = length(labels_train);
55  for k = 1:parameters.nbFolds
56  test_set = folds{k};
57  train_set = setdiff(1:nbTrials,test_set);
58  model = svmtrain(labels_train(train_set), xTrain(train_set,:), svm_prms);
59  [lbls_fld(test_set)] = svmpredict(labels_train(test_set), ...
60  xTrain(test_set,:), model);
61  end
62  [~, ~, ~, f1s] = classificationPrec(lbls_fld, labels_train, parameters.nbClasses);
63  perf_metr_m(iC,iGamma) = mean(f1s);
64  perf_metr_sd(iC,iGamma) = std(f1s);
65  end
66  end
67  f1_max = max(perf_metr_m(:));
68  iBest = find(perf_metr_m == f1_max);
69  if length(iBest)>1
70  temp = perf_metr_sd(:);
71  sd_min = min(temp(iBest));
72  else
73  sd_min = perf_metr_sd(iBest);
74  end
75  [iBestC iBestGamma] = find(perf_metr_m==f1_max & perf_metr_sd==sd_min,1,'first');
76  disp(['Best C = ' num2str(Cs(iBestC)) ' Best Gamma = ' num2str(Gammas(iBestGamma)) ', f1 = ' num2str(f1_max)]);
77  svm_prms = ['-s 0 -t 2 -d 3 -c ' num2str(Cs(iBestC)) ' -h 0 -b 1 -g ' num2str(Gammas(iBestGamma)) ' ' paramSV];
78  else
79  svm_prms = '-s 0 -t 2 -d 3 -c 1 -g 0.15 -h 0 -b 1 ';
80  end
81  model = svmtrain(labels_train, xTrain, svm_prms);
82  if parameters.is_fusion
83  [estimated_training, ~, post_p_tr] = svmpredict(labels_train, xTrain, model,' -b 1');
84  scores_training(:,model.Label) = post_p_tr;
85  end
86  [estimated, ~, post_p] = ...
87  svmpredict(ones(size(xTest,1),1), xTest, model,' -b 1');
88  scores(:,model.Label) = post_p;
89  case 'ridge_reg'
90  b = ridge(labels_train,xTrain,10,0);
91  estimated= b(1)*ones(size(xTest,1),1);
92  estimated = estimated+ xTest*b(2:end);
93  estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
94  estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
95  scores = zeros(length(estimated),1);
96  case 'lin_reg'
97  xTrain = [ones(size(xTrain,1),1) xTrain];
98  b = regress(labels_train,xTrain);
99  estimated= b(1)*ones(size(xTest,1),1);
100  estimated = estimated+ xTest*b(2:end);
101  estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
102  estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
103  scores = zeros(length(estimated),1);
104  case 'SVRLin'
105  x = xTrain;
106  y = labels_train;
107  x_val = xVal;
108  y_val = labels_val;
109  clear xTrain
110  if size(y,1)<size(y,2)
111  y = y';
112  end
113 
114  Cs = 10.^[-4:4];
115 
116  if parameters.grid_search
117  perf_metr_m = zeros(length(Cs),1);
118  perf_metr_sd = zeros(length(Cs),1);
119  params = parameters;
120  params.nbFolds = 3;
121  folds = kfold_gen(y,params);
122  for iC = 1:length(Cs)
123 
124  svm_prms = ['-s 11 -c ' num2str(Cs(iC))];
125  lbls_fld = ones(size(y));
126  nbTrials = length(y);
127  if isempty(xVal)
128  for k = 1:length(folds)
129  test_set = folds{k};
130  train_set = setdiff(1:nbTrials,test_set);
131  %model = svmtrain( y(train_set), x(train_set,:), svm_prms);
132  model = train(y(train_set), sparse(x(train_set,:)), svm_prms);
133  % [lbls_fld(test_set)] = svmpredict(y(test_set), ...
134  % x(test_set,:), model);
135  [lbls_fld(test_set)] = predict(y(test_set), ...
136  sparse(x(test_set,:)), model);
137  end
138  [evals] = regressionPrec(lbls_fld, y);
139  else
140  model = train(y, sparse(x), svm_prms);
141  % [lbls_fld(test_set)] = svmpredict(y(test_set), ...
142  % x(test_set,:), model);
143  lbls_est = predict(y_val, ...
144  sparse(x_val), model);
145  [evals] = regressionPrec(lbls_est, y_val);
146  end
147 
148 
149  perf_metr_rho(iC) = evals.rho;
150  perf_metr_rmse(iC) = evals.RMSE;
151  end
152  inds = find(perf_metr_rmse==min(perf_metr_rmse));
153  if length(inds) >1
154 
155  iBestC = inds(perf_metr_rho(inds) == max(perf_metr_rho(inds)));
156  else
157  iBestC = inds;
158  end
159  rmse_min = min(perf_metr_rmse(:));
160 
161  disp(['Best C = ' num2str(Cs(iBestC)) ', RMSE = ' num2str(rmse_min)]);
162 
163  svm_prms = ['-s 11 -c ' num2str(Cs(iBestC)) ];
164  else
165  svm_prms = '-s 11 -c 10 ';
166 
167  end
168 
169  model = train(y, sparse(x), svm_prms);
170  estimated = predict(ones(size(xTest,1),1), sparse(xTest), model);
171  estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
172  estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
173 
174 
175  scores = zeros(length(estimated),1);
176  case 'SVR_RBF'
177  addpath('../toolboxes/libsvm-3.19/matlab');
178  x = xTrain;
179  y = labels_train;
180 
181  x_val = xVal;
182  y_val = labels_val;
183  if size(y,1)<size(y,2)
184  y = y';
185  end
186 
187  Cs = 2.^[-3:3];%5 10 % instead of starting at -6
188  Gammas = 1/size(xTrain,2);%2.^[-15:2:-9];%instead of +9
189  %
190 
191  if parameters.grid_search
192  perf_metr_m = zeros(length(Cs),length(Gammas));
193  perf_metr_sd = zeros(length(Cs),1);
194  parameters.nbFolds = 3;
195  nbFolds = 2;
196  folds = kfold_gen(y,parameters);
197  for iC = 1:length(Cs)
198  for iGamma = 1:length(Gammas)
199  svm_prms = ['-s 4 -t 2 -d 3 -c ' num2str(Cs(iC)) ' -g ' num2str(Gammas(iGamma)) ' -h 0' ];
200  lbls_fld = ones(size(y));
201  nbTrials = length(y);
202  if isempty(xVal)
203 
204  for k = 1:parameters.nbFolds
205  test_set = folds{k};
206  train_set = setdiff(1:nbTrials,test_set);
207  model = svmtrain(y(train_set), x(train_set,:), svm_prms);
208  [lbls_fld(test_set)] = svmpredict(y(test_set), ...
209  x(test_set,:), model);
210  end
211  [evals] = regressionPrec(lbls_fld, y);
212  else
213  model = svmtrain(y, x, svm_prms);
214 
215  lbls_est = svmpredict(y_val, ...
216  x_val, model);
217  [evals] = regressionPrec(lbls_est, y_val);
218 
219  end
220  perf_metr_rho(iC,iGamma) = evals.rho;
221  perf_metr_rmse(iC,iGamma) = evals.RMSE;
222  end
223 
224  end
225  inds = find(perf_metr_rmse==min(perf_metr_rmse(:)));
226  if length(inds) >1
227 
228  iBests = inds(find(perf_metr_rho(inds) == max(perf_metr_rho(inds)),1,'first'));
229 
230  else
231  iBests = inds;
232  end
233 
234  [iBestC iBestGamma] = find(perf_metr_rmse==perf_metr_rmse(iBests) ...
235  & perf_metr_rho==perf_metr_rho(iBests),1,'first');
236  disp(['Best C = ' num2str(Cs(iBestC)) ' Best Gamma = ' num2str(Gammas(iBestGamma)) ', rho = ' num2str(max(perf_metr_rho(:)))]);
237 
238  rho_max = max(perf_metr_rho(:));
239  svm_prms = ['-s 4 -t 2 -d 3 -c ' num2str(Cs(iBestC)) ' -g ' num2str(Gammas(iBestGamma)) ' -h 0' ];
240  else
241  svm_prms = '-s 4 -t 2 -d 3 -c 10 -g 0.01 -h 0 ';
242 
243  end
244 
245 
246  model = svmtrain(y, x, svm_prms);
247  estimated = svmpredict(ones(size(xTest,1),1), xTest, model);
248  estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
249  estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
250  scores = zeros(length(estimated),1);
251  case {'diaglinear' 'linear' 'quadratic' 'diagquadratic'}
252  try
253  [estimated,~,scores] = classify(xTest, xTrain,labels_train,parameters.classifier);
254  catch
255  disp('pooled variance problem - passing')
256  estimated = ones(size(xTest,1));
257  scores = zeros(size(xTest,1),parameters.nbClasses);
258  end
259  try
260  [estimated_training, ~, scores_training] = classify(xTrain, xTrain, labels_train,parameters.classifier);
261  catch
262  disp('pooled variance problem for training - passing')
263  estimated_training = ones(size(xTrain,1));
264  scores_training = zeros(size(xTrain,1),parameters.nbClasses);
265  end
266 
267 end
regressionPrec
function regressionPrec(in labels, in groundTruth)
kfold_gen
function kfold_gen(in labels, in params)
classif_module
function classif_module(in xTrain, in xVal, in xTest, in labels_train, in labels_val, in parameters)
classificationPrec
function classificationPrec(in labels, in groundTruth, in nbClasses)