/
Recognizer.m
171 lines (158 loc) · 6.32 KB
/
Recognizer.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
classdef Recognizer < handle
%RECOGNIZER Facial recognition using the eigenfaces algorithm.
% Documentation forthcoming...
% Copyright (C) 2012 Kaelin Colclasure
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
properties
LabelNames = {};
KnownSet = repmat(struct('label', 0, 'image', []), 1, 0);
Training = struct('isvalid', false, 'mean', [], 'eigenfaces', [], ...
'indexlabel', [], 'labelcoord', [], 'rawepsilon', [], 'epsilon', Inf);
end
methods
function [ self ] = Recognizer( filename )
self = self@handle;
if nargin > 0
matf = matfile(filename);
self.LabelNames = matf.labelNames;
self.KnownSet = matf.knownSet;
self.Training = matf.training;
end
end
function addKnown( self, labelname, image )
set = self.KnownSet;
set(end + 1).label = self.labelForName(labelname);
set(end).image = image;
self.KnownSet = set;
end
function [ label ] = labelForName( self, labelname )
tmp = arrayfun(@(name) isequal(name{1}, labelname), self.LabelNames);
label = find(tmp);
if (isempty(label))
self.LabelNames(end + 1) = {labelname};
label = length(self.LabelNames);
end
end
function saveState( self, filename )
matf = matfile(filename, 'Writable', true);
matf.labelNames = self.LabelNames;
matf.knownSet = self.KnownSet;
matf.training = self.Training;
end
function [ label, score, W ] = step( self, image )
X = image(:) - self.Training.mean; % Normalize
W = self.Training.eigenfaces' * X; % Compute coordinates in eigenfaces space
[label, score] = self.assignLabelAndScore(W);
end
function [ stats ] = train( self, k )
self.Training.isvalid = false;
self.Training.epsilon = Inf;
tic;
nlabels = length(self.LabelNames);
fprintf('Training with %i labels on %i images (k = %i):\n', ...
nlabels, length(self.KnownSet), k);
% PCA considers only one image for each label.
T = self.partitionKnownSet(1);
self.Training.mean = mean(T, 2);
T = T - repmat(self.Training.mean, 1, size(T, 2)); % Normalize
L = T' * T;
eigs_k = min(30, size(T, 2) - 1);
fprintf('Computing %i eigenfaces dimensions.\n', eigs_k);
[E, ~] = eigs(L, eigs_k);
self.Training.eigenfaces = normc(T * E); % Compute eigenfaces
% Now partition again, using k images to derive the coordinates
% (including the image used for PCA).
[T, V, labelT, labelV] = self.partitionKnownSet(k);
T = T - repmat(self.Training.mean, 1, size(T, 2)); % Normalize
WT = self.Training.eigenfaces' * T; % Compute coordinates in eigenfaces space
% For a first approximation, save all of the training image
% coordinates.
self.Training.indexlabel = labelT;
self.Training.labelcoord = WT;
self.computeRawEpsilon(labelT, WT);
% Now we run through the validation set and see if we can learn
% a suitable final value for epsilon.
V = V - repmat(self.Training.mean, 1, size(V, 2)); % Normalize
WV = self.Training.eigenfaces' * V; % Compute coordinates in eigenfaces space
[labelA, scoreA] = self.assignLabelAndScore(WV);
% In the interest of starting simple, initially we'll base
% epsilon solely on the measured variance in the training set.
epsilon = quantile(self.Training.rawepsilon, .50)
elideA = scoreA > epsilon;
self.Training.epsilon = epsilon;
self.Training.isvalid = true;
stats.T = T;
stats.labelT = labelT;
% stats.L = L;
% stats.E = E;
stats.WT = WT;
stats.V = V;
stats.labelV = labelV;
stats.WV = WV;
stats.labelA = labelA;
stats.scoreA = scoreA;
stats.elideA = elideA;
toc;
end
end
methods (Access = private)
function [ label, score ] = assignLabelAndScore( self, W )
ncoord = size(self.Training.labelcoord, 2);
n = size(W, 2);
label = zeros(1, n);
score = NaN(1, n);
for i = 1:n
scores = arrayfun(@(j) norm(W(:, i) - self.Training.labelcoord(:, j)), 1:ncoord);
score(i) = min(scores);
if score(i) < self.Training.epsilon
label(i) = self.Training.indexlabel(scores == score(i));
end
end
end
function computeRawEpsilon( self, labelT, WT )
% Compute the raw epsilon score for each label.
nlabels = length(self.LabelNames);
self.Training.rawepsilon = NaN(1, nlabels);
for i = 1:nlabels
X = WT(:, labelT == i);
meanX = mean(X, 2);
highscore = max(arrayfun(@(j) norm(X(:, j) - meanX), 1:size(X, 2)));
if highscore > 0
self.Training.rawepsilon(:, i) = highscore;
end
end
end
function [ T, V, labelT, labelV ] = partitionKnownSet( self, k )
nlabels = length(self.LabelNames);
T = [];
V = [];
labelT = [];
labelV = [];
for i = 1:nlabels
selected = [self.KnownSet.label] == i;
% fprintf(' %-20s %2i image(s)\n', self.LabelNames{i}, sum(selected));
indexes = find(selected);
assert(~isempty(indexes), 'KnownSet contains no images for label %i', i);
for j = 1:min(k, length(indexes))
T = [T self.KnownSet(indexes(j)).image(:)];
labelT = [labelT i];
end
for j = j + 1:length(indexes)
V = [V self.KnownSet(indexes(j)).image(:)];
labelV = [labelV i];
end
end
end
end
end