-
Notifications
You must be signed in to change notification settings - Fork 1
/
FaceDataset.m
executable file
·180 lines (164 loc) · 6.92 KB
/
FaceDataset.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
classdef FaceDataset < handle
%FaceDataset Loads features from file and creates the train and test
%sets
properties %(SetAccess = protected)
info % face dataset info (FaceDatasetInfo type)
gallery % gallery of training images (Gallery type)
num_test % number of test images
num_val
test_db % test dataset of features
val_db
test_ID % test IDs of features
val_ID
K = 1 % num training images x subject
num_feats % overall num. of features
idx_train_test
idxABS
TestAugSplit
end
methods
% load a face dataset
function fds = FaceDataset(name, SERVER)
fds.info = FaceDatasetInfo(name, SERVER);
end
function set.gallery(fds,gallery)
fds.gallery = gallery;
end
% set the num of training images x subject
function set.K(fds,K)
fds.K = K;
end
% load features from files and create train & test set. If seed
% is set it is used to set the seed of random gen
function create_train_test_sets_SingleImg(fds,seed, DictSplit, TestAugSplit, LDAon)
if nargin >= 2
rng(seed)
end
feat_filename = fds.set_filenames();
if nargin<3
DictSplit = ones(1,size(feat_filename,1));
end
if nargin<4
TestAugSplit = zeros(1,size(feat_filename,1));
TestAugSplit(round(end/2))=1;
end
nDicts = max(DictSplit); %n. of distinct dictionaries to be referred
if size(TestAugSplit,1) >1
SwitchOnTestFeat = max(TestAugSplit);
else
SwitchOnTestFeat = TestAugSplit;
end
nTestAugmented = sum(SwitchOnTestFeat); %n. of augmeted view for each test image
fds.TestAugSplit = TestAugSplit;
train_LDA = cell(1,nDicts); % train feature struct
fds.test_db = cell(1,nTestAugmented); % test feature struct
fds.val_db = cell(1,nTestAugmented);
train_ID = [];
nFeats = 0;
nTest = 0;
for fname = feat_filename
load(fname{1})
nFeats = nFeats+1;
if strcmp(fname{1},'RAW')
I{nFeats} = imgdb;
else
I{nFeats} = imgdb_feat;
end
if nFeats == 1 % define train and tests set IDs
idx = fds.define_IDs(I{1}.class_ID);
fds.idxABS =idx;
fds.test_ID = I{1}.class_ID(idx.test);
fds.val_ID = I{1}.class_ID(idx.val);
fds.num_test = length(fds.test_ID);
fds.num_val = length(fds.val_ID);
end
%Dict data organization, according to DictSplit
train_LDA{DictSplit(nFeats)} = [train_LDA{DictSplit(nFeats)}, I{nFeats}.images(:,idx.train)];
if DictSplit(nFeats)==1
train_ID = [train_ID, I{nFeats}.class_ID(idx.train)];
end
if SwitchOnTestFeat(nFeats)
nTest = nTest +1;
%test data organization according to TestAugmented
fds.test_db{nTest} = I{nFeats}.images(:,idx.test);
fds.val_db{nTest} = I{nFeats}.images(:,idx.val);
end
end
fds.gallery = Gallery(train_ID,train_LDA, 1, LDAon);
fds.idx_train_test = idx;
end
% disp object
function disp(fds)
fprintf(' *** REPORT on face dataset ***\n');
fprintf(' Dataset name : %s\n',fds.info.name);
fprintf(' Number of subjects : %d\n',fds.info.num_sbj);
fprintf(' Number of features : %d\n',fds.num_feats);
fprintf(' Num training images : %d\n',fds.gallery.num_train);
fprintf(' Num Validation images : %d\n',fds.num_val);
fprintf(' Num test images : %d\n',fds.num_test);
fprintf(' Num training img per subject : %d\n',fds.K);
end
end
methods (Access = private)
% check and set filenames of illuminations x features
function filename = set_filenames(fds)
h = fds.info;
num_file = numel(h.feat_name)*numel(h.illu_name);
filename = cell(1,num_file);
n = 0;
warn = '';
for j = 1:numel(h.illu_name)
for i = 1:numel(h.feat_name)
p = [h.path{1} h.db_name '/' h.filename '_' h.illu_name{j} '_' h.feat_name{i} '.mat'];
if exist(p,'file')
n = n+1;
filename{n} = p;
else
warn = strcat(warn,[p ',']);
end
end
end
if num_file ~= n
warning('Warning: features %s not found!',warn);
filename = filename(~cellfun('isempty',filename)); % remove empty cells
end
fds.num_feats = n;
end
% define the train and test indeces for db selection
function idx = define_IDs(fds,IDs)
IDu = unique(IDs);
num_sbj = length(IDu);
idx.test = [];
idx.val = [];
idx.train = [];
IDrand = randperm(size(IDu,2));
s = 0;
sb = 0;
validation_num = 0; % Number of examples for validation
while sb < fds.info.num_sbj & s < size(IDrand,2)
s = s+1;
nc = sum(IDs == IDu(IDrand(s)));
if nc>=2 % at least 2 images
sb = sb+1;
s_idx = find(IDs == IDu(IDrand(s)));
rd = randperm(nc);
T = sort(s_idx(rd(1:fds.K)));
idx.train = [idx.train T]; % select K imgs of sbj s
V = sort(s_idx(rd(fds.K+1:fds.K + validation_num)));
idx.val = [idx.val V];
idx.test = [idx.test setdiff(setdiff(s_idx,T),V)]; % take the remaining imgs of sbj s
end
end
s=0;
while length(idx.train) < fds.info.num_sbj %GUARANTEE THAT AT LEAST THE TRAIN RESPECT THE CARDINALITY!
s = s+1;
nc = sum(IDs == IDu(IDrand(s)));
if nc==1
sb = sb+1;
s_idx = find(IDs == IDu(IDrand(s)));
idx.train = [idx.train s_idx]; % select K imgs of sbj s
end
end
end
end
end