This repository provides all the necessary code and resources to perform liver segmentation using the Monai framework and PyTorch. The project is structured to allow customization for segmenting other organs as well.
A special shoutout to Mohammed El Amine Mokhtari for his great tutorials on medical imaging and computer vision. His work has been an inspiration and a valuable resource for the community! Youtube Tut
- Project Overview
- Dataset
- Data Preparation
- Model Architecture
- Training and Logging
- Installation and Usage
- References
This project focuses on automatic 3D liver segmentation from medical imaging data using a UNet architecture. The implementation is powered by the Monai framework, an open-source PyTorch-based library for deep learning in healthcare imaging.
Key features include:
- Support for mixed precision training.
- Advanced logging and experiment tracking with Weights and Biases (WandB).
- Incorporation of the Adopt optimizer and Adam optimizer for performance tuning.
The datasets used for this project are sourced from Kaggle:
These datasets consist of liver tumor segmentation volumes stored in NIfTI format. For detailed information, please refer to the Kaggle links above.
The data preparation process is automated and outlined in the Datapreparation.ipynb
notebook. Key steps include:
- Conversion of NIfTI files to DICOM format.
- Grouping DICOM slices: Groups of 74 slices are created for consistency.
- Reconversion to compressed NIfTI: For training efficiency.
- Empty segmentation cleanup: Removal of empty segmentation masks and their corresponding volumes.
Ensure you execute the notebook before running the training scripts to prepare the data in the correct format.
The UNet architecture is utilized for segmentation, featuring:
- Encoder-decoder structure with skip connections.
- Convolutional layers to extract and learn spatial features.
- Compatibility with Monai for medical image processing.
For detailed insights into the UNet design, visit the Monai documentation.
This project implements the Adopt optimizer, which combines the strengths of:
- Adaptive gradient clipping for robust optimization.
- Momentum-based updates to enhance convergence.
Resources for learning about Adopt:
Mixed precision training leverages both float32
and float16
data types to accelerate training while reducing memory usage. It is implemented using PyTorch’s torch.cuda
package:
- Forward and backward passes are performed in
float16
where possible. - Critical computations, such as loss scaling, are performed in
float32
to maintain numerical stability.
- Speeds up training by 1.5–2x.
- Reduces GPU memory usage, enabling larger batch sizes.
from torch.cuda import autocast, GradScaler
scaler = GradScaler()
with autocast(device=device):
outputs = model(inputs)
loss = loss_function(outputs.float(), label.float())
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
Weights and Biases (WandB) is used for experiment tracking, visualization, and hyperparameter tuning.
- Install WandB:
pip install wandb
- Login to WandB:
wandb login
- Initialize in your script:
import wandb wandb.init(project="Liver-Segmentation")
- Training and validation losses.
- Dice metrics.
- Checkpoints for model performance.
Install the required packages:
pip install -r requirements.txt
git clone https://github.com/SharifEhab/Automatic-3D-Liver-Segmentation.git
cd Automatic-3D-Liver-Segmentation
Prepare the dataset by running the Datapreparation.ipynb
notebook.
Run the training script:
python train.py
- Monai Documentation
- Adopt Optimizer Paper
- Adopt GitHub Repository
- Liver Tumor Segmentation Dataset (Kaggle)