Coders Packet

Pneumonia Detection using Python

By Akhilesh Ketkar

Here we have created a CNN model using Keras to detect Pneumonia using Chest X-ray with 90% accuracy.

This application can detect Pneumonia using Keras.

To build this application at first we need to create neural network model that can predict Pneumonia.

To create neural network model:
Requirments:
                  Python 3.5–3.8
                  pip 19.0 or later (requires manylinux2010 support)
                  Ubuntu 16.04 or later (64-bit)
                  macOS 10.12.6 (Sierra) or later (64-bit) (no GPU support)
                  Windows 7 or later (64-bit)
                  Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019
                  GPU support requires a CUDA®-enabled card
PROCEDURE:
                  Install tensorflow using "pip install tensorflow".
                  Install Keras using "pip install keras".
                  We can get training images and testing images from kaggle pneumonia dataset.
                  We need to create neural network architecture (or We can implement predefined architecture ).
                  After creating neural network next step is to train the model using mnist dataset.
                  Store the weights in '.h5' format.

First we need to define our Metric.

METRICS = [
        tf.keras.metrics.BinaryAccuracy(),
        tf.keras.metrics.Precision(name="precision"),
        tf.keras.metrics.Recall(name="recall"),
    ]

Then we'll create CNN architecture.

model = Sequential()
model.add(Conv2D(16,(3,3),padding='Same',activation='relu',input_shape = (128,128,3)))
model.add(Conv2D(16,(3,3),activation='relu',padding='Same'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(BatchNormalization())
model.add(Dropout(0.3))

model.add(Conv2D(32,(3,3),activation='relu',padding='Same'))
model.add(Conv2D(32,(3,3),activation='relu',padding='Same'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(BatchNormalization())
model.add(Dropout(0.3))

model.add(Conv2D(64,(3,3),activation='relu',padding='Same'))
model.add(Conv2D(64,(3,3),activation='relu',padding='Same'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Conv2D(64,(3,3),activation='relu',padding='Same'))
model.add(Conv2D(64,(3,3),activation='relu',padding='Same'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.7))

model.add(Dense(128,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(64,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))

model.add(Dense(1 , activation = 'sigmoid'))
adam = Adam(learning_rate=lr_schedule,beta_1=0.9,beta_2=0.999,epsilon=1e-07)
model.compile(optimizer = adam,loss = 'binary_crossentropy' , metrics = METRICS)

 

Download Complete Code

Comments

No comments yet