Package: diffdrr
🆕For examples runningDiffDRRon real data, please check out our latest work,DiffPose.DiffDRRAuto-differentiable DRR synthesis and optimization in PyTorchDiffDRRis a PyTorch-based digitally reconstructed radiograph (DRR) generator that providesAuto-differentiable DRR syntheisisGPU-accelerated renderingA pure Python implementationMost importantly,DiffDRRimplements DRR synthesis as a PyTorch module, making it interoperable in deep learning pipelines.Below is a comparison ofDiffDRRto a real X-ray (X-rays and CTs from theDeepFluoro dataset):Installation GuideTo installDiffDRRfrom PyPI:pipinstalldiffdrrUsageThe following minimal example specifies the geometry of the projectional radiograph imaging system and traces rays through a CT volume:importmatplotlib.pyplotaspltimporttorchfromdiffdrr.drrimportDRRfromdiffdrr.dataimportload_example_ctfromdiffdrr.visualizationimportplot_drr# Read in the volume and get the isocentervolume,spacing=load_example_ct()bx,by,bz=torch.tensor(volume.shape)*torch.tensor(spacing)/2# Initialize the DRR module for generating synthetic X-raysdevice="cuda"iftorch.cuda.is_available()else"cpu"drr=DRR(volume,# The CT volume as a numpy arrayspacing,# Voxel dimensions of the CTsdr=300.0,# Source-to-detector radius (half of the source-to-detector distance)height=200,# Height of the DRR (if width is not seperately provided, the generated image is square)delx=4.0,# Pixel spacing (in mm)).to(device)# Set the camera pose with rotation (yaw, pitch, roll) and translation (x, y, z)rotation=torch.tensor([[torch.pi,0.0,torch.pi/2]],device=device)translation=torch.tensor([[bx,by,bz]],device=device)# 📸 Also note that DiffDRR can take many representations of SO(3) 📸# For example, quaternions, rotation matrix, axis-angle, etc...img=drr(rotation,translation,parameterization="euler_angles",convention="ZYX")plot_drr(img,ticks=False)plt.show()On a single NVIDIA RTX 2080 Ti GPU, producing such an image takes33.6 ms ± 27.8 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)The full example is available atintroduction.ipynb.2D/3D RegistrationWe demonstrate the utility of our auto-differentiable DRR generator by
solving the 2D/3D registration problem with gradient-based optimization.
Here, we generate two DRRs:A fixed DRR from a set of ground truth parametersA moving DRR from randomly initialized parametersTo solve the registration problem, we use gradient descent to maximize
an image loss similarity metric between the two DRRs. This produces
optimization runs like this:The full example is available atoptimizers.ipynb.DevelopmentDiffDRRsource code, docs, and CI are all built usingnbdev. To get set up withnbdev, install
the followingmambainstalljupyterlabnbdev-cfastai-cconda-forgenbdev_install_quarto# To build docsnbdev_install_hooks# Make notebooks git-friendlyRunningnbdev_helpwill give you the full list of options. The most
important ones arenbdev_preview# Render docs locally and inspect in browsernbdev_clean# NECESSARY BEFORE PUSHINGnbdev_test# tests notebooksnbdev_export# builds package and builds docsFor more details, follow thisin-depth
tutorial.How doesDiffDRRwork?DiffDRRreformulates Siddon’s method,[^1] the
canonical algorithm for calculating the radiologic path of an X-ray
through a volume, as a series of vectorized tensor operations. This
version of the algorithm is easily implemented in tensor algebra
libraries like PyTorch to achieve a fast auto-differentiable DRR
generator.[^1]:Siddon RL. Fast calculation of
the exact radiological path for a three-dimensional CT array. Medical
Physics, 2(12):252–5, 1985.CitingDiffDRRIf you findDiffDRRuseful in your work, please cite ourpaper(or thefreely
accessible arXiv version):@inproceedings{gopalakrishnanDiffDRR2022,
    author    = {Gopalakrishnan, Vivek and Golland, Polina},
    title     = {Fast Auto-Differentiable Digitally Reconstructed Radiographs for Solving Inverse Problems in Intraoperative Imaging},
    year      = {2022},
    booktitle = {Clinical Image-based Procedures: 11th International Workshop, CLIP 2022, Held in Conjunction with MICCAI 2022, Singapore, Proceedings},
    series    = {Lecture Notes in Computer Science},
    publisher = {Springer},
    doi       = {https://doi.org/10.1007/978-3-031-23179-7_1},
}