How mobaidoctor/med-ddpm works
Main Components
• Data Management & Preprocessing:
• Dataset Folder: Contains the raw input data (NIfTI files for 3D MRIs and segmentation masks), sample CSV files, and scripts like dataset.py and preprocess_brats_data.py.
• Input types include whole-head images and BraTS modalities (T1, T1ce, T2, Flair).
• Diffusion Model Core:
• Folder “diffusion_model” holds core routines: FP16 utilities, logger, modules, trainers (trainer.py, trainer_brats.py), and U-Net model implementations. This is where model training and conditional diffusion logic are implemented.
• Fast Sampling Modules:
• The “fast_sampling” folder (with guided_diffusion and th_deis submodules) is dedicated to running inference quickly. It contains IPython notebooks and scripts for sample generation and optimized sampling routines (e.g., inference_ddpm.py, inference_deis.py).
• Training & Inference Scripts:
• Scripts under “scripts” (train.sh, train_brats.sh, sample.sh, sample_brats.sh) orchestrate training and sampling pipelines.
• Utility Functions:
• The “utils” folder includes helper functions (e.g., dtypes, lowpass_filter) that support data type conversions and filtering needed during processing.
• External Dependencies:
• The README highlights dependencies such as Torchio and Nibabel for handling MRI data.
Relationships and Interactions
• Data Flow:
• Data enters via the dataset files (organized per modality and segmentation masks) and is processed through dataset.py. A transformation or preprocessing module prepares the images for model consumption.
• Model Training:
• The training scripts (train.py, train_brats.py) invoke diffusion_model modules. The conditional diffusion process is implemented via U-Net architectures and related utilities (found in diffusion_model/unet.py and unet_brats.py). The training process logs its progress using the logger modules.
• Model Sampling (Inference):
• The fast_sampling modules (fast_sampling/guided_diffusion and fast_sampling/th_deis) illustrate the fast sampling routines optimised specifically for inference. These components take a segmentation mask input and generate synthetic MRI images.
• Integration:
• The system integrates data preprocessing, model training, and sampling into a cohesive workflow controlled by the provided shell scripts.
Architectural Patterns & Design Principles
• Modularity: The project separates concerns by having distinct modules for data management, model training, and fast sampling. This separation enables easier testing and potential reusability for different MRI synthesis tasks.
• Pipeline Architecture: A linear yet well-partitioned data pipeline exists, beginning from dataset ingestion, through preprocessing, to model training and sampling.
• Conditional Generation: The architecture emphasizes conditional synthesis, which requires the diffusion model to be fed not only random noise but also semantic information (segmentation masks).
Relevant Technologies and Frameworks
• PyTorch: The central framework used to implement the diffusion models.
• Medical Imaging Libraries: Torchio and Nibabel facilitate 3D MRI data handling.
• External GitHub Projects: The project builds upon methodologies from repositories such as denoising-diffusion-pytorch and guided-diffusion.