Covid-19 predictor using posteroanterior (PA) chest X-ray images
An attempt to detect covid-19 from x-ray images of suspected individuals
The X-ray dataset is taken from - https://github.com/ieee8023/covid-chestxray-dataset.
I have been running this code on Google Colab environment using a GPU as a hardware accelerator. Since the notebook is using Resnet50 for transfer learning you will need a machine with an NVIDIA GPU otherwise the training might take ages
The folder structure I am using here to store the images is -
Covid/
----| covid_positive
----| covid_negative
----| metadata.csv (This can be downloaded from the open source database)
Fast AI makes it extremely simple if you make a folder for each of your class and put all the respective images in that folder
from fastai.vision import *
from fastai.widgets import *
import pandas as pd
Mount Google Drive with path to folders containing images. You need to change this path to point to the folder which contains the x-ray images
drive_path = 'drive/My Drive/FastAI/Covid/'
df = pd.read_csv(drive_path+'metadata.csv')
covid_positive = df['finding'] == 'COVID-19'
xrays = df['modality'] == 'X-ray'
CT = df['modality'] == 'CT'
PA = df['view'] == 'PA'
AP = df['view'] == 'AP'
I have cloned the image repository to my local machine. Since I am running this notebook on Colab I have to run a script on my local machine to upload the covid_postive and covid_negative images to my google drive.
PA_covid and PA_non_covid gives me the list of file names which are then uploaded to google drive
PA_covid = df[covid_positive & PA ]
PA_non_covid = df[PA & ~covid_positive]
#List files belonging to both the classes
covid_images = [files for files in PA_covid['filename']]
non_covid_images = [files for files in PA_non_covid['filename']]
path = Path(drive_path)
classes = ['covid_positive', 'covid_negative']
#sanity check images
for c in classes:
verify_images(path/c)
#Split data into train and validation sets
np.random.seed(42)
data = ImageDataBunch.from_folder(path, train=".", test='test', valid_pct=0.20,
ds_tfms=get_transforms(), bs=8, size=512, num_workers=4).normalize(imagenet_stats)
#View data
data.show_batch(rows=3, figsize=(7,8))
precision=Precision()
recall=Recall()
AUC=AUROC()
learn = cnn_learner(data, models.resnet50, metrics=(accuracy, precision, recall, AUC))
learn.fit_one_cycle(1)
learn.save('stage-1')
Use lr_find() to choose learning rate
learn.lr_find()