Skip to main content

A CUDA-based library for computed tomography (CT) projection and reconstruction with differentiable operators

Project description

DiffCT: Differentiable Computed Tomography Operators

License DOI PyPI version Ask DeepWiki

A high-performance, CUDA-accelerated library for circular orbits CT reconstruction with end-to-end differentiable operators, enabling advanced optimization and deep learning integration.

โญ Please star this project if you find it useful!

โœจ Features

  • Fast: CUDA-accelerated projection and backprojection operations
  • Differentiable: End-to-end gradient propagation for deep learning workflows

๐Ÿ“ Supported Geometries

  • Parallel Beam: 2D parallel-beam geometry
  • Fan Beam: 2D fan-beam geometry
  • Cone Beam: 3D cone-beam geometry

๐Ÿงฉ Code Structure

diffct/
โ”œโ”€โ”€ diffct/
โ”‚   โ”œโ”€โ”€ __init__.py            # Package initialization
โ”‚   โ”œโ”€โ”€ non_differentiable.py  # CUDA implementation
โ”‚   โ”œโ”€โ”€ differentiable.py      # Differentiable implementation
โ”œโ”€โ”€ examples/                  # Example usages
โ”‚   โ”œโ”€โ”€ non_differentiable     # Non-differentiable examples
โ”‚   โ”‚   โ”œโ”€โ”€ parallel.py        
โ”‚   โ”‚   โ”œโ”€โ”€ fan.py             
โ”‚   โ”‚   โ”œโ”€โ”€ cone.py            
โ”‚   โ”œโ”€โ”€ differentiable         # Differentiable examples
โ”‚   โ”‚   โ”œโ”€โ”€ parallel.py        
โ”‚   โ”‚   โ”œโ”€โ”€ fan.py             
โ”‚   โ”‚   โ”œโ”€โ”€ cone.py            
โ”œโ”€โ”€ pyproject.toml             # Project metadata
โ”œโ”€โ”€ README.md                  # README
โ”œโ”€โ”€ LICENSE                    # License
โ”œโ”€โ”€ requirements.txt           # Dependencies

๐Ÿš€ Quick Start

Prerequisites

  • CUDA-capable GPU
  • Python 3.10+
  • PyTorch, NumPy, Numba with CUDA support

Installation

# Create and activate environment
conda create -n diffct python=3.10
conda activate diffct

# Install CUDA support
conda install cudatoolkit

git clone https://github.com/sypsyp97/diffct.git
cd diffct

pip install -r requirements.txt
pip install diffct

๐Ÿ“š Usage Examples

Non-Differentiable CUDA Implementation

import torch
import numpy as np
from diffct.non_differentiable import forward_parallel_2d, back_parallel_2d

# Create phantom
phantom = shepp_logan_2d(256, 256)

# Configure geometry
angles = np.linspace(0, 2*np.pi, 360, endpoint=False)

# Forward projection
sinogram = forward_parallel_2d(
    phantom, 
    num_views=360,
    num_detectors=512, 
    detector_spacing=1.0, 
    angles=angles
)

# Reconstruction
sinogram_filtered = ramp_filter(torch.from_numpy(sinogram)).numpy()
reconstruction = back_parallel_2d(
    sinogram_filtered, 
    Nx=256, Ny=256,
    detector_spacing=1.0, 
    angles=angles
) / 360  # Normalize by number of angles

Differentiable Implementation

import torch
from diffct.differentiable import ParallelProjectorFunction, ParallelBackprojectorFunction

# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Create phantom tensor with gradient tracking
phantom = torch.tensor(shepp_logan_2d(256, 256), device=device, requires_grad=True)
angles = torch.linspace(0, 2*np.pi, 360, device=device)

# Forward projection
sinogram = ParallelProjectorFunction.apply(
    phantom, 
    angles, 
    num_detectors=512, 
    detector_spacing=1.0, 
)

# Filtered backprojection
sinogram_filtered = ramp_filter(sinogram).requires_grad_(True)
reconstruction = ParallelBackprojectorFunction.apply(
    sinogram_filtered,
    angles, 
    detector_spacing=1.0, 
    Nx=256, Ny=256
) / 360  # Normalize

# Compute loss and gradients
loss = torch.mean((reconstruction - phantom)**2)
loss.backward()  # Gradients flow through the entire pipeline

๐Ÿ“Š HU Calibration

  1. With Original Image:

    • Normalize image before forward projection
    • Apply inverse transformation to restore HU range
    • Consider histogram matching if needed
  2. With Sinogram Only:

    • Reconstruct to get raw values
    • Calibrate using reference points (air โ‰ˆ -1000 HU, water โ‰ˆ 0 HU)
    • Or use calibration markers with known attenuation coefficients

๐Ÿ“ Citation

If you use this library in your research, please cite:

@software{DiffCT2025,
  author       = {Yipeng Sun},
  title        = {DiffCT: Differentiable Computed Tomography 
                 Reconstruction with CUDA},
  year         = 2025,
  publisher    = {Zenodo},
  doi          = {10.5281/zenodo.14999333},
  url          = {https://doi.org/10.5281/zenodo.14999333}
}

๐Ÿ“„ License

This project is licensed under the Apache 2.0 - see the LICENSE file for details.

๐Ÿ™ Acknowledgements

This project was highly inspired by:

Issues and contributions are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffct-1.1.5.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diffct-1.1.5-py2.py3-none-any.whl (15.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file diffct-1.1.5.tar.gz.

File metadata

  • Download URL: diffct-1.1.5.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for diffct-1.1.5.tar.gz
Algorithm Hash digest
SHA256 48b4e32d266877f34b1a0afe31224973a6e10534df8824959c9a7191393893cb
MD5 e6eb30279aaa6b05071072a8f2865779
BLAKE2b-256 e901cd2dd6477bdf83ec03c46e7ea8b2da2a62a52acb26616500b6ee507d25d9

See more details on using hashes here.

File details

Details for the file diffct-1.1.5-py2.py3-none-any.whl.

File metadata

  • Download URL: diffct-1.1.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for diffct-1.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 14711b491f4304f7cf7aa9bfae11076c9817493669ee5c26e8dc1ac4a896410e
MD5 31aae83b813fcdec8165147175ff527c
BLAKE2b-256 722a7e95e48c09534e8ddd8436cc21f8de0d63275e3ea42699f41b4871827886

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page