Kartikey Sharma's other Models Reports

Major Concepts


Sign-Up/Login to access Several ML Models and also Deploy & Monetize your own ML solutions for free

Models Home » Domain Usecases » Health Care and Pharmaceuticals » Pneumonia Detection using Chest X-ray Images

Pneumonia Detection using Chest X-ray Images

Models Status

Model Overview

Pneumonia Detection
Pneumonia is an infection in one or both lungs, caused by bacteria, virus or fungi. It involves inflammation of alveoli and causes difficulty in breathing. With Pneumonia, air sacs will be filled with fluid or pus. The disease can be life threatening, particularly to infants and people above the age of 65.

The model can be used in the Healthcare industry, especially in cases of medical emergencies where attention cannot be provided to each patient. In those cases, the chest X-rays of the individual can directly be given as input to the model and steps can be decided based on the results. Such models are already in experimental phases.


Data Source
Data was taken from Chest X-rays Dataset from kaggle. The dataset comprises three folders train, test and val, and these folders are subdivided into PNEUMONIA and NORMAL folders. These folders contain chest X-ray images of patients from Guangzhou Women and Children’s Medical Center, Guangzhou, China.

Data Preprocessing
The images were scaled to size 128x128 and were converted to grayscale. The training set was imbalanced as the number of NORMAL samples were less compared to PNEUMONIA ones. So SMOTE was used for oversampling the minority samples. Further variations were added to the present images using ImageDataGenerator. The new images were zoomed in between 0.85 to 1.00, and rotation of 5 degrees was added.

import os
from sklearn.utils import shuffle
def give_set (path) :

X = []
y = []
for label in os.listdir(path) :
for img in os.listdir(path+label + '/') :

img_path = path + label + '/' + img
image = keras.preprocessing.image.load_img(img_path, color_mode = 'grayscale', target_size = (128,128))

y.append(1 if label == 'PNEUMONIA' else 0)

X = np.array(X, dtype = float)
y = np.array(y)

return shuffle(X,y)​

X_train, y_train = SMOTE(sampling_strategy = 'auto', k_neighbors = 5, random_state = 1).fit_resample(X_train, y_train)

train_gen = ImageDataGenerator(zoom_range = [0.85,1.0], width_shift_range = 0.02, rotation_range = 5)

Model has stacked CNN layers. There are three CNN layers with 128, 32, and 32 filters respectively. Each CNN block has a Conv2D layer with dropout and BatchNormalization, and MaxPooling layer.

Conv2D layer
- Kernels or filters are applied on this layer, which are convolved with portions of images to capture specific features (some capture edges, others capture contrast, etc). This also assists in reducing image size. Filters can be considered as weights for images (which they are sort of).

Dropout layer - Assists in regularizing the weights by randomly deleting some units, so that the other nodes don't get too accustomed to the inputs and able to take variations into account.

Batch Normalization - It happens that the cost function curve may thin out at some axis. So this normalizes the Inputs for faster training.

Max Pooling layer - Move a window along the image to capture the dominant features in the image. Here the window size is (2,2).

More information about convolutional layers can be found here.

def conv_layer (filterx) :

model = Sequential()

model.add(Conv2D(filterx, (3,3), activation = 'relu', padding = 'same', kernel_regularizer = 'l2'))
model.add(MaxPooling2D((2,2), padding = 'valid'))

return model

def dens_layer (hiddenx) :

model = Sequential()

model.add(Dense(hiddenx, activation = 'relu', kernel_regularizer = 'l2'))

return model

def cnn (filter1, filter2, filter3, hidden1) :

# create the cnn model
model = Sequential()
model.add(Input(shape = (128, 128, 1,)))

# add convolution layer

# flatten and hidden layer

# output layer
model.add(Dense(1, activation = 'sigmoid'))

# compile the model
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model


The model was trained for 125 epochs with a batch-size of 32. Adam optimizer was used with the default learning rate of 0.01.

Loss function wrt. Epoch

Performance on Test set

where 0 represents patients not suffering from pneumonia and 1 represents patients suffering from pneumonia.