/
facesRegression.py
137 lines (94 loc) · 3.52 KB
/
facesRegression.py
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
import os as os
import glob as glob
import skimage.data as skimData
from skimage.color import rgb2grey
from skimage.transform import resize
import tensorflow as tf
import matplotlib.pyplot as plt
rootDir = '/Users/mcmenamin/GitHub/tensorFlowFaces/'
os.chdir(rootDir)
import tfModels as tfModels
######################################
#
# Reading in all face images, resizing
#
def imageProcess(x):
return resize(rgb2grey(skimData.load(x)), [150, 150])
trainFnames = glob.glob(rootDir + 'SaidAndTodorov_Model/faces_training_jpg/[m,f]*.jpg')
testFnames = glob.glob(rootDir + 'SaidAndTodorov_Model/faces_testing_jpg/[m,f]*.jpg')
train_images = [imageProcess(f) for f in trainFnames]
test_images = [imageProcess(f) for f in testFnames]
train_images = [t.ravel() for t in train_images]
test_images = [t.ravel() for t in test_images]
train_images = [t / np.sqrt(np.mean(t**2)) for t in train_images]
test_images = [t / np.sqrt(np.mean(t**2)) for t in test_images]
######################################
#
# Reading in attractiveness ratings for each face
#
attr_maleFaces_train = np.loadtxt(rootDir + 'SaidAndTodorov_Model/FrM_attractivenessratings_formatlab.csv', delimiter=',')[:, 0]
attr_femlFaces_train = np.loadtxt(rootDir + 'SaidAndTodorov_Model/MrF_attractivenessratings_formatlab.csv', delimiter=',')[:, 0]
attr_maleFaces_test = np.loadtxt(rootDir + 'SaidAndTodorov_Model/validationresultsFrM_formatlab.csv', delimiter=',')[1:, 0]
attr_femlFaces_test = np.loadtxt(rootDir + 'SaidAndTodorov_Model/validationresultsMrF_formatlab.csv', delimiter=',')[1:, 0]
train_attr = []
for f in trainFnames:
f = f.split('/')[-1]
num = int(f[1:-4])
if f[0] == 'm':
train_attr.append(attr_maleFaces_train[num])
elif f[0] == 'f':
train_attr.append(attr_femlFaces_train[num])
else:
print('bad gender?')
test_attr = []
for f in testFnames:
f = f.split('/')[-1]
num = int(f[1:-4])
if f[0] == 'm':
test_attr.append(attr_maleFaces_test[num])
elif f[0] == 'f':
test_attr.append(attr_femlFaces_test[num])
else:
print('bad gender?')
####################################
#
# Linear regression
#
# import importlib
# importlib.reload(tfModels)
Xtrain = np.vstack(train_images)
Ytrain = np.vstack(train_attr)
Xtest = np.vstack(test_images)
Ytest = np.vstack(test_attr)
"""
# Use this code to drop out 'empty' pixels
goodPix = np.mean(Xtrain**2, axis=0) > 0.01
Xtrain = Xtrain[:, goodPix]
Xtest = Xtest[:, goodPix]
"""
# Simplify input using SVD to project into a lower-dimensional space
U, S, Vt = np.linalg.svd(Xtrain, full_matrices=False)
toKeep = S > 1
print('Keeping {} features ({:.1f}%)'.format(np.sum(toKeep), 100 * np.mean(toKeep)))
Xtrain_lodim = Xtrain.dot(Vt[toKeep, :].T) / S[toKeep].reshape(1, -1)
Xtest_lodim = Xtest.dot(Vt[toKeep, :].T) / S[toKeep].reshape(1, -1)
# Use linear regression to find 'attractive' face dimensions
linearModel = tfModels.linReg(Xtrain_lodim, Ytrain,
Xtest_lodim, Ytest)
"""
The result is pretty good if the faces are really downsampled (i.e., LSF features
can predict attractiveness with r2 ~ 0.45).
Let's make things more fun (and predictive) by getting a full convolutional network model!
"""
####################################
#
# Multilayer convolutional network
#
import importlib
importlib.reload(tfModels)
Xtrain = np.vstack(train_images)
Ytrain = np.vstack(train_attr)
Xtest = np.vstack(test_images)
Ytest = np.vstack(test_attr)
deepModel = tfModels.convNet(Xtrain, Ytrain,
Xtest, Ytest)