Skip to main content

A differentiable implementation of scikit-image's Radon transform and filtered backprojection in PyTorch

Project description

torchskradon

LICENSE Python PyPI CI Coverage

About

torchskradon mimics the implementation of radon and iradon from scikit-image. All transforms work with batched multi-channel data and are fully differentiable. This allows backpropagation through torchskradon functions to train neural networks or solve optimization problems with torch.optim (see examples).

Installation

pip install torchskradon

Basic Usage

import torch
from skimage.data import shepp_logan_phantom
from skimage.transform import rescale
from torchskradon.functional import skradon, skiradon

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

image = shepp_logan_phantom()
image = rescale(image, scale=0.4, mode='reflect', channel_axis=None)
image = torch.from_numpy(image).unsqueeze(0).unsqueeze(0).to(device)
theta = torch.linspace(0.0, 180.0, max(image.size()[2:])+1)[:-1].to(device)
sinogram = skradon(image, theta=theta)
reconstruction_fbp = skiradon(sinogram, theta=theta, filter_name='ramp')

Examples

For more detailed examples and use cases, see the examples directory:

Accuracy on MNIST

----------------------- Absolute Error of Inverse Radon Transform on MNIST test dataset: 2 tests -----------------------
Name                             Min           Max          Mean        StdDev        Median           IQR      Outliers
------------------------------------------------------------------------------------------------------------------------
torchskradon (CPU)        0.0000e+00    2.2054e-06    3.1475e-07    2.1740e-07    2.5937e-07    2.6217e-07 2159268;272232
torchskradon (GPU)        0.0000e+00    1.9670e-06    1.4094e-07    1.2985e-07    1.0803e-07    1.2759e-07 1415341;427665
------------------------------------------------------------------------------------------------------------------------

--------------------------- Absolute Error of Radon Transform on MNIST test dataset: 2 tests ---------------------------
Name                             Min           Max          Mean        StdDev        Median           IQR      Outliers
------------------------------------------------------------------------------------------------------------------------
torchskradon (CPU)        0.0000e+00    6.2466e-05    1.0514e-06    1.9718e-06    0.0000e+00    1.4305e-06 7713886;6355122
torchskradon (GPU)        0.0000e+00    6.1989e-05    1.0435e-06    1.9558e-06    0.0000e+00    1.4305e-06 7633118;6271687
------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.

Performance on MNIST

--------------------------------------------------------- benchmark 'Inverse Radon Transform on MNIST test dataset': 3 tests --------------------------------------------------------
Name (time in s)           Min                Max               Mean            StdDev             Median               IQR            Outliers     OPS            Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
skimage (CPU)          16.8473 (157.73)   16.8693 (130.60)   16.8537 (150.81)   0.0091 (1.06)     16.8511 (155.21)   0.0096 (4.75)          1;0  0.0593 (0.01)          5           1
torchskradon (CPU)      4.8955 (45.83)     5.2628 (40.75)     4.9870 (44.63)    0.1554 (18.10)     4.9192 (45.31)    0.1215 (59.92)         1;1  0.2005 (0.02)          5           1
torchskradon (GPU)      0.1068 (1.0)       0.1292 (1.0)       0.1118 (1.0)      0.0086 (1.0)       0.1086 (1.0)      0.0020 (1.0)           1;1  8.9483 (1.0)           6           1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------- benchmark 'Radon Transform on MNIST test dataset': 3 tests ------------------------------------------------------------
Name (time in s)           Min                Max               Mean            StdDev             Median               IQR            Outliers     OPS            Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
skimage (CPU)          36.6816 (78.12)    37.3837 (78.01)    36.8518 (77.68)    0.2996 (79.20)    36.7269 (77.48)    0.2385 (41.00)         1;1  0.0271 (0.01)          5           1
torchskradon (CPU)     10.4956 (22.35)    10.6328 (22.19)    10.5554 (22.25)    0.0532 (14.07)    10.5572 (22.27)    0.0763 (13.12)         2;0  0.0947 (0.04)          5           1
torchskradon (GPU)      0.4695 (1.0)       0.4792 (1.0)       0.4744 (1.0)      0.0038 (1.0)       0.4740 (1.0)      0.0058 (1.0)           2;0  2.1080 (1.0)           5           1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean

You can run benchmarks yourself by running:

  python -m pytest --benchmark-only --benchmark-sort="name"

Other Packages

For users interested in more flexible implementations of projection transforms check out:

  1. ASTRA Toolbox

  2. torch-radon

Acknowledgements

This package is inspired by implementations of the Radon transform and its' inverse in skimage.transform, which are based on [1-3].

References

  1. JK Romberg, "Image Projections and the Radon Transform"

  2. AC Kak, M Slaney, “Principles of Computerized Tomographic Imaging”, IEEE Press 1988.

  3. B.R. Ramesh, N. Srinivasa, K. Rajgopal, “An Algorithm for Computing the Discrete Radon Transform With Some Applications”, Proceedings of the Fourth IEEE Region 10 International Conference, TENCON ‘89, 1989

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

torchskradon-0.1.2.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

torchskradon-0.1.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file torchskradon-0.1.2.tar.gz.

File metadata

  • Download URL: torchskradon-0.1.2.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchskradon-0.1.2.tar.gz
Algorithm Hash digest
SHA256 26ed4790300e5c4335148fc654b79822e031f3577cdb2b17d3a43230beede80c
MD5 d65f71bb2edd3bd23051f18724e74b4d
BLAKE2b-256 3ac89dd9840255333e7cd33f08ae4467405e217ceed515628242efbcf554d37b

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchskradon-0.1.2.tar.gz:

Publisher: python-publish.yml on tomluetjen/torchskradon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchskradon-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: torchskradon-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchskradon-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 29670f006276b1dbfe886182c930377c98cb705e977dacc2b77cec71284a7303
MD5 db45be44b0bb6dcb5454c3e69413ad61
BLAKE2b-256 2d8696776ce8e594ea1889e93263d8668cfefc6b25709071d9f7fcc3c93977ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchskradon-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on tomluetjen/torchskradon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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