Digital Signal Processing Project on
LUNG CT IMAGE SEGMENTATION
Submitted in partial fulfillment of the requirement for the award of degree
BACHELOR OF TECHNOLOGY
In
ELECTRONICS AND COMMUNICATION ENGINEERING
By
POTLACHERUVU NIKITHA (21071A0445)
PRANITHA MUSUNURU (21071A0446)
RAMOJU VEERA BHADRA SUBRAHMANYA RAGHAVAMSI (21071A0447)
SAI PRANEETH VASA (21071A0448)
DEPARTMENT OF ELECTRONICS AND COMMUNICATION
ENGINEERING
VNR VIGNANA JYOTHI INSTITUTE OF ENGINEERING
AND TECHNOLOGY
(An Autonomous Institute, Affiliated to JNTUH,Accredited with NAAC ‘A++’ grade,
Approved by AICTE, New Delhi) Bachupally, Nizampet SO, Hyderabad, Telangana -
500090.
VNR VIGNANA JYOTHI INSTITUTE OF
ENGINEERING AND TECHNOLOGY
(An Autonomous Institute, Affiliated to JNTUH, Accredited
with NAAC ‘A++’ grade, Approved by AICTE, New Delhi)
Bachupally, Nizampet SO, Hyderabad, Telangana - 500090.
DEPARTMENT OF ELECTRONICS AND COMMUNICATION
ENGINEERING
BONAFIDE CERTIFICATE
This is to certify that the Project titled “LUNG CT IMAGE SEGMENTATION” is a
bonafide record of the work done by POTLACHERUVU
NIKITHA(21071A0445) ,PRANITHA MUSUNURU (21071A0446), RAMOJU VEERA
BHADRA SUBRAHMANYA RAGHAVAMSI (21071A0447), SAI PRANEETH VASA
(21071A0448) in partial fulfillment of the requirements for the award of Bachelor of
Technology in ELECTRONICS AND COMMUNICATION ENGINEERING of the
VNR VIGNANA JYOTHI INSTITUTE OF ENGINEERING AND
TECHNOLOGY,HYDERABAD during the academic year of 2023-2024.
Lab Mentor
(Name in capitals with signature) (Name in Capitals with Signature)
Head of the Department
VNR VIGNANA JYOTHI INSTITUTE OF
ENGINEERING AND TECHNOLOGY
(An Autonomous Institute, Affiliated to JNTUH, Accredited with NAAC
‘A++’ grade, Approved by AICTE, New Delhi)
Bachupally, Nizampet SO, Hyderabad, Telangana - 500090.
DECLARATION
We POTLACHERUVU NIKITHA(21071A0445),PRANITHA MUSUNURU
(21071A0446), RAMOJU VEERA BHADRA SUBRAHMANYA RAGHAVAMSI
(21071A0447), SAI PRANEETH VASA (21071A0448) hereby declare that the Course
Project Report entitled “LUNG CT IMAGE SEGMENTATION”, done by us under the
guidance of Dr. /Prof. /Mr. /Ms. Sireesha Mam is submitted in partial fulfillment of the
requirements for the award of Bachelor of Technology degree in Electronics and
Communication Engineering.
1.
2.
3.
4.
5.
DATE : SIGNATURE OF THE CANDIDATES
PLACE :
ABSTRACT
This research presents a novel approach for the automated
segmentation of lung images to facilitate the early detection of viral
pneumonia. Leveraging the power of deep learning, specifically
employing TensorFlow, we developed a robust model capable of
accurately delineating lung regions affected by viral pneumonia from
medical imaging data. The proposed model combines convolutional
neural networks (CNNs) and advanced segmentation techniques to
achieve precise identification of pathological regions within the lung
images.
The dataset utilized in this study comprises a diverse collection of
annotated lung images obtained from patients diagnosed with viral
pneumonia. The TensorFlow-based model is trained on this dataset to
learn intricate patterns and features indicative of pneumonia-infected
lung regions. The training process involves optimizing a tailored loss
function, ensuring the model's ability to generalize across various
pneumonia manifestations and imaging conditions.
Furthermore, we explore the model's interpretability by conducting
extensive validation on an independent dataset, assessing its
performance in comparison to ground truth annotations. The results
demonstrate the model's effectiveness in accurately segmenting viral
pneumonia-affected areas, exhibiting promising sensitivity and
specificity. Additionally, we discuss the implications of our findings
for potential integration into clinical workflows, highlighting the
model's potential to assist radiologists in diagnosing viral pneumonia
in a timely and accurate manner.
In conclusion, our research contributes to the ongoing efforts in
leveraging deep learning for medical image analysis, specifically
focusing on the segmentation of lung images for viral pneumonia
detection. The TensorFlow-based model showcases notable
performance, laying the groundwork for future advancements in
automated diagnostic tools for respiratory infections.
Image Segmentation:
APPROACH:
**Coding Approach for UNet Segmentation with Over 100,000
Parameters Using TensorFlow:**
In TensorFlow, begin by importing necessary libraries, including
TensorFlow itself, and create a custom UNet model with a carefully
designed architecture. This architecture should include convolutional
layers with varying kernel sizes, max-pooling layers for
downsampling, and dense layers for semantic interpretation. Utilize
the Sequential API or Functional API in TensorFlow to define the
model's structure, incorporating skip connections to enhance feature
fusion.
Next, compile the model using an appropriate optimizer (e.g., Adam)
and a custom loss function tailored for segmentation tasks. Implement
data preprocessing using TensorFlow data augmentation techniques,
ensuring the input data is prepared for training. Train the UNet model
on the preprocessed dataset, monitoring training and validation
metrics. Fine-tune hyperparameters and architecture as needed based
on performance evaluation results.
Finally, save the trained model for future use and deploy it for
inference in a clinical setting. Implement post-processing steps, such
as morphological operations, for refining segmentation masks if
necessary. Provide clear documentation for model integration,
enabling seamless utilization by healthcare professionals for viral
pneumonia segmentation in lung images.
DATA & TOOLS / LIBRARIES USED:
Viral Pneumonia Dataset: For this U-Net segmentation project
focused on detecting viral pneumonia in lung images, a diverse and
comprehensive dataset was employed. The dataset consists of high-
resolution lung images obtained from various medical imaging
sources, capturing a range of pneumonia manifestations. Each image
is accompanied by corresponding ground truth masks, meticulously
annotated by medical experts to indicate the regions affected by viral
pneumonia. The dataset's diversity ensures that the model generalizes
well across different patient demographics, imaging conditions, and
pneumonia presentations.
Python: Python is renowned for its versatility, serving as a high-level
programming language suitable for diverse applications, from web
development and data analysis to artificial intelligence and scientific
computing.
Tensorflow: TensorFlow served as the primary deep learning
framework for model development and training. Leveraging
TensorFlow's high-level APIs, such as the Keras Sequential and
Functional APIs, streamlined the process of defining, compiling, and
training the UNet architecture.
CODE:
import numpy as np
import tensorflow as tf
import pandas as pd
from tqdm import tqdm
import os
from cv2 import imread, createCLAHE
import cv2
from glob import glob
%matplotlib inline
import matplotlib.pyplot as plt
image_path = os.path.join("../input/data/Lung Segmentation/CXR_png")
mask_path = os.path.join("../input/data/Lung Segmentation/","masks/")
images = os.listdir(image_path)
mask = os.listdir(mask_path)
mask = [fName.split(".png")[0] for fName in mask]
image_file_name = [fName.split("_mask")[0] for fName in mask]
check = [i for i in mask if "mask" in i]
print("Total mask that has modified name:",len(check))
testing_files = set(os.listdir(image_path)) & set(os.listdir(mask_path))
training_files = check
def getData(X_shape, flag = "test"):
im_array = []
mask_array = []
if flag == "test":
for i in tqdm(testing_files):
im = cv2.resize(cv2.imread(os.path.join(image_path,i)),
(X_shape,X_shape))[:,:,0]
mask = cv2.resize(cv2.imread(os.path.join(mask_path,i)),
(X_shape,X_shape))[:,:,0]
im_array.append(im)
mask_array.append(mask)
return im_array,mask_array
if flag == "train":
for i in tqdm(training_files):
im = cv2.resize(cv2.imread(os.path.join(image_path,i.split("_mask")
[0]+".png")),(X_shape,X_shape))[:,:,0]
mask = cv2.resize(cv2.imread(os.path.join(mask_path,i+".png")),
(X_shape,X_shape))[:,:,0]
def plotMask(X,y):
sample = []
for i in range(6):
left = X[i]
right = y[i]
combined = np.hstack((left,right))
sample.append(combined)
for i in range(0,6,3):
plt.figure(figsize=(25,10))
plt.subplot(2,3,1+i)
plt.imshow(sample[i])
plt.subplot(2,3,2+i)
plt.imshow(sample[i+1])
plt.subplot(2,3,3+i)
plt.imshow(sample[i+2])
plt.show()
In [6]:
linkcode
# Load training and testing data
dim = 256*2
X_train,y_train = getData(dim,flag="train")
X_test, y_test = getData(dim)
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras import backend as keras
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
def dice_coef(y_true, y_pred):
y_true_f = keras.flatten(y_true)
y_pred_f = keras.flatten(y_pred)
intersection = keras.sum(y_true_f * y_pred_f)
return (2. * intersection + 1) / (keras.sum(y_true_f) + keras.sum(y_pred_f) +
1)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def unet(input_size=(256,256,1)):
inputs = Input(input_size)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2),
padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2),
padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2),
padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2),
padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
return Model(inputs=[inputs], outputs=[conv10])
model = unet(input_size=(512,512,1))
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss,
metrics=[dice_coef, 'binary_accuracy'])
model.summary()
from keras.callbacks import ModelCheckpoint, LearningRateScheduler,
EarlyStopping, ReduceLROnPlateau
weight_path="{}_weights.best.hdf5".format('cxr_reg')
checkpoint = ModelCheckpoint(weight_path, monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only =
True)
reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
patience=3,
verbose=1, mode='min', epsilon=0.0001, cooldown=2,
min_lr=1e-6)
early = EarlyStopping(monitor="val_loss",
mode="min",
patience=15) # probably needs to be more patient, but kaggle time
is limited
callbacks_list = [checkpoint, early, reduceLROnPlat]
from IPython.display import clear_output
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
model.compile(optimizer=Adam(lr=2e-4),
loss=[dice_coef_loss],
metrics = [dice_coef, 'binary_accuracy'])
train_vol, validation_vol, train_seg, validation_seg = train_test_split((images-
127.0)/127.0,
(mask>127).astype(np.float32),
test_size = 0.1,random_state = 2018)
train_vol, test_vol, train_seg, test_seg = train_test_split(train_vol,train_seg,
test_size = 0.1,
random_state = 2018)
loss_history = model.fit(x = train_vol,
y = train_seg,
batch_size = 16,
epochs = 50,
validation_data =(test_vol,test_seg) ,
callbacks=callbacks_list)
clear_output()
pred_candidates = np.random.randint(1,validation_vol.shape[0],10)
preds = model.predict(validation_vol)
plt.figure(figsize=(20,10))
for i in range(0,9,3):
plt.subplot(3,3,i+1)
plt.imshow(np.squeeze(validation_vol[pred_candidates[i]]))
plt.xlabel("Base Image")
plt.subplot(3,3,i+2)
plt.imshow(np.squeeze(validation_seg[pred_candidates[i]]))
plt.xlabel("Mask")
plt.subplot(3,3,i+3)
plt.imshow(np.squeeze(preds[pred_candidates[i]]))
plt.xlabel("Pridiction")
OUTPUT:
REFERENCES:
https://www.kaggle.com/code/nikhilpandey360/lung-segmentation-
from-chest-x-ray-dataset/notebook