This repository contains code for training and evaluating U-Net models for medical image segmentation using the MONAI framework. The implementation includes both basic and advanced U-Net variants along with LRP (Local Rule-based Perturbation) analysis.
The project consists of three main components:
-
Model Training (
unet_training_array.py):- Trains various U-Net architectures for medical image segmentation.
- Supports different normalization modes: instance and batch normalization.
- Implements data augmentation techniques including random rotations, flips, and padding/cropping to fixed sizes.
- Provides sliding window inference for efficient validation.
-
Data Preparation (
DataPrepair.py):- Processes both medical and XCT (X-ray Computed Tomography) data into suitable formats.
- Converts 3D medical images and other supported formats into 2D slices for training.
- Handles DICOM files and other medical image formats, outputting prepared images in PNG format.
-
LRP Analysis & Evaluation (
TestMonai.py):- Implements LRP (Local Rule-based Perturbation) analysis for model interpretability.
- Provides visualization of attention maps to understand model decisions.
- Includes evaluation metrics and visualizations for both positive and negative attributions, with overlay plots for better interpretation.
- Clone the repository:
git clone https://github.com/yourusername/MONAI-UNet.git
cd MONAI-UNet- Install Python and required packages:
python --version # Ensure Python 3.8+
pip install -r requirements.txt- MONAI:
monai>=0.6.0 - PyTorch:
torch>=1.9.0 - Captum:
captum>=0.2.1(For LRP analysis) - Numpy & Scipy: For numerical operations.
- Matplotlib: For visualization tasks.
- SimpleITK: For medical image processing.
- Install additional dependencies for medical image processing:
pip install pydicom dask-image scikit-imageTo train a model on your dataset:
-
Prepare your dataset in the following structure:
/your_dataset/ ├── train/ ├── img/ # Input images └── mask/ # Corresponding segmentation masks └── val/ ├── img/ └── mask/ -
Run the training script:
python unet_training_array.py \
--train_imgs /path/to/train/img/ \
--train_labels /path/to/train/mask/ \
--val_imgs /path/to/val/img/ \
--val_labels /path/to/val/mask/- Evaluate a trained model:
python TestMonai.py \
--model_path /path/to/trained/model.pth \
--input_image /path/to/test/image.png \
--output_folder ./evaluation_results/- For full dataset analysis:
python evaluate.py \
--models_dir /path/to/models/ \
--input_images_dir /path/to/input/images/To prepare medical and XCT data:
-
Place your input images in the following structure:
/your_input_data/ ├── DICOM/ # DICOM format files └── Other_Format/ # Other supported image formats -
Run the data preparation script:
python DataPrepair.pyMONAI-UNet/
├── unet_training_array.py # Model training implementation
├── TestMonai.py # LRP analysis and evaluation
├── DataPrepair.py # Data preparation script
└── README.md # This documentation
-
Supported Models:
- BasicUNet
- ResidualUNet (ResiduelUnet)
- UNet++ (BasicUNetPlusPlus)
-
Normalization Modes:
- Instance normalization: Maintains statistics per sample.
- Batch normalization: Applies the same transformation across a batch of samples.
-
Data Augmentation:
- Random rotations
- Random flips
- Normalization
- Padding/cropping to fixed size
-
LRP Analysis:
- Visualizes attention maps for model decision-making.
- Provides detailed attributions (positive and negative) for each prediction.
- Includes overlay plots on original images for better interpretability.
Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please feel free to:
- Open an issue on GitHub
- Fork the repository and submit a pull request
GPL v3
We hope this README provides a clear understanding of the project and its functionalities. Happy coding!