Covid-19 X-ray prediction using Deep Learning
Introduction
The world right now is still fighting hard with the coronavirus. This blog is a prediction of the virus based on Covid-19 X-ray of humans. Learn how to build an ANN model on Pytorch using the ReLU activation function to predict the presence of the virus. For deep learning refer to blogs https://ainewgeneration.com/category/deep-learning/
Here is a video that helps you understand the concepts of ANN. It is a hands-on project and I recommend you watch the video https://www.youtube.com/watch?v=9hS6oEzC4Oo
Table of Contents
- About Covid-19 X-ray Dataset
- Import Libraries
- Load Dataset
- Visualization of Dataset
- ImageFolder & DataLoader
- Creating Model
- Training Model
- Visualizing the Accuracy & Loss Graph
- Visualizing the results on Real World Covid-19 X-ray Images
About Covid-19 X-ray Dataset
Dataset https://drive.google.com/drive/folders/1Ax8PttO8sC2_uWFwqo570lz-4Q3gCsu9?usp=sharing
The COVID-19 pandemic has been gripping plenty of the world recently. despite the fact that there have been a lot efforts gone into developing less costly checking out for the hundreds, it’s been shown that the hooked up and extensively available chest x-rays (CXR) may be used as a screening criteria. thanks to the committed paintings by diverse people and businesses, publicly to be had chest x-rays of COVID-19 subjects are available for analytic usage.

Import Libraries
Let us first import the libraries we will be using for the model
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data import random_split
import torchvision
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
from torchvision.utils import make_grid
Load Dataset
Let us use Pandas to import the data now. Make sure you give the right datapath.
data_path_train = r"C:\Users\prana\Downloads\Covid19-dataset-20211122T042133Z-001\Covid19-dataset\train"
data_path_test = r"C:\Users\prana\Downloads\Covid19-dataset-20211122T042133Z-001\Covid19-dataset\test"
Image Augmentation
Its time to transform the images of our dataset into a standard format and here is how to do it:
img_size = 120
img_transform = transforms.Compose([
transforms.Resize((img_size, img_size)), transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])])
ImageFolder & DataLoader
The dataset that we are using has the images sorted and labeled in 3 different folders. ie, Covid, Normal and Viral Pneumonia. To make our training and testing dataset from these organized folders, we use ImageFolder that we have imported earlier.
train_data = ImageFolder(root = data_path_train, transform =img_transform)
testing_data = ImageFolder(root = data_path_test, transform = img_transform)
Let us now check the length and indexes of the data
input: len(testing_data)
Output: 66
input: train_data.class_to_idx
output: {'Covid': 0, 'Normal': 1, 'Viral Pneumonia': 2}
Let us split our testing data into two datasets using random_split. One is for testing and the other is for validation.
test_data, val_data = random_split(testing_data,[16,50])
Now we use DataLoader to turn our dataset into batches so that our model will not be overburdened. Since our dataset is small, we divide our data into batches of 16.
train_loader = DataLoader(train_data, batch_size=16, shuffle=True)
val_loader = DataLoader(val_data, batch_size=16, shuffle=False)
Let us now check the shape of our images and labels
input: for images, labels in train_loader:
print(images.shape, labels.shape)
break
output: torch.Size([16, 3, 120, 120]) torch.Size([16])
Visualization of Dataset
Let us now visualize the dataset by creating a function so that we can use the same function for our train, test, and Val datasets. We use matplotlib.pyplot and make grid libraries to visualize the images.
def show_img_batch(data):
for images, lables in data:
plt.figure(figsize=(12,5))
plt.imshow(make_grid(images, n_row=5).permute(1,2,0))
plt.show()
break
input: show_img_batch(train_loader)
output:
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Creating Model
Let us now create a class function for our ANN model
class ANN(nn.Module):
def __init__(self, hidden_layer1=64):
super(ANN, self).__init__()
self.hidden_layer1 = hidden_layer1
self.fc1 = nn.Linear(img_size*img_size*3, hidden_layer1)
self.fc2 = nn.Linear(hidden_layer1, 3)
self.relu = nn.ReLU()
def forward(self, img):
out = img.view(-1, img_size*img_size*3)
out = self.fc1(out)
out = self.relu(out)
out = self.fc2(out)
return out
input: model = ANN()
print(model.parameters)
output: <bound method Module.parameters of ANN(
(fc1): Linear(in_features=43200, out_features=64, bias=True)
(fc2): Linear(in_features=64, out_features=3, bias=True)
(relu): ReLU()
)>
Define the Loss function and Optimizer for our model. Here we are using Cross-Entropy Loss and SGD.
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD( model.parameters(), lr= 0.001)
Training the Model
Lets us now create a function to train our model.
def ANN_train(loss_fn, optimizer):
epochs=15
training_loss =[]
training_acc =[]
testing_loss = []
testing_acc =[]
for epoch in range(epochs):
train_acc = 0.0
train_loss = 0.0
model.train()
for images, labels in train_loader:
optimizer.zero_grad()
output = model(images)
loss = loss_fn(output, labels)
loss.backward()
optimizer.step()
predictions = torch.argmax(output, 1)
train_acc += (predictions == labels).sum().item()
train_loss += loss.item()
training_acc.append(train_acc/len(train_data))
training_loss.append(train_loss/len(train_loader))
test_acc = 0.0
test_loss = 0.0
with torch.no_grad():
for images, labels in val_loader:
output = model(images)
loss = loss_fn(output,labels)
predictions = torch.argmax(output,1)
test_acc += (predictions == labels).sum().item()
test_loss += loss.item()
testing_acc.append(test_acc/len(val_data))
testing_loss.append(test_loss/len(val_loader))
print("Epochs:{}, Training Accuracy:{:.2f}, Training Loss:{:.2f}, Validation Accuracy:{:2f}, Validation Loss:{:.2f}".format(
epoch+1,train_acc/len(train_data),train_loss/len(train_loader),test_acc/len(val_data),test_loss/len(val_loader)))
input: ANN_train(loss_fn, optimizer)
output: Epochs:1, Training Accuracy:0.80, Training Loss:0.58, Validation Accuracy:0.760000, Validation Loss:0.57
Epochs:2, Training Accuracy:0.86, Training Loss:0.38, Validation Accuracy:0.800000, Validation Loss:0.86
Epochs:3, Training Accuracy:0.89, Training Loss:0.29, Validation Accuracy:0.840000, Validation Loss:0.69
Epochs:4, Training Accuracy:0.88, Training Loss:0.26, Validation Accuracy:0.740000, Validation Loss:0.69
Epochs:5, Training Accuracy:0.94, Training Loss:0.23, Validation Accuracy:0.800000, Validation Loss:0.67
Epochs:6, Training Accuracy:0.93, Training Loss:0.21, Validation Accuracy:0.900000, Validation Loss:0.48
Epochs:7, Training Accuracy:0.95, Training Loss:0.18, Validation Accuracy:0.820000, Validation Loss:0.75
Epochs:8, Training Accuracy:0.95, Training Loss:0.18, Validation Accuracy:0.920000, Validation Loss:0.53
Epochs:9, Training Accuracy:0.95, Training Loss:0.15, Validation Accuracy:0.900000, Validation Loss:0.55
Epochs:10, Training Accuracy:0.95, Training Loss:0.15, Validation Accuracy:0.920000, Validation Loss:0.47
Epochs:11, Training Accuracy:0.96, Training Loss:0.14, Validation Accuracy:0.940000, Validation Loss:0.50
Epochs:12, Training Accuracy:0.97, Training Loss:0.12, Validation Accuracy:0.900000, Validation Loss:0.52
Epochs:13, Training Accuracy:0.98, Training Loss:0.12, Validation Accuracy:0.940000, Validation Loss:0.45
Epochs:14, Training Accuracy:0.97, Training Loss:0.12, Validation Accuracy:0.920000, Validation Loss:0.45
Epochs:15, Training Accuracy:0.97, Training Loss:0.11, Validation Accuracy:0.920000, Validation Loss:0.50
Visualizing the Accuracy & Loss Graph
Now that we attained 92 percent accuracy on our validation data, it is time to visualize the Accuracy and Loss graphs of the model using matplotlib.pyplot
plt.title("Accuracy Vs Epochs")
plt.plot(range(epochs), training_acc, label ="Training Accuracy")
plt.plot(range(epochs), testing_acc, label = "Validation Accuracy")
plt.legend()
plt.xlabel("Accuracy")
plt.ylabel("Epochs")
plt.show()

plt.title("Loss Vs Epochs")
plt.plot(range(epochs), training_loss, label ="Training Loss")
plt.plot(range(epochs), testing_loss, label = "Validation Loss")
plt.legend()
plt.xlabel("Accuracy")
plt.ylabel("Epochs")
plt.show()

Visualizing the results on Real World Covid X-RAYImage
To visualize our model’s predictions, we need to create a function that can read our image and return the predicted label
def predict_image( img, model):
x = img.unsqueeze(0)
y = model(x)
pred = torch.argmax(y, dim=1)
return testing_data.classes[pred]
input: img, label = test_data[2]
plt.imshow(img.permute(1,2,0))
print("Actual Label:", testing_data.classes[label],
"Predicted Label:", predict_image(img, model))
output:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Actual Label: Normal Predicted Label: Normal

input: img, label = test_data[10]
plt.imshow(img.permute(1,2,0))
print("Actual Label:", testing_data.classes[label],
"Predicted Label:", predict_image(img, model))
output: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Actual Label: Covid Predicted Label: Covid

input: img, label = test_data[10]
plt.imshow(img.permute(1,2,0))
print("Actual Label:", testing_data.classes[label],
"Predicted Label:", predict_image(img, model))
output: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Actual Label: Viral Pneumonia Predicted Label: Viral Pneumonia

Conclusion
The main aim of medical AI is to aid the doctors in better diagnosis. The model we created has attained 92 percent accuracy in predicting the existence of viruses.
To learn more about the Deep Learning techniques, you can watch videos here in our youtube channel https://www.youtube.com/channel/UCZUUitv-us8zJ0_JyKM-bqg/videos