Hello and welcome back to CosmoTechno!
As blog title suggest, today you would be going to get a significant intuition of
As blog title suggest, today you would be going to get a significant intuition of
Deep Learning. In this blog you will be understanding how to perform Hello World
project in Deep Learning. Recognizing Handwritten Digits is one of the most
famous as well as beginner friendly project of Deep Learning. If your completely
newbie or you are not familiar with basic concept of Deep Learning then I will
recommend you to read the previous blog on deep learning.
In this blog I am going to perform Handwritten digit recognition using
Convolutional Neural Network(CNN) which we will implement with the help of
Tensorflow, Keras libraries.
Before getting into hands on coding part we will try to understand how CNN works.
CNN is a one of the significant Neural Network which is commonly used in Deep
CNN is a one of the significant Neural Network which is commonly used in Deep
Learning. In image recognition or classification task CNN divide each image into
very small boxes and try to get some meaningful patterns from the same and
store those patterns into memory, so that next time it come across any image of
same object, it will able to recognize or classify it successfully.
Now we are completely ready to move towards our project!
This hands on project contains following steps:
1. Installing and importing required libraries.
2. Loading and Preprocessing Dataset
3. Defining CNN model
4. Training and Testing model
Installing and importing required libraries:
In this section we will import all the libraries or packages required for our project.
This hands on project contains following steps:
1. Installing and importing required libraries.
2. Loading and Preprocessing Dataset
3. Defining CNN model
4. Training and Testing model
Installing and importing required libraries:
In this section we will import all the libraries or packages required for our project.
Importing all required libraries or packages at the top of whole code is always a
good practice. In this project we will be requiring packages from libraries like
Keras, numpy, matplotlib, etc.
Loading and Preprocessing Dataset:
After importing libraries, we will load dataset into our project. For this
After importing libraries, we will load dataset into our project. For this
Handwritten digit recognition project we are using MNIST standard dataset which
is availble in keras.dataset. After execution of following code, dataset will be
loaded in respective training and testing sets. After loading dataset , each image
from both testing and training set is resized to 28 X 28 pixels.
In our dataset Y_train and Y_test contains output for training and testing set
respectively. These outputs are in respresented using 10 classes from 0 to 9, but
while learning regressor and classifier requires target data in binary format i.e.
(0,1). For example, calss 4 will be represented as [0 0 0 0 1 0 0 0 0 0].
To normalize dataset and make it efficient we need to change the data type of our
training and testing set images from 'int' to 'float32'. Further we convert pixels
from RGB format to gray scale format by normalizing them.
Once we are done with all the above preprocessing, dataset will look like following sample.
Defining CNN model:
After data preprocessing next most important step is Defining a CNN model.
Here we are using Sequential model provided by Keras library. This Sequential
model includes 2 Convolution layers and 1 fully connected layer. In this model
we have used 'ReLU' activation function for all layers except output layer which
uses 'SoftMax' layer.
Above plot gives clear understanding of model and layers of CNN.
Once model is defined it is time to compile the model! Compiling
model is nothing but defining the loss function, the optimizer and metrics.
For this binary classification model we will be using 'SGD optimizer' for defining
learning rate and 'categorical crossentropy' as a loss function.
Now we have model, compiler and dataset, so what are we waiting for, it's time
to train the model. But before training it is required to split training dataset into
training and validation in order to make model unbiased. This model will be trained
for 10 epochs or iterations with batch size 32.
After training a model on particular dataset it is important to test it on different
dataset in order to verify the accuracy and reliability of model. Here we will be
using test dataset which includes images of handwritten digits and associated
labels.
For this model we have got 0.04368 loss and 99.01% accuracy! And these results
are pretty good.
Now last but not least, predicting output i.e. digit written on given image, following
code snippet will predict digit from given image.
| Digit:9 |
I hope you have enjoyed the first step toward deep learning. Stay tuned for
converting this python file to an amazing web app!


Comments
Post a Comment