Skip to content

cassxw/SAMSeg

Repository files navigation

SAMSeg: U-SAM 🧠

Overview

The Segment Anything Model (SAM) is a cutting-edge, promptable deep learning model designed for 2D natural image segmentation. However, medical imagingβ€”particularly brain tumor segmentation πŸ§ β€”presents unique challenges, including highly irregular tumor boundaries, variations in intensity, and the complex, multidimensional nature of medical scans.

To address these challenges, this project extends SAM's segmentation capabilities by integrating Parameter-Efficient Fine-Tuning (PEFT) techniques and training on the BraTS Intracranial Meningioma 2023 dataset. Furthermore, we introduce U-SAM, a novel framework that combines SAM’s segmentation power with the precision and structural awareness of U-Net architectures, thereby enhancing segmentation accuracy for complex medical imaging tasks.

Given the difficulties encountered in programmatic data retrieval from Synapse, I have made our curated dataset (including our specific training/test split) and model checkpoints available for easy access: πŸ”— U-SAM Project SharePoint

πŸ“’ If you experience any accessibility issues with the dataset or checkpoints, please feel free to contact me.

Project Directory Structure πŸ—‚οΈ

β”œβ”€β”€ U-SAM/                               # Scripts for running and evaluating U-SAM
β”‚   β”œβ”€β”€ U1-SAM2.py                        > Runs U1-SAM2
β”‚   └── U-SAM-Combo.py                    > Runs the U-SAM combination
β”œβ”€β”€ U-Net/                               # U-Net model scripts
β”‚   β”œβ”€β”€ unet_test.py                      > Tests U-Net
β”‚   β”œβ”€β”€ unet_train.py                     > Trains U-Net
β”‚   └── unet3d.py                         > 3D U-Net architecture implementation
β”œβ”€β”€ SAM/                                 # SAM model scripts
β”‚   β”œβ”€β”€ sam_test.py                       > Tests SAM
β”‚   β”œβ”€β”€ sam_train.py                      > Trains SAM
β”‚   └── sam_tuning.py                     > Tunes SAM hyperparameters
β”œβ”€β”€ dataset/                             # Dataset folders (must be downloaded separately)
β”‚   β”œβ”€β”€ BraTS-MEN-Test/
β”‚   β”œβ”€β”€ BraTS-MEN-Train/
β”‚   └── BraTS-MEN-Validate/
β”œβ”€β”€ checkpoints/                         # Pre-trained model checkpoints (must be downloaded separately)
β”‚   β”œβ”€β”€ peft-sam_epoch_125.pth.tar
β”‚   β”œβ”€β”€ unet_epoch_100.pth.tar
β”‚   └── sam_vit_b_01ec64.pth
β”œβ”€β”€ utilities/                           # Utility scripts
β”‚   β”œβ”€β”€ brats_dataset.py                   > Custom dataset loader for BraTS
β”‚   β”œβ”€β”€ general_utils.py                   > General helper functions
β”‚   β”œβ”€β”€ model_utils.py                     > Model-related utilities
β”‚   β”œβ”€β”€ plot.py                            > Visualization functions
β”‚   β”œβ”€β”€ preprocess.py                      > Preprocessing functions
β”œβ”€β”€ requirements.txt                     # Dependencies list

Setup & Installation βš™οΈ

To set up the environment for this project:

  1. Create a Virtual Environment (recommended):
    # Using venv
    python3 -m venv samseg-env
    source samseg-env/bin/activate
    
    # Using conda
    conda create --name samseg-env python=3.8
    conda activate samseg-env
  2. Install Required Libraries:
    pip install -r requirements.txt

Usage πŸš€

If you run into module import issues, ensure you're in the root project directory and run:

export PYTHONPATH=$(pwd):$PYTHONPATH

Running Scripts

Hyperparameter Tuning for SAM 🎯

python3 SAM/sam_tuning.py --checkpoint_path checkpoints/sam_vit_b_01ec64.pth \
                          --output_dir SAM/tuning_outputs --n_trials 50

Training SAM πŸ‹οΈβ€β™‚οΈ

python3 SAM/sam_train.py --checkpoint_path checkpoints/peft-sam_epoch_125.pth.tar \
                         --output_dir SAM/training_outputs --init_lr 0.00006 \
                         --decay_rate 0.995 --max_epoch 50 --batch_size 1 \
                         --samples_per_epoch 150 --backup_interval 10 \
                         --validate --validation_interval 5

Testing SAM πŸ§ͺ

  • Vanilla SAM:
    python3 SAM/sam_test.py --vanilla --visualise --results_dir SAM/vanilla_test_results --batch_size 1
  • PEFT-SAM:
    python3 SAM/sam_test.py --peft_path checkpoints/peft-sam_epoch_125.pth.tar \
                            --visualise --results_dir SAM/peft_test_results --batch_size 1

Training U-Net πŸ‹οΈ

python3 UNet/unet_train.py --checkpoint_path checkpoints/unet_epoch_100.pth.tar \
                           --output_dir UNet/training_outputs --init_lr 0.00006 \
                           --decay_rate 0.995 --max_epoch 50 --batch_size 1 \
                           --samples_per_epoch 150 --backup_interval 10 \
                           --validate --validation_interval 5

Testing U-Net πŸ§ͺ

python3 UNet/unet_test.py --checkpoint_path checkpoints/unet_epoch_100.pth.tar \
                          --visualise --results_dir UNet/unet_test_results --batch_size 1

Running U-SAM-Combo πŸ€–

python3 U-SAM/U-SAM-Combo.py --unet_checkpoint checkpoints/unet_epoch_100.pth.tar \
                             --sam_checkpoint checkpoints/peft-sam_epoch_125.pth.tar \
                             --visualise --output_dir U-SAM/u-sam-combo-results --bbox_emulation

Running U1-SAM2 🧠

python3 U-SAM/U1-SAM2.py --unet_checkpoint checkpoints/unet_epoch_100.pth.tar \
                         --sam_checkpoint checkpoints/peft-sam_epoch_125.pth.tar \
                         --visualise --output_dir U-SAM/u1-sam2-results --bbox_emulation

Notes πŸ“Œ

  • Dataset & Checkpoints: The dataset must be downloaded separately from the SharePoint link above.
  • Ensure paths are correct before running any scripts.
  • GPU Support: Requires CUDA for optimal performance.
  • Tested Environment: A100 Core GPU on UCT's HPC.
  • Citations: Full citations can be found in the accompanying paper and code, with special thanks to the Kurtlab BraTS 2023 submission for their adaptable project setup.

πŸš€ Happy segmenting! 🧠 For any questions or comments about this project, please don’t hesitate to reach out!

About

🧠 U-SAM: Enhancing brain tumour segmentation by integrating SAM with U-Net architectures, fine-tuned using PEFT on the BraTS Intracranial Meningioma dataset.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages