Malaria Detection using Deep Learning

Sameer Kumar
Analytics Vidhya
Published in
5 min readJan 23, 2022

Introduction

In this article, we will go through the mini project of Malaria detection using Convolutional Neural Networks (CNN) by feeding certain images to the model and it will identify whether the person is infected or not.

We will also go through the basic concepts that are used in a CNN model for more clarity of those concepts. So, let us begin!!

Let us first understand the basic layout/approach of a CNN problem statement:

  1. Convolution
  2. RELU/tanh activation function
  3. Pooling (Max Pooling or Min Pooling)
  4. Flattening
  5. Fully Connected Neural Network

We create the CNN architecture by above 5 steps and then we fit the CNN to the images using ImageDataGenerator library.

We will understand each concept one by one. First, let us have a look at the dataset.

The training dataset contains 416 images of infected and non infected whereas the testing dataset consists of 134 images of infected and non infected.

Infected image
Non infected image

Let us first see the code for building CNN architecture.

Below’s image shows all the libraries required for the task.

We create a sequential model first which helps in stacking up the layers and we can add convolutional layer or a pooling layer one by one in that stack.

Convolution

Convolution is the process of filtering out different kinds of information from an image matrix using kernels or filters. As we know that an image is made up of pixels and each pixel represents a property of image like shape,size etc so each filter represents or filters one property of that image.

We multiply the image matrix with with the filter of size 2(in code) to get a feature map whose size is less than original image.

The input shape is (224,224,1) and number of filters used is 16 so it filters 16 properties.

RELU/tanh

We then perform element-wise activation function on each pixel to bring non linearity in our CNN network.

Pooling

We have now filtered our image using the kernel or a filter. Now, we should focus more on those pixel values which represent the dominating properties. So, we use max pooling for this to concentrate only on important pixels, we use a matrix of size n*n and the max value of that window is the output and the stride (pixel shift) generally is kept as 2 in pooling so that no overlap happens.

The parameter in code used is pool_size=2 which means the size of pool marix is 2.

Flattening

We can use any number of convolutional layers and any number of pooling layers for our CNN model.

After getting the filtered image, we tend to flatten the matrix into a 1D array and and feed it to the Neural Network(Dense Layers) as features and then normal Backpropagation and Forward propagation happens in the network.

We have used the dense layer after flatten and the dense layer has 500 neurons and relu activation function is used here. The last layer consists of the output and 2 nodes are used with softmax activation function.

Our CNN architecture is ready and now lets read the data from the folder.

Image Data Generator and Data augumentation

In CNN problems, we can read the dataset(images) from our folders in many ways. One of the ways is Image Data generator library present in Keras,

Image data generator helps us to apply some feature scaling on the images so that it can internally create more images to adjust to inverted, zoomed images etc. This process is also called Data Augumentation. We have used the batch_size as 32 which means 32 images will be in a batch and will be used for one iteration.

We prepare all this for our data and we fetch our data from our directory.

So our training and testing set is ready and now we use fit in the model and get an accuracy of approx 80%.

Conclusion

This was a short overview on how we use CNN approach for these mini deep learning projects by going through the basic processes that basically happens while training a deep learning model.

I will keep posting more and more materials and articles related to Machine Learning and Deep Learning. You can connect with me on LinkedIn to follow my work related to Data Science :)

Do have a look at my other articles !!

--

--

Sameer Kumar
Analytics Vidhya

AI Research intern at SCAAI || Kaggle 2x Expert || Machine Learning || Deep Learning || NLP || Python