-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM_training.m
103 lines (82 loc) · 4.05 KB
/
SVM_training.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function training = SVM_training(img, roi, labels)
% Exercise 4B - Supervised classification
% Version: adapted version of the Exercice 4B October 31, 2017
% Author(s): Matthew Parkan, Pinar Akyazi, Frank de Morsier
%==========================================================================
%% Input
%==========================================================================
labels = 1:length(labels);
%==========================================================================
%% Upload polygonal regions of interest (roi)
%==========================================================================
% Get image info:
R = geotiffinfo('./Data/2019-01-06/2019-01-06, Sentinel-2A L1C, B08');
% Get x,y locations of pixels:
[x,y] = pixcenters(R);
% Convert x,y arrays to grid:
[X,Y] = meshgrid(x,y);
% Remove trailing nan from shapefile
mask = zeros(size(X,1),size(X,2), length(roi));
% mask_new = zeros(size(X,1),size(X,2),length(roi));
% Prepare parallel computing
delete(gcp) % stop any parallel pool started
parpool % start parallel pool
parfor r = 1:length(roi)
for p = 1:length(roi{r})
rx = roi{r}(p).X(1:end-1);
ry = roi{r}(p).Y(1:end-1);
% mask_new(:,:,r) = inpolygon(X,Y,rx,ry);
% mask(:,:,r) = mask(:,:,r) + mask_new(:,:,r);
mask(:,:,r) = mask(:,:,r) + inpolygon(X,Y,rx,ry);
end
end
mask_tot = sum(mask(:,:,:),3);
%==========================================================================
%% supervised classification
%==========================================================================
% Reshape the data into a 2d matrix
data = reshape(img,size(img,1)*size(img,2),size(img,3));
% data = reshape(images.B03,size(images.B03,1)*size(images.B03,2),1);
% Get pixels from each class mask (polygons)
index = find(mask_tot);
data_roi = data(index,:);
% concatenate the vector of labels
label_roi = [];
for r = 1:length(labels) % for each polygon
% Create a vector with the label of the polygon class
label_roi = [label_roi; repmat(labels(r),size(mask(:,:,r),1),1)];
end
% Split into training and testing samples to evaluate performances
trainID = 1:10:length(label_roi);
testID = setdiff(1:length(label_roi),trainID);
% Subsample the training and the validation (test) data + labels
data_train = data_roi(trainID,:);
label_train = label_roi(trainID);
data_valid = data_roi(testID,:);
label_valid = label_roi(testID);
%==========================================================================
%% Rescale the training data using this function classificationScaling
%==========================================================================
% The following function can rescale the data between [0,1] or that it has
% a unit variance and zero mean
typeNorm = 'minmax'; % use 'std' to rescale to a unit variance and zero mean
[data_train_sc, dataMax, dataMin] = classificationScaling(double(data_train), [], [], typeNorm);
% The same for the validation pixels
data_valid_sc = classificationScaling(double(data_valid), dataMax, dataMin, typeNorm);
%==========================================================================
%% Training model
%==========================================================================
% Train a SVM model (fitcecoc() function is for multi-class problems)
model_svm = fitcecoc(data_train_sc,label_train);
% Performs cross-validation to tune the parameters: crossval() function
% (split the training data in order to train and test on different samples and tune correctly the parameters)
model_svm_cv = crossval(model_svm);
%==========================================================================
%% Output
%==========================================================================
training.model_svm_cv = model_svm_cv;
training.label_valid = label_valid;
training.data_valid_sc = data_valid_sc;
training.dataMax = dataMax;
training.dataMin = dataMin;
end