AI for Good — Medical Image Analyses for Malaria Detection

In this blog we will talk about why Malaria detection is important to detect early presence of parasitized cells in a thin blood smear and some hands on for same.

Introduction

Malaria is a deadly, infectious mosquito-borne disease caused by Plasmodium parasites. These parasites are transmitted by the bites of infected female Anopheles mosquitoes. While we won’t get into details about the disease, there are five main types of malaria.
Let’s now look at the significance of how deadly this disease can be in the following plot.
It is pretty clear that malaria is prevalent across the world particularly in tropical regions. The motivation for this project is but supported the character and fatality of this sickness. initially if associate infected mosquito bites you, parasites carried by the mosquito can get in your blood and begin destroying oxygen-carrying RBCs (red blood cells). Usually the primary symptoms of malaria are kind of like the flu or a virus once you usually start feeling sick within a few days or weeks once the mosquito bite. however these deadly parasites will sleep in your body for over a year with none problems! so, a delay in the right treatment will cause complications and even death. therefore early and effective testing and detection of malaria will save lives.

Approach to the solution

Although the malaria virus doesn’t take the form of a mutant mosquito, it sure feels like a mutant problem. The deadly disease has reached epidemic, even endemic proportions in different parts of the world — killing around 400,000 people annually . In other areas of the world, it’s virtually nonexistent. Some areas are just particularly prone to a disease outbreak — there are certain factors that make an area more likely to be infected by malaria .
  • High poverty levels
  • Lack of access to proper healthcare
  • Political instability
  • Presence of disease transmission vectors (ex. mosquitos) [6]
With this mixture of these problems, we must keep some things in mind when building our model:
  • There may be a lack of a reliable power source
  • Battery-powered devices have less computational power
  • There may be a lack of Internet connection (so training/storing on the cloud may be hard!)

Traditional Methods for Malaria Detection

There are several methods and tests which can be used for malaria detection and diagnosis.
These include but are not limited to, thick and thin blood smear examinations, polymerase chain reaction (PCR) and rapid diagnostic tests (RDT). I will not going to cover all the methods but the thing is , traditional tests typically used an alternative particularly where good quality microscopy services cannot be readily provided.
Microscopic examination of blood is the best known method for diagnosis of malaria. A patient’s blood is smeared on a glass slide and stained with a contrasting agent that facilitates identification of parasites within red blood cells.
A trained clinician examines 20 microscopic fields of view at 100 X magnification, counting red blood cells that contain the parasite out of 5,000 cells (WHO protocol).
thanks Carlos Atico for this wonder blog on data science insights
Thus, malaria detection is definitely an intensive manual process, which can perhaps be automated using deep learning which forms the basis of this blog.

Deep learning for Malaria Detection

Deep Learning models, or if I have to say more specifically, Convolutional Neural Networks (CNNs) have proven to be really effective in a wide variety of computer vision tasks. While we assume that you have some knowledge on CNNs, in case you don’t, feel free to dive deeper into them by checking out this article here. Briefly, The key layers in a CNN model include convolution and pooling layers as depicted in the following figure.
Convolutional neural networks(CNN) can automatically extract features and learn filters. In previous machine learning solutions, features had to be manually programmed in — for example, size, color, the morphology of the cells. Utilizing Convolutional neural networks (CNN) will greatly speed up prediction time while mirroring (or even exceeding) the accuracy of clinicians.
CNN learns hierarchical patterns from our data. Thus they are able to learn different aspects of images. For example, the first convolution layer will learn small and local patterns such as edges and corners, a second convolution layer will learn larger patterns based on the features from the first layers, and so on.
You can go through an very interesting Research paper ‘Pre-trained convolutional neural networks as feature extractors toward improved parasite detection in thin blood smear images’ by Rajaraman et al. It explains a six pre-pretrained models on the data mentioned in the above paper. to obtain an accuracy of 95.9% in detecting malaria vs non-infected samples.

Dataset Explanation

Lets see what data we are using for this problem set , I am very thankful to researchers at the Lister Hill National Center for Biomedical Communications (LHNCBC), part of National Library of Medicine (NLM) who have carefully collected and annotated this dataset of healthy and infected blood smear images. You can download these images from the official site.
They had also launched an mobile application , that can run on an andriod smartphone attached to a conventional light microscope (Poostchi et al., 2018). Giemsa-stained thin blood smear slides from 150 P. falciparum-infected and 50 healthy patients were collected and photographed at Chittagong Medical College Hospital, Bangladesh. The smartphone’s built-in camera acquired images of slides for each microscopic field of view. The images were manually annotated by an expert slide reader at the Mahidol-Oxford Tropical Medicine Research Unit in Bangkok, Thailand.
Note: Now before we begin, I’d like to point out that I am neither a doctor nor a healthcare researcher and I’m nowhere near to being as qualified as they are. I do have interests though in applying AI for healthcare research. The intent of this article is not to dive into the hype that AI would be replacing jobs and taking over the world, but to showcase how AI can be useful in assisting with malaria detection, diagnosis and reducing manual labor with low-cost effective and accurate open-source solutions.
This is what our training data is look like
Note: I am using google colab and the code will be according to the same , i recommend you all to use google colab for ease.
lets go to the code:

Code

Initialization

Virtual machine testing on google colab

If Google’s servers are crowded, you’ll eventually have access to only part of a GPU. If your GPU is shared with another Colab notebook, you’ll see a smaller amount of memory made available for you.
When I started running the experiments described here, I was lucky: I had 11441 MB RAM! My output looked like this:

Import libraries

Here we import all the necessary packages. We are going to work with the fast.ai V1 library which sits on top of Pytorch 1.0. The fast.ai library provides many useful functions that enable us to quickly and easily build neural networks and train our models.

Some util functions: Export and restoration

Now here is an export network for deployment and one for create an copy
Restoration of a deployment model, for example in order to continue fine-tuning
Now let’s download the NIH dataset , on which we will work today:
The backups=1 parameter of wget will allow you to repeat the command line many times, if a download fails, without creating a lot of new versions of the files.
The ouput of the last line should be like thi:
Lets unzip the NIH cell images dataset:
This will produce a very large verbose output, that will look like this:

Prepare your data

Change the name of the cell_images folder to train, and then mv it on top of a new data folder, so fast.ai can use it to automatically generate train, validation and test sets, without any further fuss

Deep dive on data folder

Install the tree command if you haven’t it:
lets run the command now
this will show an tree structure of your folder:

Variable Initialization

create training and validation data bunches

With fast.ai it is not necessary: if you only have a ‘train’ folder, you can automatically split it while creating the DataBunch by simply passing a few parameters. We will split the data into a training set (80%) and a validation set (20%). This is done with the valid_pct = 0.2 parameter in the ImageDataBunch.from_folder() constructor method:
Limit your augmentations: it’s medical data! You do not want to phantasize data…
Warping, for example, will let your images badly distorted, so don’t do it!
This dataset is big, so don’t rotate the images either. Lets stick to flipping…
The print() will output the transforms and the classes:
and the data command in the last will simply output the return value of the ImageDataBunch instance:

A look on augmented Databunches

ResNet18

The ResNet18 is much smaller, so we’ll have more GPU RAM for us. We will create the DataBunch again, this time with a bigger batch size.
I had also used ResNet50 , It was giving an 92% accuracy.
But ResNet18 is good fit for this data and in this blog we are going to use that,
Now, create the learner:
If you Colab environment doesn’t have the pretrained data for the ResNet18, fast.ai will automatically download it:
Lets have an look on the model:

Let’s train it

We will again use the fit_one_cycle HYPO training strategy. Limit the training to 10 epochs to see how this smaller network behaves:
This table below shows that the network learned to an accuracy of roughly 96.1% and suggests that the network should not be trained further: the loss between epoch #8 and #9 shows a 0.005 decrease but the accuracy has remained the same, suggesting that the network has started overfitting.
Let’s generate a ClassificationInterpretation and look at the confusion matrix and the loss curves.
Let’s have an look on confusion matrix:
Let’s look at the losses:
ResNet18 started overfitting a bit after about 290 batches.
Remember that our bs is 512 here and was 256 for the ResNet34.

Fine tuning the model

Here we will introduce another fast.ai HYPO: automatically chosen variable learning rates. We will let fast.ai choose which learning rate to use for each epoch and each layer, providing a range of learning rates we consider adequate. We will train the network for 30 epochs.
Here , our fast.ai model achieved the accuracy of approx 97% after some fine tunning , that is quite good enough .
The validation loss, however, was becoming worse for the last epochs. This indicates that we have been overfitting from about epoch #20 on.
So if we want to deploy the model , choose the one with 20 epochs and note that this is the best accuracy at least with this network ,we can you different network for better result.

Results

This is better than what we’ve had before. Let’s look at the loss curves:
Here we see that the network seems to start to over-fit after 500 batches, which would confirm our suspicion inferred from the results table above. If you look at the curves above, you’ll see that the validation loss starts to grow in the last third of the training, suggesting that this part of the training only over-fitted the network.
Now i will give this task to you guys , tune the network or use any technique and control this situation.
Hint: save your model after every epoch.
Happy learning
Follow MachineX for more”

References

Towards Data Science

Sharing concepts, ideas, and codes.

Comments

Popular posts from this blog

Flutter for Single-Page Scrollable Websites with Navigator 2.0

A Data Science Portfolio is More Valuable than a Resume

Better File Storage in Oracle Cloud