Abstract
Brain metastasis is a critical neurological condition caused by the spread of cancer cells to the brain. Accurate segmentation of metastatic regions in MRI scans is essential for diagnosis and treatment planning. Manual segmentation is time-consuming and highly dependent on expert radiologists.
In this work, we propose a deep learning-based framework for brain metastasis segmentation using multiple architectures including Nested U-Net (U-Net++), Attention U-Net, and an InceptionV3-based encoder-decoder model. MRI images are preprocessed using CLAHE for contrast enhancement and normalized for training stability. The models are trained using Dice-based evaluation and compared using segmentation performance metrics.
Additionally, a real-time web application is developed using FastAPI (backend) and Streamlit (frontend) to allow users to upload MRI scans and visualize segmentation outputs interactively.
Objectives:
Automatically segment brain metastasis in MRI images
Compare performance of multiple deep learning architectures
Improve tumor visibility using CLAHE preprocessing
Handle class imbalance using Dice-based evaluation
Deploy model using web interface for real-time usage
๐๏ธ Dataset Preparation
MRI images and corresponding segmentation masks are loaded from structured directories.
๐ฅ Loading Data
def load_data(image_dir, mask_dir):
images, masks = [], []
for img_name in os.listdir(image_dir):
img_path = os.path.join(image_dir, img_name)
mask_path = os.path.join(mask_dir, img_name)
if os.path.exists(mask_path):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
# CLAHE enhancement
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
img = clahe.apply(img)
images.append(img)
masks.append(mask)
return np.array(images), np.array(masks)
๐งช Preprocessing Pipeline
CLAHE for contrast enhancement
Normalization to [0,1]
Train-test split (80/20)
Data augmentation (rotation, flipping)
images = images / 255.0
X_train, X_test, y_train, y_test = train_test_split(images, masks, test_size=0.2)
๐ง Model Architectures
๐งฉ 1. Nested U-Net (U-Net++)
U-Net++ improves segmentation accuracy by introducing dense skip connections between encoder and decoder, reducing semantic gap and improving fine-grained tumor boundary detection.
๐ฏ 2. Attention U-Net
Attention U-Net improves performance by focusing on relevant regions using attention gates, helping detect small metastasis lesions.
๐งฌ 3. InceptionV3-Based Encoder-Decoder Model
A hybrid architecture using transfer learning.
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = layers.Conv2D(256, 3, activation='relu', padding='same')(x)
x = layers.UpSampling2D()(x)
x = layers.Conv2D(128, 3, activation='relu', padding='same')(x)
x = layers.UpSampling2D()(x)
x = layers.Conv2D(64, 3, activation='relu', padding='same')(x)
outputs = layers.Conv2D(1, (1,1), activation='sigmoid')(x)
model = models.Model(base_model.input, outputs)
๐ Loss Function
def dice_score(y_true, y_pred):
smooth = 1.0
y_true_f = tf.keras.backend.flatten(y_true)
y_pred_f = tf.keras.backend.flatten(y_pred)
intersection = tf.keras.backend.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (
tf.keras.backend.sum(y_true_f) + tf.keras.backend.sum(y_pred_f) + smooth
)
๐ Evaluation Metrics
Dice Coefficient (primary metric)
IoU (Jaccard Index)
Precision
Recall
๐ Model Training
unetpp_model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
att_unet_model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
๐งพ Results Comparison
Model Dice Score Observation
U-Net++ 0.xx Best for complex structures
Attention U-Net 0.xx Best for small lesions
InceptionV3 U-Net 0.xx Faster convergence
๐ Web Application Deployment
โ๏ธ FastAPI Backend
@app.post("/predict/")
async def predict(file: UploadFile):
image = np.array(Image.open(file.file).convert('L'))
prediction = unetpp_model.predict(image)
return {"segmentation": prediction.tolist()}
๐จ Streamlit Frontend
uploaded_file = st.file_uploader("Upload MRI Image")
if uploaded_file:
img = Image.open(uploaded_file)
st.image(img)
response = requests.post(
"http://localhost:8000/predict/",
files={"file": uploaded_file}
)
st.image(response.json()["segmentation"])
๐งฉ System Workflow
MRI Image
โ
CLAHE Preprocessing
โ
Deep Learning Model (U-Net++ / Attention U-Net / InceptionV3)
โ
Segmentation Mask
โ
Web Visualization (Streamlit)
โ ๏ธ Challenges & Solutions
Challenge Solution
Small lesions Attention U-Net + CLAHE
Class imbalance Dice Loss
Low contrast MRI CLAHE enhancement
Overfitting Data augmentation
๐ Conclusion
This project presents a multi-model deep learning framework for brain metastasis segmentation using U-Net++, Attention U-Net, and InceptionV3-based architecture. The system improves segmentation accuracy and provides a practical deployment pipeline through a web-based interface.
๐ฎ Future Work
Transformer-based segmentation (Swin UNETR)
Multi-modal MRI fusion (T1, T2, FLAIR)
Cloud deployment (AWS/GCP)
Real-time clinical integration
๐ References
U-Net: https://arxiv.org/abs/1505.04597
Attention U-Net: https://arxiv.org/abs/1804.03999
InceptionV3: https://arxiv.org/abs/1512.00567
FastAPI: https://fastapi.tiangolo.com/
Streamlit: https://streamlit.io/