-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part_1.m
383 lines (315 loc) · 10.5 KB
/
Part_1.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
clc
close all
clear all
%load face data
load face.mat
%% Q1
TrainNum=52*9;
TestNum=52;
%10-fold crossvalidation
%10 items in each class and 9 data into training set, 1 into test set, same
%as leave-one-out in this case
k=10; %Define ratio of partition, k is the proportion sorted into test set
c = cvpartition(l,'Kfold',k); %Create partition object
%Demonstrate with 1st set
TestIdx=test(c,1); %Create index list for test set
TrainingIdx=training(c,1); %Index list for training set
TestDataTemp=X(:,TestIdx);
TrainDataTemp=X(:,TrainingIdx);
%find mean face image for training data
mean_image = mean(TrainDataTemp,2);
TestData=TestDataTemp-repmat(mean_image,1,TestNum); %Obtain test data by subtracting from mean face
TrainData=TrainDataTemp-repmat(mean_image,1,TrainNum);%Obtain train data
%Compute the covariance matrix s
PhiTrain=TrainData;
s = (PhiTrain*PhiTrain')/TrainNum;
%Find the eigenvectors and eigenvalues of covariance matrix s
tic;
[V,D] = eig(s);
AATtime=toc;
[Y,I] = sort(diag(D),'descend');
Vnorm=normc(V);
%Show the magnitude of eigenvalues
figure;
subplot(1,2,1)
plot(Y)
xlabel('No. of eigenvalues');
ylabel('Magnetude of eigenvalues');
title('Eigenvalues in Decreasing Order');
subplot(1,2,2)
plot(Y)
axis([0 700 0 1300])
xlabel('No. of eigenvalues');
ylabel('Magnetude of eigenvalues');
title('Eigenvalues in Decreasing Order');
figure;
plot(Y)
axis([0 700 0 1300])
xlabel('No. of eigenvalues');
ylabel('Magnetude of eigenvalues');
title('Eigenvalues in Decreasing Order');
figure;
imagesc(reshape(mean_image,56,46));
title('mean face')
colormap gray
%Plot the three most significant eigenfaces
figure(1);
subplot(4,3,2)
imagesc(reshape(mean_image,56,46));
title('mean face')
subplot(4,3,4)
imagesc(reshape(Vnorm(:,I(1)),56,46));
title('Eigenface 1');
subplot(4,3,5)
imagesc(reshape(Vnorm(:,I(2)),56,46));
title('Eigenface 2');
subplot(4,3,6)
imagesc(reshape(Vnorm(:,I(3)),56,46));
title('Eigenface 3');
subplot(4,3,7)
imagesc(reshape(Vnorm(:,I(4)),56,46));
title('Eigenface 4');
subplot(4,3,8)
imagesc(reshape(Vnorm(:,I(5)),56,46));
title('Eigenface 5');
subplot(4,3,9)
imagesc(reshape(Vnorm(:,I(6)),56,46));
title('Eigenface 6');
subplot(4,3,10)
imagesc(reshape(Vnorm(:,I(465)),56,46));
title('Eigenface 465');
subplot(4,3,11)
imagesc(reshape(Vnorm(:,I(466)),56,46));
title('Eigenface 466');
subplot(4,3,12)
imagesc(reshape(Vnorm(:,I(467)),56,46));
title('Eigenface 467');
colormap gray
set(figure(1),'Position', [100,100,600,600]);
%maximum of 260 eigenvectors to be used according to figure 1.
figure;
subplot(1,3,1)
imagesc(reshape(Vnorm(:,I(5)),56,46));
subplot(1,3,2)
imagesc(reshape(Vnorm(:,I(5)),56,46));
subplot(1,3,3)
imagesc(reshape(Vnorm(:,I(5)),56,46));
%Find the suitable number of eigenvectors
cv=0;
i=0;
while cv<0.95*sum(Y)
i=i+1;
cv=cv+Y(i);
end
%% Q2
s2 = (PhiTrain'*PhiTrain)/TrainNum;
tic;
[V2,D2] = eig(s2);
ATAtime=toc;
[Y2,I2] = sort(diag(D2),'descend');
figure
plot(Y2)
%Dimension change
U2=PhiTrain*V2;
U2norm=normc(U2);
figure
subplot(2,3,1);
imagesc(reshape(U2(:,I2(1)),56,46));
subplot(2,3,2);
imagesc(reshape(U2(:,I2(2)),56,46));
subplot(2,3,3);
imagesc(reshape(U2(:,I2(3)),56,46));
subplot(2,3,4);
imagesc(reshape(U2(:,I2(4)),56,46));
subplot(2,3,5);
imagesc(reshape(U2(:,I2(5)),56,46));
subplot(2,3,6);
imagesc(reshape(U2(:,I2(6)),56,46));
colormap gray
%Eigenvalues are same and eigenvectors are closely related. u=Av, ppt p42
%The eigenvalues are not normalised, therefore after performing u=Av, the
%intensities of eigenvector varies, some becomes negative. See report. ppt
%p43
%% Q3
%Speficy the number of eigenvectors to select
EigenNum=468;
EigenVctSel=U2norm(:,I2(1:EigenNum));
%Projection of training data onto eigenface space
OmegaTrain = EigenVctSel'*PhiTrain; %[w1,w2...]
ReconImages = EigenVctSel*OmegaTrain;%[v1,v2...]*[w1,w2...]
%Plot the first image and its reconstruction from the training set
figure;
subplot(1,2,1)
imagesc(reshape(TrainDataTemp(:,1),56,46));
subplot(1,2,2)
recon = mean_image + ReconImages(:,1);
imagesc(reshape(recon,56,46));
colormap gray
title('testtest');
%Plot the second image and its reconstruction from the training set
figure;
subplot(1,2,1)
imagesc(reshape(TrainDataTemp(:,2),56,46));
subplot(1,2,2)
recon = mean_image + ReconImages(:,2);
imagesc(reshape(recon,56,46));
colormap gray
%Perform reconstruction on test set
PhiTest=TestData;
%Projection of test data onto eigenface space
OmegaTest = EigenVctSel'*PhiTest;
ReconImages = EigenVctSel*OmegaTest;
%Plot the first image in test set and its reconstruction
figure;
subplot(1,2,1)
imagesc(reshape(TestDataTemp(:,1),56,46));
subplot(1,2,2)
recon = mean_image + ReconImages(:,1);
imagesc(reshape(recon,56,46));
colormap gray
%% Q4
%Perform recognition on first test set (For demonstration)
%for i=1:TestNum
% [ErrMatrix(1,i),ResultMatrix(1,i)]=min(sqrt(sum((repmat(OmegaTest(:,i),1,TrainNum)-OmegaTrain).^2)));
%end
%%The following is the alternative method. It is time-consuming using AA^T
%A complete training and testing process for k-fold crossvalidation
NNTimeArray=[];
NNAccuracyArray=[];
NNErrorArray=[];
for eigNum=129:129
tic
for fold = 1:10
TestIdx=test(c,fold); %Create index list for test set
TrainingIdx=training(c,fold); %Index list for training set
TestDataTemp=X(:,TestIdx);
TrainDataTemp=X(:,TrainingIdx);
%find mean face image for training data
mean_image = mean(TrainDataTemp,2);
TestData=TestDataTemp-repmat(mean_image,1,TestNum); %Obtain test data by subtracting from mean face
TrainData=TrainDataTemp-repmat(mean_image,1,TrainNum);%Obtain train data
%Compute the covariance matrix s
PhiTrain=TrainData;
s = (PhiTrain'*PhiTrain)/TrainNum;
%Find the eigenvectors and eigenvalues of covariance matrix s
[V,D] = eig(s);
[Y,I] = sort(diag(D),'descend');
%Speficy the number of eigenvectors to select
EigenNum=eigNum;
EigenVctSelV=V(:,I(1:EigenNum));
%Normalise u
EigenVctSelU=PhiTrain*EigenVctSelV;
EigenVctSelUnorm=normc(EigenVctSelU);
%Perform reconstruction on test set
PhiTest=TestData;
%Projection of Training data onto eigenface space
OmegaTrain = EigenVctSelUnorm'*PhiTrain;
OmegaTest = EigenVctSelUnorm'*PhiTest;
%Modify result matrix for the remaining 9 tests
for i=1:TestNum
[NNErrMatrix(fold,i),NNResultMatrix(fold,i)]=min(sqrt(sum((repmat(OmegaTest(:,i),1,TrainNum)-OmegaTrain).^2)));
end
%The alternative method of using minimum reconstructed image error
end
NNtime=toc/10;
%find mean error for all folds for a fixed number of eigenvectors
NNErrorArray=[NNErrorArray,mean(mean(NNErrMatrix,1))];
%Find recognition accuracy
for i = 1 : TestNum
NNLabelMatrix(:,i)=ceil(NNResultMatrix(:,i)/9);
end
NN=reshape(NNLabelMatrix,[520,1]);
LBtemp=repmat([1:52],10,1);
LB=reshape(LBtemp,[520,1]);
NNAccuracy=size(NN(NN==LB),1)/520;
NNTimeArray=[NNTimeArray,NNtime];
NNAccuracyArray=[NNAccuracyArray,NNAccuracy];
end
figure;
plot(NNErrorArray);
title('Mean error v.s No.')
figure;
plot(NNTimeArray);
title('Accuracy v.s No.')
figure;
plot(NNAccuracyArray);
ErrMatrixClass=[];
ErrMatrixTrainClass=[];
NIPArray=[];
NIPTrainArray=[];
AMTimeArray=[];
AMAccuracyArray=[];
for eigNum=1:9
tic
for fold = 1 : 10
for i=1:52
EigenNumClass=eigNum;
TestIdx=test(c,fold); %Create index list for test set
TrainingIdx=training(c,fold); %Index list for training set
TestDataTemp=X(:,TestIdx);
TrainDataTemp=X(:,TrainingIdx);
MeanImageClass = mean(TrainDataTemp(:,9*(i-1)+1:9*i),2);
TestDataClass=TestDataTemp-repmat(MeanImageClass,1,52); %Obtain test data by subtracting from mean face
TrainDataClass=TrainDataTemp(:,9*(i-1)+1:9*i)-repmat(MeanImageClass,1,9);%Obtain train data
PhiTrainClass=TrainDataClass;
PhiTestClass=TestDataClass;
sClass=PhiTrainClass'*PhiTrainClass;
[VClass,IClass]=eig(sClass);
[YClass,IClass] = sort(diag(IClass),'descend');
EigenVctSelClassV=VClass(:,IClass(1:EigenNumClass));
EigenVctSelClassU=PhiTrainClass*EigenVctSelClassV;
EigenVctSelClassUnorm=normc(EigenVctSelClassU);
OmegaTestClass = EigenVctSelClassUnorm'*PhiTestClass;
OmegaTrainClass = EigenVctSelClassUnorm'*PhiTrainClass;
ReconImagesClass = EigenVctSelClassUnorm*OmegaTestClass;
ReconImagesTrainClass = EigenVctSelClassUnorm*OmegaTrainClass;
ErrMatrixClass=[ErrMatrixClass;sqrt(sum((ReconImagesClass-PhiTestClass).^2))];
ErrMatrixTrainClass=[ErrMatrixTrainClass;sqrt(sum((ReconImagesTrainClass-PhiTrainClass).^2))];
end
[NIP(fold,:),AMLabelMatrix(fold,:)]=min(ErrMatrixClass);
[NIPTrain(fold,:),AMLabelTrainMatrix(fold,:)]=min(ErrMatrixTrainClass);
ErrMatrixClass=[];
ErrMatrixTrainClass=[]
end
NIPArray=[NIPArray,mean(mean(NIP,1),2)];
NIPTrainArray=[NIPTrainArray,mean(mean(NIPTrain,1),2)];
AMtime=toc/10;
AM=reshape(AMLabelMatrix,[520,1]);
LBtemp=repmat([1:52],10,1);
LB=reshape(LBtemp,[520,1]);
AMAccuracy=size(AM(AM==LB),1)/520;
AMTimeArray=[AMTimeArray,AMtime];
AMAccuracyArray=[AMAccuracyArray,AMAccuracy];
end
figure;
subplot(121)
plot(NIPArray)
title('Recon Error on Testing Data.');
xlabel('No. of Eigenvector')
ylabel('Reconstruction Error')
grid on;
subplot(122)
NIPTrainArray(end)=0;
plot(NIPTrainArray)
title('Recon Error on Training Data');
xlabel('No. of Eigenvector')
ylabel('Reconstruction Error')
grid on;
figure;
plot(AMTimeArray);
figure;
plot(AMAccuracyArray);
AM=reshape(AMLabelMatrix,[520,1]);
NN=reshape(NNLabelMatrix,[520,1]);
%LBtemp=repmat([1:52],10,1);
%LB=reshape(LBtemp,[520,1]);
AMAccuracy=size(AM(AM==LB),1)/520
NNAccuracy=size(NN(NN==LB),1)/520
cfmAM=confusionmat(LB,AM);
hmo=HeatMap(cfmAM,'Colormap','redbluecmap','RowLabels',[1:52],'ColumnLabels',[1:52]);
addTitle(hmo,'Confusion Matrix for the Alternative Method');
plot(hmo)
ctmNN=confusionmat(NN,LB);
hmo=HeatMap(ctmNN,'Colormap','redbluecmap','RowLabels',[1:52],'ColumnLabels',[1:52]);
addTitle(hmo,'Confusion Matrix for the Nearest Neighbor Method');
plot(hmo)