Skip to content

ahendriksen/tomosipo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

img

Tomosipo

Anaconda-Server Badge Anaconda-Server Badge License: GPL v3

Tomosipo is a pythonic wrapper for the ASTRA-toolbox of high-performance GPU primitives for 3D tomography.

The aim of this library is to:

  • Expose a user-friendly API for high-performance 3D tomography, while allowing strict control over resource usage
    • The ts_algorithms library contains implementations of reconstruction algorithms using Tomosipo
  • Enable easy manipulation and visualization of 3D geometries
  • Provide easy integration with

The documentation can be found here. An introduction and demonstration of tomosipo was published in Optics Express.

  1. Installation
  2. Usage
    1. Create and visualize geometries
    2. Express algorithms succinctly
    3. More examples
  3. Authors and contributors

Citing tomosipo

If you use tomosipo in scientific publications, we would appreciate citations of our paper using the following Bibtex entry:

@Article{hendriksen-2021-tomos,
  author          = {Hendriksen, Allard and Schut, Dirk and Palenstijn, Willem
                  Jan and Viganò, Nicola and Kim, Jisoo and Pelt, Dani{\"e}l and
                  van Leeuwen, Tristan and Batenburg, K. Joost},
  title           = {Tomosipo: Fast, Flexible, and Convenient {3D} Tomography for
                  Complex Scanning Geometries in {Python}},
  journal         = {Optics Express},
  year            = 2021,
  doi             = {10.1364/oe.439909},
  url             = {https://doi.org/10.1364/oe.439909},
  issn            = {1094-4087},
  month           = {Oct},
  publisher       = {The Optical Society},
}

Installation

A minimal installation requires:

  • python >= 3.6
  • ASTRA-toolbox >= 2.0
  • CUDA

The requirements can be installed using the anaconda package manager. The following snippet creates a new conda environment named tomosipo (replace X.X by your CUDA version)

conda create -n tomosipo cudatoolkit=<X.X> tomosipo -c astra-toolbox -c aahendriksen -c defaults

An installation with Pytorch and ts_algorithms can be created with the following snippet

conda create -n tomosipo tomosipo pytorch==2.0.1 pytorch-cuda=11.7 tqdm -c pytorch -c nvidia -c astra-toolbox/label/dev -c aahendriksen -c defaults

conda activate tomosipo
pip install git+https://github.com/ahendriksen/ts_algorithms.git

From PyTorch version 2 the cuda toolkit dependencies have changed from the cudatoolkit package to the pytorch-cuda package. The development version of Astra uses the cuda-cudart and libcufft packages which are automatically included by installing pytorch-cuda.

More information about installation is provided in the documentation.

Usage

Simple examples:

Create and visualize geometries

You can follow along on Google Colab.

import astra
import numpy as np
import tomosipo as ts

# Create 'unit' cone geometry
pg = ts.cone(angles=20, size=np.sqrt(2), cone_angle=0.5)
print(pg)

# Create volume geometry of a unit cube on the origin
vg = ts.volume()
print(vg)

# Display an animation of the acquisition geometry
ts.svg(pg, vg)

Express algorithms succinctly

In the following example, we implement the simultaneous iterative reconstruction algorithm (SIRT) in a couple of lines. This examples demonstrates the use of the forward and backward projection.

First, the SIRT algorithm is implemented using numpy arrays, which reside in system RAM. Then, we move all data onto the GPU, and compute the same algorithm using PyTorch. This is faster, because no transfers between system RAM and GPU are necessary.

import astra
import numpy as np
import tomosipo as ts
from timeit import default_timer as timer

# Create 'unit' cone geometry, and a
pg = ts.cone(size=np.sqrt(2), cone_angle=1/2, angles=100, shape=(128, 192))
# Create volume geometry of a unit cube on the origin
vg = ts.volume(shape=128)
# Create projection operator
A = ts.operator(vg, pg)

# Create a phantom containing a small cube:
phantom = np.zeros(A.domain_shape)
phantom[20:50, 20:50, 20:50] = 1.0

# Prepare preconditioning matrices R and C
R = 1 / A(np.ones(A.domain_shape))
R = np.minimum(R, 1 / ts.epsilon)
C = 1 / A.T(np.ones(A.range_shape))
C = np.minimum(C, 1 / ts.epsilon)

# Reconstruct from sinogram y into x_rec in 100 iterations
y = A(phantom)
x_rec = np.zeros(A.domain_shape)
num_iters = 100

start = timer()
for i in range(num_iters):
    x_rec += C * A.T(R * (y - A(x_rec)))
print(f"SIRT finished in {timer() - start:0.2f} seconds")

# Perform the same computation on the GPU using PyTorch.
# First, import pytorch:
import torch

# Move all data to GPU:
dev = torch.device("cuda")
y = torch.from_numpy(y).to(dev)
R = torch.from_numpy(R).to(dev)
C = torch.from_numpy(C).to(dev)
x_rec = torch.zeros(A.domain_shape, device=dev)

# Perform algorithm
start = timer()
for i in range(num_iters):
    x_rec += C * A.T(R * (y - A(x_rec)))

# Convert reconstruction back to numpy array:
x_rec = x_rec.cpu().numpy()
print(f"SIRT finished in {timer() - start:0.2f} seconds using PyTorch")
SIRT finished in 2.07 seconds
SIRT finished in 0.94 seconds using PyTorch

A similar implementation of SIRT and succinct implementations of some other reconstruction algorithms are available in the ts_algorithms library.

More examples

Please checkout the examples and notebooks directory for more examples.

Authors and contributors

tomosipo is developed by the Computational Imaging group at CWI.

Current maintainer:

  • Dirk Schut

Original author:

  • Allard Hendriksen

We thank the following authors for their contribution

  • Johannes Leuschner - ODL integration

See also the list of contributors who participated in this project.