In the ever-evolving landscape of medical technology, the integration of artificial intelligence (AI) has brought about remarkable advancements in disease detection and diagnosis. A prime example of this is the use of Convolutional Neural Networks (CNNs) to analyze medical images for potential signs of illness. In this blog post, we delve into the fascinating world of medical image analysis by exploring how a CNN can be employed to detect pneumonia from chest X-ray images.
Understanding Pneumonia: A Global Health Challenge
Pneumonia, a serious respiratory infection affecting the lungs, remains a major global health concern. Timely and accurate diagnosis is crucial for effective treatment, as delayed intervention can lead to severe complications. While traditional diagnostic methods have served their purpose, AI-driven solutions offer a new paradigm for enhancing accuracy and efficiency.
The Power of Convolutional Neural Networks
CNNs, a class of deep learning models, have proven to be exceptionally effective in image recognition tasks. Their ability to automatically learn and extract relevant features from images makes them ideal for detecting patterns indicative of medical conditions.
Step 1: Data Collection and Preparation
To build and train our pneumonia-detection CNN, we begin by obtaining a robust dataset of chest X-ray images. This dataset, sourced from Kaggle, is organized into three categories: training, validation, and test. Data augmentation is employed on the training set, a technique that artificially expands the dataset by applying transformations such as rotation and scaling. This augmentation aids the model in generalizing better to real-world scenarios.
|
!mkdir -p ~/.kaggle !cp kaggle.json ~/.kaggle/ !kaggle datasets download -d paultimothymooney/chest-xray-pneumonia import zipfile zip_ref = zipfile.ZipFile('/content/chest-xray-pneumonia.zip','r') zip_ref.extractall('/content') zip_ref.close() import tensorflow as tf import numpy as np from tensorflow import keras import os import cv2 from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.preprocessing import image import matplotlib.pyplot as plt train=ImageDataGenerator(rescale=1/255) test=ImageDataGenerator(rescale=1/255) train_dataset=train.flow_from_directory("/content/chest_xray/chest_xray/train", target_size=(150,150), batch_size=32, class_mode='binary') val_dataset=validate.flow_from_directory("/content/chest_xray/chest_xray/val", target_size=(150,150), batch_size=32, class_mode='binary') test_dataset=test.flow_from_directory("/content/chest_xray/chest_xray/test", target_size=(150,150), batch_size=32, class_mode='binary') test_dataset.class_indices val_dataset.class_indices train_dataset.class_indices |
Step 2: Constructing the CNN Architecture
Our CNN architecture is designed to progressively learn complex features from the input images. This is achieved through a sequence of convolutional and pooling layers. These layers act as feature detectors, learning to recognize patterns that distinguish between healthy and pneumonia-affected lungs. Additionally, fully connected (dense) layers at the end of the architecture help make the final prediction based on the learned features.
|
from keras.api._v2.keras import activations from tensorflow import keras model= keras.Sequential() model.add(keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3))) model.add(keras.layers.MaxPool2D(2,2)) model.add(keras.layers.Conv2D(64,(3,3),activation='relu')) model.add(keras.layers.MaxPool2D(2,2)) model.add(keras.layers.Conv2D(128,(3,3),activation='relu')) model.add(keras.layers.MaxPool2D(2,2)) model.add(keras.layers.Conv2D(128,(3,3),activation='relu')) model.add(keras.layers.MaxPool2D(2,2)) model.add(keras.layers.Flatten()) model.add(keras.layers.Dense(512,activation='relu')) model.add(keras.layers.Dense(1,activation='sigmoid')) model.summary() |
Step 3: Compilation, Training, and Validation
Before training, the model is compiled with an optimizer and a loss function appropriate for binary classification tasks. Training the model involves repeatedly presenting the training data and adjusting the model's internal weights to minimize the loss function. The validation dataset is utilized during training to monitor the model's performance and prevent overfitting, a phenomenon where the model memorizes the training data without generalizing well to new data.
|
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy']) r = model.fit(train_dataset,epochs=10,validation_data= val_dataset) |
Step 4: Evaluating Model Performance
Once the model is trained, it's evaluated on the test dataset to determine its real-world accuracy in detecting pneumonia. The model's ability to generalize to previously unseen data is a crucial measure of its effectiveness.
|
predictions = model.predict(test_dataset) predictions = np.round(predictions) score= model.evaluate(test_dataset) print('test aqccuracy :',score[1]*100) |
Step 5: Visualizing Training Progress
To gain insights into the training process, visualizations are created to showcase how the model's accuracy and loss evolve over the course of training epochs. These visualizations help us understand how well the model is learning and when it might start overfitting.
|
plt.plot(r.history['accuracy'], label='accuracy') plt.plot(r.history['val_accuracy'], label='val_accuracy') plt.plot(r.history['loss'], label='loss') plt.plot(r.history['val_loss'], label='Val_loss') plt.title('model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epochs') plt.legend() |
Step 6: Predicting Pneumonia from New Images
To make the model accessible and practical, we provide a function that takes a new chest X-ray image as input and predicts whether the image indicates pneumonia or not. This binary prediction showcases the model's diagnostic capability.
|
def predictImage(filename): img1 = image.load_img(filename,target_size=(150,150)) plt.imshow(img1) Y = image.img_to_array(img1) X = np.expand_dims(Y,axis=0) val=model.predict(X) print(val) if val == 1: plt.xlabel("PNEUMONIA",fontsize=30) elif val == 0: plt.xlabel("NORMAL",fontsize=30) predictImage('/content/chest_xray/chest_xray/test/PNEUMONIA/person101_bacteria_486.jpeg') #{'NORMAL': 0, 'PNEUMONIA': 1} model.save("/content/Chest X-Ray Images (Pneumonia).h5") |
Dataset :https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia
In Conclusion
The integration of Convolutional Neural Networks in medical image analysis presents a significant leap forward in diagnosing diseases like pneumonia. As technology continues to advance, we're likely to witness further enhancements in accuracy, speed, and accessibility of medical diagnostics, ultimately leading to improved patient outcomes.

0 Comments