-
Notifications
You must be signed in to change notification settings - Fork 0
/
orl_training_test_sets.m
139 lines (116 loc) · 3.42 KB
/
orl_training_test_sets.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
function [TR, TE] = orl_training_test_sets(perc, individual)
% orldata - read face image data from orl database
%
%global
imloadfunc = 'pgma_read';
% Reduces the size of the images (by a factor 0.5)
% Set to 0 to avoid reducing. Set to 1 to reduce.
reducesize = 1;
% This is where the cbcl face images reside
thepath = './orl-faces/';
% Step through each subject and each image
fprintf('Reading in the images...\n');
i = 0;
% Get the number of directories
dirs = dir(thepath);
ndir = -2;
for i = 1:numel(dirs),
if (dirs(i).isdir)
ndir = ndir + 1;
end
end
fprintf('%d\n', ndir);
totImgs = 0;
for subj=1:ndir,
dirname = [thepath 's' num2str(subj) '/'];
dirregex = [dirname '*.pgm'];
imgNames = dir(dirregex);
fprintf('%d ', numel(imgNames));
totImgs = totImgs + numel(imgNames);
end
fprintf('\n Tot imgs %d tot dir %d\n', totImgs, ndir);
% Create the data matrix
if reducesize,
TR = zeros(46*56, totImgs * perc);
TE = zeros(46*56, totImgs * (1 - perc));
else
TR = zeros(92*112, totImgs * perc);
TE = zeros(92*112, totImgs * (1 - perc));
end
% if individual is true (1) then I will extract the test and training set
% with regard to each subject
if individual,
i = 0;
k = 0;
for subj=1:ndir,
dirname = [thepath 's' num2str(subj) '/'];
dirregex = [dirname '*.pgm'];
imgNames = dir(dirregex);
fprintf('i %d, subj %d, num %d', i, subj, numel(imgNames));
trNUM = ceil(numel(imgNames) * perc);
fprintf('TOT %d perc %f TRAINING %d\n', numel(imgNames), perc, trNUM);
perm = randperm(numel(imgNames));
j = 0;
for imag=1:numel(imgNames),
j = j + 1;
index = perm(j);
fname = [dirname num2str(index) '.pgm'];
switch imloadfunc,
case 'pgma_read',
I = pgma_read(fname);
otherwise,
I = imread(fname);
end
if reducesize,
if j <= trNUM,
i = i + 1;
TR(:,i) = reshape(imresize(I,0.5,'bilinear'),[46*56, 1]);
else
k = k + 1;
TE(:,k) = reshape(imresize(I,0.5,'bilinear'),[46*56, 1]);
end
else
if j <= trNUM,
i = i + 1;
TR(:,i) = reshape(I,[92*112, 1]);
else
k = k + 1;
TE(:,k) = reshape(I,[92*112, 1]);
end
end
end
fprintf('[%d/40]\n',subj);
end
else %random sampling
% I create a single matrix V from data
V = orldata;
samples = size(V,2);
perm = randperm(samples);
trNUM = ceil(samples * perc);
j = 0;
for i=1:samples,
if i <= trNUM,
TR(:,i) = V(:, perm(i));
else
j = j + 1;
TE(:,j) = V(:, perm(i));
end
end
end
fprintf('\n');
% % Same preprocessing as Stan Li et al
% minval = min(V);
% V = V - ones(size(V,1),1)*minval;
% maxval = max(V);
% V = (V*255) ./ (ones(size(V,1),1)*maxval);
TR = contrasten(TR);
TE = contrasten(TE);
% Additionally, this is required to avoid having any exact zeros:
% (divergence objective cannot handle them!)
TR = max(TR,1e-4);
TE = max(TE,1e-4);
% % Finally, divide by 10000 to avoid too large values for nmfsc algorithm
% V = V/10000;
TR = TR/10000;
TE = TE/10000;
% Done!