Convolutional Neural Networks Using Keras

In this tutorial, We will learn how to implement Convolutional Neural Networks using Keras in Python language.

CNN: A Brief Introduction

A Convolutional Neural Network works on the principle of Weights. There are many reasons why they are being used so extensively. They reduce processing power without the loss of important features. The main difference is the use of the Kernel Layer. It is also known as a filter. Its size is smaller than the Input layer. Used for Extraction of Important Features (Convolutes over the input).

When the dimension is reduced, it is known as valid padding. If it remains the same, it is known as the same padding. The Final Layer is just like a normal fully connected Neural Network. Keras has made it a whole lot easier to implement these networks with good optimization.

Implementation of Keras Convolutional Neural Networks

Tools Required

  • NumPy
  • Keras
  • Python
  • Spyder or Any Python IDE

Keras has some built-in datasets which can be loaded easily. You can use any of the datasets from Datasets-Keras. I have used the MNIST digit Classification Dataset for this tutorial. So let’s start coding!

Step-1 Loading the Libraries

Not many Libraries Required. Will use built-in Keras’ Functions.

#Import Libraries
import numpy as np
import pandas as pd
import keras

Step-2 Loading the Dataset

Layers Required for the CNN are:

  • MaxPooling2D: Selects the Max value for the convoluted matrix. Better than Average Pooling.Co
  • Conv2D: The Convolution layer.
  • Dense: The fully connected layer as normal Neural Networks.
  • Flatten Increases efficiency by converting data into a 1-D array.
  • Dropout: Prevents Overfitting.
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense
from keras.utils import to_categorical
from keras.layers import Dropout
from keras.layers.advanced_activations import LeakyReLU
#Load the Dataset
#x_train has 60k samples of size 28x28

Now the Data would be loaded, but it still needs to be preprocessed into 1 channel.

We will use Numpy’s Reshape Function.

Step-3 Preprocessing the Data.

Steps for Preprocessing:

  • Reshape Into Single Channel
  • Change datatype to “float32”
  • x_test and x_train values to vary from 0-1

We will also “One Hot Encode” the output values. The given dataset has 10 classes (0-9). We will change it into a binary vector with 1 for the correct digit and 0 for others.


Step-4 Defining the Model

Building a “Sequential” Model using Keras.add function. Batch Size is the number of elements picked at once. One complete Representation of Dataset is Known as Epoch. Here I have trained for 10 epochs with a batch size of 128. You can Increase it if the dataset is large. It will require more computational power. If you want to learn more about Layers in Keras, you can visit Layers in Keras.

batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),activation='relu',input_shape=(28,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dense(256, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

Step-5 Training and Saving the Model

Verbose 0 will not show the training Steps. Verbose 1 will show accuracy after each step.

hist =, y_train,batch_size=batch_size,
                 ,validation_data=(x_test, y_test))'hand_wr.h5')
print("Saved Succesfuly")

Training will take approx. 15 minutes on Intel(R) Core (TM) i7 CPU.

Step-6 Finding Accuracy

As this is not a very huge dataset, it achieves very high accuracy. The model is 99% percent accurate and not overfitting. Use “load_model” from Keras.

#model Accuracy
from keras.models import load_model
print('Test Loss: ',values[0])
print('Test Accuracy: ',values[1])

So we understood the basics of a Convolutional Network. You can ask any doubt in the comments section.

Machine Learning is ❤️.

Leave a Reply

Your email address will not be published. Required fields are marked *