In [1]:
%matplotlib inline
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.optimizers import SGD
from keras.utils import np_utils
from sklearn.cross_validation import train_test_split
from sklearn.externals import joblib
from sklearn.decomposition import RandomizedPCA
import numpy as np
import pandas as pd
In [2]:
np.set_printoptions(precision=4)
np.set_printoptions(suppress=True)
In [3]:
nb_classes = 9
batch_size = 64
nb_epoch = 4
np.random.seed(1337) # for reproducibility
In [4]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.4)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
In [5]:
print valX.shape
print valY.shape
In [6]:
pca = RandomizedPCA(n_components=2)
valX_pca = pca.fit_transform(valX)
In [9]:
print valX_pca[0]
print valX_pca.shape
In [10]:
df1 = pd.DataFrame(valX_pca)
df1['digits'] = valY
df1.columns = ['x', 'y', 'digits']
In [11]:
df1.head(5)
Out[11]:
In [32]:
# color example
# http://matplotlib.org/examples/color/named_colors.html
ax = df1[df1['digits'] == 0].plot(kind='scatter', x='x', y='y', color='blue', label='0')
ax = df1[df1['digits'] == 1].plot(kind='scatter', x='x', y='y', color='green', label='1', ax=ax)
ax = df1[df1['digits'] == 2].plot(kind='scatter', x='x', y='y', color='red', label='2', ax=ax)
ax = df1[df1['digits'] == 3].plot(kind='scatter', x='x', y='y', color='cyan', label='3', ax=ax)
ax = df1[df1['digits'] == 4].plot(kind='scatter', x='x', y='y', color='magenta', label='4', ax=ax)
ax = df1[df1['digits'] == 5].plot(kind='scatter', x='x', y='y', color='yellow', label='5', ax=ax)
ax = df1[df1['digits'] == 6].plot(kind='scatter', x='x', y='y', color='black', label='6', ax=ax)
ax = df1[df1['digits'] == 7].plot(kind='scatter', x='x', y='y', color='pink', label='7', ax=ax)
df1[df1['digits'] == 8].plot(kind='scatter', x='x', y='y', color='hotpink', label='8', ax=ax, figsize=(10,8), grid=False, title='Original dataset visualized by PCA')
Out[32]:
In [13]:
# reshape for convolutions
trainX = trainX.reshape((trainX.shape[0], 1, 28, 28))
testX = testX.reshape((testX.shape[0], 1, 28, 28))
valX = valX.reshape((valX.shape[0], 1, 28, 28))
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
In [14]:
# Creating the model which consists of 3 conv layers followed by
# 2 fully conntected layers
# Sequential wrapper model
model = Sequential()
# first convolutional layer
model.add(Convolution2D(32, 1, 2, 2))
model.add(Activation('relu'))
# second convolutional layer
model.add(Convolution2D(48, 32, 2, 2))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2,2)))
# third convolutional layer
model.add(Convolution2D(32, 48, 2, 2))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2,2)))
# convert convolutional filters to flatt so they can be feed to
# fully connected layers
model.add(Flatten())
# first fully connected layer
model.add(Dense(32*6*6, 144, init='lecun_uniform'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
# second fully connected layer
model.add(Dense(144, 144, init='lecun_uniform'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
# last fully connected layer which output classes
model.add(Dense(144, 9, init='lecun_uniform'))
model.add(Activation('softmax'))
# setting sgd optimizer parameters
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=nb_epoch,
show_accuracy=True,
verbose=1,
validation_data=(testX, testY))
Out[14]:
In [8]:
model.layers
Out[8]:
In [9]:
for l in range(len(model.layers)):
if model.layers[l].get_weights() != []:
print l
print model.layers[l]
# print model.layers[l].get_weights()
In [15]:
# Sequential wrapper model
model1 = Sequential()
# first convolutional layer
model1.add(Convolution2D(32, 1, 2, 2, weights=model.layers[0].get_weights()))
model1.add(Activation('relu'))
# second convolutional layer
model1.add(Convolution2D(48, 32, 2, 2, weights=model.layers[2].get_weights()))
model1.add(Activation('relu'))
model1.add(MaxPooling2D(poolsize=(2,2)))
# third convolutional layer
model1.add(Convolution2D(32, 48, 2, 2, weights=model.layers[5].get_weights()))
model1.add(Activation('relu'))
model1.add(MaxPooling2D(poolsize=(2,2)))
# convert convolutional filters to flatt so they can be feed to
# fully connected layers
model1.add(Flatten())
# first fully connected layer
model1.add(Dense(32*6*6, 144, init='lecun_uniform', weights=model.layers[9].get_weights()))
model1.add(Activation('relu'))
model1.add(Dropout(0.5))
model1.compile(loss='categorical_crossentropy', optimizer=sgd)
In [25]:
valX_pca = model1.predict(valX)
In [36]:
pca = RandomizedPCA(n_components=2)
valX_pca = pca.fit_transform(valX_pca)
df2 = pd.DataFrame(valX_pca)
df2['digits'] = np_utils.probas_to_classes(valY)
df2.columns = ['x', 'y', 'digits']
ax = df2[df2['digits'] == 0].plot(kind='scatter', x='x', y='y', color='blue', label='0')
ax = df2[df2['digits'] == 1].plot(kind='scatter', x='x', y='y', color='green', label='1', ax=ax)
ax = df2[df2['digits'] == 2].plot(kind='scatter', x='x', y='y', color='red', label='2', ax=ax)
ax = df2[df2['digits'] == 3].plot(kind='scatter', x='x', y='y', color='cyan', label='3', ax=ax)
ax = df2[df2['digits'] == 4].plot(kind='scatter', x='x', y='y', color='magenta', label='4', ax=ax)
ax = df2[df2['digits'] == 5].plot(kind='scatter', x='x', y='y', color='yellow', label='5', ax=ax)
ax = df2[df2['digits'] == 6].plot(kind='scatter', x='x', y='y', color='black', label='6', ax=ax)
ax = df2[df2['digits'] == 7].plot(kind='scatter', x='x', y='y', color='pink', label='7', ax=ax)
df2[df2['digits'] == 8].plot(kind='scatter', x='x', y='y', color='hotpink', label='8', ax=ax, figsize=(10,8), grid=False, title='first fully connected layer of dataset visualized by PCA')
Out[36]:
In [37]:
valX_pca = model.predict(valX)
In [38]:
pca = RandomizedPCA(n_components=2)
valX_pca = pca.fit_transform(valX_pca)
df3 = pd.DataFrame(valX_pca)
df3['digits'] = np_utils.probas_to_classes(valY)
df3.columns = ['x', 'y', 'digits']
ax = df3[df3['digits'] == 0].plot(kind='scatter', x='x', y='y', color='blue', label='0')
ax = df3[df3['digits'] == 1].plot(kind='scatter', x='x', y='y', color='green', label='1', ax=ax)
ax = df3[df3['digits'] == 2].plot(kind='scatter', x='x', y='y', color='red', label='2', ax=ax)
ax = df3[df3['digits'] == 3].plot(kind='scatter', x='x', y='y', color='cyan', label='3', ax=ax)
ax = df3[df3['digits'] == 4].plot(kind='scatter', x='x', y='y', color='magenta', label='4', ax=ax)
ax = df3[df3['digits'] == 5].plot(kind='scatter', x='x', y='y', color='yellow', label='5', ax=ax)
ax = df3[df3['digits'] == 6].plot(kind='scatter', x='x', y='y', color='black', label='6', ax=ax)
ax = df3[df3['digits'] == 7].plot(kind='scatter', x='x', y='y', color='pink', label='7', ax=ax)
df3[df3['digits'] == 8].plot(kind='scatter', x='x', y='y', color='hotpink', label='8', ax=ax, figsize=(10,8), grid=False, title='last fully connected layer of dataset visualized by PCA')
Out[38]: