Skip to main content

torchfields

A PyTorch add-on for working with image mappings and displacement fields, including Spatial Transformers

Torchfields provides an abstraction that neatly encapsulates the functionality of displacement fields as used in Spatial Transformer Networks and Optical Flow Estimation.

Fields can be treated as normal PyTorch tensors for most purposes, and also include additional functionality for composing displacements and sampling from tensors.

Installation

To install torchfields simply do

pip install torchfields

Introduction

A displacement field represents a mapping or flow that indicates how an image should be warped.

It is essentially a spatial tensor containing displacement vectors at each pixel, where each displacement vector indicates the displacement distance and direction at that pixel.

Displacement field conventions

Units

The standard unit of displacement is a half-image, so a displacement vector of magnitude 2 means that the displacement distance is equal to the side length of the displaced image.

Note: This convention originates from the original Spatial Transformer Networks paper where such fields were presented as mappings, with -1 representing the left or top edge of the image, and +1 representing the right or bottom edge.

torchfields also supports seamlessly converting to and from units of pixels using the pixels() and from_pixels() functions.

Displacement direction

The most common way to warp an image by a displacement field is by sampling from it at the points pointed to by the field vectors. This is often referred to as the Eulerian or pull convention, since the vectors in the field point to the locations from which the image should be pulled. This is achieved by calling the sample() function (which in fact wraps PyTorch's built-in grid_sample(), while converting the conventions as necessary).

An alternative way to warp an image by a displacement field is by sending each pixel of the image along the corresponding displacement vector to its new location. This is referred to as the Lagrangian or push convention, since the vectors of the field indicate where an image pixel should be pushed to. This direction, while seemingly intuitive, is much less straight-forward to implement, since there is no definitive way to handle the discretization (for instance, what to do when the destinations are not whole pixel coordinates, when two sources map to the same destination, and when nothing maps into a destination pixel). The solution for warping in the Lagrangian direction is to first invert the field using inverse(), and then warp the image normally using sample().

To read more about the two ways to describe flow fields, see the Wikipedia article on the subject.

Relationship to PyTorch tensors

Displacement fields inherit from torch.Tensor, so all functionality from PyTorch tensors also works with displacement fields. That is, any PyTorch function that accepts a torch.Tensor type will also implicitly accept a torchfields displacement field.

Furthermore, the module installs itself (through monkey patching) as

torch.Field

mirroring the torch.Tensor module, and all the functionality of the torchfields package can be conveniently accessed through that shortcut. This shortcut gets activated at the first import (using import torchfields).

Note, however, that the torchfields package is neither endorsed by nor maintained by the PyTorch developer community, and is instead a separate project maintained by researchers at Princeton University.

Tutorial

To learn more and get started with using torchfields check out the tutorial.

Metadata

Release files for torchfields 0.1.2

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

Source distribution (sdist)

Source distribution for torchfields 0.1.2
File Size Uploaded
torchfields-0.1.2.tar.gz 24.9 kB Details

Built distribution (wheel)

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

Total release size: 48.8 kB

Release files / torchfields-0.1.2.tar.gz

Download URL torchfields-0.1.2.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fc4ddbe93c2a643017f732132760750f0f86fd953bf2a77032e5baa18954986f
BLAKE2b-256 checksum
How to use checksums
b03169458c840ce7b1dc3ea576a12b8ad21b081417334ac3e0c2af492db75b55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/30.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.4 tqdm/4.56.0 importlib-metadata/4.8.1 keyring/23.2.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.10

Release files / torchfields-0.1.2-py3-none-any.whl

Download URL torchfields-0.1.2-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
448219bd37cbbd44975cdc835812a9fa1df6ec302e7d120267b18f50942cee18
BLAKE2b-256 checksum
How to use checksums
71e8d26fe11b4349d2be10e45527a4e9523f0b88755556a0f1fa1e8fd590e991
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/30.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.4 tqdm/4.56.0 importlib-metadata/4.8.1 keyring/23.2.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

1 release file

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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