forked from mrychlik/faces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
104 lines (90 loc) · 2.47 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
%----------------------------------------------------------------
% File: main.m
%----------------------------------------------------------------
%
% Author: Marek Rychlik (rychlik@arizona.edu)
% Date: Fri Nov 22 20:02:05 2024
% Copying: (C) Marek Rychlik, 2020. All rights reserved.
%
%----------------------------------------------------------------
% A basic face recognition system workflow
%
targetSize=[128,128];
location = fullfile('lfw');
svd_cache = fullfile('cache','svd.mat');
mkdir(fullfile('cache'));
disp('Creating image datastore...');
imds = imageDatastore(location,'IncludeSubfolders',true,'LabelSource','foldernames',...
'ReadFcn', @(filename)imresize(im2gray(imread(filename)),targetSize));
montage(preview(imds));
disp('Reading all images...');
A = readall(imds);
% Play faces
if false
for j=1:length(A)
imshow(A{j}),title(imds.Labels(j),'Interpreter','none');
colormap gray;
drawnow;
pause(1);
end
end
B = cat(3,A{:});
imshow(B(:,:,1))
D = prod(targetSize);
B = reshape(B,D,[]);
disp('Normalizing data...');
B = single(B)./256;
%[N,C,SD] = normalize(B);
N=B;
if exist(svd_cache,'file') == 2
disp('Loading SVD from cache...');
load(svd_cache)
else
disp('Finding SVD...');
tic;
[U,S,V] = svd(N,'econ');
toc;
disp('Writing SVD cache...')
save(svd_cache,'U','S','V');
end
if false
for j=1:size(U,2)
imagesc(reshape(U(:,j), targetSize));
title([num2str(j),': ',num2str(S(j,j))]);
colormap gray;
drawnow;
pause(1);
end
end
% N = U*S*V';
k=512;Z=U(:,1:k)*S(1:k,1:k)*V(:,1:k)';
if false
colormap gray;
for j=1:size(Z,2)
imagesc(reshape(Z(:,j),targetSize));
title(imds.Labels(j),'Interpreter','none');
colormap gray;
drawnow;
pause(0.5);
end
end
disp('Training Support Vector Machine...');
X = V(:,1:k);
Y = imds.Labels=='George_W_Bush';
% Map to +1/-1
Y = 2.*Y-1;
tTree = templateTree('surrogate','on');
tEnsemble = templateEnsemble('GentleBoost',100,tTree);
%mdl = fitcsvm( X, Y,'Verbose',true);
options = statset('UseParallel',true);
Mdl = fitcecoc(X,Y,'Coding','onevsall','Learners',tEnsemble,...
'Prior','uniform','NumBins',50,'Options',options,...
'Verbose',2);
disp('Testing on "Dabya"...');
W = X(3949,:);
I = reshape(U(:,1:k)*S(1:k,1:k)*W',targetSize);
imagesc(I);
colormap gray;
drawnow;
disp('Running prediction...');
[label, score] = predict(Mdl, W)