Skip to main content

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

Release files for torchreg 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchreg 0.1.3
File Size Uploaded
torchreg-0.1.3.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchreg 0.1.3
File Interpreter ABI Platform
torchreg-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / torchreg-0.1.3.tar.gz

Download URL torchreg-0.1.3.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3e1b5fb9a0ae86ffa9232db655645412c884e7cd8378701cd50f9570a8cd0301
BLAKE2b-256 checksum
How to use checksums
9a78024d4a80ec67c2008e5fba4b10915a56d3497e55148c3c0b7e22bbce1a76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-21-generic

Release files / torchreg-0.1.3-py3-none-any.whl

Download URL torchreg-0.1.3-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2580150566026400a086c1043bbdccc808996ed86bafd7a23a0610e3193693b
BLAKE2b-256 checksum
How to use checksums
09b41c443f97cf1bc4c1ea96b605160215ec15c2b475997f712da76091e84b2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-21-generic

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page