Neural Network to Detect Handwritten Digits using python
This project based on book by Michael Nielson
Various folders in this project are:
-
data: It contains the mnist database
-
digits: It has two folders testing and myimage
- testing: It contains various images from 0-9 that can be used for the purpose of testing the neural network
- myimage: It contains 9 images written by me.
-
src: src folder has 3 files:
- mnist_loader.py: Used to load mnist data that is meant for training testing and validation. Mnist Database contains 60,000, 50,000 of them is used for training purpose and rest 10,000 is used for validation and testing
- network1.py: Naive neural network uses concept of cost function, backpropagation and stochastic gradient descent
- network2.py: Advancement of network1.py has additional feature overfitting and crossEntropyCost function, in addtion to features in networ1.py
-
models: It contains various models made by me, model5.txt is most efficient model of them.
Following python libraries are required:
- numpy: install using pip install numpy
- PIL: install using pip install pillow
Running is based on the steps:
- Load mnist data
- Create the model file
- Create an image file containing a handwritten number
- Format the handwritten number to suit mnist format
- Predict the integer
The easiest way is to cd to src directory where the python files are located. Then run:
- python
- >>> import mnist_loader #This step is used to import mnist_loader.py
- >>> training, validation, testing = mnist_loader.load_data_wrapper() #This step is used to load training validation and testing data
- >>> import network2 #This step is used to import network2.py
- >>> net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost) #This step creates a neural network with 784 neuron in input layer, 30 neuron in 1st hidden layer, 10 neuron in output layer
- >>> net.large_weight_initializer()
- >>> net.SGD(training_data, 30, 10, 0.5,
... lmbda = 5.0,
... evaluation_data=validation_data,
... filename=("../models/modelx.txt")
... monitor_evaluation_accuracy=True,
... monitor_evaluation_cost=True,
... monitor_training_accuracy=True,
... monitor_training_cost=True)
These steps will create and save neural network model in "../models/modelx.txt".
In order to test any handwritten image we need to first process it so that it matches with mnist data, to process the image we follow these steps (Image processing is done using gimp)
- convert 81.png -monochrome a1.png
- convert -resize 28x28 a1.png a1.png
To load and test the image we follow following steps:
- cd to the src directory
- python
- >>> import mnist_loader
- >>> test_against = mnist_loader.imageread("../filepath/filename_of_image")
To predict the interger written in image
- >>> import network2
- >>> network2.load(test_against, filename="../modelpath/modelname.txt") #modelpath is where the model was saved