-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.m
205 lines (161 loc) · 5.84 KB
/
lab2.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%coursework: face recognition with eigenfaces
% need to replace with your own path
%addpath software;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Loading of the images: You need to replace the directory
Imagestrain = loadImagesInDirectory ( 'images/training-set/23x28/');
[Imagestest, Identity] = loadTestImagesInDirectory ( 'images/testing-set/23x28/');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Computation of the mean, the eigenvalues, amd the eigenfaces stored in the
%facespace:
ImagestrainSizes = size(Imagestrain);
Means = floor(mean(Imagestrain));
CenteredVectors = (Imagestrain - repmat(Means, ImagestrainSizes(1), 1));
CovarianceMatrix = cov(CenteredVectors);
[U, S, V] = svd(CenteredVectors);
Space = V(: , 1 : ImagestrainSizes(1))';
Eigenvalues = diag(S);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Display of the mean image:
MeanImage = uint8 (zeros(28, 23));
for k = 0:643
MeanImage( mod (k,28)+1, floor(k/28)+1 ) = Means (1,k+1);
end
figure;
subplot (1, 1, 1);
imshow(MeanImage);
title('Mean Image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display of the 20 first eigenfaces : Write your code here
EigenFace = uint8 (zeros(28, 23));
figure;
for i = 1:20
subplot(5,5,i);
imshow(reshape(Space(i,:),[28,23]), []);
title(['Eigenface- ',num2str(i)])
end
sgtitle('First 20 eigenfaces');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Projection of the two sets of images omto the face space:
Locationstrain=projectImages (Imagestrain, Means, Space);
Locationstest=projectImages (Imagestest, Means, Space);
Threshold =20;
TrainSizes=size(Locationstrain);
TestSizes = size(Locationstest);
Distances=zeros(TestSizes(1),TrainSizes(1));
%Distances contains for each test image, the distance to every train image.
for i=1:TestSizes(1),
for j=1: TrainSizes(1),
Sum=0;
for k=1: Threshold,
Sum=Sum+((Locationstrain(j,k)-Locationstest(i,k)).^2);
end,
Distances(i,j)=Sum;
end,
end,
Values=zeros(TestSizes(1),TrainSizes(1));
Indices=zeros(TestSizes(1),TrainSizes(1));
for i=1:70,
[Values(i,:), Indices(i,:)] = sort(Distances(i,:));
end,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Display of first 6 recognition results, image per image:
figure;
x=6;
y=2;
for i=1:6,
Image = uint8 (zeros(28, 23));
for k = 0:643
Image( mod (k,28)+1, floor(k/28)+1 ) = Imagestest (i,k+1);
end,
subplot (x,y,2*i-1);
imshow (Image);
title('Image tested');
Imagerec = uint8 (zeros(28, 23));
for k = 0:643
Imagerec( mod (k,28)+1, floor(k/28)+1 ) = Imagestrain ((Indices(i,1)),k+1);
end,
subplot (x,y,2*i);
imshow (Imagerec);
title(['Image recognised with ', num2str(Threshold), ' eigenfaces:',num2str((Indices(i,1))) ]);
end,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%recognition rate compared to the number of test images: Write your code here to compute the recognition rate using top 20 eigenfaces.
% Initialising the rate for test set
rate = [];
for i = 1: length(Imagestest(:,1))
% Compare the train indices with test identity. If equals then rate = 1
if ceil(Indices(i,1)/5) == Identity(i)
rate(i) = 1;
else
rate(i) = 0;
end
end
% Overall Recognition rate using 20 eigenfaces for the images in the test set
recognitionRate = (sum(rate)/70) *100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%effect of threshold (i.e. number of eigenfaces):
averageRR=zeros(1,20);
for t=1:40,
Threshold =t;
Distances=zeros(TestSizes(1),TrainSizes(1));
for i=1:TestSizes(1),
for j=1: TrainSizes(1),
Sum=0;
for k=1: Threshold,
Sum=Sum+((Locationstrain(j,k)-Locationstest(i,k)).^2);
end,
Distances(i,j)=Sum;
end,
end,
Values=zeros(TestSizes(1),TrainSizes(1));
Indices=zeros(TestSizes(1),TrainSizes(1));
number_of_test_images=zeros(1,40);% Number of test images of one given person.%YY I modified here
for i=1:70,
number_of_test_images(1,Identity(1,i))= number_of_test_images(1,Identity(1,i))+1;%YY I modified here
[Values(i,:), Indices(i,:)] = sort(Distances(i,:));
end,
rec_rate = [];
for i = 1: length(Imagestest(:,1))
% if the indices of train does not match with Identity in test then rate is 0.
if ceil(Indices(i,1)/5) == Identity(i)
rec_rate(i) = 1;
else
rec_rate(i) = 0;
end
end
recognition_rate = sum(rec_rate)/70 *100;
averageRR(1,t) = recognition_rate;
end,
figure;
plot(averageRR(1,:));
title('Recognition rate against the number of eigenfaces used');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%effect of K: You need to evaluate the effect of K in KNN and plot the recognition rate against K. Use 20 eigenfaces here.
TrainingLabels = []; % initialize the training labels
for i = 1:40
TrainingLabels = horzcat(TrainingLabels, repmat(i,1,5));
end
% Overall Recoginition Rate for different k values
overall_recog_rate = [];
for k = 1:200 % iterating over k neighbours (i.e. from 1 to 200)
%fit knn model to training data
knn_model = fitcknn(Imagestrain, TrainingLabels, 'NumNeighbors', k,'BreakTies', 'nearest');
knn_predict = predict(knn_model, Imagestest); % Prediction on the test data
% Initalise recoginition rate for every k
knn_recog_rate = [];
for i = 1:length(Imagestest(:,1))
% Compare the predictions with identity of test image and predicted
if ceil(Indices(i,1)/5) == knn_predict(i)
knn_recog_rate(i) = 1;
else
knn_recog_rate(i) = 0;
end
end
overall_recog_rate(k) = ((sum(knn_recog_rate)/70)*100);
end
figure
plot(1:200, overall_recog_rate);
xlabel('K'); ylabel('Recognition rate')
title('Recoginition rate against different k');