-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
221 lines (191 loc) · 6.3 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
%author: Rui Wang
%date: 2018
%Copyright@ JNU_B411
%department: school of artificial intelligence and computer science
clear;
close all;
clc;
%step1 upload .mat data
%load ImgData_HE_ETH_100_1
% load ETH_80_Data_New
load demo-ETH
t_star = cputime; %
%step2: add label to the training data and test data
Train_lables = zeros(1,40);
Test_lables = zeros(1,40);
% step2
l=5;
k=l;
a=linspace(1,8,8);
i1=1;
while(k<=40)
while(i1<=8)
for i=1:8
i_train=l*(i-1)+1;
Train_lables(i_train:k)=a(i1);
k=k+5;
i1=i1+1;
end
end
end
% step3
l1=5;
k1=l1;
a1=linspace(1,8,8);
i2=1;
while(k1<=40)
while(i2<=8)
for i=1:8
i_test=l1*(i-1)+1;
Test_lables(i_test:k1)=a(i2);
k1=k1+5;
i2=i2+1;
end
end
end
%step5:
param.d = 400;%
d = param.d;
basis = eye(d);%
% cov_train_disturb=cell(1,40);
ImgData_HE_train = cell(1,40);
ImgData_HE_test = cell(1,40);
accuracy_matrix = zeros(1,10); %
for iteration = 1 : 1
log_cov_train_Gras = cell(1,40); %
log_cov_train_Spd = cell(1,40); %
% cov_test_disturb=cell(1,32);
log_cov_test_Gras=cell(1,40);
log_cov_test_Spd=cell(1,40);
ImgData_HE_train = ETH_train; % All_ImgData_HE_train{iteration};
ImgData_HE_test = ETH_test; % All_ImgData_HE_test{iteration};
tic
[ls_train, q1] = compute_sub(ImgData_HE_train); %
cov_train = compute_cov(ImgData_HE_train);
t_star_train1=cputime;
for i=1:40
temp_tr_Gras=ls_train{i};
temp_tr_Spd=cov_train{i};
log_cov_train_Spd{i}=logm(temp_tr_Spd); %
log_cov_train_Gras{i}=temp_tr_Gras; %
end
toc
disp('obtaining traing data')
%clear center_matrix_train;
t_train1= cputime - t_star_train1; %
% clear cputime;
t_star_test1=cputime; % 计时用;
%step6:
tic
[ls_test, q2] = compute_sub(ImgData_HE_test);
cov_test = compute_cov(ImgData_HE_test);
for i=1:40
temp_te_Gras = ls_test{i};
temp_te_Spd = cov_test{i};
log_cov_test_Gras{i} = temp_te_Gras; %
log_cov_test_Spd{i} = logm(temp_te_Spd); %
end
toc
disp('obtaing test data')
t_test1= cputime - t_star_test1;
% clear cputime;
%step7: building the kernel matrices for the Grassmannian data and SPD manifold-valued data, respectively
kmatrix_train=zeros(size(log_cov_train_Gras,2),size(log_cov_train_Gras,2)); %
kmatrix_test=zeros(size(log_cov_train_Gras,2),size(log_cov_test_Gras,2)); %
kmatrix_train_Spd=zeros(size(log_cov_train_Spd,2),size(log_cov_train_Spd,2)); %
kmatrix_test_Spd=zeros(size(log_cov_train_Spd,2),size(log_cov_test_Spd,2)); %
t_star_train2=cputime;
tic
for i=1:size(log_cov_train_Gras,2)
for j=1:size(log_cov_train_Gras,2)
cov_i_Train=log_cov_train_Gras{i}; %
cov_j_Train=log_cov_train_Gras{j}; %
temp_i = cov_i_Train * cov_i_Train';
temp_j = cov_j_Train*cov_j_Train';
temp_i = temp_i(:);
temp_j = temp_j(:);
kmatrix_train(i,j) = temp_i' * temp_j; % trace((cov_i_Train*cov_i_Train')*(cov_j_Train*cov_j_Train'));
kmatrix_train(j,i)=kmatrix_train(i,j);
end
end
toc
disp('train kernel Grass')
tic
for i=1:size(log_cov_train_Spd,2)
for j=1:size(log_cov_train_Spd,2)
cov_i_Train=log_cov_train_Spd{i};
cov_j_Train=log_cov_train_Spd{j};
cov_i_Train_reshape=reshape(cov_i_Train,size(cov_i_Train,1)*size(cov_i_Train,2),1);
cov_j_Train_reshape=reshape(cov_j_Train,size(cov_j_Train,1)*size(cov_j_Train,2),1);
kmatrix_train_Spd(i,j)=cov_i_Train_reshape'*cov_j_Train_reshape;
kmatrix_train_Spd(j,i)=kmatrix_train_Spd(i,j);
end
end
toc
disp('train kernel SPD')
t_train2=cputime-t_star_train2;
% clear cputime;
t_star_test2=cputime;
tic
for i=1:size(log_cov_train_Gras,2)
for j=1:size(log_cov_test_Gras,2)
cov_i_Train=log_cov_train_Gras{i};
cov_j_Test=log_cov_test_Gras{j};
temp_i = cov_i_Train * cov_i_Train';
temp_j = cov_j_Test*cov_j_Test';
temp_i = temp_i(:);
temp_j = temp_j(:);
kmatrix_test(i,j) = temp_i' * temp_j; % trace((cov_i_Train*cov_i_Train')*(cov_j_Test*cov_j_Test'));%240*141
end
end
toc
disp('test kernel Grass')
tic
for i=1:size(log_cov_train_Spd,2)
for j=1:size(log_cov_test_Spd,2)
cov_i_Train=log_cov_train_Spd{i};
cov_j_Test=log_cov_test_Spd{j};
cov_i_Train_reshape=reshape(cov_i_Train,size(cov_i_Train,1)*size(cov_i_Train,2),1);
cov_j_Test_reshape=reshape(cov_j_Test,size(cov_j_Test,1)*size(cov_j_Test,2),1);
kmatrix_test_Spd(i,j)=cov_i_Train_reshape'*cov_j_Test_reshape;
end
end
toc
disp('test kernel SPD')
% two parameters for these two models
lamda1 = 0.8; % for Grasmann kernel feature
lamda2 = 0.2; % for Spd kernel feature
alpha = 0.2; % balance parameter of the objective function
% Compute the core matrix U
tic
U = compute_metric_learning(kmatrix_train, kmatrix_train_Spd, lamda1, lamda2, Train_lables);
toc
disp('mmml')
dist = zeros(size(Train_lables,2),size(Test_lables,2)); % distance matrix
%% classification
tic
for i_dist=1:size(Train_lables,2)
Y_train_spd = kmatrix_train_Spd(:,i_dist);
Y_train_gras = kmatrix_train(:,i_dist);
for j_dist=1:size(Test_lables,2)
Y_test_spd = kmatrix_test_Spd(:,j_dist);
Y_test_gras = kmatrix_test(:,j_dist);
Y_dist1 = lamda1*(Y_train_gras-Y_test_gras)' * U * U' * (Y_train_gras-Y_test_gras) * lamda1;
Y_dist2 = lamda2*(Y_train_spd-Y_test_spd)' * U * U' * (Y_train_spd-Y_test_spd) * lamda2;
dist(i_dist,j_dist) = Y_dist1 + Y_dist2;
end
end
toc
disp('classification')
test_num=size(Test_lables,2); % number of test samples
[dist_sort,index] = sort(dist,1,'ascend');
%right_num=length(find((Test_labels'-Train_labels'(index(1,:)))==0));
right_num = length(find((Test_lables'-Train_lables(index(1,:))')==0));
accuracy = right_num/test_num;
accuracy_matrix(iteration) = accuracy * 100;
fprintf(1,'the number of right predicted samples of the %d-th combination is: %d %d\n',iteration,right_num );
fprintf(1,'the classification score of the %d-th combination is: %d %d\n', iteration ,accuracy*100);
end
mean_accuracy=sum(accuracy_matrix) / 1.0;
fprintf(1,'mean classification score is: %d\n',mean_accuracy);
fprintf(1,'standard derivation is: %d\n',std(accuracy_matrix));