In [1]:
import pandas as pd
import numpy as np
import tensorflow as tf

In [16]:
num_faces=5000

In [17]:
# read in faces and left eye classifcations
df = pd.read_csv('../data/facial keypoints training.csv', nrows=num_faces)
df = df[['left_eye_center_x','Image']]
df = df[df['left_eye_center_x'].notnull()]
df[['left_eye_center_x']] = df[['left_eye_center_x']].round(0)
left_eye_x = pd.get_dummies(df['left_eye_center_x'], columns=['left_eye_center_x'])
faces = df.Image.apply(lambda x: pd.Series(np.fromstring(x, dtype=np.float32, sep=' ')))
# free space
del df['Image']
faces = faces.as_matrix()[:10,:]
left_eye_x = left_eye_x.as_matrix()[:10,:]

In [18]:
print(faces.shape)
print(left_eye_x.shape)


(10, 9216)
(10, 48)

In [19]:
image_size = 96
num_labels = left_eye_x.shape[1]

graph = tf.Graph()
with graph.as_default():
    
    # graph
    face = tf.placeholder(tf.float32, [None, image_size**2])
    label = tf.placeholder(tf.float32, [None, 48])
    
    weights = tf.Variable(tf.truncated_normal([image_size*image_size, num_labels]))
    biases = tf.Variable(tf.zeros([num_labels]))
        
    # loss
    logits = tf.matmul(face, weights) + biases
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=logits))
    
    # optimizer
    optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
    
    # predictions
    train_prediction = tf.nn.softmax(logits)

In [31]:
# tf.reset_default_graph()
# import saved graph
with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())
    new_saver = tf.train.import_meta_graph('../models/left-eye-test-model.meta')
    new_saver.restore(sess, tf.train.latest_checkpoint('../models/'))
    logits = tf.get_collection('logits')[0]
    predictions = sess.run(logits, feed_dict={face:faces, label:left_eye_x})
    print('predicted x-coordinate:\t{}'.format(predictions.argmax(axis=1)))
    print('actual x-coordinate:\t{}'.format(left_eye_x.argmax(axis=1)))


predicted x-coordinate:	[32  0  0 32 32 32 17 32  0 32]
actual x-coordinate:	[31 29 30 30 32 35 29 32 31 29]

In [ ]:


In [ ]: