In the last blog post, using Keras, you learned how to create a simple neural network (Multilayer Perceptron) using Keras that classified as MNIST dataset images with 97% accuracy. You may think 97% is a good number, but you can still improve it. Using Convolutional Neural Networks (or Convnets), a type of deep learning model most commonly applied for the analysis of visual images, it is possible to achieve about 99% accuracy in training and validation.
You can clone this project here: https://github.com/waslleysouza/keras.
The model structure is similar the Neural Network we created here: Getting Started with Keras.
The first change we need to make is in the imported libraries.
Here we will add the Numpy library and 3 new layer types: Conv2D, MaxPooling2D, Flatten.
from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten from keras.utils import to_categorical import matplotlib.pyplot as plt import numpy as np
We will need to change the image reshaping because the convnets accepts the 3D tensor (height, width, channels) rather than 1D-shaped flattened data. We need to set the channel value to 1 because all the images in the MNIST dataset are in grayscale.
train_images = train_images.reshape((60000, 28, 28, 1)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28, 28, 1)) test_images = test_images.astype('float32') / 255
As a last change, we will need to change the structure of the model by adding layers Conv2D and MaxPooling2D.
In this model, each Conv2D layer will extract features using a 3×3 kernel and each MaxPooling2D layer will halve the image size.
model = Sequential() model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1))) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64, kernel_size=(3,3), activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64, kernel_size=(3,3), activation='relu')) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax')) model.summary()
Now, when we train the network, accuracy increases by around 99%.
Have a good time!