Image Classification With ResNet50 Convolution Neural Network (CNN) on Covid-19 Radiography
By: Emmanuella Anggi
Tags: Resnet, Artificial Intelligence, Cnn, Image Classification, Machine Learning

Image Classification With ResNet50 Convolution Neural Network (CNN) on Covid-19 Radiography

On final year of uni, they offered three selection subject to choose: Image Processing, AR/VR, and Digital Forensic. I was trying so hard to be different and chose Digital Forensic, my own worst decision. It was so hard and without any interest on the field, i didn’t actually understand (surviving CS 101). Anyway, a lesson for me (and probably you): just be realistic.

So because I was so silly to choose another subject instead of image processing, I tried to learn it myself. Turned out, it’s even more fun to learn by yourself. I will list every source to understand Image Processing the easy way on every point. Anyway, Kaggle provides this free course explaining very detailed about Image Processing with Deep Learning.

In this article, I used Kaggle dataset with X-ray images that classify COVID-19, Viral pneumonia and Normal Chest. There are 219 COVID-19 Positive images, 1341 Normal images and 1345 Viral Pneumonia images.

As always, head to my Github for the code❤️

I linked every article I used as references as the underlined hyperlinks. Big thanks to each authors.

Image Classification

Image Classification is a method to extract information of image classes digitally. Shortly, it used to classify images. Image Classification can be supervised (you give label to each images) and also unsupervised (the training model learned to classify the images based on the patterns).

This article used Supervised Image Classification with three classes.

CNN

As I mentioned before, I am using Resnet50 layers, which is one of CNN architectures. To know more about CNN, this article explained CNN so clear. To sum it up, the flow of CNN are:

Convolution Layer (extract feature with filtering) Strides (shifting pixels over the input matrix) Padding Rectified Linear Unit (RelU) (introduce non-linearity to the network) Padding Layer (reduce number of parameters) Fully Connected Layer (flatten matrix into vector and feed it to a fully connected neural network layer.)

There are few architecture of CNN (some of the most common is ResNet, VGGNet), but in this post, I will use the ResNet50.

ResNet or Residual Network uses the residual learning instead of trying to learn some features. Residual can be simply understood as subtraction of feature learned from input of that layer. There are some variants of ResNet other than ResNet50. The core idea of ResNet is introducing a shortcut connection that skips one or more layers.

ResNet50 has 50 layers deep, below is the architecture of ResNet50 with 34 layer residual. Here is the paper of the network and here is why ResNet is a good CNN architecture to be used.

Data

I actually wanted to try Image Processing for my research but in that time there were so many people said that image training consumed too much time and memory, plus if you don’t have decent laptop/computer, it might be kind of impossible to do so. So I didn’t. But turned out, you could just use Google Colab. I don’t know how much time if I train it on my computer but with Colab it’s not that long like my friends had said.

If you wanted to access the data from Kaggle, you could just download the JSON API of your Kaggle account, upload to your Colab files then download and unzip the image data to your Colab environment.

! pip install -q kaggle

! mkdir ~/.kaggle

! cp kaggle.json ~/.kaggle/

! chmod 600 ~/.kaggle/kaggle.json ! kaggle datasets download -d tawsifurrahman/covid19-radiography-database

! unzip "covid19-radiography-database.zip"

You can also access data from your Google Drive with mounting your Colab session and access your image data location from the Drive.

from google.colab import drive

drive.mount('/content/drive')

If you want to access the data from an open source website, you can just use request library from Python. To take an example, here’s how to download 20GB data from Physio Net.

import requests, zipfile, io zip_file_url = "https://physionet.org/static/published-projects/siena-scalp-eeg/siena-scalp-eeg-database-1.0.0.zip" r = requests.get(zip_file_url)

z = zipfile.ZipFile(io.BytesIO(r.content)) z.extractall()

Pre-Processing

Library

Some libraries that I used

from fastai.vision import *

from fastai.metrics import error_rate import torch

import torch.nn as nn

import torch.optim as optim

from torch.optim import lr_scheduler

from torch.autograd import Variable import numpy as np import time

import os from fastai.callbacks import ActivationStats

%matplotlib inline

import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

Access the image from folder

Define the folder path

path = Path('/content/COVID-19 Radiography Database/') folder = path folder.mkdir(parents=True, exist_ok=True)

Extract the image data bunch

np.random.seed(42) data = ImageDataBunch.from_folder(path, train='.', valid_pct=0.35, ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)

Training with CNN Resnet50

With fastai, we can instantly use the Resnet model by the function of cnn_learner. Don’t forget to define the accuracy metrics so that we can see the loss and accuracy.

To reduce time, I will just use four epochs.

learn = cnn_learner(data, models.resnet50, metrics=accuracy) learn.fit_one_cycle(4)

With the model, we can find the optimal learning rate to get the better performance. Unfreezing the model is used to let the model continue the training. In this case, unfreezing will let the model to train again with the better learning rate after we got the observation.

learn.unfreeze() learn.lr_find()

learn.recorder.plot(suggestion=True)

Next training with better learning rate can train for just 2 epochs.

Result

The very good thing that I really love using FastAI is that the model evaluation is so so easy. We can just interpret the classification model like the loss prediction, confusion matrix, most confused.

interp = ClassificationInterpretation.from_learner(learn) losses, idxs = interp.top_losses()

First, we can plot the loss of prediction from the model.

interp.plot_top_losses(9, figsize=(15,11))

And then the Confusion Matrix.

interp.plot_confusion_matrix(figsize=(12,12), dpi=100)

Conclusion

It’s very convenient to apply many AI methods now, especially with FastAI Image Processing. I think the most important thing to be on studying Machine Learning is to be creative, innovative and never stops learning new things!

Thank you for reading!

More Source to Learn