-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
112 lines (79 loc) · 2.38 KB
/
training.py
File metadata and controls
112 lines (79 loc) · 2.38 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
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 08 14:24:57 2016
@author: vs26
"""
import warnings
import cv2
import numpy as np
import os
import glob
from scipy.fftpack import dct
from sklearn.metrics import accuracy_score
from sklearn import linear_model
from sklearn.mixture import GMM
import cPickle as Pickle
from skimage import io
# FIXME Ignoring warnings
# Some method being called is deprecated
def train():
w, h = 100, 100
retain = 8
face_images = []
x_train = []
y_train = []
x_test = []
y_test = []
def dct_2d(a):
return dct(dct(a.T).T)
# Get all images from the data set
print len(glob.glob("person_*"))
print os.getcwd()
for i, el in enumerate(glob.glob("person_*")):
print el, i
key = el
print (key)
pngs = glob.glob("{}/*.png".format(key))
face_features = []
for img_f in pngs:
if not os.path.exists(img_f):
print ("Can't find images...")
continue
face_img = io.imread(img_f)
face_img = face_img.astype("float32")
# 2d-dct and truncate
face_dct = dct_2d(face_img)
face_x = face_dct[:retain, :retain].flatten()
# face_features is a 64-dimensional feature vector of the face
# look at zig zag
face_features.append(face_x)
print len(face_features)
if not len(face_features):
continue
test = face_features[-10:]
x_test.append(test)
y_test += [i] * len(test)
train = face_features[:-10]
x_train += train
y_train += [i] * len(train)
y_train = np.array(y_train)
y_test = np.array(y_test)
x_train = np.vstack(x_train)
x_test = np.vstack(x_test)
print (x_train.shape, y_train.shape)
print (x_test.shape, y_test.shape)
gmm = GMM(n_components=8)
gmm.fit(x_train)
thresh = np.percentile(gmm.score(x_test), 5)
# Build model here
clf2 = linear_model.LogisticRegression()
clf2.fit(x_train, y_train)
print "Subject number given to the algorithm: "
print clf2.predict(x_test)
print "Subject number predicted by the algorithm: "
print y_test
print "Accuracy score:"
print accuracy_score(clf2.predict(x_test), y_test)
with open("data/face-model.pkl", "wb") as fh:
Pickle.dump([clf2, gmm, thresh], fh)
train()