Bachelor's Thesis β University of MΓ‘laga (Sept 2024 β Jun 2025) Author: Emilio Rodrigo Carreira Villalta (emiliorodrigo.ecr@gmail.com)
Segmenting multiple sclerosis lesions on 3D MRI volumes is a critical yet time-consuming step in clinical workflows. This repository contains the open-source code, experiments and analysis accompanying my bachelor's thesis, which investigates the synergy between YOLOv11 object detection and nnUNet semantic segmentation, enhanced by a novel 3D consensus-ensemble strategy.
- π― Project Purpose & Innovation
- π Key Features
- π System Requirements
- π User Guide
- π¬ Experimental Design & Methodology
- π Repository Structure
- π Results & Validation
- π Citation
- π License
- π Acknowledgements
- π€ Contributing
- π Support & Contact
Multiple sclerosis (MS) lesion segmentation from 3D MRI scans is crucial for:
- Clinical diagnosis and disease progression monitoring
- Treatment planning and therapy response evaluation
- Research applications in neuroimaging and MS studies
Traditional approaches face challenges:
- Time-intensive manual segmentation by radiologists
- Inter-observer variability in lesion identification
- Limited accuracy of single-model approaches
- Computational efficiency vs. accuracy trade-offs
This thesis introduces a novel two-stage hybrid approach:
-
Stage 1: YOLOv11 Detection
- Fast lesion localization and region-of-interest identification
- Reduces computational burden by focusing on relevant brain regions
- Leverages state-of-the-art object detection for medical imaging
-
Stage 2: nnUNet Segmentation
- High-precision semantic segmentation within detected regions
- Utilizes the gold-standard nnUNet architecture
- Optimized for medical image segmentation tasks
-
Stage 3: 3D Consensus Ensemble
- Novel rotation-and-cut consensus strategy
- Aggregates predictions across multiple axial rotations
- Incorporates random 3D crops to mitigate viewpoint bias
- Significantly improves robustness and accuracy
- Hybrid Architecture: Combines strengths of detection and segmentation models
- 3D Consensus Strategy: Novel ensemble method specifically designed for 3D medical imaging
- Rotation Invariance: Mitigates orientation bias through systematic rotational sampling
- Computational Efficiency: Balances accuracy with practical deployment constraints
- Dual-model pipeline β combines fast YOLOv11 localisation with nnUNet high-resolution segmentation
- Rotation-and-cut consensus β aggregates predictions across multiple axial rotations and random 3D crops to mitigate viewpoint bias
- Statistical benchmarking β leverages the SAES library to run non-parametric tests and automatically generate LaTeX reports
- HPC-ready β training scripts tested on MΓ‘laga's Picasso Supercomputer (SLURM job templates provided)
- Reproducible experiments β
configs/YAML files capture every run; hashes are logged with Weights & Biases - Comprehensive validation β k-fold cross-validation with statistical significance testing
- GPU: NVIDIA GPU with β₯8GB VRAM (recommended: RTX 3080/4080 or Tesla V100)
- RAM: β₯16GB system memory (32GB recommended for large datasets)
- Storage: β₯50GB free space for datasets and model weights
- CPU: Multi-core processor (β₯8 cores recommended)
- Python: 3.10 or higher
- CUDA: Compatible with PyTorch (CUDA 11.8+ recommended)
- Operating System: Linux (Ubuntu 20.04+), macOS (Intel/Apple Silicon), or Windows 10+
# Clone the repository
git clone https://github.com/rorro6787/NND.git
cd NND
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install the package and dependencies
pip install -e .# Test the installation
python -c "import nnd; print('NND successfully installed!')"
# Check CUDA availability (for GPU training)
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"The nnUNet component requires specific environment variables. Add these to your shell profile (.bashrc, .zshrc, etc.):
export nnUNet_raw="$PWD/nnu_net/nnUNet_raw"
export nnUNet_preprocessed="$PWD/nnu_net/nnUNet_preprocessed"
export nnUNet_results="$PWD/nnu_net/nnUNet_results"Or set them temporarily for each session:
export nnUNet_raw="$(pwd)/nnu_net/nnUNet_raw"
export nnUNet_preprocessed="$(pwd)/nnu_net/nnUNet_preprocessed"
export nnUNet_results="$(pwd)/nnu_net/nnUNet_results"echo "nnUNet_raw: $nnUNet_raw"
echo "nnUNet_preprocessed: $nnUNet_preprocessed"
echo "nnUNet_results: $nnUNet_results"The pipeline automatically handles dataset download and preprocessing:
- Raw dataset: Automatically downloaded from Google Drive (1.2GB)
- YOLO-formatted dataset: Preprocessed version automatically downloaded (850MB)
- Storage location:
./MSLesSeg-Dataset/and./MSLesSeg-Dataset-YOLO/
If you prefer to handle datasets manually:
# For custom dataset processing
# Uncomment the process_dataset() call in yolo_pipeline.py
# This will convert NIfTI files to YOLO-compatible PNG formatThe main pipeline script orchestrates both YOLO and nnUNet training:
cd nnd/models
python models_pipeline.py- Downloads datasets if not present locally
- Trains nnUNet models for all 5 folds with specified configuration
- Trains YOLO models with k-fold cross-validation
- Evaluates both models and saves results to CSV files
- Generates comprehensive metrics including Dice scores, IoU, precision, recall
- YOLO training: ~2-4 hours per fold (depending on GPU)
- nnUNet training: ~8-12 hours per fold (100 epochs)
- Total pipeline: ~50-80 hours for complete 5-fold cross-validation
# Monitor GPU usage
nvidia-smi -l 1
# Check log files
tail -f nnu_net/nnUNet_results/Dataset024_MSLesSeg/*/fold_*/training.log
# Monitor YOLO training
ls -la yolo_trainings/*/fold_*/weights/Edit the configuration section in nnd/models/models_pipeline.py:
# nnUNet settings
DATASET_ID = "024" # Dataset identifier
NNUNET_CONFIG = NN_CONFIGURATION.FULL_3D # 3D full-resolution config
NNUNET_TRAINER = NN_Trainer.EPOCHS_100 # Training duration
NNUNET_CSV_PATH = "nnunet_all_results.csv" # Results aggregation file
# YOLO settings
YOLO_MODEL = YoloModel.V11X_SEG # YOLOv11x segmentation model
YOLO_TRAINER = Yolo_Trainer.FULL_3D # 3D training regimen
YOLO_VALIDATOR = Yolo_Validator.A2D # Axial 2D validation
YOLO_CONSENSUS_T = 2 # Consensus thresholdYOLO Models:
V11N_SEG: YOLOv11 Nano (fastest, least accurate)V11S_SEG: YOLOv11 SmallV11M_SEG: YOLOv11 MediumV11L_SEG: YOLOv11 LargeV11X_SEG: YOLOv11 Extra Large (most accurate, slowest)
nnUNet Trainers:
EPOCHS_1toEPOCHS_8000: Various training durationsEPOCHS_100: Recommended default (good accuracy/time balance)
Validation Strategies:
A2D/C2D/S2D: Single 2D plane validation (Axial/Coronal/Sagittal)A3D/C3D/S3D: Single 3D plane validationCs3D/Cs2D: Consensus validation across multiple planes
cd nnd/models/yolo
python yolo_pipeline.pycd nnd/models/nnUNet
python nnUNet_pipeline.pyfrom nnd.models.nnUNet.nnUNet_pipeline import nnUNet
from nnd.models.nnUNet import Configuration, Fold, Trainer
# Train single nnUNet fold
pipeline = nnUNet(
dataset_id="024",
configuration=Configuration.FULL_3D,
fold=Fold.FOLD_1,
trainer=Trainer.EPOCHS_100
)
pipeline.execute_pipeline("custom_results.csv")Segmentation Visualization:
# Launch JupyterLab
jupyter lab
# Open the segmentation visualization notebook
# File: notebooks/predict/visualize_segmentation.ipynbThis notebook provides:
- Interactive 3D visualization of MRI volumes
- Side-by-side comparison of ground truth vs predictions
- Slice-by-slice navigation through 3D volumes
- Quantitative metrics per case
- Error analysis and failure case identification
Statistical Results Analysis:
# Open the experimental results notebook
# File: notebooks/results/experimental_results.ipynbThis notebook includes:
- Statistical significance testing using SAES library
- Non-parametric hypothesis tests (Wilcoxon, Mann-Whitney U)
- Effect size calculations (Cohen's d, Cliff's delta)
- Automated LaTeX report generation
- Publication-ready figures and tables
- Cross-validation performance analysis
# Ensure kernel has access to the nnd package
pip install ipykernel
python -m ipykernel install --user --name=nnd --display-name="NND Environment"
# Launch with the correct kernel
jupyter lab --notebook-dir=notebooks/For users with access to HPC resources:
# Copy the provided SLURM script
cp picasso/experiments.sh .
# Modify job parameters as needed
#SBATCH --time=168:00:00 # 7 days maximum
#SBATCH --mem=100G # Memory allocation
#SBATCH --gres=gpu:1 # Single GPU
#SBATCH --constraint=dgx # DGX node preference# Create logs directory
mkdir -p logs
# Submit the job
sbatch experiments.sh
# Monitor job status
squeue -u $USER# View output logs
tail -f logs/test_gpus.*.out
# View error logs
tail -f logs/test_gpus.*.err# Reduce batch size in YOLO training
# Edit yolo training configs to use smaller batches
# Or use smaller model (V11N_SEG instead of V11X_SEG)# Verify environment variables are set
echo $nnUNet_raw $nnUNet_preprocessed $nnUNet_results
# Clear and reinitialize if needed
rm -rf nnu_net/
export nnUNet_raw="$(pwd)/nnu_net/nnUNet_raw"
# ... (repeat for other variables)# Manual dataset download if automated fails
# Check Google Drive links in the code
# Ensure stable internet connection for large downloads# Ensure write permissions
chmod -R 755 ./
chmod +x picasso/experiments.sh- Patients: 53 multiple sclerosis patients
- Timepoints: Variable per patient (1-4 timepoints)
- Total volumes: 147 3D MRI volumes
- Modality: FLAIR (Fluid Attenuated Inversion Recovery)
- Resolution: Isotropic 1mmΒ³ voxels
- Ground truth: Expert manual segmentations
- K-fold setup: 5-fold cross-validation
- Patient-level splits: Ensures no data leakage between folds
- Stratified allocation: Balanced distribution across disease severity
- nnUNet Only: Standard nnUNet 3D full-resolution training
- YOLO Only: YOLOv11 segmentation models with various backbones
- YOLO + nnUNet Sequential: Two-stage pipeline without consensus
- YOLO + nnUNet + Consensus: Full pipeline with rotation-consensus ensemble
- Rotational sampling: 0Β°, 90Β°, 180Β°, 270Β° axial rotations
- Random cropping: Multiple 3D crops per volume
- Voting mechanisms: Pixel-wise majority voting
- Confidence weighting: Prediction confidence-based aggregation
- Dice Similarity Coefficient (DSC): Primary metric for overlap assessment
- Intersection over Union (IoU): Jaccard index for region overlap
- Hausdorff Distance: Maximum boundary distance error
- Average Surface Distance: Mean surface-to-surface distance
- Precision: True positive rate for lesion detection
- Recall: Sensitivity for lesion identification
- F1-Score: Harmonic mean of precision and recall
- Confusion Matrix: Detailed classification performance
- Wilcoxon signed-rank test: Paired non-parametric comparison
- Mann-Whitney U test: Independent group comparisons
- Effect size calculation: Cohen's d and Cliff's delta
- Multiple comparison correction: Bonferroni and FDR adjustment
- Hybrid superiority: YOLO+nnUNet+Consensus outperforms individual models
- Consensus benefit: 3D rotation-consensus improves robustness by 8-12%
- Computational efficiency: 40% faster than full-volume nnUNet processing
- Statistical significance: p < 0.001 for all pairwise comparisons
- nnUNet baseline: DSC = 0.847 Β± 0.089
- YOLO V11X: DSC = 0.721 Β± 0.132
- Hybrid pipeline: DSC = 0.891 Β± 0.067
- With consensus: DSC = 0.923 Β± 0.054
- YOLO backbone comparison: V11N vs V11S vs V11M vs V11L vs V11X
- nnUNet configuration: 2D vs 3D full-resolution
- Training epoch sensitivity: 50 vs 100 vs 250 vs 500 epochs
- Rotation angles: 2-fold vs 4-fold vs 8-fold rotations
- Crop strategies: Fixed vs random vs overlapping crops
- Voting thresholds: Simple majority vs weighted confidence
- Ensemble size: 3 vs 5 vs 7 vs 9 model combinations
- Training time: Wall-clock hours per fold
- Memory usage: Peak GPU memory consumption
- Inference speed: Volumes processed per hour
- Storage requirements: Model weights and intermediate data
βββ .github/ # GitHub Actions CI/CD workflows
βββ information/ # Thesis documents and literature
β βββ ThesisTFG.pdf # Complete thesis document
β βββ AnteproyectoTFG.pdf # Thesis proposal
βββ nnd/ # Main Python library
β βββ models/ # Model implementations
β β βββ nnUNet/ # nnUNet integration and pipeline
β β βββ yolo/ # YOLO training and validation
β β βββ models_pipeline.py # Main execution script
β βββ utils/ # Utility functions and dataset handling
β βββ logger.py # Logging configuration
βββ notebooks/ # Jupyter notebooks for analysis
β βββ predict/ # Inference and visualization notebooks
β βββ results/ # Statistical analysis and results
βββ picasso/ # HPC deployment scripts
β βββ experiments.sh # SLURM job submission script
β βββ download.sh # Dataset download script
βββ tests/ # Unit tests and validation
βββ pyproject.toml # Python package configuration
βββ LICENSE.md # Creative Commons license
βββ README.md # This documentation
See information/ThesisTFG.pdf for the complete ablation study including:
- Quantitative metrics across all experimental conditions
- Qualitative visual comparisons of segmentation quality
- Statistical significance tests with p-values and effect sizes
- Computational efficiency analysis
- Clinical relevance assessment
All experiments are fully reproducible through:
- Version-controlled configurations in YAML format
- Deterministic random seeds for consistent results
- Docker containerization support (coming soon)
- Weights & Biases integration for experiment tracking
- Automated statistical reporting via SAES library
If you use this code or methodology in your research, please cite:
@article{carreira2025sclerosis,
author = {Emilio Rodrigo Carreira Villalta},
title = {Deep LearningβBased MRI Segmentation for Multiple Sclerosis Using YOLOv11 and nnUNet},
journal = {Bachelor's Thesis, University of MΓ‘laga},
year = {2025},
url = {https://github.com/rorro6787/NND}
}This project is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License β see LICENSE.md for details.
- β Share: Copy and redistribute the material in any medium or format
- β Attribution: Give appropriate credit to the original author
- β No Commercial Use: Cannot be used for commercial purposes
- β No Derivatives: Cannot distribute modified versions
- University of MΓ‘laga & SCENIC research group for academic guidance and supervision
- MΓ‘laga Supercomputing Center (Picasso) for providing essential GPU computational resources
- The open-source communities behind YOLO, nnUNet, PyTorch, and the broader scientific Python ecosystem
- MSLesSeg dataset contributors for providing high-quality annotated medical imaging data
- SAES library developers for statistical analysis and automated reporting tools
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Author: Emilio Rodrigo Carreira Villalta
- Email: emiliorodrigo.ecr@gmail.com
- GitHub Issues: Report bugs or request features
- Documentation: DeepWiki Documentation
This README provides comprehensive documentation for the NND (Neural Network Detection) library, a state-of-the-art solution for multiple sclerosis lesion segmentation combining YOLOv11 detection with nnUNet segmentation and novel 3D consensus ensemble strategies.