-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFDAtest.m
277 lines (221 loc) · 8.54 KB
/
FDAtest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
%% FDA magic
% it is recomended that your machine has atleast 8GB of ram. 4GB will most
% likely not be enough. You may need to clear portions of your workspace
% during the program.
% script created by alex odonnell
fprintf(['The program has been paused. This flag has been placed here to avoid \n '...
'the scenario where the user accidentally runs the entire script. It is \n'...
'recommended that you run it by section in order to gain a better understanding \n'...
'of the methods used during the experiment. Press any key to continue\n'])
pause;
%% start by clearing the workspace
% =========================================================================
fprintf(['Are you sure you wish to clear the workspace? Press any key to continue\n'])
pause;
clear
clc
close all
%% setting parameters
% =========================================================================
facefolder = 'img_align_celeba_crop/img_align_celeba_crop/';
nonfacefolder = 'non_face/';
generated = 'generated_faces/generated_faces/';
a = dir(facefolder);
a = a(3:end);
b = dir(nonfacefolder);
b = b(3:end);
c = dir(generated);
c = c(3:end);
n_imgs = 48000; % total number of images for each class
imgsize = 64*64*1; % 64x64 b and w
% initialize features
X = zeros(2*(n_imgs),imgsize);
Xgen = zeros(5000,imgsize);
Zgen = zeros(1,5000);
% set up classes
Y = zeros(2*n_imgs,1);
Y(1:n_imgs) = 1;
% Using 10 fold cross validation
partition = cvpartition(Y,'KFold',10);
%% Read images and create feature matrix
% =========================================================================
tic
for i = 1 : n_imgs
if mod(i,1000) == 0
disp(i);
end
I = rgb2gray(imread([facefolder,a(i).name]));
X(i,:) = I(:);
I = imread([nonfacefolder,b(i).name]);
X((i+n_imgs),:) = I(:);
end
disp('Done reading images.')
toc
%% clear unused variables
clear a b facefolder nonfacefolder
%% run FDA. May take a while. Load W and Z from the model to avoid this.
% =========================================================================
tic
[Z,W] = FDA(X',Y);
disp('Done FDA. New dimentionality reduced feature is stored in Z.')
toc
%% test FDA by transforming some artificial faces
% =========================================================================
tic
for i = 1:5000
if mod(i,1000) == 0
disp(i);
end
I = rgb2gray(imread([generated,c(i).name]));
Xgen((i),:) = I(:);
Zgen(i) = W'*double(Xgen((i),:))';
end
disp('Done transforming artificaial images.')
toc
%% clear some more unused variables
clear c generated
%% histogram of our new feature. Zgen is used here to show the relationship
% between the distributions of real and generated faces.
% =========================================================================
figure(1)
hold on;
histogram(Z(Y==0),'FaceColor',[0.8 0 0])
histogram(Z(Y==1),'FaceColor',[0 0.8 0])
histogram(Zgen,'FaceColor',[0 0 0.8])
legend('Artificial', 'Not Faces', 'Real Faces');
hold off;
%%
% LDA
% =========================================================================
LDApredictions = zeros(10,5000);
tic
for L = 1:10 % for each partition
% separate training and test data
idxTrn = training(partition,L); % training set indices
idxTest = test(partition,L); % test set indices
% train discriminator for each feature
linDisc = fitcdiscr(Z(idxTrn)',Y(idxTrn));
% n is the numer of test elements
n = size(Y(idxTest),1);
%Accuracy set q of cross validation
accuracyLDA(L) = sum(Y(idxTest) == predict(linDisc, Z(idxTest)'))/n;
LDApredictions(L,:) = predict(linDisc, Zgen')/n;
end
disp(['LDA complete. The mean accuracy was ' num2str(mean(accuracyLDA)) '.'])
toc
%% visualize a subset of the generated images that failed the linear discriminator
failidx = find(LDApredictions(1,:) == 0);
disp(['There were ' num2str(size(failidx,2)) ' samples that failed the LDA discriminator.'])
for i = 1:64
failedimg{i} = reshape(Xgen(failidx(i),:),[64,64]);
end
imagemat = cat(4,failedimg{1:64});
figure(2)
montage(imagemat, 'DisplayRange', [0 255]);
%% Select optimum K value and distance metric
% =========================================================================
arithmetic_index = 1;
ind = 1;
max_k = 30;
distance_metric = {'cityblock','chebychev','euclidean'};
accuracyKNN = zeros(10,1);
accuracyMean_k = zeros(30,1);
accuracyMean_m = zeros(3,1);
k_best = accuracyMean_m;
% Test with different values of k
figure(5)
hold on;
for m = 1:3
tic
for k = 1:max_k
for L = 1:10 % for each partition
idxTrn = training(partition,L); % training set indices
idxTest = test(partition,L); % test set indices
KNNDisc = fitcknn(Z(idxTrn)',Y(idxTrn), 'NumNeighbors', k, 'Distance', distance_metric{m} ); % Update value of k with each iteration
% n is the numer of test elements
%
n = size(Y(idxTest),1);
accuracyKNN(L) = sum((predict(KNNDisc, Z(idxTest)')) == Y(idxTest))/n;
% n is the numer of test elements
n = size(Y(idxTest),1);
end
accuracyMean_k(k) = mean(accuracyKNN);
end
accuracyMean_m(m) = mean(accuracyMean_k);
disp(['KNN complete using ' distance_metric{m} ' distance.'])
% Choose the value of k that yields the best perfo rmance
k_best(m) = find(accuracyMean_k == max(accuracyMean_k));
disp(['Optimal k value below ' num2str(max_k) ' for ' distance_metric{m} 'distance: ' num2str(k_best(m)) '.'] )
% plot accuracy vs number of partitions
plot([1:30],accuracyMean_k)
xlim([1 30])
toc
end
legend(distance_metric);
%% plot accuracy vs number of partitions
figure(5)
plot([1:30],accuracyMean_k)
%% Free up some more space
clear accuracyMean_m accuracyMean_k
%% train KNN using best model
% this is also after we concluded that the distance metrics all work the
% same so we'll just use Euclidean distance (m = 1). Retraining KNN is a
% sideffect of clearing our model everytime. We didn't do this in order to
% preserve as many resources as we can
m = 1;
% =========================================================================
KNNpredictions = zeros(10,5000);
k = k_best(m);
tic
for L = 1:10 % for each partition
idxTrn = training(partition,L); % training set indices
idxTest = test(partition,L); % test set indices
KNNDisc = fitcknn(Z(idxTrn)',Y(idxTrn), 'NumNeighbors', k, 'Distance', distance_metric{m} ); % Update value of k with each iteration
% n is the numer of test elements
%
n = size(Y(idxTest),1);
accuracyKNN(L) = sum((predict(KNNDisc, Z(idxTest)')) == Y(idxTest))/n;
% n is the numer of test elements
n = size(Y(idxTest),1);
% test with artificial faces
KNNpredictions(L,:) = predict(KNNDisc, Zgen')/n;
end
accuracyMean = mean(accuracyKNN);
disp(['KNN complete. With optimal parameters, mean accuracy for all partitions: ' num2str(accuracyMean) '.'])
toc
%% visualize a subset of the generated images that failed the KNN discriminator
failidx = find(KNNpredictions(1,:) == 0);
disp(['There were ' num2str(size(failidx,2)) ' samples that failed the KNN discriminator.'])
for i = 1:64
failedimg{i} = reshape(Xgen(failidx(i),:),[64,64]);
end
imagemat = cat(4,failedimg{1:64});
figure(3)
montage(imagemat, 'DisplayRange', [0 255]);
%% SVM (takes roughly 15 mins for an i7 CPU with 16 gigs of RAM)
% =========================================================================
SVMpredictions = zeros(10,5000);
tic
for L = 1:10 % for each partition
% separate training and test data
idxTrn = training(partition,L); % training set indices
idxTest = test(partition,L); % test set indices
% train discriminator for each feature
SVMDisc = fitcsvm(Z(idxTrn)',Y(idxTrn));
% n is the numer of test elements
n = size(Y(idxTest),1);
%Accuracy set q of cross validation
accuracy(L) = sum(Y(idxTest) == predict(SVMDisc, Z(idxTest)'))/n;
SVMpredictions(L,:) = predict(SVMDisc, Zgen')/n;
end
disp('SVM complete.')
toc
%% visualize a subset of the generated images that failed the SVM discriminator
failidx = find(SVMpredictions(1,:) == 0);
disp(['There were ' num2str(size(failidx,2)) ' samples that failed the SVM discriminator.'])
for i = 1:64
failedimg{i} = reshape(Xgen(failidx(i),:),[64,64]);
end
imagemat = cat(4,failedimg{1:64});
figure(4)
montage(imagemat, 'DisplayRange', [0 255]);