Friday, May 5, 2017

Handwriting Recognition with Deep Learning

In this post, we will go over an example to apply deep learning, in particular convolutional neural network, to recognize handwritten numbers from 0 to 9 using TensorFlow backend Keras. If you don't have deep learning environment setup yet, checkout this post.

Thankfully, there is publicly available handwritten digits and its labels on the web that we can use. For more info, check out MNIST website. Even better, Keras has MNIST data module, so we will use  it.

Use your favorite editor and create mnist.py file with the following:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Lambda, Dropout, Activation
from keras.optimizers import SGD, Adam
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
X_train = mnist.train.images.reshape(-1, 28, 28, 1)
y_train = mnist.train.labels
X_test = mnist.test.images.reshape(-1, 28, 28, 1)
y_test = mnist.test.labels
input_shape = (28,28,1)
model = Sequential()
model.add(Conv2D(24, 5, 5, input_shape=input_shape, init='he_normal')) # output = 24x24x24
model.add(Activation('elu'))
model.add(MaxPooling2D((2,2), border_mode='same')) # output = 12x12x24
model.add(Conv2D(32, 5, 5, input_shape=input_shape, init='he_normal')) # output = 8x8x32
model.add(Activation('elu'))
model.add(MaxPooling2D((2,2), border_mode='same')) # output = 4x4x32
model.add(Conv2D(48, 4, 4, input_shape=input_shape, init='he_normal')) # output = 1x1x48
model.add(Activation('elu'))
model.add(Flatten()) # output = 48
model.add(Dense(24, init='he_normal')) # output = 24
model.add(Activation('elu'))
model.add(Dropout(0.5))
model.add(Dense(10, init='he_normal')) # output = 10
model.add(Activation('softmax'))
adam = Adam(lr=0.001)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
model.fit(X_train, y_train, nb_epoch=10, batch_size=100)
loss_and_metrics = model.evaluate(X_test, y_test)
print('test accuracy: ' + str(loss_and_metrics[1])) # Accuracy
import numpy as np
idx = np.random.randint(0, X_test.shape[0])
img = X_test[idx].reshape(28,28)
label = y_test[idx]
label = np.argmax(label)
import matplotlib.pyplot as plt
fig = plt.figure()
print('label = ' + str(label))
pred = np.argmax(model.predict(X_test[idx].reshape(1,28,28,1)))
print('prediction = ' + str(pred))
plt.imshow(img, cmap='gray')
plt.show()
view raw mnist.py hosted with ❤ by GitHub

Upon running the file with
$ python mnist.py

you will see convolutional network being trained with data and tested with unseen data with accuracy of about 99%!

No comments:

Post a Comment