Open In App

Pneumonia Detection Using CNN in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to build a classifier using a simple Convolution Neural Network which can classify the images of patient’s xray to detect whether the patient is Normal or affected by Pneumonia.

To get more understanding, follow the steps accordingly.

Importing Libraries

The libraries we will using are : 

  • Pandas- The pandas library is a popular open-source data manipulation and analysis tool in Python. It provides a data structure called a DataFrame, which is similar to a spreadsheet or a SQL table, and allows for easy manipulation and analysis of data.
  • Numpy- NumPy is a popular open-source library in Python for scientific computing, specifically for working with numerical data. It provides tools for working with large, multi-dimensional arrays and matrices, and offers a wide range of mathematical functions for performing operations on these arrays.
  • Matplotlib- It is a popular open-source data visualization library in Python. It provides a range of tools for creating high-quality visualizations of data, including line plots, scatter plots, bar plots, histograms, and more.
  • TensorFlow- TensorFlow is a popular open-source library in Python for building and training machine learning models. It was developed by Google and is widely used in both academia and industry for a variety of applications, including image and speech recognition, natural language processing, and recommendation systems.

Python3




import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
import numpy as np
   
import warnings
warnings.filterwarnings('ignore')
   
from tensorflow import keras
from keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.utils import image_dataset_from_directory
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.preprocessing import image_dataset_from_directory
   
import os
import matplotlib.image as mpimg


Importing Dataset 

To run the notebook in the local system. The dataset can be downloaded from [ https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia ]. The dataset is in the format of a zip file. So to import and then unzip it, by running the below code. 

Python3




import zipfile
zip_ref = zipfile.ZipFile('/content/chest-xray-pneumonia.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()


Read the image dataset

In this section, we will try to understand visualize some images which have been provided to us to build the classifier for each class.

Let’s load the training image

Python3




# For local system
path = '/content/chest_xray/chest_xray/train'
# For kaggle
path = '/kaggle/input/chest-xray-pneumonia/chest_xray/train'
classes = os.listdir(path)
print(classes)


Output:

['PNEUMONIA', 'NORMAL']

This shows that, there are two classes that we have here i.e. Normal and Pneumonia.

Python3




# Define the directories for the X-ray images
PNEUMONIA_dir = os.path.join(path + '/' + classes[0])
NORMAL_dir = os.path.join(path + '/' + classes[1])
 
# Create lists of the file names in each directory
pneumonia_names = os.listdir(PNEUMONIA_dir)
normal_names = os.listdir(NORMAL_dir)
 
print('There are ', len(pneumonia_names),
      'images of pneumonia infected in training dataset')
print('There are ', len(normal_names), 'normal images in training dataset')


Output:

There are  3875 images of pneumonia infected in training dataset
There are  1341 normal images in training dataset

Plot the Pneumonia infected Chest X-ray images

Python3




# Set the figure size
fig = plt.gcf()
fig.set_size_inches(16, 8)
 
# Select the starting index for the images to display
pic_index = 210
 
# Create lists of the file paths for the 16 images to display
pneumonia_images = [os.path.join(PNEUMONIA_dir, fname)
                    for fname in pneumonia_names[pic_index-8:pic_index]]
# Loop through the image paths and display each image in a subplot
for i, img_path in enumerate(pneumonia_images):
    sp = plt.subplot(2, 4, i+1)
    sp.axis('Off')
 
    # Read in the image using Matplotlib's imread() function
    img = mpimg.imread(img_path)
    plt.imshow(img)
 
# Display the plot with the 16 images in a 4x4
plt.show()


Output:

Pneumonia infected Chest X-ray images - Geeksforgeeks

Pneumonia infected Chest X-ray images

Plot the Normal Chest X-ray images

Python3




# Set the figure size
fig = plt.gcf()
fig.set_size_inches(16, 8)
 
# Select the starting index for the images to display
pic_index = 210
 
# Create lists of the file paths for the 16 images to display
normal_images = [os.path.join(NORMAL_dir, fname)
              for fname in normal_names[pic_index-8:pic_index]]
# Loop through the image paths and display each image in a subplot
for i, img_path in enumerate(normal_images):
    sp = plt.subplot(2, 4, i+1)
    sp.axis('Off')
 
    # Read in the image using Matplotlib's imread() function
    img = mpimg.imread(img_path)
    plt.imshow(img)
 
# Display the plot with the 16 images in a 4x4 grid
plt.show()


Output:

Normal Chest X-ray images - Geeksforgeeks

Normal Chest X-ray images

Data Preparation for Training

In this section, we will classify the dataset into train,test and validation format.

Python3




Train = keras.utils.image_dataset_from_directory(
    directory='/content/chest_xray/chest_xray/train',
    labels="inferred",
    label_mode="categorical",
    batch_size=32,
    image_size=(256, 256))
Test = keras.utils.image_dataset_from_directory(
    directory='/content/chest_xray/chest_xray/test',
    labels="inferred",
    label_mode="categorical",
    batch_size=32,
    image_size=(256, 256))
Validation = keras.utils.image_dataset_from_directory(
    directory='/content/chest_xray/chest_xray/val',
    labels="inferred",
    label_mode="categorical",
    batch_size=32,
    image_size=(256, 256))


Output:

Found 5216 files belonging to 2 classes.
Found 624 files belonging to 2 classes.
Found 16 files belonging to 2 classes.

Model Architecture

The model architecture can be described as follows:

  • Input layer: Conv2D layer with 32 filters, 3×3 kernel size, ‘relu’ activation function, and input shape of (256, 256, 3)
  • MaxPooling2D layer with 2×2 pool size
  • Conv2D layer with 64 filters, 3×3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2×2 pool size
  • Conv2D layer with 64 filters, 3×3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2×2 pool size
  • Conv2D layer with 64 filters, 3×3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2×2 pool size
  • Flatten layer
  • Dense layer with 512 neurons, ‘relu’ activation function
  • BatchNormalization layer
  • Dense layer with 512 neurons, ‘relu’ activation function
  • Dropout layer with a rate of 0.1
  • BatchNormalization layer
  • Dense layer with 512 neurons, ‘relu’ activation function
  • Dropout layer with a rate of 0.2
  • BatchNormalization layer
  • Dense layer with 512 neurons, ‘relu’ activation function
  • Dropout layer with a rate of 0.2
  • BatchNormalization layer
  • Output layer: Dense layer with 2 neurons and ‘sigmoid’ activation function, representing the probabilities of the two classes (pneumonia or normal)

In summary our model has : 

  • Four Convolutional Layers followed by MaxPooling Layers.
  • Then One Flatten layer to receive and flatten the output of the convolutional layer.
  • Then we will have three fully connected layers followed by the output of the flattened layer.
  • We have included some BatchNormalization layers to enable stable and fast training and a Dropout layer before the final layer to avoid any possibility of overfitting.
  • The final layer is the output layer which has the activation function sigmoid to classify the results into two classes(i,e Normal or Pneumonia).

Python3




model = tf.keras.models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)),
    layers.MaxPooling2D(2, 2),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D(2, 2),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D(2, 2),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D(2, 2),
 
    layers.Flatten(),
    layers.Dense(512, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu'),
    layers.Dropout(0.1),
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu'),
    layers.Dropout(0.2),
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu'),
    layers.Dropout(0.2),
    layers.BatchNormalization(),
    layers.Dense(2, activation='sigmoid')
])


Print the summary of the model architecture:

Python3




model.summary()


Output:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 254, 254, 32)      896       
                                                                 
 max_pooling2d (MaxPooling2D  (None, 127, 127, 32)     0         
 )                                                               
                                                                 
 conv2d_1 (Conv2D)           (None, 125, 125, 64)      18496     
                                                                 
 max_pooling2d_1 (MaxPooling  (None, 62, 62, 64)       0         
 2D)                                                             
                                                                 
 conv2d_2 (Conv2D)           (None, 60, 60, 64)        36928     
                                                                 
 max_pooling2d_2 (MaxPooling  (None, 30, 30, 64)       0         
 2D)                                                             
                                                                 
 conv2d_3 (Conv2D)           (None, 28, 28, 64)        36928     
                                                                 
 max_pooling2d_3 (MaxPooling  (None, 14, 14, 64)       0         
 2D)                                                             
                                                                 
 flatten (Flatten)           (None, 12544)             0         
                                                                 
 dense (Dense)               (None, 512)               6423040   
                                                                 
 batch_normalization (BatchN  (None, 512)              2048      
 ormalization)                                                   
                                                                 
 dense_1 (Dense)             (None, 512)               262656    
                                                                 
 dropout (Dropout)           (None, 512)               0         
                                                                 
 batch_normalization_1 (Batc  (None, 512)              2048      
 hNormalization)                                                 
                                                                 
 dense_2 (Dense)             (None, 512)               262656    
                                                                 
 dropout_1 (Dropout)         (None, 512)               0         
                                                                 
 batch_normalization_2 (Batc  (None, 512)              2048      
 hNormalization)                                                 
                                                                 
 dense_3 (Dense)             (None, 512)               262656    
                                                                 
 dropout_2 (Dropout)         (None, 512)               0         
                                                                 
 batch_normalization_3 (Batc  (None, 512)              2048      
 hNormalization)                                                 
                                                                 
 dense_4 (Dense)             (None, 2)                 1026      
                                                                 
=================================================================
Total params: 7,313,474
Trainable params: 7,309,378
Non-trainable params: 4,096
_________________________________________________________________

The input image we have taken initially resized into 256 X 256. And later it transformed into the binary classification value. 

Plot the model architecture:

Python3




# Plot the keras model
keras.utils.plot_model(
    model,
    # show the shapes of the input/output tensors of each layer
    show_shapes=True,
    # show the data types of the input/output tensors of each layer
    show_dtype=True,
    # show the activations of each layer in the output graph
    show_layer_activations=True
)


Output:

Model - Geeksforgeeks

Model

Compile the Model:

Python3




model.compile(
    # specify the loss function to use during training
    loss='binary_crossentropy',
    # specify the optimizer algorithm to use during training
    optimizer='adam',
    # specify the evaluation metrics to use during training
    metrics=['accuracy']
)


Train the model

Now we can train our model,  here we define epochs = 10, but you can perform hyperparameter tuning for better results.

Python3




history = model.fit(Train,
          epochs=10,
          validation_data=Validation)


Output:

Epoch 1/10
163/163 [==============================] - 59s 259ms/step - loss: 0.2657 - accuracy: 0.9128 - val_loss: 2.1434 - val_accuracy: 0.5625
Epoch 2/10
163/163 [==============================] - 34s 201ms/step - loss: 0.1493 - accuracy: 0.9505 - val_loss: 3.0297 - val_accuracy: 0.6250
Epoch 3/10
163/163 [==============================] - 34s 198ms/step - loss: 0.1107 - accuracy: 0.9626 - val_loss: 0.5933 - val_accuracy: 0.7500
Epoch 4/10
163/163 [==============================] - 33s 197ms/step - loss: 0.0992 - accuracy: 0.9640 - val_loss: 0.3691 - val_accuracy: 0.8125
Epoch 5/10
163/163 [==============================] - 34s 202ms/step - loss: 0.0968 - accuracy: 0.9651 - val_loss: 3.5919 - val_accuracy: 0.5000
Epoch 6/10
163/163 [==============================] - 34s 199ms/step - loss: 0.1012 - accuracy: 0.9653 - val_loss: 3.8678 - val_accuracy: 0.5000
Epoch 7/10
163/163 [==============================] - 34s 198ms/step - loss: 0.1026 - accuracy: 0.9613 - val_loss: 3.2006 - val_accuracy: 0.5625
Epoch 8/10
163/163 [==============================] - 35s 204ms/step - loss: 0.0785 - accuracy: 0.9701 - val_loss: 1.7824 - val_accuracy: 0.5000
Epoch 9/10
163/163 [==============================] - 34s 198ms/step - loss: 0.0717 - accuracy: 0.9745 - val_loss: 3.3485 - val_accuracy: 0.5625
Epoch 10/10
163/163 [==============================] - 35s 200ms/step - loss: 0.0699 - accuracy: 0.9770 - val_loss: 0.5788 - val_accuracy: 0.6250

Model Evaluation

Let’s visualize the training and validation accuracy with each epoch.

Python3




history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot()
history_df.loc[:, ['accuracy', 'val_accuracy']].plot()
plt.show()


Output:

Losses per iterations -Geeksforgeeks

Losses per iterations

Accuracy per iterations -Geeksforgeeks

Accuracy per iterations

Our model is performing good on training dataset, but not on test dataset. So, this is the case of overfitting. This may be due to imbalanced dataset.

Find the accuracy on Test Datasets

Python3




loss, accuracy = model.evaluate(Test)
print('The accuracy of the model on test dataset is',
      np.round(accuracy*100))


Output:

20/20 [==============================] - 4s 130ms/step - loss: 0.4542 - accuracy: 0.8237
The accuracy of the model on test dataset is 82.0

Prediction

Let’s check the model for random images.

Python3




# Load the image from the directory
# "/content/chest_xray/chest_xray/test/NORMAL/IM-0010-0001.jpeg"
# with the target size of (256, 256)
test_image = tf.keras.utils.load_img(
    "/content/chest_xray/chest_xray/test/NORMAL/IM-0010-0001.jpeg",
    target_size=(256, 256))
 
# Display the loaded image
plt.imshow(test_image)
 
# Convert the loaded image into a NumPy array and
# expand its dimensions to match the expected input shape of the model
test_image = tf.keras.utils.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
 
# Use the trained model to make a prediction on the input image
result = model.predict(test_image)
 
# Extract the probability of the input image belonging
# to each class from the prediction result
class_probabilities = result[0]
 
# Determine the class with the highest probability and print its label
if class_probabilities[0] > class_probabilities[1]:
    print("Normal")
else:
    print("Pneumonia")


Output:

1/1 [==============================] - 0s 328ms/step
Pneumonia

Normal Chest X-ray

Python3




# Load the image from the directory
# "/content/chest_xray/chest_xray/test/N
# ORMAL/IM-0010-0001.jpeg" with the target size of (256, 256)
test_image = tf.keras.utils.load_img(
    "/content/chest_xray/chest_xray/test/PNEUMONIA/person100_bacteria_478.jpeg",
     target_size=(256, 256))
 
# Display the loaded image
plt.imshow(test_image)
 
# Convert the loaded image into a NumPy array
# and expand its dimensions to match the
# expected input shape of the model
test_image = tf.keras.utils.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
 
# Use the trained model to make a prediction on the input image
result = model.predict(test_image)
 
# Extract the probability of the input
# image belonging to each class from
# the prediction result
class_probabilities = result[0]
 
# Determine the class with the highest
# probability and print its label
if class_probabilities[0] > class_probabilities[1]:
    print("Normal")
else:
    print("Pneumonia")


Output:

1/1 [==============================] - 0s 328ms/step
Pneumonia
Pneumonia Infected Chest - Geeksforgeeks

Pneumonia Infected Chest

Conclusions:

Our model is performing well but as per losses and accuracy curve per iterations. It is overfitting. This may be due to the unbalanced dataset. By balancing the dataset with an equal number of normal and pneumonia images. We can get a better result.



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads