How benbillot/spatial_regularisation works
1. Repository purpose
- This is a Python 3.12 research codebase for keypoint-based image registration with spatial regularisation.
- The core goal is to train and evaluate unsupervised affine or rigid registration models, then apply them to either pairwise scans or time series.
2. Top-level split: reusable package vs runnable experiments
- spatial_regularisation/ contains the reusable library code.
- scripts/ contains experiment-specific entry points showing how the library is used for two application settings:
- foetal time-series rigid tracking
- adult brain MRI affine registration
3. Main execution paths
- The repo has two major runtime flows:
- Training flow: data loading → augmentation → model forward pass → keypoint/moment computation → registration + regularisation losses → optimisation.
- Inference flow: load trained model → run prediction on scan pairs or time series → generate aligned outputs and registration results.
4. Model subsystem
- networks.py and equivariant_unet_physical_units.py define the neural registration models and related building blocks.
- The naming suggests the code supports architectures that produce keypoints or transformation parameters while being aware of physical voxel spacing / units, which is important for geometry-consistent registration.
- __init__.py exposes the package as a single importable module boundary.
5. Training subsystem
- training.py is the orchestration layer for optimisation.
- It is responsible for wiring together:
- input pipelines
- model instantiation
- loss computation
- training loops and evaluation during training
- The readme indicates the training code is specifically for unsupervised registration, so the supervision signal comes from image similarity plus the proposed regularisers rather than ground-truth transforms.
6. Prediction / evaluation subsystem
- predict.py handles standard pairwise prediction and testing on fixed/moving scan pairs.
- predict_time_series.py handles longitudinal data, where each later frame is aligned back to the first frame.
- This is a distinct architectural boundary: pairwise registration and temporal tracking are implemented separately because their input structure and output aggregation differ.
7. Loss and regularisation subsystem
- losses.py implements the three proposed spatial regularisation terms from the paper.
- These losses are a central architectural component, because they sit between the registration model and optimisation, shaping the learned keypoints/transforms.
- They are complemented by spatial_moments.py, which computes the first- and second-order moments of volumetric fields used by those losses.
8. Geometry and measurement layer
- spatial_moments.py is the main geometric utility layer.
- Its role is to turn volumetric outputs into interpretable spatial statistics, which are then consumed by the regularisation terms.
- This is the main bridge between image-space model outputs and the paper’s moment-based regularisation objective.
9. Data handling and augmentation
- loaders.py provides dataset ingestion and batching.
- augmenters.py provides data augmentation for training.
- Together they form the input boundary of the system: raw scans/time series enter here before any model or loss code sees them.
10. Utility layer
- utils.py likely contains shared helpers used across training, inference, and data handling.
- This module acts as a cross-cutting support boundary rather than a primary subsystem.
11. Experiment scripts as concrete application wiring
- scripts/training_adult.py and scripts/predict_adult.py show the adult MRI affine-registration pipeline.
- scripts/training_foetal.py and scripts/predict_foetal.py show the foetal rigid tracking pipeline.
- These scripts are important for architecture diagrams because they demonstrate how the same core library is configured differently for two datasets and two motion models.
12. Data flow summary
- Input data enters through loaders, optionally passes through augmentation, then reaches the model.
- The model produces registration-relevant outputs that are interpreted via spatial moments.
- The losses combine image/registration objectives with the proposed spatial regularisation.
- Inference flows bypass optimisation and instead return predicted alignments for pairwise or temporal evaluation.
13. Technology and runtime boundaries
- The code is pure Python and depends on the packages listed in requirements.txt.
- No external service, database, or web layer is implied by the repository structure.
- The architecture is therefore a local research ML pipeline: training and inference are script-driven and package-backed, not service-oriented.
14. What to show in an architecture diagram
- The key boxes are: scripts → loaders/augmenters → networks → spatial moments/losses → training/predict.
- The important conceptual boundaries are: data ingestion, modeling, geometry/regularisation, and execution mode (training vs prediction, pairwise vs time series).
Main entry points: training.py Training · predict.py Predict · networks.py Networks · predict_time_series.py Time-series predict
Components 15
Scripts
Experiment entry points for adult and foetal workflows
- Adult train · Adult brain MRI affine-registration training script
scripts/training_adult.py - Adult predict · Adult brain MRI affine-registration inference script
scripts/predict_adult.py - Foetal train · Foetal time-series rigid tracking training script
scripts/training_foetal.py - Foetal predict · Foetal time-series rigid tracking inference script
scripts/predict_foetal.py
Core package
Reusable library for loading, modeling, losses, and execution
- Package API · Public package boundary for the reusable registration code
spatial_regularisation/__init__.py - Loaders · Dataset ingestion and batching for scans and sequences
spatial_regularisation/loaders.py - Augmenters · Training-time data augmentation
spatial_regularisation/augmenters.py - Training · Unsupervised optimisation and training loop orchestration
spatial_regularisation/training.py - Predict · Pairwise registration prediction and evaluation
spatial_regularisation/predict.py - Time-series predict · Longitudinal alignment back to the first frame
spatial_regularisation/predict_time_series.py - Networks · Registration model definitions and building blocks
spatial_regularisation/networks.py - Physical U-Net · Geometry-aware U-Net variant using physical voxel spacing
spatial_regularisation/equivariant_unet_physical_units.py - Losses · Spatial regularisation terms and training objectives
spatial_regularisation/losses.py - Moments · First- and second-order moments for volumetric fields
spatial_regularisation/spatial_moments.py - Utils · Cross-cutting helper functions
spatial_regularisation/utils.py
Connections 21
- Adult train → Training: configures · Adult training script wires the reusable training pipeline
- Adult predict → Predict: runs · Adult inference script uses pairwise prediction
- Foetal train → Training: configures · Foetal training script wires the reusable training pipeline
- Foetal predict → Time-series predict: runs · Foetal inference script uses longitudinal prediction
- Training → Loaders: reads data · Training starts from dataset ingestion and batching
- Training → Augmenters: augments · Training applies augmentation before optimisation
- Training → Networks: optimises · Training instantiates and updates the registration model
- Training → Losses: computes · Training combines image and spatial regularisation losses
- Training → Moments: measures · Training derives spatial statistics for regularisation
- Training → Utils: uses · Training shares helpers across the pipeline
- Predict → Loaders: reads data · Pairwise prediction consumes fixed/moving scan pairs
- Predict → Networks: infers · Pairwise prediction applies the trained model
- Predict → Utils: uses · Prediction reuses shared helpers
- Time-series predict → Loaders: reads data · Time-series prediction consumes longitudinal sequences
- Time-series predict → Networks: infers · Time-series prediction applies the trained model
- Time-series predict → Utils: uses · Time-series prediction reuses shared helpers
- Losses → Moments: depends on · Regularisation terms consume moment computations
- Networks → Physical U-Net: builds from · Network code relies on the geometry-aware U-Net block
- Package API → Networks: exposes · Package boundary makes model modules importable
- Package API → Training: exposes · Package boundary makes orchestration importable
- Package API → Predict: exposes · Package boundary makes pairwise inference importable