In [1]:
from sklearn import datasets
from sklearn.cross_validation import cross_val_score
from sklearn import svm, metrics

from itertools import cycle

from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import RandomizedPCA
from sklearn.ensemble import RandomForestClassifier

import pylab as pl
import numpy as np

In [2]:
import pandas as pd
import csv

In [3]:
def images2Vector(filePath):
    full_file_lines = [x for x in csv.reader(file(filePath))][1:]
    lab=[]
    dat=[]
    for line in full_file_lines:
        lab.append(line[0])
        dat.append(np.fromstring(line[1], dtype=np.uint8, sep=' '))
  
    dat = np.asarray(dat, dtype='uint8')
    lab = np.asarray(lab, dtype='uint8')
    return dat, lab

In [4]:
faces_data = '/home/ubuntu/stableDisk/img/gkt/kaggle/facial_expression_recognition/fer2013_10000.csv'

In [5]:
X, y =  images2Vector(faces_data)

In [6]:
len(X), len(y)


Out[6]:
(10000, 10000)

In [7]:
y[:5]


Out[7]:
array([0, 0, 2, 4, 6], dtype=uint8)

In [8]:
pl.imshow(np.random.permutation(X)[2].reshape((48,48)), cmap=pl.gray())


Out[8]:
<matplotlib.image.AxesImage at 0x9ef4810>

In [19]:
for i,j in enumerate(np.random.permutation(xrange(len(X)))[:5]):
    print "Selecting %d-th item from array" %(j)
    pl.subplot(1, 5, i+1)
    pl.imshow(X[j].reshape((48,48)), cmap=pl.gray())
    pl.title("Actual: %d" % int(y[j]))
    pl.xticks(()), pl.yticks(())


Selecting 386-th item from array
Selecting 8752-th item from array
Selecting 1520-th item from array
Selecting 7760-th item from array
Selecting 8533-th item from array

In [20]:
type(X), type(y)


Out[20]:
(numpy.ndarray, numpy.ndarray)

In [21]:
X.shape, y.shape


Out[21]:
((10000, 2304), (10000,))

In [10]:
# lets see data scatter
from sklearn.decomposition import KernelPCA, PCA, ProbabilisticPCA, ProjectedGradientNMF
X_pca_for2d_plotting = KernelPCA(n_components=2).fit_transform(X)

from itertools import cycle
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
for i, c in zip(np.unique(y), cycle(colors)):
    pl.scatter(X_pca_for2d_plotting[y==i,0], X_pca_for2d_plotting[y==i,1],
               label=str(i), c=c, alpha=.8)
    pl.legend(loc='upper right')



In [23]:
faces_data = '/home/ubuntu/stableDisk/img/gkt/kaggle/facial_expression_recognition/fer2013_2000.csv'
X, y =  images2Vector(faces_data)
    
# lets start kicking
# first hit:  svm with rbf, default C & gamma
clf_rbf = svm.SVC(kernel='rbf')
cross_val_score(clf_rbf, X, y, cv=10)
#scores of .25 is not bad to start with[note we are just using 2000 of all training data]


Out[23]:
array([ 0.25 ,  0.255,  0.26 ,  0.255,  0.245,  0.25 ,  0.255,  0.25 ,
        0.26 ,  0.245])

In [38]:
eigenfaces_count = 80
X_pca = RandomizedPCA(n_components=eigenfaces_count).fit_transform(X)
clf_rbf_pca = svm.SVC(kernel='rbf')
cross_val_score(clf_rbf_pca, X_pca, y, cv=5)
X_pca.shape


Out[38]:
array([ 0.245 ,  0.255 ,  0.255 ,  0.2575,  0.245 ])

In [42]:
clf_rf = RandomForestClassifier()
cross_val_score(clf_rf, X, y, cv=5)


Out[42]:
array([ 0.24  ,  0.295 ,  0.2725,  0.2675,  0.255 ])

In [44]:
eigenfaces_count = 200
X_pca = RandomizedPCA(n_components=eigenfaces_count).fit_transform(X)
clf_rbf_pca = svm.SVC(kernel='poly', degree=3)
cross_val_score(clf_rbf_pca, X_pca, y, cv=5)


Out[44]:
array([ 0.28  ,  0.2975,  0.28  ,  0.28  ,  0.24  ])

In [24]:
import numpy as np

def tanh(x):
    return np.tanh(x)

def tanh_deriv(x):
    return 1.0 - x**2

def logistic(x):
    return 1/(1 + np.exp(-x))

def logistic_derivative(x):
    return logistic(x)*(1-logistic(x))

class NeuralNetwork:

    def __init__(self, layers, activation='tanh'):
        """
        :param layers: A list containing the number of units in each layer. Should be at least two values
        :param activation: The activation function to be used. Can be "logistic" or "tanh"
        """
        if activation == 'logistic':
            self.activation = logistic
            self.activation_deriv = logistic_derivative
        elif activation == 'tanh':
            self.activation = tanh
            self.activation_deriv = tanh_deriv

        self.weights = []
        for i in range(1, len(layers) - 1):
            self.weights.append((2*np.random.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)
        self.weights.append((2*np.random.random((layers[i] + 1, layers[i + 1]))-1)*0.25)
        
    def fit(self, X, y, learning_rate=0.2, epochs=10000):
        X = np.atleast_2d(X)
        temp = np.ones([X.shape[0], X.shape[1]+1])
        temp[:, 0:-1] = X  # adding the bias unit to the input layer
        X = temp
        y = np.array(y)

        for k in range(epochs):
            i = np.random.randint(X.shape[0])
            a = [X[i]]

            for l in range(len(self.weights)):
                    a.append(self.activation(np.dot(a[l], self.weights[l])))
            error = y[i] - a[-1]
            deltas = [error * self.activation_deriv(a[-1])]

            for l in range(len(a) - 2, 0, -1): # we need to begin at the second to last layer
                deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))
            deltas.reverse()
            for i in range(len(self.weights)):
                layer = np.atleast_2d(a[i])
                delta = np.atleast_2d(deltas[i])
                self.weights[i] += learning_rate * layer.T.dot(delta)
    
    def predict(self, x):
        x = np.array(x)
        temp = np.ones(x.shape[0]+1)
        temp[0:-1] = x
        a = temp
        for l in range(0, len(self.weights)):
            a = self.activation(np.dot(a, self.weights[l]))
        return a

In [40]:
import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import LabelBinarizer
from sklearn.cross_validation import train_test_split

faces_data = '/home/ubuntu/stableDisk/img/gkt/kaggle/facial_expression_recognition/fer2013_2000.csv'
X,y = images2Vector(faces_data)
X = X.astype(float)

#normalize data
X -= X.min()     # normalize the values to bring them into the range 0-1
X /= X.max()

eigenfaces_count = 64
X_pca = RandomizedPCA(n_components=eigenfaces_count).fit_transform(X)

In [46]:
X_pca -= X_pca.min()
X_pca /= X_pca.max()
X_pca.shape, y.shape


Out[46]:
((2000, 64), (2000,))

In [54]:


In [55]:
X_train.shape, y_train.shape, X_test.shape, y_test.shape


Out[55]:
((1500, 64), (1500,), (500, 64), (500,))

In [56]:
labels_train[0], np.unique(y)


Out[56]:
(array([1, 0, 0, 0, 0, 0, 0]), array([0, 1, 2, 3, 4, 5, 6], dtype=uint8))

In [59]:
nn = NeuralNetwork([2304,2304,7],'logistic')
X_train, X_test, y_train, y_test = train_test_split(X, y)
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)

nn.fit(X_train,labels_train,epochs=5000)
predictions = []
for i in range(X_test.shape[0]):
    o = nn.predict(X_test[i] )
    predictions.append(np.argmax(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)


[[  0   0   0   0  66   0   2]
 [  0   0   0   0   6   1   0]
 [  0   0   0   0  83   2   1]
 [  0   0   0   0 123   0  10]
 [  0   0   0   0  69   1   0]
 [  0   0   0   0  50   4   1]
 [  0   0   0   0  79   0   2]]
             precision    recall  f1-score   support

          0       0.00      0.00      0.00        68
          1       0.00      0.00      0.00         7
          2       0.00      0.00      0.00        86
          3       0.00      0.00      0.00       133
          4       0.14      0.99      0.25        70
          5       0.50      0.07      0.13        55
          6       0.12      0.02      0.04        81

avg / total       0.10      0.15      0.06       500

/usr/local/lib/python2.7/dist-packages/scikit_learn-0.14.1-py2.7-linux-x86_64.egg/sklearn/metrics/metrics.py:1905: UserWarning: The sum of true positives and false positives are equal to zero for some labels. Precision is ill defined for those labels [0 1 2 3]. The precision and recall are equal to zero for some labels. fbeta_score is ill defined for those labels [0 1 2 3]. 
  average=None)

In [ ]: