In [1]:
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from joblib import Parallel, delayed
import scipy
import pandas as pd
from numpy import array
from PIL import Image
import matplotlib.pyplot as plt
np.random.seed(1)
In [2]:
#Loading the training and testing data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape)
#Reshaping from(,28,28) to (,784) for training using MLP
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train.shape)
In [3]:
#Selecting 6000 random examples from the test data
test_rows = np.random.randint(0,X_test.shape[0],6000)
X_test = X_test[test_rows]
Y = y_test[test_rows]
#Converting the output to binary classification(Six=1,Not Six=0)
Y_test = Y == 6
Y_test = Y_test.astype(int)
#Selecting the examples where the output is 6
X_six = X_train[y_train == 6]
Y_six = y_train[y_train == 6]
#Selecting the examples where the output is not 6
X_not_six = X_train[y_train != 6]
Y_not_six = y_train[y_train != 6]
#Selecting 1000 random examples each from the data(Six and Not Six)
random_rows = np.random.randint(0,X_six.shape[0],1000)
X_six = X_six[random_rows]
Y_six = Y_six[random_rows]
random_rows = np.random.randint(0,X_not_six.shape[0],1000)
X_not_six = X_not_six[random_rows]
Y_not_six = Y_not_six[random_rows]
In [4]:
#Appending the data with output as 6 and data with output as not six
X_train = np.append(X_six,X_not_six)
#Reshaping the appended data to appropraite form - (,784)
X_train = X_train.reshape(X_six.shape[0] + X_not_six.shape[0], 784)
#Appending the labels and converting the labels to binary classification(Six=1,Not Six=0)
Y_labels = np.append(Y_six,Y_not_six)
Y_train = Y_labels == 6
Y_train = Y_train.astype(int)
In [5]:
#Converting the classes to its binary categorical form
nb_classes = 2
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)
In [6]:
#Reshaping to (28,28) and (1,28,28) is done because we are rotating images, so we cant use the (,784) dimension
#array, we need to use the original image dimension array which is (28,28)
#Initializing the array which will contain images rotated by 15 degrees anti clockwise
anti_X_train = scipy.misc.imrotate(X_train[0].reshape(28,28), angle = 15)
anti_X_train = anti_X_train.reshape(1, 28,28)
#Initializing the array which will contain images rotated by 15 degrees clockwise
clock_X_train = scipy.misc.imrotate(X_train[0].reshape(28,28), angle = -15)
clock_X_train = clock_X_train.reshape(1, 28,28)
In [7]:
#Performing clockwise and anticlockwise rotation for the rest of the images. Again reshaping needs to be done
#below for the same reason as described above
for i in range(1,2000):
rotate_anti = scipy.misc.imrotate(X_train[i].reshape(28,28), angle = 15)
rotate_anti = rotate_anti.reshape(1, 28,28)
rotate_clock = scipy.misc.imrotate(X_train[i].reshape(28,28), angle = -15)
rotate_clock = rotate_clock.reshape(1, 28,28)
#Appending the rotated images to the resoective arrays
anti_X_train = np.append(anti_X_train,rotate_anti,axis=0)
clock_X_train = np.append(clock_X_train,rotate_clock,axis=0)
In [8]:
#Displaying the original and rotated images
def image_compare(original,clockwise,anticlockwise):
original = original.reshape(28,28)
plt.figure(figsize=(20, 6))
ax = plt.subplot(1, 3, 1)
plt.imshow(original)
plt.xlabel('ORIGINAL')
plt.gray()
ax = plt.subplot(1, 3, 2)
plt.imshow(clockwise)
plt.xlabel('ROTATED CLOCKWISE')
plt.gray()
ax = plt.subplot(1, 3, 3)
plt.imshow(anticlockwise)
plt.xlabel('ROTATED ANTI-CLOCKWISE')
plt.gray()
plt.show()
In [9]:
image_compare(X_train[0],clock_X_train[0],anti_X_train[0])
In [10]:
image_compare(X_train[1500],clock_X_train[1500],anti_X_train[1500])
In [12]:
#Reshaping the rotated arrays which have dimensions(,28,28) back to (,784) to train the arrays using MLP
anti_X_train = anti_X_train.reshape(anti_X_train.shape[0], 784)
clock_X_train = clock_X_train.reshape(clock_X_train.shape[0], 784)
anti_X_train = anti_X_train.astype('float32')#(**)
clock_X_train = clock_X_train.astype('float32')#(**)
anti_X_train /= 255#(**)
clock_X_train /= 255#(**)
In [12]:
#Appening the arrays to get the final rotated training array
rotated_X_train = np.append(X_train, anti_X_train, axis = 0)
rotated_Y_train = np.append(Y_train, Y_train, axis = 0)
#The array that includes the original arrays as well as the 15 degree rotated arrays(2000 original, 2000 15 degree
#clockwise rotated, 2000 15 degree anticlockwise rotated)
rotated_X_train = np.append(rotated_X_train, clock_X_train, axis = 0)
rotated_Y_train = np.append(rotated_Y_train, Y_train,axis = 0)
In [13]:
#Function for constructing the multi layer perceptron
# 1 Hidden Layer
def build_layer_1(nb_epoch,X_train,Y_train):
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=128, nb_epoch=nb_epoch,verbose=1,
validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
In [14]:
%%time
build_layer_1(1,X_train,Y_train)
In [15]:
%%time
build_layer_1(1,rotated_X_train,rotated_Y_train)
In [16]:
%%time
build_layer_1(5,X_train,Y_train)
In [17]:
build_layer_1(5,rotated_X_train,rotated_Y_train)
In [18]:
%%time
build_layer_1(10,X_train,Y_train)
In [19]:
%%time
build_layer_1(10,rotated_X_train,rotated_Y_train)
In [20]:
%%time
build_layer_1(30,X_train,Y_train)
In [21]:
%%time
build_layer_1(30,rotated_X_train,rotated_Y_train)
In [22]:
#Function for constructing the multi layer perceptron
# 2 Hidden Layers
def build_layer_2(nb_epoch,X_train,Y_train):
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=128, nb_epoch=nb_epoch,verbose=1,
validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
In [23]:
%%time
build_layer_2(1,X_train,Y_train)
In [24]:
%%time
build_layer_2(1,rotated_X_train,rotated_Y_train)
In [25]:
%%time
build_layer_2(5,X_train,Y_train)
In [26]:
%%time
build_layer_2(5,rotated_X_train,rotated_Y_train)
In [27]:
%%time
build_layer_2(10,X_train,Y_train)
In [28]:
%%time
build_layer_2(10,rotated_X_train,rotated_Y_train)
In [29]:
%%time
build_layer_2(30,X_train,Y_train)
In [30]:
%%time
build_layer_2(30,rotated_X_train,rotated_Y_train)
In [31]:
#Function for constructing the multi layer perceptron
## Hidden Layers
def build_layer_3(nb_epoch,X_train,Y_train):
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=128, nb_epoch=nb_epoch,verbose=1,
validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
In [32]:
%%time
build_layer_3(1,X_train,Y_train)
In [33]:
%%time
build_layer_3(1,rotated_X_train,rotated_Y_train)
In [34]:
%%time
build_layer_3(5,X_train,Y_train)
In [35]:
%%time
build_layer_3(5,rotated_X_train,rotated_Y_train)
In [36]:
%%time
build_layer_3(10,X_train,Y_train)
In [37]:
%%time
build_layer_3(10,rotated_X_train,rotated_Y_train)
In [38]:
%%time
build_layer_3(30,X_train,Y_train)
In [39]:
%%time
build_layer_3(30,rotated_X_train,rotated_Y_train)
In [ ]: