Skip to main content

Lightweight image registration library using PyTorch

Project description

torchreg

torchreg is a tiny (~300 lines) PyTorch-based library for 2D and 3D image registration.

Usage

Affine Registration of two image tensors is done via:

from torchreg import AffineRegistration

# Load images as torch Tensors
big_alice = ...    # Tensor with shape [1, 3 (color channel), 1024 (pixel), 1024 (pixel)]
small_alice = ...  # Tensor with shape [1, 3 (color channel), 1024 (pixel), 1024 (pixel)]
# Intialize AffineRegistration
reg = AffineRegistration(is_3d=False)
# Run it!
moved_alice = reg(big_alice, small_alice)

Features

Multiresolution approach to save compute (per default 1/4 + 1/2 of original resolution for 500 + 100 iterations)

reg = AffineRegistration(scales=(4, 2), iterations=(500, 100))

Choosing which operations (translation, rotation, zoom, shear) to optimize

reg = AffineRegistration(with_zoom=False, with_shear=False)

Custom initial parameters

reg = AffineRegistration(zoom=torch.Tensor([[1.5, 2.]]))

Custom dissimilarity functions and optimizers

def dice_loss(x1, x2):
    dim = [2, 3, 4] if len(x2.shape) == 5 else [2, 3]
    inter = torch.sum(x1 * x2, dim=dim)
    union = torch.sum(x1 + x2, dim=dim)
    return 1 - (2. * inter / union).mean()

reg = AffineRegistration(dissimilarity_function=dice_loss, optimizer=torch.optim.Adam)

CUDA support (NVIDIA GPU)

moved_alice = reg(moving=big_alice.cuda(), static=small_alice.cuda())

After the registration is run, you can apply it to new images (coregistration)

another_moved_alice = reg.transform(another_alice, shape=(256, 256))

with desired output shape.

You can access the affine

affine = reg.get_affine()

and the four parameters (translation, rotation, zoom, shear)

translation = reg.parameters[0]
rotation = reg.parameters[1]
zoom = reg.parameters[2]
shear = reg.parameters[3]

Installation

pip install torchreg

Examples/Tutorials

There are three example notebooks:

Background

If you want to know how the core of this package works, read the blog post!

TODO

  • Add 2D support to SyN, NCC and LinearElasticity
  • Add tests for SyN

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

torchreg-0.1.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

torchreg-0.1.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file torchreg-0.1.3.tar.gz.

File metadata

  • Download URL: torchreg-0.1.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-21-generic

File hashes

Hashes for torchreg-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3e1b5fb9a0ae86ffa9232db655645412c884e7cd8378701cd50f9570a8cd0301
MD5 443cd657910416516f990a2c2aa36e25
BLAKE2b-256 9a78024d4a80ec67c2008e5fba4b10915a56d3497e55148c3c0b7e22bbce1a76

See more details on using hashes here.

File details

Details for the file torchreg-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: torchreg-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-21-generic

File hashes

Hashes for torchreg-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e2580150566026400a086c1043bbdccc808996ed86bafd7a23a0610e3193693b
MD5 b26fd45c6dbf38c8b5c92e86c3c2905f
BLAKE2b-256 09b41c443f97cf1bc4c1ea96b605160215ec15c2b475997f712da76091e84b2a

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