Title: A Novel method for Handwritten
digit recognition.
Project Objectives
By the end of this project, you will:
Know fundamental concepts and techniques of the Artificial Neural
Network and Convolution Neural Networks
Gain a broad understanding of image data.
Work with Sequential type of modelling
Work with Keras capabilities
Work with image processing techniques
know how to build a web application using the Flask framework.
Project Flow
Project Flow:
The user interacts with the UI (User Interface) to upload the image
as input
The uploaded image is analysed by the model which is integrated
Once the model analyses the uploaded image, the prediction is
showcased on the UI
To accomplish this, we have to complete all the activities and tasks listed
below
Understanding the data.
o Importing the required libraries
o Loading the data
o Analysing the data
o Reshaping the data.
o Applying One Hot Encoding
Model Building
o Creating the model and adding the input, hidden and output
layers to it
o Compiling the model
o Training the model
o Predicting the result
o Testing the model by taking image inputs
o Saving the model
Application Building
o Create an HTML file
o Build Python Code
Project Structure
Create a Project folder which contains files as shown below
We are building a Flask Application which needs HTML pages stored
in the templates folder and a python script app.py for server-side
scripting.
The model is built in the notebook Hand written recognition
system.ipynb
We need the model which is saved and the saved model in this
content is mnistCNN.h5
The static folder will contain js and css files.
The templates mainly used here are main.html and index6.html for
showcasing the UI
Prerequisites
To complete this project, you should have the following software
and packages
Anaconda Navigator:
Anaconda Navigator is a free and open-source distribution of the Python
and R programming languages for data science and machine learning
related applications. It can be installed on Windows, Linux, and macOS.
Conda is an open-source, cross-platform, package management system.
Anaconda comes with so very nice tools like JupyterLab, Jupyter
Notebook, QtConsole, Spyder, Glueviz, Orange, Rstudio, Visual Studio
Code. For this project, we will be using Jupiter notebook and Spyder
To install Anaconda navigator and to know how to use Jupyter Notebook a
Spyder using Anaconda watch the video
To build Deep learning models you must require the following packages
Tensor flow: TensorFlow is an end-to-end open-source platform for
machine learning. It has a comprehensive, flexible ecosystem of tools,
libraries, and community resources that lets researchers push the state-
of-the-art in ML and developers can easily build and deploy ML powered
applications.
Keras: Keras leverages various optimization techniques to make high level
neural network API easier and more performant. It supports the following
features:
Consistent, simple and extensible API.
Minimal structure - easy to achieve the result without any frills.
It supports multiple platforms and backends.
It is user friendly framework which runs on both CPU and GPU.
Highly scalability of computation.
Flask: Web frame work used for building Web applications
Prior Knowledge
One should have knowledge on the following Concepts:
Supervised and unsupervised learning
Regression Classification and Clustering
Artificial Neural Networks
Convolution Neural Networks
Flask
Understanding The Data
ML depends heavily on data, without data, it is impossible for a machine
to learn. It is the most crucial aspect that makes algorithm training
possible. In Machine Learning projects, we need a training data set. It is
the actual data set used to train the model for performing various actions.
TensorFlow already has MNist Data set so there is no need to explicitly
download or create Dataset
The MNSIT dataset contains ten classes: Digits from 0-9. Each digit is
taken as a class.
Importing The Required Libraries
Let’s first import the libraries
Importing the required libraries which are required for the model to run.
The dataset for this model is imported from the Keras module.
The dataset contains ten classes: Digits from 0-9. Each digit is taken as a
class
For a detail point of view on Keras and TensorFlow refer to the link here:
Loading The Data
The dataset for this model is imported from the Keras module.
We split the data into train and test. Using the training dataset, we train
the model and the testing dataset is used to predict the results.
We are finding out the shape of X_train and x_test for better
understanding. It lists out the dimensions of the data present in it.
in trainset, we have 60000 images, and in the test set we have 10000
images
Analysing The Data
Let's see the Information of an image lying inside the x_train variable
Basically, the pixel values range from 0-255. Here we are printing the first
image pixel value which is index [0] of the training data. As you see it is
displayed in the output.
With respect to this image, the label of this image will be stored in y_train
let's see what is the label of this image by grabbing it from the y_train
variable
As we saw in the previous screenshot, we get to know that the pixel
values are printed. Now here we are finding to which image the pixel
values belong to. From the output displayed we get to know that the
image is ‘5’.
Lets Plot the image on a graph using the Matplot library
Matplotlib is a comprehensive library for creating static, animated, and
interactive visualizations in Python. By using the Matplotlib library we are
displaying the number ‘5’ in the form of an image for proper
understanding.
Note: You can see the results by replacing the index number till 59999 as
the train set has 60K images
Reshaping The Data
As we are using Deep learning neural network, the input for this network
to get trained on should be of higher dimensional. Our dataset is having
three-dimensional images so we have to reshape them too higher
dimensions
We are reshaping the dataset because we are building the model using
CNN. As CNN needs four attributes batch, height, width, and channels we
reshape the data.
Applying One Hot Encoding
If you see our y_train variable contains Labels representing the images
containing in x_train. AS these are numbers usually, they can be
considered as numerical or continuous data, but with respect to this
project these Numbers are representing a set of class so these are to be
represented as categorical data, and we need to binaries these
categorical data that’s why we are applying One Hot encoding for y_train
set
One hot encoding is a process by which categorical variables are
converted into a form that could be provided to ML algorithms to do a
better job in prediction. We apply One-Hot Encoding in order to convert
the values into 0’s and 1’s. For a detailed point of view, look at this link
Now let's see how our label 5 is index 0 of y_train is converted
As we see the new the label is printed in the form of 0’s and 1’s and is of
type float.
Model Building
This activity includes the following steps
Initializing the model
Adding CNN Layers
Training and testing the model
Saving the model
Add CNN Layers
Creating the model and adding the input, hidden, and output layers to it
The Sequential model is a linear stack of layers. You can create a
Sequential model by passing a list of layer instances to the
constructor:
To know more about layers, watch the below video
Compiling The Model
With both the training data defined and model defined, it's time to
configure the learning process. This is accomplished with a call to the
compile () method of the Sequential model class. Compilation requires 3
arguments: an optimizer, a loss function, and a list of metrics.
Note: In our project, we have 2 classes in the output, so the loss
is binary_crossentropy.
If you have more than two classes in output put “loss = categorical_cross
entropy”.
Train The Model
Now, let us train our model with our image dataset.
Fit: functions used to train a deep learning neural network
Arguments:
steps_per_epoch: it specifies the total number of steps taken from the
generator as soon as one epoch is finished and the next epoch has
started. We can calculate the value of steps_per_epoch as the total
number of samples in your dataset divided by the batch size.
Epochs: an integer and number of epochs we want to train our model for.
Validation_data:
an inputs and targets list
a generator
inputs, targets, and sample_weights list which can be used to
evaluate the loss and metrics for any model after any epoch has
ended.
validation_steps: only if the validation_data is a generator then only this
argument can be used. It specifies the total number of steps taken from
the generator before it is stopped at every epoch and its value is
calculated as the total number of validation data points in your dataset
divided by the validation batch size.
Observing The Metrics
We here are printing the metrics which lists out the Test loss and Test
accuracy
Loss value implies how poorly or well a model behaves after each
iteration of optimization.
An accuracy metric is used to measure the algorithm's performance
in an interpretable way.
Test The Model
Firstly, we are slicing the x_test data until the first four images. In the next
step we the printing the predicted output.
As we already predicted the input from the x_test. According to that by
using argmax function here we are printing the labels with high prediction
values
Test The Model
Firstly, we are slicing the x_test data until the first four images. In the next
step we the printing the predicted output.
As we already predicted the input from the x_test. According to that by
using argmax function here we are printing the labels with high prediction
values
Save The Model
Your model is to be saved for future purposes. This saved model can also
be integrated with an android application or web application in order to
predict something.
The model is saved with .h5 extension as follows:
An H5 file is a data file saved in the Hierarchical Data Format (HDF). It
contains multidimensional arrays of scientific data.
Test With Saved Model
Now open another jupyter file and write the below code
Firstly, we are loading the model which was built. Then we are applying
for a loop for the first four images and converting the image to the
required format. Then we are resizing the input image, converting the
image as per the CNN model and we are reshaping it according to the
requirement. At last, we are predicting the result.
You can use predict_classes for just predicting the class of an image
Application Building
In this section, we will be building a web application that is integrated into
the model we built. A UI is provided for the uses where he has uploaded
an image. The uploaded image is given to the saved model and prediction
is showcased on the UI.
This section has the following tasks
Building HTML Pages
Building server-side script
Create An HTML File
We use HTML to create the front-end part of the web page.
Here, we created 2 html pages- index.html, web.html.
index.html displays home page.
web.html accepts the values from the input and displays the
prediction.
For more information regarding HTML refer the link below
Please refer to the link for HTML code files
Let's see how our index.html file looks like
This is the main page which describes about the project and summarizes
it.
Let's see how our web.html page looks like
This is the prediction page where we get to choose the image from our
local system and predict the output.
Build Python Code (Part 1)
Let us build the flask file ‘app.py’ which is a web framework written in
python for server-side scripting. Let’s see step by step procedure for
building the backend application.
App starts running when the “__name__” constructor is called in
main.
render_template is used to return HTML file.
“GET” method is used to take input from the user.
“POST” method is used to display the output to the user.
Import Libraries:
Libraries required for the app to run are to be imported.
Routing to the html Page
We are routing the app to the HTML templates which we want to render.
Firstly, we are rendering the main.html template and from there we are
navigating to our prediction page that is index6.html
Returning the prediction on UI:
Build Python Code (Part 2)
Here the route for prediction is given and necessary steps are performed
in order to get the predicted output.
Necessary conditions are given according to the input classes and the app
will be returning the templates according to that.
Main Function:
This function runs your app in a web browser
Lastly, we run our app on the localhost. Here we are running it on
localhost:8000
Run The Application
Open anaconda prompt from the start menu
Navigate to the folder where your python script is.
Now type “python app.py” command
Navigate to the localhost where you can view your web page
Upload an image and see the predicted output on UI your page
and output look like:
Train The Model On IBM
In this milestone, you will learn how to build Deep Learning Model Using
the IBM cloud.
Register For IBM Cloud
To complete this project, you must have an IBM account
IBM Account:
Please click here to register for IBM
Please click here to log in to IBM Account.
Watch the below video to register and login into your IBM account
Train The Model On IBM
Please watch the below video to train the model on IBM and integrate it
with the flask Application.
Ideation Phase
In this milestone you are expected to get started with the Ideation process.
Literature Survey on The Selected Project
& Information Gathering
In this activity you are expected to gather/collect the relevant information on
project use case, refer the existing solutions, technical papers, research publications
etc.
Prepare Empathy Map
In this activity you are expected to prepare the empathy map canvas to capture the
user Pains & Gains, Prepare list of problem statements.
Ideation
In this activity you are expected to list the ideas (at least 4 per each team member)
by organizing the brainstorming session and prioritize the top 3 ideas based on the
feasibility & importance.
Project Design Phase – I
From this milestone you will be starting the project design phase. You are expected
to cover the activities given.
Proposed Solution
In this activity you are expected to prepare the proposed solution document, which
includes the novelty, feasibility of idea, business model, social impact, scalability
of solution, etc.
Problem Solution Fit
In this activity you are expected to prepare problem - solution fit document and
submit for review.
Solution Architecture
In this activity you are expected to prepare solution architecture document and
submit for review.
Project Design Phase -II
From this milestone you will be continue working on the project design phase. You
are expected to cover the activities given.
Customer Journey
Prepare the customer journey maps to understand the user interactions &
experiences with the application (entry to exit).
Functional Requirement
In this activity you are expected to prepare the functional requirement document.
Data Flow Diagrams
In this activity you are expected to prepare the data flow diagrams and submit for
review.
Technology Architecture
In this activity you are expected to draw the technology architecture diagram.
Project Planning Phase
In this milestone you are expected to prepare milestones & tasks, sprint schedules.
Prepare Milestone & Activity List
In this activity you are expected to prepare the milestones & activity list of the
project.
Spirit Delivery Plan
In this activity you are expected to prepare the sprint delivery plan.
Project Development Phase
In this milestone you will start the project development and expected to perform
the coding & solutioning, acceptance testing, performance testing based as per the
sprint and submit them.
Project Development - Delivery of Sprint-
1
In this activity you are expected to develop & submit the developed code by testing
it.
Project Development - Delivery of Sprint-
2
In this activity you are expected to develop & submit the developed code by testing
it.
Project Development - Delivery of Sprint-
3
In this activity you are expected to develop & submit the developed code by testing
it.
Project Development - Delivery of Sprint-
4
In this activity you are expected to develop & submit the developed code by testing
it.