Skip to main content

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

Project description

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.

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

torchfields-0.1.2.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

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

torchfields-0.1.2-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torchfields-0.1.2.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for torchfields-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fc4ddbe93c2a643017f732132760750f0f86fd953bf2a77032e5baa18954986f
MD5 881036a7d676792d9ea812938865a678
BLAKE2b-256 b03169458c840ce7b1dc3ea576a12b8ad21b081417334ac3e0c2af492db75b55

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchfields-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for torchfields-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 448219bd37cbbd44975cdc835812a9fa1df6ec302e7d120267b18f50942cee18
MD5 cdf14574a9b6b2cf2c3592fd5fd153cf
BLAKE2b-256 71e8d26fe11b4349d2be10e45527a4e9523f0b88755556a0f1fa1e8fd590e991

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