This notebook proceeds with using the trained convolutional neural network to predict emotion for each of the face images in the provided test set as part of the Emotion Detection Form Facial Expressions Kaggle competition.
In [38]:
import csv
import random
import numpy as np
import emotion_model
import dwdii_transforms
random.seed(20275)
The model was trained on 150x150 images, therefore we will use this size of the test data as well.
In [62]:
imgDimension = []
imgDimension.append(150)
imgDimension.append(150)
In [63]:
testDataPath = "../../facial_expressions/test"
In [64]:
import os
ls = os.listdir(testDataPath)
len(ls)
Out[64]:
In [65]:
testData, fileList = dwdii_transforms.load_test_data(testDataPath, imgResize=imgDimension, verboseFreq=20)
In [66]:
testData.shape
Out[66]:
In [67]:
testX = testData.reshape(testData.shape[0], 1, testData.shape[1],testData.shape[2])
In [80]:
# Load the emotion array for our count in the model definition
emotions = dwdii_transforms.emotionNumerics()
print emotions
print len(emotions)
# Construct the model using our help function
model = emotion_model.emotion_model_jh_v5(len(emotions), verbose=True,
input_shape=(1,imgDimension[0],imgDimension[1]))
In [81]:
loadWeights = True
if loadWeights:
#model.load_weights("dwdii-emo-150-jhv5a-Cloud.hdf5")
#model.load_weights("dwdii-emo-150-jhv5-9tf-Cloud.hdf5")
model.load_weights("dwdii-emo-150-jhv5-19tf-25e-Cloud.hdf5")
In [82]:
predictOutput = model.predict(testX, batch_size=32, verbose=1)
In [83]:
# How many predictions?
len(predictOutput)
Out[83]:
In [84]:
# Show an example of the prediction output
print predictOutput[0]
In [85]:
np.array(predictOutput[0]).argmax()
Out[85]:
In [86]:
numEmo = dwdii_transforms.numericEmotions()
numEmo[2]
Out[86]:
In [87]:
fileList[0]
Out[87]:
In [88]:
# For each prediction output, build the row for the CSV and add to our list for saving.
outputData = []
outputData.append(["Image", "Emotion"])
for i in range(len(predictOutput)):
arPred = np.array(predictOutput[i])
predictionProb = arPred.max()
predictionNdx = arPred.argmax()
predictedEmo = numEmo[predictionNdx]
fileName = fileList[i]
outputData.append([fileName,predictedEmo])
In [89]:
print outputData[0:10]
In [90]:
with open('../output/dwdii_predictions-19tf-25e.csv', 'w') as mycsvfile:
dw = csv.writer(mycsvfile)
for row in outputData:
dw.writerow(row)
In [ ]:
from scipy import misc
path = testDataPath + "/JayaBhaduri_42.jpg"
img = misc.imread(path)
img = misc.imresize(img, (150, 150))
misc.imsave("test1000.jpg", img)
In [ ]: