MNIST Handwritten Digit Recognition Using Pytorch
Overview of Data
MNIST dataset consists of 60,000 images of hand written digit. Where each image has size 28X28.Here MNIST stands for Modified National institute of standard and technology. It consists of 10 different classes ranging from 0–9 . It means the problem we are going to approach is multiclass classification problem. On this Blog you will understand the basic Pytorch implementation.
Step 1 :- Importing necessary libraries & Parameter initialization
import torch
import torchvision
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch.nn as nn
from torchvision.transforms import transforms
import torch.nn.functional as F
initializing parameter
input_size=784 #28X28 pixel of image
hidden_size1=200 #size of 1st hidden layer(number of perceptron)
hidden_size2=150 #size of second hidden layer
hidden_size3=100 #size of third hidden layer
hidden_size=80 #size of fourth hidden layer
output =10 #output layer
bach_size=100
lr_rate=0.01
Step 2:- Loading the dataset using Pytorch Dataset and Data Loader
MNIST dataset is already present inside torch vision library.
train_dataset=torchvision.datasets.MNIST('/content',train=True,trans
forms=transforms.ToTensor(),download=True)
- ‘/content’ : Directory where data would have been saved/downloaded …
- train=True : -Dataset will be taken as training example for further ops….
- transforms.ToTensor() : Converts the image dataset into tensor…………
4. download=True:- Data will be download will be downloaded from Pytorch collection
test_dataset=torchvision.datasets.MNIST('/content',train=False,
transforms=transforms.ToTensor(),download=False)
taking train data loader and test data loader for further operation.
train_dataloader=torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=100,shuffle=True)
train_dataloader=torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=100,shuffle=True)
Here we have fixed the data overloading issue by using data loader we will pass only a fixed batch of data from huge dataset.
let’s see the shape of the data data that has been passed in batch
data=iter(train_dataloader)
samples,labels=next(data)
print(f"number of samples{samples.shape}")
print(f"number of labels {labels.shape}")[out]>>shape of samplestorch.Size([100, 1, 28, 28])
shape of labels torch.Size([100])
step 3:- Printing Images from dataset
plt.figure(figsize=(10,8))
for i in range(10):
plt.subplot(2,5,i+1)
plt.imshow(samples[i][0],cmap='BuPu')
plt.show()
Step 4:- Defining Training Pipeline
training pipeline design will consist following three steps;
- Designing the neural network by taking input size ,hidden size and output size with activation function
- construct loss and optimizer function for specific problem
- training loop consists of three steps:- a. forward pass :- a. gives prediction b. backward pass :- Gradient and c. weight update
class MNIST(nn.Module):
def __init__(self,input_size,hidden_size1,hidden_size2
,hidden_size3,hidden_size,output):
super(MNist,self).__init__()
self.f_connected1=nn.Linear(input_size,hidden_size1)
self.f_connected2=nn.Linear(hidden_size1,hidden_size2)
self.f_connected3=nn.Linear(hidden_size2,hidden_size3)
self.f_connected4=nn.Linear(hidden_size3,hidden_size)
self.out_connected=nn.Linear(hidden_size,output)def forward(self,x):
out=F.relu(self.f_connected1(x))
out=F.relu(self.f_connected2(out))
out=F.relu(self.f_connected3(out))
out=F.relu(self.f_connected4(out))
out=self.out_connected(out)
return out
nn.module
:- The nn.module
in pytorch help us to create the artificial neural network .nn.Linear
makes the linear connection between feature and neuron &Torch.nn.functional
module consists of all the activation functions and output function (eg:- relu , leaky relu , softmax ,sigmoid etc.) No we have successfully built the neural network and forward pass.
Mnist_model=MNIST(input_size,hidden_size1,hidden_size2
,hidden_size3,hidden_size,output)
#calling the object that has been created inside MNIST class
Printing all the parameters that has been used to created the neural network. weights and biases are considered as parameter of neural network.
print(Mnist_model.paramaters)
Constructing loss and optimize for the model
loss=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(Mnist_model.parameters(),lr=lr_rate)
Now We will build a training loop which consists of forward pass to predict result, backward pass for gradient and at the last the weight will be updated.
As we have seen the shape of training sample above ,that is [100,1,28,28], we need to convert the sample shape into [100,28*28].So our first step would be to reshape the size of sample then we will again go for further training process.
Let’s check the model accuracy on validation dataset .In our case the validation dataset would be test_dataloader.
with torch.no_grad():
n_correct=0
n_samples=0
for images,labels in test_dataloader:
images=images.reshape(-1,784)
output=Mnist_model(images)
labels=labels
_,prediction=torch.max(output,1)
n_samples=labels.shape[0]
n_correct=(prediction==labels).sum().item()
accuracy=(n_correct/n_samples)*100
On this model i am getting the accuracy of 97% on validation set.
let’s predict the result
predicted=[]
with torch.no_grad():
n_correct=0
n_samples=0
for images,labels in test_dataloader:
images=images.reshape(-1,784)
output=Mnist_model(images) #applying the model we have built
labels=labels
_,prediction=torch.max(output,1)
predicted.append(prediction)
print(prediction)
Conclusion :-
Hope You Liked The Blog Please Give Your Valuable Feedback For Further Improvement. This Blog is inspired from python engineer video please don’t miss to watch the video for better understanding .Keep Exploring Keep Learning………………..