In [3]:
%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
import numpy as np
import pandas as pd
In [4]:
np.set_printoptions(precision=4)
np.set_printoptions(suppress=True)
In [5]:
nb_classes = 9
batch_size = 64
nb_epoch = 1
np.random.seed(1337) # for reproducibility
In [6]:
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)
# 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 [7]:
# 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[7]:
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 [10]:
model.layers[0].get_weights()[0].shape
Out[10]:
In [11]:
valX.shape
Out[11]:
In [12]:
valX[0].ravel().reshape(28,28).shape
Out[12]:
In [13]:
sample_0 = valX[0].ravel().reshape(28,28)
sample_1 = valX[1].ravel().reshape(28,28)
sample_2 = valX[2].ravel().reshape(28,28)
sample_3 = valX[3].ravel().reshape(28,28)
sample_4 = valX[4].ravel().reshape(28,28)
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_0, cmap='gray', interpolation='nearest')
ax1.imshow(sample_1, cmap='gray')
ax2.imshow(sample_2, cmap='gray')
ax3.imshow(sample_3, cmap='gray')
ax4.imshow(sample_4, cmap='gray')
Out[13]:
In [14]:
model1 = Sequential()
model1.add(Convolution2D(32, 1, 2, 2, weights=model.layers[0].get_weights()))
model1.add(Activation('relu'))
model1.compile(loss='categorical_crossentropy', optimizer=sgd)
In [15]:
valX_h_1 = model1.predict(valX)
In [16]:
valX_h_1.shape
Out[16]:
In [17]:
valX_h_1[0][0].shape
Out[17]:
In [18]:
sample_0 = valX_h_1[0][0]
sample_1 = valX_h_1[0][1]
sample_2 = valX_h_1[0][2]
sample_3 = valX_h_1[0][3]
sample_4 = valX_h_1[0][4]
sample_5 = valX_h_1[0][5]
sample_6 = valX_h_1[0][6]
sample_7 = valX_h_1[0][7]
sample_8 = valX_h_1[0][8]
sample_9 = valX_h_1[0][9]
sample_10 = valX_h_1[0][10]
sample_11 = valX_h_1[0][11]
sample_12 = valX_h_1[0][12]
sample_13 = valX_h_1[0][13]
sample_14 = valX_h_1[0][14]
sample_15 = valX_h_1[0][15]
sample_16 = valX_h_1[0][16]
sample_17 = valX_h_1[0][17]
sample_18 = valX_h_1[0][18]
sample_19 = valX_h_1[0][19]
sample_20 = valX_h_1[0][20]
sample_21 = valX_h_1[0][21]
sample_22 = valX_h_1[0][22]
sample_23 = valX_h_1[0][23]
sample_24 = valX_h_1[0][24]
sample_25 = valX_h_1[0][25]
sample_26 = valX_h_1[0][26]
sample_27 = valX_h_1[0][27]
sample_28 = valX_h_1[0][28]
sample_29 = valX_h_1[0][29]
sample_30 = valX_h_1[0][30]
sample_31 = valX_h_1[0][31]
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_0, cmap='gray')
ax1.imshow(sample_1, cmap='gray')
ax2.imshow(sample_2, cmap='gray')
ax3.imshow(sample_3, cmap='gray')
ax4.imshow(sample_4, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_10, cmap='gray')
ax1.imshow(sample_11, cmap='gray')
ax2.imshow(sample_12, cmap='gray')
ax3.imshow(sample_13, cmap='gray')
ax4.imshow(sample_14, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_20, cmap='gray')
ax1.imshow(sample_21, cmap='gray')
ax2.imshow(sample_22, cmap='gray')
ax3.imshow(sample_23, cmap='gray')
ax4.imshow(sample_24, cmap='gray')
Out[18]:
In [19]:
# Sequential wrapper model
model2 = Sequential()
# first convolutional layer
model2.add(Convolution2D(32, 1, 2, 2, weights=model.layers[0].get_weights()))
model2.add(Activation('relu'))
# second convolutional layer
model2.add(Convolution2D(48, 32, 2, 2, weights=model.layers[2].get_weights()))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(poolsize=(2,2)))
model2.compile(loss='categorical_crossentropy', optimizer=sgd)
In [20]:
valX_h_2 = model2.predict(valX)
In [21]:
valX_h_2.shape
Out[21]:
In [22]:
valX_h_2[0].shape
Out[22]:
In [23]:
valX_h_2[0][0].shape
Out[23]:
In [24]:
sample_0 = valX_h_2[0][0]
sample_1 = valX_h_2[0][1]
sample_2 = valX_h_2[0][2]
sample_3 = valX_h_2[0][3]
sample_4 = valX_h_2[0][4]
sample_5 = valX_h_2[0][5]
sample_6 = valX_h_2[0][6]
sample_7 = valX_h_2[0][7]
sample_8 = valX_h_2[0][8]
sample_9 = valX_h_2[0][9]
sample_10 = valX_h_2[0][10]
sample_11 = valX_h_2[0][11]
sample_12 = valX_h_2[0][12]
sample_13 = valX_h_2[0][13]
sample_14 = valX_h_2[0][14]
sample_15 = valX_h_2[0][15]
sample_16 = valX_h_2[0][16]
sample_17 = valX_h_2[0][17]
sample_18 = valX_h_2[0][18]
sample_19 = valX_h_2[0][19]
sample_20 = valX_h_2[0][20]
sample_21 = valX_h_2[0][21]
sample_22 = valX_h_2[0][22]
sample_23 = valX_h_2[0][23]
sample_24 = valX_h_2[0][24]
sample_25 = valX_h_2[0][25]
sample_26 = valX_h_2[0][26]
sample_27 = valX_h_2[0][27]
sample_28 = valX_h_2[0][28]
sample_29 = valX_h_2[0][29]
sample_30 = valX_h_2[0][30]
sample_31 = valX_h_2[0][31]
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_0, cmap='gray')
ax1.imshow(sample_1, cmap='gray')
ax2.imshow(sample_2, cmap='gray')
ax3.imshow(sample_3, cmap='gray')
ax4.imshow(sample_4, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_10, cmap='gray')
ax1.imshow(sample_11, cmap='gray')
ax2.imshow(sample_12, cmap='gray')
ax3.imshow(sample_13, cmap='gray')
ax4.imshow(sample_14, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_20, cmap='gray')
ax1.imshow(sample_21, cmap='gray')
ax2.imshow(sample_22, cmap='gray')
ax3.imshow(sample_23, cmap='gray')
ax4.imshow(sample_24, cmap='gray')
Out[24]:
In [25]:
# Sequential wrapper model
model3 = Sequential()
# first convolutional layer
model3.add(Convolution2D(32, 1, 2, 2, weights=model.layers[0].get_weights()))
model3.add(Activation('relu'))
# second convolutional layer
model3.add(Convolution2D(48, 32, 2, 2, weights=model.layers[2].get_weights()))
model3.add(Activation('relu'))
model3.add(MaxPooling2D(poolsize=(2,2)))
# third convolutional layer
model3.add(Convolution2D(32, 48, 2, 2, weights=model.layers[5].get_weights()))
model3.add(Activation('relu'))
model3.add(MaxPooling2D(poolsize=(2,2)))
model3.compile(loss='categorical_crossentropy', optimizer=sgd)
In [26]:
valX_h_3 = model3.predict(valX)
In [27]:
valX_h_3.shape
Out[27]:
In [28]:
valX_h_3[0][0].shape
Out[28]:
In [29]:
sample_0 = valX_h_3[0][0]
sample_1 = valX_h_3[0][1]
sample_2 = valX_h_3[0][2]
sample_3 = valX_h_2[0][3]
sample_4 = valX_h_3[0][4]
sample_5 = valX_h_3[0][5]
sample_6 = valX_h_3[0][6]
sample_7 = valX_h_3[0][7]
sample_8 = valX_h_3[0][8]
sample_9 = valX_h_3[0][9]
sample_10 = valX_h_3[0][10]
sample_11 = valX_h_3[0][11]
sample_12 = valX_h_3[0][12]
sample_13 = valX_h_3[0][13]
sample_14 = valX_h_3[0][14]
sample_15 = valX_h_3[0][15]
sample_16 = valX_h_3[0][16]
sample_17 = valX_h_3[0][17]
sample_18 = valX_h_3[0][18]
sample_19 = valX_h_3[0][19]
sample_20 = valX_h_3[0][20]
sample_21 = valX_h_3[0][21]
sample_22 = valX_h_3[0][22]
sample_23 = valX_h_3[0][23]
sample_24 = valX_h_3[0][24]
sample_25 = valX_h_3[0][25]
sample_26 = valX_h_3[0][26]
sample_27 = valX_h_3[0][27]
sample_28 = valX_h_3[0][28]
sample_29 = valX_h_3[0][29]
sample_30 = valX_h_3[0][30]
sample_31 = valX_h_3[0][31]
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_0, cmap='gray')
ax1.imshow(sample_1, cmap='gray')
ax2.imshow(sample_2, cmap='gray')
ax3.imshow(sample_3, cmap='gray')
ax4.imshow(sample_4, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_10, cmap='gray')
ax1.imshow(sample_11, cmap='gray')
ax2.imshow(sample_12, cmap='gray')
ax3.imshow(sample_13, cmap='gray')
ax4.imshow(sample_14, cmap='gray')
fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(1, 5)
ax0.imshow(sample_20, cmap='gray')
ax1.imshow(sample_21, cmap='gray')
ax2.imshow(sample_22, cmap='gray')
ax3.imshow(sample_23, cmap='gray')
ax4.imshow(sample_24, cmap='gray')
Out[29]:
In [30]:
# Creating the model which consists of 3 conv layers followed by
# 2 fully conntected layers
# Sequential wrapper model
model4 = Sequential()
# first convolutional layer
model4.add(Convolution2D(32, 1, 2, 2, weights=model.layers[0].get_weights()))
model4.add(Activation('relu'))
# second convolutional layer
model4.add(Convolution2D(48, 32, 2, 2, weights=model.layers[2].get_weights()))
model4.add(Activation('relu'))
model4.add(MaxPooling2D(poolsize=(2,2)))
# third convolutional layer
model4.add(Convolution2D(32, 48, 2, 2, weights=model.layers[5].get_weights()))
model4.add(Activation('relu'))
model4.add(MaxPooling2D(poolsize=(2,2)))
# convert convolutional filters to flatt so they can be feed to
# fully connected layers
model4.add(Flatten())
model4.compile(loss='categorical_crossentropy', optimizer=sgd)
In [31]:
valX_c_0 = model4.predict(valX)
In [32]:
32*6*6
Out[32]:
In [33]:
valX_c_0.shape
Out[33]:
In [40]:
valX_c_0[0]
Out[40]:
In [41]:
# df = pd.DataFrame(valX_c_0[0])
df = pd.DataFrame({'fully connected layers' : valX_c_0[0]})
In [42]:
df.head(5)
Out[42]:
In [43]:
df.plot(color='DarkGreen')
Out[43]: