Pneumonia Detection Utilizing CNN in Python

In this post, we will discover how to construct a classifier utilizing a basic Convolution Neural Network which can categorize the images of client’s xray to spot wheather the client is Regular or impacted by Pneumonia.

To get more understanding, follow the actions appropriately.

Importing Libraries

The libraries we will utilizing are:

  • Pandas- The pandas library is a popular open-source information control and analysis tool in Python. It offers an information structure called a DataFrame, which resembles a spreadsheet or a SQL table, and permits simple control and analysis of information.
  • Numpy- NumPy is a popular open-source library in Python for clinical computing, particularly for dealing with mathematical information. It offers tools for dealing with big, multi-dimensional varieties and matrices, and provides a wide variety of mathematical functions for carrying out operations on these varieties.
  • Matplotlib- It is a popular open-source information visualization library in Python. It offers a variety of tools for developing top quality visualizations of information, consisting of line plots, scatter plots, bar plots, pie charts, and more.
  • TensorFlow- TensorFlow is a popular open-source library in Python for structure and training maker discovering designs. It was established by Google and is commonly utilized in both academic community and market for a range of applications, consisting of image and speech acknowledgment, natural language processing, and suggestion systems.

Python3

import matplotlib.pyplot as plt

import tensorflow as tf

import pandas as pd

import numpy as np

import cautions

warnings.filterwarnings(' overlook')

from tensorflow import keras

from keras import layers

from tensorflow.keras.models import Consecutive

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 note pad in the regional system. The dataset can be downloaded from[ https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia ] The dataset remains in the format of a zip file. So to import and after that unzip it, by running the below code.

Python3

import zipfile

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

zip_ref. extractall('/ material')

zip_ref. close()

Check out the image dataset

In this area, we will attempt to comprehend imagine some images which have actually been offered to us to construct the classifier for each class.

Let’s load the training image

Python3

course = '/ content/chest _ xray/chest _ xray/train'

course = '/ kaggle/input/chest-xray-pneumonia/ chest_xray/ train'

classes = os.listdir( course)

print( classes)

Output:

['PNEUMONIA', 'NORMAL']

This reveals that, there are 2 classes that we have here i.e. Regular and Pneumonia.

Python3

PNEUMONIA_dir = os.path.join( course + '/' + classes[0])

NORMAL_dir = os.path.join( course + '/' + classes[1])

pneumonia_names = os.listdir( PNEUMONIA_dir)

normal_names = os.listdir( NORMAL_dir)

print(' There are ', len( pneumonia_names),

' pictures of pneumonia imfected in training dataset')

print(' There are ', len( normal_names), ' typical images in training dataset')

Output:

 There are 3875 pictures of pneumonia imfected in training dataset

. There are 1341 typical images in training dataset

Plot the Pneumonia contaminated Chest X-ray images

Python3

fig = plt.gcf()

fig.set _ size_inches( 16, 8)

pic_index = 210

pneumonia_images = [os.path.join(PNEUMONIA_dir, fname)

                    for fname in pneumonia_names[pic_index-8:pic_index]]

for i, img_path in mention( pneumonia_images):

sp = plt.subplot( 2, 4, i+ 1)

sp.axis(' Off')

img = mpimg.imread( img_path)

plt.imshow( img)

plt.show()

Output:

Pneumonia infected Chest X-ray images - Geeksforgeeks

Pneumonia contaminated Chest X-ray images

Plot the Regular Chest X-ray images

Python3

fig = plt.gcf()

fig.set _ size_inches( 16, 8)

pic_index = 210

normal_images = [os.path.join(NORMAL_dir, fname)

              for fname in normal_names[pic_index-8:pic_index]]

for i, img_path in mention( normal_images):

sp = plt.subplot( 2, 4, i+ 1)

sp.axis(' Off')

img = mpimg.imread( img_path)

plt.imshow( img)

plt.show()

Output:

Normal Chest X-ray images - Geeksforgeeks

Regular Chest X-ray images

Information Preparation for Training

In this area, we will categorize the dataset into train, test and recognition format.

Python3

Train = keras.utils.image _ dataset_from_directory(

directory site ='/ content/chest _ xray/chest _ xray/train',

labels =" presumed",

label_mode =" categorical",

batch_size = 32,

image_size =( 256, 256))

Test = keras.utils.image _ dataset_from_directory(

directory site ='/ content/chest _ xray/chest _ xray/test',

labels =" presumed",

label_mode =" categorical",

batch_size = 32,

image_size =( 256, 256))

Recognition = keras.utils.image _ dataset_from_directory(

directory site ='/ content/chest _ xray/chest _ xray/val',

labels =" presumed",

label_mode =" categorical",

batch_size = 32,

image_size =( 256, 256))

Output:

 Found 5216 files coming from 2 classes.

. Found 624 files coming from 2 classes.

. Discovered 16 files coming from 2 classes.

Design Architecture

The design architecture can be referred to 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 swimming pool size
  • Conv2D layer with 64 filters, 3 × 3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2 × 2 swimming pool size
  • Conv2D layer with 64 filters, 3 × 3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2 × 2 swimming pool size
  • Conv2D layer with 64 filters, 3 × 3 kernel size, ‘relu’ activation function
  • MaxPooling2D layer with 2 × 2 swimming pool size
  • Flatten layer
  • Thick layer with 512 nerve cells, ‘relu’ activation function
  • BatchNormalization layer
  • Thick layer with 512 nerve cells, ‘relu’ activation function
  • Dropout layer with a rate of 0.1
  • BatchNormalization layer
  • Thick layer with 512 nerve cells, ‘relu’ activation function
  • Dropout layer with a rate of 0.2
  • BatchNormalization layer
  • Thick layer with 512 nerve cells, ‘relu’ activation function
  • Dropout layer with a rate of 0.2
  • BatchNormalization layer
  • Output layer: Thick layer with 2 nerve cells and ‘sigmoid’ activation function, representing the possibilities of the 2 classes (pneumonia or typical)

In summary our design has:

  • 4 Convolutional Layers followed by MaxPooling Layers.
  • Then One Flatten layer to get and flatten the output of the convolutional layer.
  • Then we will have 3 completely linked layers followed by the output of the flattened layer.
  • We have actually consisted of some BatchNormalization layers to allow steady and quick training and a Dropout layer prior to the last layer to prevent any possibility of overfitting.
  • The last layer is the output layer which has the activation function sigmoid to categorize the outcomes into 2 classes( i, e Regular or Pneumonia).

Python3

design = 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 design architecture:

Output:

 Design: "consecutive"

. _________________________________________________________________ 
. Layer (type) Output Forming 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 
.


. thick (Thick) (None 
,
512) 
6423040 
.

. batch_normalization (BatchN( None, 512) 2048 
.
ormalization) 
. 
.

dense_1(
Thick)( None, 512) 262656 
. 
. dropout( Dropout)(
None, 512) 
0


. 
. batch_normalization_1 (Batc( None, 512) 2048 
.
hNormalization) 
. 
.

dense_2(
Thick)( None, 512) 262656 
. 
. dropout_1( Dropout)( 
None, 512) 
0


. 
. batch_normalization_2 (Batc( None, 512) 2048 
.
hNormalization) 
. 
.

dense_3(
Thick)( None, 512) 262656 
. 
. dropout_2( Dropout) (None 
,
512) 
0


. 
. batch_normalization_3 (Batc( None, 512) 2048 
.

hNormalization)

. 
. dense_4( Thick)( None, 2) 1026 
. 
.===============================
=
===== ======= =========
=
========= == 
.
Overall params: 7,313,474 
.
Trainable params: 7,309,378 
. Non-trainable params: 4,096 
. _________________________________________________________________

The input image we have actually taken at first resized into 256 X 256. And later on it changed into the binary category worth.

Plot the design architecture:

Python3

keras.utils.plot _ design (

design,

show_shapes = Real,

show_dtype = Real,

show_layer_activations = Real

)

Output:

Model - Geeksforgeeks

Design

Assemble the Design:

Python3

design. put together(

loss =' binary_crossentropy',

optimizer =' adam',

metrics =['accuracy']

)

Train the design

Now we can train our design, here we specify dates = 10, however you can carry out hyperparameter tuning for much better outcomes.

Python3

history = model.fit( Train,

dates = 10,

validation_data = Recognition)

Output:

 Date 1/10

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

Design Assessment

Let’s imagine the training and recognition precision with each date.

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 models

Accuracy per iterations -Geeksforgeeks

Precision per models

Our design is carrying out great on training dataset, however not on test dataset. So, this holds true of overfitting. This might be because of imbalanced dataset.

Discover the precision on Test Datasets

Python3

loss, precision = model.evaluate( Test)

print(' The precision of the design on test dataset is',

np. round( precision * 100))

Output:

 20/20 [==============================] - fours 130ms/step - loss: 0.4542 - precision: 0.8237

. The precision of the design on test dataset is 82.0

Forecast

Let’s examine the design for random images.

Python3

test_image = tf.keras.utils.load _ img(

"/ content/chest _ xray/chest _ xray/test/NORMAL/ IM-0010-0001. jpeg",

target_size =( 256, 256))

plt.imshow( test_image)

test_image = tf.keras.utils.img _ to_array( test_image)

test_image = np.expand _ dims( test_image, axis = 0)

result = model.predict( test_image)

class_probabilities = result[0]

if class_probabilities[0] > > class_probabilities[1]:

print(" Regular")

else:

print(" Pneumonia")

Output:

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

. Pneumonia

Regular Chest X-ray

Python3

test_image = tf.keras.utils.load _ img(

"/ content/chest _ xray/chest _ xray/test/PNEUMONIA/ person100_bacteria_478. jpeg",

target_size =( 256, 256))

plt.imshow( test_image)

test_image = tf.keras.utils.img _ to_array( test_image)

test_image = np.expand _ dims( test_image, axis = 0)

result = model.predict( test_image)

class_probabilities = result[0]

if class_probabilities[0] > > class_probabilities[1]:

print(" Regular")

else:

print(" Pneumonia")

Output:

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

.
Pneumonia
Pneumonia Infected Chest - Geeksforgeeks

Pneumonia Contaminated Chest

Conclusions:

Our design is carrying out well however according to losses and precision curve per models. It is overfitting. This might be because of the out of balance dataset. By stabilizing the dataset with an equivalent variety of typical and pneumonia images. We can get a much better outcome.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: