Learn practical skills, build real-world projects, and advance your career

Character Classification using ResNet9

The Model is trained over EMNIST dataset

For my course project of course Deep Learning with PyTorch: Zero to GANs. I decided to create a neural network model which can classify Numbers, Uppercase English Alphabet characters and Lowercase English Alphabet characters.

For model I have chosen the ResNet9 which I learned in lecture 5 and it is the best model we have learned so far in the course. For model Training I am using EMNIST (Extended MNIST) dataset which have total of 814,255 images 697,932 for training and 116,323 for testing the model. Im using ByClass classification means it has 62 classes [0 - 1] digits, [A - Z] UpperCase Character and [a - z] LowerCase Character. Read more about the dataset in this PDF

#Importing the module required for the project

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torchvision.datasets import EMNIST
from torch.utils.data import DataLoader
import torchvision.transforms as tt
from torch.utils.data import random_split
from torchvision.utils import make_grid
import matplotlib
import matplotlib.pyplot as plt
import pickle 
import requests
import zipfile
import os, shutil
from PIL import Image
%matplotlib inline

matplotlib.rcParams['figure.facecolor'] = '#ffffff'
# The project name
project_name='emnist-project'

Now Dowloading the EMNIST dataset using torch.datasets.EMNIST function. First we have defined the root path, split argument to byclass because we need all the 62 classes. then download to true so that the dataset will be downloaded if it not available, then train and at the last transform. The the transform we are first rotating the image by -90 degree and then a horizintal flip and at last converting them to Tensor.

We have done such transform because the original dataset is little rotated and weird.

dataset = EMNIST(root="data/", split="byclass", download=True, train=True, 
                transform=tt.Compose([
                    lambda img: tt.functional.rotate(img, -90),
                    lambda img: tt.functional.hflip(img),
                    tt.ToTensor()
                ]))
                
test_dataset = EMNIST(root="data/", split="byclass", download=True, train=False, 
                transform=tt.Compose([
                    lambda img: tt.functional.rotate(img, -90),
                    lambda img: tt.functional.hflip(img),
                    tt.ToTensor()
                ]))
Downloading and extracting zip archive Downloading http://www.itl.nist.gov/iaui/vip/cs_links/EMNIST/gzip.zip to data/EMNIST/raw/emnist.zip
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting data/EMNIST/raw/emnist.zip to data/EMNIST/raw Processing byclass
/usr/local/lib/python3.6/dist-packages/torchvision/datasets/mnist.py:480: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:141.) return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
Processing bymerge Processing balanced Processing letters Processing digits Processing mnist Done!