1. 程式人生 > >Implementation of Convolutional Neural Network Using Keras

Implementation of Convolutional Neural Network Using Keras

Implementation of Convolutional Neural Network Using Keras

In this article, we will see the implementation of Convolutional Neural Network (CNN) using Keras on MNIST data set and then we will compare the results with the regular neural network. It is highly recommended to first read the post “Convolutional Neural Network — In a Nutshell”

before moving on to CNN implementation to develop intuition about CNN.

1- Introduction

The MNIST dataset is most commonly used for the study of image classification. The MNIST database contains images of handwritten digits from 0 to 9 by American Census Bureau employees and American high school students. It is divided into 60,000 training images and 10,000 testing images. Both Tensorflow and Keras allow us to download the MNIST dataset directly using the API.

2- Download Data Set Using API

We will use only two lines of code to import TensorFlow and download the MNIST dataset under the Keras API. We will assign the data into train and test sets. x_train and x_test parts contain greyscale RGB codes (from 0 to 255) while y_train and y_test parts contain labels from 0 to 9. We shall visualize the images using ‘matplotlib’ library.

It is important to highlight that each image in the MNIST data set has a size of 28 X 28 pixels which means that the shape of x_train is (60000, 28, 28) where 60,000 is the number of samples. We have to reshape the x_train from 3 dimensions to 4 dimensions as a requirement to process through Keras API. Further, we have to normalize our data else our computation cost will be very high. We can achieve this by dividing the RGB codes to 255 as follows:

3- Convolutional Neural Network Architecture

First, we will define the Convolutional neural networks architecture as follows:

1- The first hidden layer is a convolutional layer called a Convolution2D. We will use 32 filters with size 5×5 each.

2- Then a Max pooling layer with a pool size of 2×2.

3- Another convolutional layer with 64 filters with size 5×5 each.

4- Then a Max pooling layer with a pool size of 2×2.

5- Then next is a Flatten layer that converts the 2D matrix data to a 1D vector before building the fully connected layers.

6- After that, we will use a fully connected layer with 1024 neurons and relu activation function.

7- Then we will use a regularization layer called Dropout. It is configured to randomly exclude 20% of neurons in the layer in order to reduce overfitting.

8- Finally, the output layer which has 10 neurons for the 10 classes and a softmax activation function to output probability-like predictions for each class.

After deciding the above, we can set up a neural network model with a few lines of code as follows:

Note:
– Comments are provided with ‘#’ to explain the commands.
– You can download the iPython notebook and train the model on your PC or just copy and paste this code on .py file.

Step 1 — Create a model:

Keras first creates a new instance of a model object and then add layers to it one after the another. It is called a sequential model API. We can add layers to the neural network just by calling model.add and passing in the type of layer we want to add. Finally, we will compile the model with two important information, loss function, and cost optimization algorithm.

Once we execute the above code, Keras will build a TensorFlow model behind the scenes.

Step 2 — Train the model:

We can train the model by calling model.fit and pass in the training data and the expected output. Keras will run the training process and print out the progress to the console. When training completes, it will report the final accuracy that was achieved with the training data.

Step 3 — Test the model:

We can test the model by calling model.evaluate and passing in the testing data set and the expected output.

Step 4 — Save and Load the model:

Once we reach the optimum results we can save the model using model.save and pass in the file name. This file will contain everything we need to use our model in another program.

Your model will be saved in the Hierarchical Data Format (HDF) with .h5 extension. It contains multidimensional arrays of scientific data.

We can load our previously trained model by calling the load model function and passing in a file name. Then we call the predict function and pass in the new data for predictions.

Summary

  • We learned how to load the MNIST dataset and normalize it.
  • We learned the implementation of CNN using Keras.
  • We saw how to save the trained model and load it later for prediction.
  • The accuracy is more than 98% which is way more what we achieved with the regular neural network.