Lung CT Image Segmentation: Bachelor of Technology Electronics and Communication Engineering
Lung CT Image Segmentation: Bachelor of Technology Electronics and Communication Engineering
By
POTLACHERUVU NIKITHA (21071A0445)
PRANITHA MUSUNURU (21071A0446)
RAMOJU VEERA BHADRA SUBRAHMANYA RAGHAVAMSI (21071A0447)
SAI PRANEETH VASA (21071A0448)
BONAFIDE CERTIFICATE
Lab Mentor
DECLARATION
1.
2.
3.
4.
5.
PLACE :
ABSTRACT
Image Segmentation:
APPROACH:
**Coding Approach for UNet Segmentation with Over 100,000
Parameters Using TensorFlow:**
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.
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
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]
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 unet(input_size=(256,256,1)):
inputs = Input(input_size)
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()
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)
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