Recognizing Malaria Cells Using Keras Convolutional Neural Network(CNN)

Artificial Intelligence has vast-ranging attention and its utilization in the healthcare business or industry. As an intense learner and a Kaggle beginner, I chose to work on the Malaria Cells dataset to get a little hands-on experience and discover how to work with CNN, Keras, and pictures on the Kaggle platform.

In many points I love about Kaggle is the extensive knowledge it exists in the form of Kernels and Discussions. Taking ideas and references from different kernels and specialists really assisted me in getting more skilled at creating highly accurate results. Take a look at other kernels and see their strategy to gain more insights for your own improvement and knowledge building.

You can download the data from Kaggle.

Importing dataset and libraries

I started by importing pandas, numpy, and matplotlib. I chose to run Keras with Tensorflow backend to perform the neural network model. So, then imported a number of layers from Keras.layers including Convolution2D, MaxPooling2D, Flatten, Dense, BatchNormalization, and dropout. We will be using the Sequential model. To get started with pictures in the dataset, I imported os, cv2, and Image libraries.

# Libraries used for data visualization and manipulation
import numpy as np
np.random.seed(1000)
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.model_selection import train_test_split

# Libraries used for creating the CNN model
import keras
from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout
from keras.models import Sequential

# Libraries used for working with pictures
import os
import cv2
from PIL import Image

Importing the dataset

As you know, in Kaggle, any data files are placed inside the input folder that is one level up from wherever the notebook is located. The pictures are in the cell_images folder. Therefore, I set up the data directory as DATA_DIR to point to that place or location. To save the characteristics or features, I utilized the variable dataset, and as labels, I did label. For this example, I set every picture size to be 64×64.

DATA_DIR = 'your_direction_of_cell_images/'
SIZE = 64
dataset = []
label = []

The next and the following measure is to import our data. The infected cell pictures are in the Parasitized folder, and the uninfected pictures are in the Uninfected folder.

Toward both folders, I iterated over every file that has the extension png.

parasitized_images = os.listdir(DATA_DIR + 'Parasitized Cells/')
for i, image_name in enumerate(parasitized_images):
    try:
        if (image_name.split('.')[1] == 'png'):
            image = cv2.imread(DATA_DIR + 'Parasitized Cells/' + image_name)
            image = Image.fromarray(image, 'RGB')
            image = image.resize((SIZE, SIZE))
            dataset.append(np.array(image))
            label.append(0)
    except Exception:
        print("Could not read image {} with name {}".format(i, image_name))

For infected cell pictures, I read the picture by using cv2.imread(), then change it from an array utilizing Image.fromarray(), and after that, resize it to 64×64. Lastly, I kept it to the dataset variable and added 0 for each of these pictures to the label. I repeated the identical method for the uninfected cell pictures but fixed the label as 1 at this time.

Visualize the dataset

We gonna use matplotlib to randomly plotting 5 infected and 5 uninfected cell pictures.

infected cells pictures

infected cells pictures

uninfected cell pictures

uninfected cell pictures

Implementing the CNN model

The CNN model is one of the most powerful and efficient neural networks to work with pictures and perform classifications. I worked with Keras to build the neural network model.

Convolution2D

We built a convolution kernel. I initiated some features as described below:

  • filter: The first parameter specifies the shape of the layer output. In our case, for both two layers, I put the value as 32.
  • kernel_size: It represents the window size that we need to do, and it will traverse along with the picture. I fixed it as 3×3.
  • input_shape: It can be used to determine the size of the input for every picture. In this design, I am working with images that have a size of 64×64, and the pictures are colored. These channels are thus 3. So, the input_shape it is gonna be (64, 64, 3). We want to set input_shape only for the first layer.
  • activation: The activation function that we used here is relu, which is the (Rectified Linear Unit) as the default activation function.

MaxPool2D

This MaxPool2D method is used to downscale the outputs, and it holds the next parameters:

  • pool_size: It represents the size of the matrix, which determines the number of pixel values that will be changed or converted to 1 value. We gonna be using the value as 2×2, so a picture of the size 62×62 gonna be converted to 31×31.
  • data_format: It explains that, at the input, the channels are determined at the start or at the end. However, in our case, this third value is for the channel in (64, 64, 3), and I fixed data_format to be channels_last.

BatchNormalization

This method normalizes the earlier activation function output, and I changed only one parameter:

  • axis: It represents the axis to be normalized. As I used channels_last, I set the value as -1.

Dropout

This method picks some of the values randomly to be set as 0 so to stop overfitting in the CNN model, and we used just the rate parameter:

  • rate: The input fractions that gonna be dropped. I put the rate at 0.2.

Flatten

This method flattens the whole n-dimensional matrix to a single array. Hence, if the array size was 64x64x3, it transformed into an array of size 12,288.

Dense

This method determines a densely connected CNN layer, and I have set the following parameters:

  • activation: This function sets the activation function, which is, in our case, the relu activation function, just for the latest output layer. For the latest dense layer, I used the sigmoid activation function.
  • units: This specifies the neurons number in the provided layer. I have built three layers with neuron count as (512, 256, 2).

Construction of the CNN Model in our example

I have built a Sequential CNN model (classifier).

classifier = Sequential()
classifier.add(Convolution2D(32, (3, 3), input_shape = (SIZE, SIZE, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2), data_format="channels_last"))
classifier.add(BatchNormalization(axis = -1))
classifier.add(Dropout(0.2))
classifier.add(Convolution2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2), data_format="channels_last"))
classifier.add(BatchNormalization(axis = -1))
classifier.add(Dropout(0.2))
classifier.add(Flatten())
classifier.add(Dense(activation = 'relu', units=512))
classifier.add(BatchNormalization(axis = -1))
classifier.add(Dropout(0.2))
classifier.add(Dense(activation = 'relu', units=256))
classifier.add(BatchNormalization(axis = -1))
classifier.add(Dropout(0.2))
classifier.add(Dense(activation = 'sigmoid', units=2))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

I have built a CNN Layer followed by a MaxPooling layer. As you see, it is followed by BatchNormalization to normalize the previous layer’s output and implement the Dropout regularization. Extra collection of these layers after that have appended. After that, we are gonna use Flatten to the outputs. Then we pass the flattened outputs to an Artificial Neural Network that holds three dense layers with (512, 256, 2) nodes. The latest layer that has the activation function sigmoid is the output layer.

The final measure is to compile the CNN model. We are gonna use an optimizer call Adam and this meaning a categorical problem; I have applied the loss function as categorical_crossentropy and an evaluation metric as accuracy.

Train the CNN model and accuracy

I cut the dataset into two categories 20% is the testing set and 80% for training data.

history = classifier.fit(np.array(X_train), 
                         y_train, 
                         batch_size = 64, 
                         verbose = 2, 
                         epochs = 50, 
                         validation_split = 0.1,
                         shuffle = False)
                         
print("Test_Accuracy: {:.2f}%".format(classifier.evaluate(np.array(X_test), np.array(y_test))[1]*100))

By working with the fit function, we have trained our convolutional neural network with y_train and X_train. we have set the total amounts of epochs as 50 epochs, which is essentially 50 cycles or iterations of the full dataset including a batch size of 64.

The neural network have reached an accuracy of 95.75%.

Dataset augmentation plus accuracy enhancement

Data augmentation benefits grow the dataset and train the neural network on more extra and different data. More available data for CNN to learn from means the greater the model performs. The Keras framework gives a subpackage ImageDataGenerator that can generate this dataset.

Augmentation of Data

from keras.preprocessing.image import ImageDataGenerator

train_generator = ImageDataGenerator(rescale = 1/255,
                                     zoom_range = 0.3,
                                     horizontal_flip = True,
                                     rotation_range = 30)

test_generator = ImageDataGenerator(rescale = 1/255)

train_generator = train_generator.flow(np.array(X_train),
                                       y_train,
                                       batch_size = 64,
                                       shuffle = False)

test_generator = test_generator.flow(np.array(X_test),
                                     y_test,
                                     batch_size = 64,
                                     shuffle = False)

As you noticed above, for our training data, I rescaled the pictures by dividing it by 255, zoomed pictures with a variety of 0.3, After that, flipped the pictures horizontally plus rotated them by 30. And for the remaining data, which is testing data, I only rescale the pictures. The train_generator and test_generator methods are built with a batch size of 64.

Calculating new accuracy

history = classifier.fit_generator(train_generator,
                                   steps_per_epoch = len(X_train)/64,
                                   epochs = 50,
                                   shuffle = False)
                                   
print("Test_Accuracy(after augmentation): {:.2f}%".format(classifier.evaluate_generator(test_generator, steps = len(X_test), verbose = 1)[1]*100))

After that, we trained the classifier (the model) by utilizing the fit_generator method and measured that new accuracy.

The neural network has reached an accuracy of 96.41% with our data augmentation.

As you saw above, using the data augmentation, we were able to improve the model accuracy while still owning the same data that we have started with. 

Conclusion

In this blog post, we have reviewed the application and the use of CNN models and also data augmentation for Malaria cell pictures and reached a test accuracy of 96.41%.


Note: This is a guest post, and the opinion in this article is of the guest writer. If you have any issues with any of the articles posted at www.marktechpost.com please contact at asif@marktechpost.com  

🚀 The end of project management by humans (Sponsored)