In [1]:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
import os
from keras.layers import Dense, Convolution2D, UpSampling2D, MaxPooling2D, ZeroPadding2D, Flatten, Dropout, Reshape
from keras.models import Sequential
from keras.utils import np_utils

In [2]:
x = np.load('./facial_data_X.npy')
y = np.load('./facial_labels.npy')

In [3]:
for ix in range(10):
    plt.figure(ix)
    plt.imshow(x[ix].reshape((48, 48)), interpolation='none', cmap='gray')
plt.show()



In [4]:
x -= np.mean(x, axis=0)
x /= np.std(x, axis=0)

In [5]:
for ix in range(10):
    plt.figure(ix)
    plt.imshow(x[ix].reshape((48, 48)), interpolation='none', cmap='gray')
plt.show()



In [8]:
X_train = x[0:28710, :]
Y_train = y[0:28710]
print X_train.shape, Y_train.shape
X_crossval = x[28710:32300, :]
Y_crossval = y[28710:32300]
print X_crossval.shape, Y_crossval.shape


(28710, 2304) (28710,)
(3590, 2304) (3590,)

In [9]:
X_train = X_train.reshape((X_train.shape[0], 1, 48, 48))
X_crossval = X_crossval.reshape((X_crossval.shape[0], 1, 48, 48))

In [10]:
model = Sequential()

model.add(Convolution2D (64, 3, 3 , input_shape=(1, 48, 48) ,activation='relu'))
model.add(Convolution2D (64, 3, 3 , activation='relu'))
model.add(Convolution2D (64, 3, 3 , activation='relu'))
model.add(Convolution2D (64, 3, 3 , activation='relu'))
model.add(Convolution2D (64, 3, 3 , activation='relu'))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
convolution2d_1 (Convolution2D)  (None, 64, 46, 46)    640         convolution2d_input_1[0][0]      
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 64, 44, 44)    36928       convolution2d_1[0][0]            
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 64, 42, 42)    36928       convolution2d_2[0][0]            
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 64, 21, 21)    0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 64, 21, 21)    0           maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D)  (None, 64, 19, 19)    36928       dropout_1[0][0]                  
____________________________________________________________________________________________________
convolution2d_5 (Convolution2D)  (None, 64, 17, 17)    36928       convolution2d_4[0][0]            
____________________________________________________________________________________________________
convolution2d_6 (Convolution2D)  (None, 64, 15, 15)    36928       convolution2d_5[0][0]            
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 64, 7, 7)      0           convolution2d_6[0][0]            
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 3136)          0           maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 128)           401536      flatten_1[0][0]                  
____________________________________________________________________________________________________
dropout_2 (Dropout)              (None, 128)           0           dense_1[0][0]                    
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 10)            1290        dropout_2[0][0]                  
====================================================================================================
Total params: 588106
____________________________________________________________________________________________________

In [11]:
print y.shape
y_ = np_utils.to_categorical(y, nb_classes=10)
print y_.shape


(35887,)

In [12]:
Y_train = y_[:28710]
Y_crossval = y_[28710:32300]
print X_crossval.shape, model.input_shape, Y_crossval.shape


(35887, 10)
(3590, 1, 48, 48) (None, 1, 48, 48) (3590, 10)

In [13]:
hist = model.fit(X_train, Y_train,
         nb_epoch=10,
         shuffle=True,
         batch_size=256,
         validation_data=(X_crossval, Y_crossval))


Train on 28710 samples, validate on 3590 samples
Epoch 1/5
28710/28710 [==============================] - 327s - loss: 1.7918 - acc: 0.2786 - val_loss: 1.5557 - val_acc: 0.4150
Epoch 2/5
28710/28710 [==============================] - 319s - loss: 1.5383 - acc: 0.4063 - val_loss: 1.4056 - val_acc: 0.4616
Epoch 3/5
28710/28710 [==============================] - 321s - loss: 1.3999 - acc: 0.4598 - val_loss: 1.3019 - val_acc: 0.4997
Epoch 4/5
28710/28710 [==============================] - 330s - loss: 1.3117 - acc: 0.5002 - val_loss: 1.2554 - val_acc: 0.5223
Epoch 5/5
28710/28710 [==============================] - 319s - loss: 1.2565 - acc: 0.5219 - val_loss: 1.2196 - val_acc: 0.5334

In [1]:
# Plotting training graph
plt.figure(0)
plt.plot(hist.history['loss'])
plt.figure(1)
plt.plot(hist.history['acc'])

plt.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-0d1a9f11d0db> in <module>()
      1 # Plotting training graph
----> 2 plt.figure(0)
      3 plt.plot(hist.history['loss'])
      4 plt.figure(1)
      5 plt.plot(hist.history['acc'])

NameError: name 'plt' is not defined

In [17]:
print X_train.shape
pred = model.predict(X_train[:10])
print X_train.shape


(28710, 1, 48, 48)

In [18]:
for ix in range(10):
    plt.figure(ix)
    plt.imshow(X_train[ix].reshape(48, 48), cmap='gray')
    print "Example:", ix, " | Predicted:", np.argmax(pred[ix]), " | Original:", np.argmax(y_[ix])
plt.show()


4 0
4 0
4 2
6 4
6 6
4 2
4 4
3 3
3 3
5 2