-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVMLSMaster.m
116 lines (95 loc) · 3.9 KB
/
SVMLSMaster.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
104
105
106
107
108
109
110
111
112
113
114
115
116
%% Load Dataset and Label
clc
clear
profile off;
profile on;
prompt1 = 'Which region? \n Input a number between 1-16 \n';
RegionSelected = strcat('R',num2str(input(prompt1)));
fig1 = strcat(RegionSelected,'_original_2019-08-18.tif');
fig2 = strcat(RegionSelected,'_original_2021-07-23.tif');
s = size(imread(fig1));
X_2019 = reshape(double(imread(fig1)),s(1)*s(2),[]);
X_2021 = reshape(double(imread(fig2)),s(1)*s(2),[]);
Xu = [rgb2lab(X_2019./255), rgb2lab(X_2021./255)];
prompt2 = 'Which dataset? \n 1) RGB Images \n 2) 6-Channel Images \n 3) 10-Channel Images \n ';
DataSelected = input(prompt2);
if DataSelected == 1
fig1 = strcat(RegionSelected,'_original_2019-08-18.tif');
fig2 = strcat(RegionSelected,'_original_2021-07-23.tif');
s = size(imread(fig1));
X_2019 = reshape(double(imread(fig1)),s(1)*s(2),[]);
X_2021 = reshape(double(imread(fig2)),s(1)*s(2),[]);
elseif DataSelected ==2
fig1 = load(strcat(RegionSelected,'_original_2019-08-18_ch6.mat'));
fig2 = load(strcat(RegionSelected,'_original_2021-07-23_ch6.mat'));
s = size(fig1.new);
X_2019 = reshape(double(fig1.new),s(1)*s(2),[]);
X_2021 = reshape(double(fig2.new),s(1)*s(2),[]);
elseif DataSelected == 3
fig1 = load(strcat(RegionSelected,'_original_2019-08-18_ch10.mat'));
fig2 = load(strcat(RegionSelected,'_original_2021-07-23_ch10.mat'));
s = size(fig1.new);
X_2019 = reshape(double(fig1.new),s(1)*s(2),[]);
X_2021 = reshape(double(fig2.new),s(1)*s(2),[]);
else
disp('Incorrect prompt input. Please enter one of [1:3].')
end
X = [X_2019,X_2021];
clear fig1 fig2 s1 s2 X_2019 X_2021 prompt1 prompt2
% Load and process the labels
prompt = 'Select the task \n 1) Binary Classification \n 2) 4-class Classification \n';
choice = input(prompt);
if choice == 1
Y = reshape(double(imread(strcat(RegionSelected,'_original_Binary_change_thr.png'))),s(1)*s(2),1);
Y = Y/255;
elseif choice == 2
Y = reshape(double(imread(strcat(RegionSelected,'_original_four_change_thr.png'))),s(1)*s(2),1);
else
disp('Incorrect prompt input. Please enter one of 1 or 2.')
end
prompt = 'Use Lab colorspace to uplifting the data? \n 1) Yes \n 2) No \n';
choice = input(prompt);
if choice == 1
X = [X,Xu];
end
HSI = reshape(X,s(1),s(2),[]);
trial_num = 10;
Y2d = reshape(Y,s(1),s(2))+1;
clear prompt4
HSI_ori = HSI;
clear X Xu Y HSI
%%
for pts_per_class = [10,20,30,50,100]
HSI = HSI_ori;
% Find the best parameters of SVM
% Y2d: Label
% HSI: 3D cube
K_Known = length(unique(Y2d));
n=0.01:0.01:0.1;
dn = (size(HSI,3)+2:-0.5:size(HSI,3)-2);
dn = dn(dn>1);
g=1./dn;
[best_param,idx_all,idx_kappa] = find_best_params(HSI,Y2d,K_Known,trial_num,pts_per_class,n,g);
n = unique([(best_param(idx_all,1)-0.005):0.0025:(best_param(idx_all,1)+0.005),(best_param(idx_kappa,1)-0.005):0.0025:(best_param(idx_kappa,1)+0.005)]);
n = n(n>0);
idx1 = find(g == best_param(idx_kappa,2));
dn1 = (idx1+0.4:-0.1:idx1-0.4);
dn1 = dn1(dn1>1);
idx2 = find(g == best_param(idx_all,2));
dn2 = (idx2+0.4:-0.1:idx2-0.4);
dn2 = dn2(dn2>1);
g = unique([1./dn1,1./dn2]);
best_param = find_best_params(HSI,Y2d,K_Known,trial_num,pts_per_class,n,g);
par = 0:0.2:1;
par2 = [0,0.5,1,2];
Prediction = gridsearch_denoised(HSI,Y2d,K_Known,trial_num,pts_per_class,best_param,par,par2);
besta1a2 = Prediction.besta1a2;
disp(besta1a2)
par = (mode(besta1a2(:,1))-0.1):0.05:(mode(besta1a2(:,1))+0.1);
par = par(par>=0);
par2 = unique([mode(besta1a2(:,2)),besta1a2(Prediction.idx,2)]);
Prediction = gridsearch_denoised(HSI,Y2d,K_Known,trial_num,pts_per_class,best_param,par,par2);
Prediction.best_param = best_param;
% Produce the best classification results
save(strcat('recon',num2str(choice),'_',num2str(pts_per_class),'_best'), 'Prediction')
end