This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSVMs.py
More file actions
482 lines (415 loc) · 18.9 KB
/
SVMs.py
File metadata and controls
482 lines (415 loc) · 18.9 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python3
"""
Python script to evaluate facial expression recognition using SVM classifiers.
In essence, this script will produce accuracy scores for different methods of
classifier when performing emotional recognition on a dataset. From my tests
I show a SVM to be the best way of achieving this that I have tested within
this scope, and so this script may be extended into the music player component
of the project to demonstrate a use of this technology (unless I can adapt a
CNN approach).
From ``Hands-On Machine Learning with Scikit-Learn & TensorFlow'' -
A Support Vector Machine (SVM) is a very powerful and versatile Machine
Learning model, capable of performing linear or nonlinear classification,
regression, and even outlier detection.''
I use OpenCV and dlib to build and manage the datasets, and the sklearn library
to obtain an implementation of a support vector machine (SVM). Here, I focus on
building 3 SVMs with different kernels and will evaluate their performance
based on the combined dataset I have created previously. Currently, each SVM is
trained using a training set made from a random sample of 80% of the
dataset and then tested on the remaining 20%. Facial landmark vectors and
eigenfaces vectors are both used for feature extraction.
I am evaluating the performance on a grayscale set of images and the facial
expression labels declared. The kernels I will be testing are linear,
radial basis function, and polynomial kernels.
After allocation of the dataset (will take a while, due to sample size), I test
each method and label its output.
"""
# Import packages.
import os
import time
import csv
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC, SVC
# My imports.
import extraction_model as exmodel
import evaluation_model as evmodel
from sort_database.utils import EMOTIONS_8, EMOTIONS_5
print(__doc__)
# Start the script.
script_name = os.path.basename(__file__) # The name of this script
print("\n{}: Beginning Support Vector Machine tests...\n".format(script_name))
start = time.clock() # Start of the speed test. clock() is most accurate.
# Build classifiers.
params = {}
for key, val in csv.reader(open('results/lin_params.csv')):
params[key] = val
for k, v in params.items():
for val in v:
svc = LinearSVC(C=0.01, loss="hinge")
svc = svc.set_params(**{k: val})
lin_svm_clf = Pipeline((
("scaler", StandardScaler()),
("linear_svc", svc)
))
print("Linear Classifier: ", lin_svm_clf)
params = {}
for key, val in csv.reader(open('results/rbf_params.csv')):
params[key] = val
for k, v in params.items():
for val in v:
svc = SVC(kernel="rbf", C=5, gamma=0.01, probability=True)
svc = svc.set_params(**{k: val})
rbf_svm_clf = Pipeline((
("scaler", StandardScaler()),
("svc_clf", svc)
))
print("Radial Basis Function Classifier: ", rbf_svm_clf)
params = {}
for key, val in csv.reader(open('results/poly_params.csv')):
params[key] = val
for k, v in params.items():
for val in v:
svc = SVC(kernel="poly", C=1, probability=True)
svc = svc.set_params(**{k: val})
poly_svm_clf = Pipeline((
("scaler", StandardScaler()),
("svc_clf", svc)
))
print("Polynomial Classifier: ", poly_svm_clf)
""" Test the classifiers! """
# Array's to hold all the acc. data to average.
# Linear Kernel.
linear_scores = []
linear_scores1 = []
linear_fit_times = []
linear_fit_times1 = []
linear_score_times = []
linear_score_times1 = []
# Radial Basis Function Kernal.
rbf_scores = []
rbf_scores1 = []
rbf_fit_times = []
rbf_fit_times1 = []
rbf_score_times = []
rbf_score_times1 = []
# Polynomial Kernel.
poly_scores = []
poly_scores1 = []
poly_fit_times = []
poly_fit_times1 = []
poly_score_times = []
poly_score_times1 = []
# Timing variables.
fit_time_start = 0
fit_time_stop = 0
score_time_start = 0
score_time_stop = 0
fit_time = 0
score_time = 0
for i in range(0, 5): # 5 testing runs
print("\nROUND {}\n".format(i+1))
X_train, y_train, X_test, y_test = exmodel.get_sets(EMOTIONS_8)
X_train1, y_train1, X_test1, y_test1 = exmodel.get_sets(EMOTIONS_5)
X_train_pca, y_train_pca, X_test_pca, y_test_pca = exmodel.get_sets_pca(EMOTIONS_8)
X_train_pca1, y_train_pca1, X_test_pca1, y_test_pca1 = exmodel.get_sets_pca(EMOTIONS_5)
# Change to numpy arrays as classifier expects them in this format.
X_train = np.array(X_train)
X_test = np.array(X_test)
X_train1 = np.array(X_train1)
X_test1 = np.array(X_test1)
# Number of classes.
n_classes = len(EMOTIONS_8)
n_classes1 = len(EMOTIONS_5)
""" LINEAR KERNEL // LIST 1 // LANDMARKS """
# Train the support vector machine.
print("***> Training SVM with LINEAR kernel, landmarks, list 1.")
fit_time_start = time.clock()
y_pred = lin_svm_clf.fit(X_train, y_train).predict(X_test)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
# Get the score for the classifier (percent it got correct).
print("Scoring the classifier on the prediction list 1.")
score_time_start = time.clock()
linear_score = lin_svm_clf.score(X_test, y_test)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
# Output classification report and confusion matrix.
name = "linear8L({})".format(i+1)
evmodel.report(y_test, y_pred, n_classes, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), False, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), True, name)
# Output the results.
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(linear_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
# Append this rounds scores and times to the respective metascore array.
linear_scores.append(linear_score)
linear_fit_times.append(fit_time)
linear_score_times.append(score_time)
""" LINEAR KERNEL // LIST 1 // PCA """
# Train the support vector machine.
print("***> Training SVM with LINEAR kernel, PCA, list 1.")
fit_time_start = time.clock()
y_pred = lin_svm_clf.fit(X_train_pca, y_train_pca).predict(X_test_pca)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
# Get the score for the classifier (percent it got correct).
print("Scoring the classifier on the prediction list 1.")
score_time_start = time.clock()
linear_score = lin_svm_clf.score(X_test_pca, y_test_pca)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
# Output classification report and confusion matrix.
name = "linear8P({})".format(i+1)
evmodel.report(y_test_pca, y_pred, n_classes, name)
evmodel.matrix(y_test_pca, y_pred, np.unique(y_train_pca), False, name)
evmodel.matrix(y_test_pca, y_pred, np.unique(y_train_pca), True, name)
# Output the results.
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(linear_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
# Append this rounds scores and times to the respective metascore array.
linear_scores.append(linear_score)
linear_fit_times.append(fit_time)
linear_score_times.append(score_time)
""" LINEAR KERNEL // LIST 2 // LANDMARKS """
print("***> Training SVM with LINEAR kernel, landmarks, list 2.")
fit_time_start = time.clock()
y_pred = lin_svm_clf.fit(X_train1, y_train1).predict(X_test1)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
print("Scoring the classfier on the prediction set 2.")
score_time_start = time.clock()
linear_score = lin_svm_clf.score(X_test1, y_test1)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
name = "linear5L({})".format(i+1)
evmodel.report(y_test1, y_pred, n_classes1, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), False, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), True, name)
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(linear_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
linear_scores1.append(linear_score)
linear_fit_times1.append(fit_time)
linear_score_times1.append(score_time)
""" LINEAR KERNEL // LIST 2 // PCA """
# Train the support vector machine.
print("***> Training SVM with LINEAR kernel, PCA, list 2.")
fit_time_start = time.clock()
y_pred = lin_svm_clf.fit(X_train_pca1, y_train_pca1).predict(X_test_pca1)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
# Get the score for the classifier (percent it got correct).
print("Scoring the classifier on the prediction list 1.")
score_time_start = time.clock()
linear_score = lin_svm_clf.score(X_test_pca1, y_test_pca1)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
# Output classification report and confusion matrix.
name = "linear5P({})".format(i+1)
evmodel.report(y_test_pca1, y_pred, n_classes, name)
evmodel.matrix(y_test_pca1, y_pred, np.unique(y_train_pca1), False, name)
evmodel.matrix(y_test_pca1, y_pred, np.unique(y_train_pca1), True, name)
# Output the results.
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(linear_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
# Append this rounds scores and times to the respective metascore array.
linear_scores.append(linear_score)
linear_fit_times.append(fit_time)
linear_score_times.append(score_time)
""" RBF KERNEL // LIST 1 // LANDMARKS """
print("***> Training SVM with RBF kernel, landmarks, list 1.")
fit_time_start = time.clock()
y_pred = rbf_svm_clf.fit(X_train, y_train).predict(X_test)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
print("Scoring the classfier on the prediction set 1.")
score_time_start = time.clock()
rbf_score = rbf_svm_clf.score(X_test, y_test)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
name = "rbf8L({})".format(i+1)
evmodel.report(y_test, y_pred, n_classes, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), False, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), True, name)
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(rbf_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
rbf_scores.append(rbf_score)
rbf_fit_times.append(fit_time)
rbf_score_times.append(score_time)
""" RBF KERNEL // LIST 2 // LANDMARKS """
print("Training SVM with RBF kernel, landmarks, list 2.")
fit_time_start = time.clock()
y_pred = rbf_svm_clf.fit(X_train1, y_train1).predict(X_test1)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
print("Scoring the classfier on the prediction set 2.")
score_time_start = time.clock()
rbf_score = rbf_svm_clf.score(X_test1, y_test1)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
name = "rbf5L({})".format(i+1)
evmodel.report(y_test1, y_pred, n_classes1, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), False, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), True, name)
with open('results/{}'.format(name), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(rbf_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
rbf_scores1.append(rbf_score)
rbf_fit_times1.append(fit_time)
rbf_score_times1.append(score_time)
""" POLY KERNEL // LIST 1 // LANDMARKS """
print("Training SVM with POLYNOMIAL kernel, landmarks, list 1.")
fit_time_start = time.clock()
y_pred = poly_svm_clf.fit(X_train, y_train).predict(X_test)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
print("Scoring the classfier on the prediction set 1.")
score_time_start = time.clock()
poly_score = poly_svm_clf.score(X_test, y_test)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
name = "poly8L({})".format(i+1)
evmodel.report(y_test, y_pred, n_classes, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), False, name)
evmodel.matrix(y_test, y_pred, np.unique(y_train), True, name)
with open('results/{}'.format(name, i+1), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(poly_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
poly_scores.append(poly_score)
poly_fit_times.append(fit_time)
poly_score_times.append(score_time)
""" POLY KERNEL // LIST 2 // LANDMARKS """
print("Training SVM with POLYNOMIAL kernel, landmarks, list 2.")
fit_time_start = time.clock()
y_pred = poly_svm_clf.fit(X_train1, y_train1).predict(X_test1)
fit_time_stop = time.clock()
fit_time = fit_time_stop-fit_time_start
print("Scoring the classfier on the prediction set 2.")
score_time_start = time.clock()
poly_score = poly_svm_clf.score(X_test1, y_test1)
score_time_stop = time.clock()
score_time = score_time_stop-score_time_start
name = "poly5L({})".format(i+1)
evmodel.report(y_test1, y_pred, n_classes1, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), False, name)
evmodel.matrix(y_test1, y_pred, np.unique(y_train1), True, name)
with open('results/{}'.format(name, i+1), "w") as text_file:
print(name, file=text_file)
print("> {:.2f} percent correct.".format(poly_score*100),
file=text_file)
print("> Fit time {}".format(fit_time), file=text_file)
print("> Test time {}".format(score_time), file=text_file)
poly_scores1.append(poly_score)
poly_fit_times1.append(fit_time)
poly_score_times1.append(score_time)
""" END OF THE ROUND """
with open('results/output{}'.format(i+1), "w") as text_file:
print("\n{}: Test round {} - ".format(script_name, (i+1)),
file=text_file)
print("\nSVM, LINEAR, Landmarks, List 1:",
file=text_file)
print("> {:.2f} percent correct.".format(linear_scores[i]*100),
file=text_file)
print("> Fit time {}".format(linear_fit_times[i]),
file=text_file)
print("> Test time {}".format(linear_score_times[i]),
file=text_file)
print("\nSVM, LINEAR, Landmarks, List 2:",
file=text_file)
print("> {:.2f} percent correct.".format(linear_scores1[i]*100),
file=text_file)
print("> Fit time {}".format(linear_fit_times1[i]),
file=text_file)
print("> Test time {}".format(linear_score_times1[i]),
file=text_file)
print("\nSVM, RBF, Landmarks, List 1:",
file=text_file)
print("> {:.2f} percent correct.".format(rbf_scores[i]*100),
file=text_file)
print("> Fit time {}".format(rbf_fit_times[i]),
file=text_file)
print("> Test time {}".format(rbf_score_times[i]),
file=text_file)
print("\nSVM, RBF, Landmarks, List 2:",
file=text_file)
print("> {:.2f} percent correct.".format(rbf_scores1[i]*100),
file=text_file)
print("> Fit time {}".format(rbf_fit_times1[i]),
file=text_file)
print("> Test time {}".format(rbf_score_times1[i]),
file=text_file)
print("\nSVM, POLYNOMIAL, Landmarks, List 1:",
file=text_file)
print("> {:.2f} percent correct.".format(poly_scores[i]*100),
file=text_file)
print("> Fit time {}".format(poly_fit_times[i]),
file=text_file)
print("> Test time {}".format(poly_score_times[i]),
file=text_file)
print("\nSVM, POLYNOMIAL, Landmarks, List 2:",
file=text_file)
print("> {:.2f} percent correct.".format(poly_scores1[i]*100),
file=text_file)
print("> Fit time {}".format(poly_fit_times1[i]),
file=text_file)
print("> Test time {}".format(poly_score_times1[i]),
file=text_file)
""" END OF THE TEST """
with open('results/final_output', "w") as text_file:
print("\nAverages for SVM with Linear kernel / Landmarks / set 1 - {:.2f}%, {}, {}."
.format((np.mean(linear_scores)*100),
(np.mean(linear_fit_times)),
(np.mean(linear_score_times))), file=text_file)
print("Averages for SVM with Linear kernel / Landmarks / set 2 - {:.2f}%, {}, {}."
.format((np.mean(linear_scores1)*100),
(np.mean(linear_fit_times1)),
(np.mean(linear_score_times1))), file=text_file)
print("\nAverages for SVM with RBF kernel / Landmarks / set 1 - {:.2f}%, {}, {}."
.format((np.mean(rbf_scores)*100),
(np.mean(rbf_fit_times)),
(np.mean(rbf_score_times))), file=text_file)
print("Averages for SVM with RBF kernel / Landmarks / set 2 - {:.2f}%, {}, {}."
.format((np.mean(rbf_scores1)*100),
(np.mean(rbf_fit_times1)),
(np.mean(rbf_score_times1))), file=text_file)
print("\nAverages for SVM with POLY kernel / Landmarks / set 1 - {:.2f}%, {}, {}."
.format((np.mean(poly_scores)*100),
(np.mean(poly_fit_times)),
(np.mean(poly_score_times))), file=text_file)
print("Averages for SVM with POLY kernel / Landmarks / set 2 - {:.2f}%, {}, {}."
.format((np.mean(poly_scores1)*100),
(np.mean(poly_fit_times1)),
(np.mean(poly_score_times1))), file=text_file)
# End the script.
end = time.clock()
hours, rem = divmod(end - start, 3600)
minutes, seconds = divmod(rem, 60)
print("\n***> Time elapsed: {:0>2}:{:0>2}:{:05.2f}."
.format(int(hours), int(minutes), seconds))