Explaining the code
Original Articles :
Numpy: NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
Keras:Keras is a high-level neural networks API, written in Python.
import numpy as np
from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense
from keras.models import Sequential
A typical Convolutional Neural Network consists of the following parts:
In this part of the network a series of convolution and pooling operations takes place, which are used for detecting different features of the input image.
# Images fed into this model are 28 x 28 pixels with 3 channels
img_shape = (28,28,1)
# Set up the model
model = Sequential()
# Add convolutional layer with 3, 3 by 3 filters
model.add(Conv2D(3,kernel_size=3,input_shape=img_shape))
This function creates a 2D Convolution Layer.
This function creates a 2D Pooling Layer.
The Activation() method is used to apply a specific activation function to the output of the Pooling layer. Here a relu activation function is added to the output layer.
# Add relu activation to the layer
model.add(Activation('relu'))
#Pooling
model.add(MaxPool2D(2))
After the convolution layer, there is classification layer that consists of fully connected layers, where neurons of one layer are connected with every activations from the previous layer. The thing about the fully connected layers is that it can only be fed 1-Dimensional data. With the output data from the previous layer being 3-Dimensional , it needs to the flatten out(converted into 1-Dimensional) before it can be fed into the Classification layer.
For this a Flatten layer is added, which takes the output of the convolution layer and turns it into a format that can be used by the densely connected neural layer.
#Fully connected layers
# Use Flatten to convert 3D data to 1D
model.add(Flatten())
# Add dense layer with 10 neurons
model.add(Dense(10))
# we use the softmax activation function for our last layer
model.add(Activation('softmax'))
The final layer of the model is of type 'Dense', a densely-connected neural layer which will give the final classification/prediction.
Now just to get an overview of the model we have created ,we will use the summary() function. This will list out the details regarding the Layer,the Output shape and the number of parameters.
# give an overview of our model
model.summary()
Before the training process, we have to put together a learning process in a particular form.This is done via compile(). It consists of 3 elements:
- The metrics defines how the success of the model is evaluated.
- we will use the ‘accuracy’ metric to calculate an accuracy score on the testing/validation set of images.
model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics=['accuracy'])
Our model is complete.
Now we will train the model.
We will use the MNIST dataset which is a benchmark deep learning dataset, containing 70,000 handwritten numbers from 0-9.
#importing the datasets
from keras.datasets import mnist
#loading the datasets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
import matplotlib.pyplot as plt
plt.imshow(X_train[0])
print(X_train[0].shape)
Reshaping of the two sets of images, X_train and X_test is done so that their shape matches the shape expected by our CNN model.
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)
In one-hot encoding we create a column for each classification category, with each column containing binary values indicating if the current image belongs to that category or not.
from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
- This method trains the model for a fixed number of epochs.
model.fit(X_train, y_train,validation_data=(X_test,y_test) ,epochs=10, verbose=2)
We can test our model against a/some specific inputs with the help of the predict( ) function as done in the code snippet below.
- Output Format The output will consists of 10 probabilities, each representing the probability of being a particular digit from 0-9.
model.predict(X_test[:4])
By directly displaying the predicted number instead of the probabilities.(i.e. just refining the output. )
def return_Digit(x):
a=np.where(x==max(x))
print(int(a[0]))
prediction=model.predict(X_test[:4])
np.apply_along_axis(return_Digit,axis=1,arr=prediction)