-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheigenfaces.m
73 lines (57 loc) · 1.77 KB
/
eigenfaces.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
%%%% Eigenfaces Demo
%% Generate the eigenfaces and run a demo.
clear ; close all; clc
fprintf('*** Eigenfaces ***\n');
fflush(stdout);
%% Loading and Visualizing Face Data
fprintf('Loading face dataset.\n\n');
fflush(stdout);
load('faces.mat')
n = 100;
fprintf('Visualizing first %d faces.\n\n', n);
fflush(stdout);
[plt, img] = displayImages(X(1:n, :));
saveas(plt, 'bin/faces_original.jpg')
%% PCA on Faces -> Eigenfaces
% Run PCA and visualize the eigenvectors which are in this case eigenfaces
fprintf('Running PCA on face dataset.\n\n');
fflush(stdout);
% Before running PCA, it is important to first normalize X
[X_norm, mu, sigma] = normalizeFeatures(X);
[U, S] = pca(X_norm);
n = 36;
printf('Visualizing first %d eigenfaces.\n\n', n);
fflush(stdout);
[plt, img] = displayImages(U(:, 1:n)');
saveas(plt, 'bin/eigenfaces.jpg')
%% Dimension Reduction for Faces
% Project images to the eigen space using the top k eigenvectors
fprintf('Dimension reduction for face dataset.\n\n');
fflush(stdout);
k = 100;
Z = X_norm * U(:, 1:k);
%% Recovery of Faces
% Recover the faces using the reduced encodings
fprintf('Visualizing the projected (reduced dimension) faces.\n\n');
fflush(stdout);
k = 100;
X_rec = Z * U(:,1:k)';
% Un-normalize recovered data
X_rec = bsxfun(@times, X_rec, sigma);
X_rec = bsxfun(@plus, X_rec, mu);
n = 100;
printf('Visualizing first %d recovered faces.\n\n', n);
fflush(stdout);
[plt, img] = displayImages(X_rec(1:n,:));
saveas(plt, 'bin/faces_recovered.jpg')
% Display the original faces and the recovered faces side-by-side
subplot(1, 2, 1);
[plt, img] = displayImages(X(1:n,:));
title('Original faces');
axis square;
subplot(1, 2, 2);
[plt, img] = displayImages(X_rec(1:n,:));
title('Recovered faces');
axis square;
saveas(plt, 'bin/faces_comparison.jpg')
close all