Javatpoint Logo
Javatpoint Logo

Pneumonia Detection Using CNN in Python

Pneumonia is a potentially life-threatening infection of the lungs caused by bacteria, viruses, or fungi. Early detection and diagnosis of pneumonia are crucial

for effective treatment and better patient outcomes. In recent years, deep learning techniques such as Convolutional Neural Networks (CNN) have shown promising results in early diagnosing pneumonia from medical images.

This article will discuss how to build a pneumonia detection system using CNN in Python. We will begin by explaining the basics of CNN and its architecture, followed by the dataset used for training our model, and finally, we will go through the steps involved in building and training the model.

Convolutional Neural Networks

Convolutional Neural Networks (CNN) are deep neural networks commonly used in image recognition and computer vision tasks. CNNs are designed to learn spatial hierarchies of features automatically and adaptively from images. They consist of several layers, including convolutional, pooling, and fully connected layers.

The convolutional layers perform a mathematical operation called convolution on

the input image to extract feature maps. These feature maps represent specific patterns and structures in the picture. The pooling layers reduce the spatial dimensions of the feature maps by performing operations such as max-pooling or average pooling. Finally, the fully connected layers use the extracted features to classify the image into different categories.

Dataset

We will use the Chest X-Ray Images dataset from Kaggle to train and test our pneumonia detection model. This dataset consists of 5,863 chest X-ray images in JPEG format, categorized into standard and pneumonia. The pneumonia class includes both bacterial and viral pneumonia cases.

We will use 80% of the dataset for training and 20% for testing our model. Before

training, we will pre-process the images by resizing them to 224x224 pixels and

normalizing the pixel values between 0 and 1.

Building and Training the Model

We will use Keras, a popular deep-learning framework, to build and train our CNN model. We will start by importing the required libraries and defining our model architecture.

Our model consists of four convolutional layers, each followed by a max-pooling layer to reduce the spatial dimensions of the feature maps. We then flatten the output and pass it through two fully connected layers. The final layer has a sigmoid activation function to classify the input image as usual or pneumonia.

We will compile our model using the binary cross-entropy loss function and the Adam optimizer. We will also monitor the accuracy metric during training.

Next, as discussed earlier, we will load the dataset and preprocess the images.

We use the `flow_from_directory()` function to load the training and testing images from their respective directories. The process also performs data augmentation on the training images, such as random rotations and flips, to improve the model's generalization ability.

Finally, we train our model using the `fit()` function and save the trained model to a file for future use.

We train our model for 10 epochs and evaluate its performance on the validation set after each period. We save the trained model to a file named pneumonia_detection_model.h5.

Evaluating the Model

After training our model, we can evaluate its performance on the testing dataset using the evaluate() function.

We get an accuracy of around 85% on the testing dataset. We can also visualize the training and validation accuracy and loss during training using the matplotlib library.

The above code plots the training and validation accuracy and loss over the number of epochs. We can see that our model achieves high accuracy on both the training and validation sets.

Now, let's define our CNN model. We will define a sequential model with four convolutional layers followed by max-pooling layers. We will also use batch normalization to normalize the activations of the previous layer at each batch.

We will use the binary cross-entropy loss as our loss function since we have a binary classification problem (pneumonia or not). We will also use the Adam optimizer with a learning rate of 0.0001.

Next, we will train our model using the fit generator function. We will use an initial learning rate of 0.0001 and decrease it by 0.1 after every five epochs to help the model converge faster. We will also use early stopping to prevent overfitting.

After training our model, we can evaluate it on our test set and print the classification report and confusion matrix to see how well it performed.

Define CNN Model Architecture

In this step, we define the architecture of our CNN model. We start by initializing the model using the Sequential() function from the Keras library. Then we add convolutional layers to the model using the Conv2D() process, followed by max-pooling layers using the MaxPooling2D() function. Finally, we flatten the output of the convolutional layers using the Flatten() part and add fully connected layers using the Dense() function.

We start by initializing the CNN model using the Sequential() function. Then we add three convolutional layers, each followed by a max pooling layer. The first convolutional layer has 32 filters, while the second and third convolutional layers have 64 and 128 filters, respectively. All the convolutional layers have a kernel size of (3,3) and use the ReLU activation function.

After the convolutional layers, we flatten the output using the Flatten() function and add two fully connected layers. The first fully connected layer has 128 units and uses the ReLU activation function, while the second layer has one team and uses the sigmoid activation function since this is a binary classification problem.

Finally, we compile the model using the Adam optimizer, binary cross-entropy loss function, and accuracy as the evaluation metric.

Data augmentation is a technique used to generate new training samples by applying random transformations to the existing models. This is useful when we have a small dataset, as it helps to increase the size of the dataset and reduce overfitting.

Conclusion

This tutorial discussed how to build a pneumonia detection system using CNN in Python. We used the Chest X-Ray Images dataset to train and test our model and achieved an accuracy of around 85% on the testing dataset. CNNs have shown promising results in medical image analysis and can be used in various healthcare applications such as disease diagnosis and drug discovery.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA