-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compute_ClassificationTrainSVM.m
executable file
·54 lines (39 loc) · 1.35 KB
/
Compute_ClassificationTrainSVM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function Model = Compute_ClassificationTrainSVM(XTrain,YTrain,cfg)
%% SHUFFLING
if ~isfield(cfg,'Shuffling')
% do nothing
else
if strcmp(cfg.Shuffling,'YES')
IndRandom = randperm(length(YTrain));
YTrain = YTrain(IndRandom);
else
% do nothing
end % if strcmp(cfg.Shuffling,'YES')
end % if isfield((cfg,'Shuffling')
%% TRAIN CLASSIFIER: COMPUTE MODEL
% % Fix random number generator seed
% rng(2)
% Separate train set into two sets: (1) train set and (2) validation set
Nsamples = size(XTrain,4);
IndRan = randperm(Nsamples);
IndVal = IndRan(1:round(Nsamples*.1));
IndTra = IndRan(round(Nsamples*.1)+1:end);
XValid = XTrain(:,:,:,IndVal);
YValid = YTrain(IndVal,1);
XTrain = XTrain(:,:,:,IndTra);
YTrain = YTrain(IndTra,1);
% Indices de XTrain:
% 1 y 2 -> Dimensiones de la imagen en pixeles
% 3 -> Canal (para todos es igual a 1)
% 4 -> Etiqueta para contar imágenes
% Size of input data
Sx = size(XTrain,1);
Sy = size(XTrain,2);
Sz = size(XTrain,3);
Sn = size(XTrain,4);
% i) flat images in XTrain, ii) remove dimension of length 1, iii) transpose
XTrain_flat = transpose(squeeze(reshape(XTrain,[Sx*Sy,Sz,Sn])));
tic
Model.SVMModel = fitcsvm(XTrain_flat,categorical(YTrain),'KernelFunction','linear',...
'Standardize',true,'ClassNames',{'1','2'});
Model.Ttrain = toc;