Convolutional Neural Networks with KERAS – Image recognition

A quick test shot with KERAS, inspired by this tutorial using the MNIST dataset of more than 60000 images of hand written digits. Task at hand: correctly identify as many as possible of these 28 x 28 images, looking like this:

six

I’m flabbergasted….! In less than 70 lines (seven-zero!) of Python/KERAS-code, and after a training session of about 20-30 min on my laptop, the CNN is able to correctly identify 99.2% of the images….!

keras_cnn_example

These really powerful libraries are way cool – almost to the point where they take the fun & challange out of it…! 🙂

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(4711)  # for reproducibility
 
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
from keras import backend as K
K.set_image_dim_ordering('th')
 
(X_train, y_train), (X_test, y_test) = mnist.load_data()
 
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

print ('X_train',X_train.shape)
print ('X_test',X_test.shape)

Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
 
model = Sequential()
 
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(1,28,28)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
 
model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
 
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
 
history = model.fit(X_train, Y_train, 
                    batch_size=32, epochs=10, verbose=1)

model.save('mnist-model.h5')

score = model.evaluate(X_test, Y_test, verbose=1)
print (score)

plt.figure(figsize=(18,12))
plt.title('Neural Network Training Progress')
plt.grid(which='both')
plt.xlabel('Epoch (60000 iterations each)')
plt.ylabel('percentage images correctly identified')
plt.plot(history.history['acc'])
plt.savefig('keras_cnn_example.jpg',format='jpg')
plt.show()


 

About swdevperestroika

High tech industry veteran, avid hacker reluctantly transformed to mgmt consultant.
This entry was posted in Big Data, Complex Systems, Data Analytics, KERAS, Machine Learning, Neural networks, Numpy, Python and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Convolutional Neural Networks with KERAS – Image recognition

  1. Joe Marasco says:

    Fantastic stuff, Tommy. Keep up the good work.

  2. Thanks Joe, but my personal contribution here is minimal – the example is taken from a tutorial, and required only minor modifications to run on my setup.

    But the results are amazing, the next couple of decades are going to see massive advances in AI, I believe.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s