diffdrr
Auto-differentiable digitally reconstructed radiographs in PyTorch
Science Score: 59.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 2 DOI reference(s) in README -
✓Academic publication links
Links to: arxiv.org -
✓Committers with academic emails
2 of 6 committers (33.3%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.6%) to scientific vocabulary
Keywords
Repository
Auto-differentiable digitally reconstructed radiographs in PyTorch
Basic Info
- Host: GitHub
- Owner: eigenvivek
- License: mit
- Language: Python
- Default Branch: main
- Homepage: https://vivekg.dev/DiffDRR
- Size: 2.82 GB
Statistics
- Stars: 209
- Watchers: 4
- Forks: 37
- Open Issues: 3
- Releases: 29
Topics
Metadata Files
README.md
DiffDRR
Auto-differentiable DRR rendering and optimization in PyTorch
DiffDRR is a PyTorch-based digitally reconstructed radiograph (DRR) generator that provides
- Differentiable X-ray rendering
- GPU-accelerated synthesis and optimization
- A pure Python implementation
Most importantly, DiffDRR implements DRR rendering as a PyTorch module, making it interoperable in deep learning pipelines.
Install
To install the latest stable release (recommended):
zsh
pip install diffdrr
To install the development version:
zsh
git clone https://github.com/eigenvivek/DiffDRR.git --depth 1
pip install -e 'DiffDRR/[dev]'
Hello, World!
The following minimal example specifies the geometry of the projectional radiograph imaging system and traces rays through a CT volume:
``` python import matplotlib.pyplot as plt import torch
from diffdrr.drr import DRR from diffdrr.data import loadexamplect from diffdrr.visualization import plot_drr
Read in the volume and get its origin and spacing in world coordinates
subject = loadexamplect()
Initialize the DRR module for generating synthetic X-rays
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") drr = DRR( subject, # An object storing the CT volume, origin, and voxel spacing sdd=1020.0, # Source-to-detector distance (i.e., focal length) height=200, # Image height (if width is not provided, the generated DRR is square) delx=2.0, # Pixel spacing (in mm) ).to(device)
Set the camera pose with rotations (yaw, pitch, roll) and translations (x, y, z)
rotations = torch.tensor([[0.0, 0.0, 0.0]], device=device) translations = torch.tensor([[0.0, 850.0, 0.0]], device=device)
Also note that DiffDRR can take many representations of SO(3)
For example, quaternions, rotation matrix, axis-angle, etc...
img = drr(rotations, translations, parameterization="eulerangles", convention="ZXY") plotdrr(img, ticks=False) plt.show() ```

On a single NVIDIA RTX 2080 Ti GPU, producing such an image takes
25.2 ms 10.5 s per loop (mean std. dev. of 7 runs, 10 loops each)
The full example is available at
introduction.ipynb.
Usage
Rendering
The physics-based pipeline in DiffDRR renders photorealistic X-rays. For example, compare
a real X-ray to a synthetic X-ray rendered from a CT of the same patient using DiffDRR
(X-rays and CTs from the DeepFluoro dataset):

2D/3D Registration
The impotus for developing DiffDRR was to solve 2D/3D registration
problems with gradient-based optimization. Here, we demonstrate DiffDRR's
capabilities by generating two DRRs:
- A fixed DRR from a set of ground truth parameters
- A moving DRR from randomly initialized parameters
To align the two images, we use gradient descent to maximize an image similarity metric between the two DRRs. This produces optimization runs like this:

The full example is available at
optimizers.ipynb.
* Examples on Real-World Data *
For examples running DiffDRR on real surgical datasets, check out our latest work, DiffPose:

This work includes a lot of real-world usecases of DiffDRR including
- Using DiffDRR as a layer in a deep learning architecture
- Alignment of real X-rays and rendered DRRs
- Achieving sub-millimeter registration accuracy very quickly
X-ray Segmentation
DiffDRR can project 3D labelmaps into 2D simply using perspective geometry, helping identify particular structures in simulated X-rays
(these labels come from the TotalSegmentator v2 dataset):

Volume Reconstruction
DiffDRR is differentiable with respect to the 3D volume as well as camera poses.
Therefore, it can be used for volume reconstruction via differentiable
rendering (see reconstruction.ipynb)!
* Examples on Real-World Data *
For an in-depth example using DiffDRR for cone-beam CT reconstruction, check out DiffVox.
Development
TLDR: Source code is stored in
notebooks/api/, notdiffdrr/. Update the notebooks instead!
DiffDRR source code, docs, and CI are all built using
nbdev. To get set up with nbdev, install
the following
zsh
mamba install jupyterlab nbdev -c fastai -c conda-forge
nbdev_install_quarto # To build docs
nbdev_install_hooks # Make notebooks git-friendly
Running nbdev_help will give you the full list of options. The most
important ones are
zsh
nbdev_preview # Render docs locally and inspect in browser
nbdev_clean # NECESSARY BEFORE PUSHING
nbdev_test # tests notebooks
nbdev_export # builds package and builds docs
For more details, follow this in-depth tutorial.
How does DiffDRR work?
DiffDRR reformulates Siddons method,[^1] an exact
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.
Citing DiffDRR
If you find DiffDRR useful in your work, please cite our
paper:
@inproceedings{gopalakrishnan2022fast,
title={Fast auto-differentiable digitally reconstructed radiographs for solving inverse problems in intraoperative imaging},
author={Gopalakrishnan, Vivek and Golland, Polina},
booktitle={Workshop on Clinical Image-Based Procedures},
pages={1--11},
year={2022},
organization={Springer}
}
If the 2D/3D registration capabilities are helpful, please cite our followup, DiffPose:
@article{gopalakrishnan2023intraoperative,
title={Intraoperative {2D/3D} image registration via differentiable {X}-ray rendering},
author={Gopalakrishnan, Vivek and Dey, Neel and Golland, Polina},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={11662--11672},
year={2024}
}
If you use the 3D CBCT reconstruction capabilities, please cite our followup, DiffVox:
@article{momeni2024voxel,
title={Voxel-based Differentiable X-ray Rendering Improves Self-Supervised 3D CBCT Reconstruction},
author={Momeni, Mohammadhossein and Gopalakrishnan, Vivek and Dey, Neel and Golland, Polina and Frisken, Sarah},
booktitle={Machine Learning and the Physical Sciences, NeurIPS 2024},
year={2024}
}
Owner
- Name: Vivek Gopalakrishnan
- Login: eigenvivek
- Kind: user
- Location: Massachusetts Institute of Technology
- Website: https://vivekg.dev
- Repositories: 57
- Profile: https://github.com/eigenvivek
PhD candidate at MIT
GitHub Events
Total
- Create event: 29
- Issues event: 51
- Release event: 7
- Watch event: 60
- Delete event: 25
- Issue comment event: 84
- Push event: 54
- Pull request review event: 1
- Pull request event: 39
- Fork event: 17
Last Year
- Create event: 29
- Issues event: 51
- Release event: 7
- Watch event: 60
- Delete event: 25
- Issue comment event: 84
- Push event: 54
- Pull request review event: 1
- Pull request event: 39
- Fork event: 17
Committers
Last synced: 11 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| eigenvivek | v****g@m****u | 752 |
| haarisr | 1****r | 4 |
| Vivek Gopalakrishnan | v****4@j****u | 3 |
| Lukas Nickel | l****6@g****m | 1 |
| Hossein Momeni | 9****h | 1 |
| François Lecomte | 6****f | 1 |
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 184
- Total pull requests: 153
- Average time to close issues: about 1 month
- Average time to close pull requests: 2 days
- Total issue authors: 47
- Total pull request authors: 7
- Average comments per issue: 1.79
- Average comments per pull request: 0.31
- Merged pull requests: 142
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 28
- Pull requests: 31
- Average time to close issues: 13 days
- Average time to close pull requests: about 4 hours
- Issue authors: 22
- Pull request authors: 2
- Average comments per issue: 2.46
- Average comments per pull request: 0.16
- Merged pull requests: 29
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- eigenvivek (106)
- shenao1994 (12)
- haarisr (4)
- LukasNickel (3)
- linquanxu (3)
- fedeface98 (2)
- equamo (2)
- Alookima21 (2)
- BraveDistribution (2)
- khh656 (2)
- asjeff (2)
- gmedan (2)
- charlesalec (2)
- jeanRassaire (2)
- MarkoLeskovar (2)
Pull Request Authors
- eigenvivek (205)
- haarisr (7)
- coolteemf (2)
- johannesu (2)
- TanveshT (2)
- LukasNickel (2)
- momen-mh (2)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/checkout v3 composite
- mamba-org/setup-micromamba v1 composite
- peaceiris/actions-gh-pages v3 composite
- actions/checkout v3 composite
- mamba-org/setup-micromamba v1 composite
- pyvista *