Skip to main content

Differentiable optical flow library using a custom flow class based on PyTorch tensors

Project description

Oflibpytorch: a handy python optical flow library, based on PyTorch tensors, that enables the manipulation and combination of flow fields while keeping track of valid areas (see “Usage”) in the context of machine learning algorithms implemented in PyTorch. It is mostly code written from scratch, but also contains useful wrappers for specific functions from libraries such as PyTorch’s grid_sample, to integrate them with the custom flow field class introduced by oflibpytorch. Importantly, the main methods are differentiable with respect to their flow field tensor inputs, allowing for a seamless integration with machine learning algorithms. Features:

  • All main methods that return a float tensor are differentiable with respect to tensor inputs

  • A custom flow field class for both backward and forward (‘target’ / ‘source’ based) flow fields, handled as tensors with a batch dimension, allowing for efficient batch-wise processing that can be performed on GPU if desired

  • A number of class methods to create flow fields from lists of affine transforms, or a transformation matrix, as well as methods to resize the flow field, visualise it, warp images, or find necessary image padding, all while keeping track of valid flow field areas

  • A class method to process three different types of flow field combination operations

  • Utility functions including a PyTorch-based approximate interpolation to a grid from unstructured data as a replacement for the SciPy griddata method

Oflibpytorch is based on oflibnumpy (code on GitHub, documentation on ReadTheDocs) and is aimed at allowing the same operations to be performed with torch tensors instead of numpy arrays as far as currently feasible.

Usage & Documentation

A user’s guide as well as full documentation of the library is available at ReadTheDocs. Some quick examples:

import oflibpytorch as of

shape = (300, 400)
transform = [['rotation', 200, 150, -30]]

# Make a flow field and display it
flow = of.Flow.from_transforms(transform, shape)
flow.show()

# Alternative option without using the custom flow class
flow = of.from_transforms(transform, shape, 't')
of.show_flow(flow)
https://raw.githubusercontent.com/RViMLab/oflibpytorch/main/docs/_static/index_flow_1.png

Above: Visualisation of optical flow representing a rotation

# Combine sequentially with another flow field, display the result
flow_2 = of.Flow.from_transforms([['translation', 40, 0]], shape)
result = of.combine_flows(flow, flow_2, mode=3)
result.show(show_mask=True, show_mask_borders=True)

# Alternative option without using the custom flow class
flow_2 = of.from_transforms([['translation', 40, 0]], shape, 't')
result = of.combine_flows(flow, flow_2, mode=3, ref='t')
of.show_flow(result)  # Note: no way to show the valid flow area (see documentation)
https://raw.githubusercontent.com/RViMLab/oflibpytorch/main/docs/_static/index_result.png

Above: Visualisation of optical flow representing a rotation, translated to the right, using the custom flow class

result.show_arrows(show_mask=True, show_mask_borders=True)

# Alternative option without using the custom flow class
of.show_flow_arrows(result, 't')  # Note: again no way to show the valid flow area
https://raw.githubusercontent.com/RViMLab/oflibpytorch/main/docs/_static/index_result_arrows.png

Above: Visualisation of optical flow representing a rotation, translated to the right, using the custom flow class

Installation

In order for oflibpytorch to work, the python environment needs to contain a PyTorch installation. To enable GPU usage, the CUDA Toolkit is required as well. As it is difficult to guarantee an automatic installation via pip will use the correct versions and work on all operating systems, it is left to the user to install PyTorch and Cudatoolkit independently. The easiest route is a virtual conda environment and the recommended install command from the PyTorch website, configured for the user’s specific system. To install oflibpytorch itself, use the following command:

pip install oflibpytorch

Testing

Oflibpytorch contains a large number of tests to verify it is working as intended. Use the command line to navigate to oflibpytorch/test and run the following code:

python -m unittest discover .

The tests will take several minutes to run. Successful completion will be marked with OK.

Contribution & Support

License

Copyright (c) 2022 Claudio S. Ravasio, PhD student at University College London (UCL), research assistant at King’s College London (KCL), supervised by:

  • Dr Christos Bergeles, PI of the Robotics and Vision in Medicine (RViM) lab in the School of Biomedical Engineering & Imaging Sciences (BMEIS) at King’s College London (KCL)

  • Prof Lyndon Da Cruz, consultant ophthalmic surgeon, Moorfields Eye Hospital, London UK

This code is licensed under the MIT License.

If you use this code, please acknowledge us with the following citation:

@article{ravasio_oflib,
  title     = {oflibnumpy {\&} oflibpytorch: Optical Flow Handling and Manipulation in Python},
  author    = {Ravasio, Claudio S. and Da Cruz, Lyndon and Bergeles, Christos},
  journal   = {Journal of Open Research Software (JORS)},
  year      = {2021},
  volume    = {9},
  publisher = {Ubiquity Press, Ltd.},
  doi       = {10.5334/jors.380}
}

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

oflibpytorch-2.1.1.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

oflibpytorch-2.1.1-py3-none-any.whl (51.6 kB view details)

Uploaded Python 3

File details

Details for the file oflibpytorch-2.1.1.tar.gz.

File metadata

  • Download URL: oflibpytorch-2.1.1.tar.gz
  • Upload date:
  • Size: 73.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 pkginfo/1.7.0 requests/2.23.0 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.7

File hashes

Hashes for oflibpytorch-2.1.1.tar.gz
Algorithm Hash digest
SHA256 6cfb582df1181a5130d7f7d1aa9b3ed7bfad4accfed8ee486a02ea4e9c230bf9
MD5 2a2f095d33d6fb51925c7a091467f337
BLAKE2b-256 6a7c15612b4bcc0d90101385d12c128f94e7a7d6ecaa20fc4b014a7a715c5a02

See more details on using hashes here.

File details

Details for the file oflibpytorch-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: oflibpytorch-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 51.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 pkginfo/1.7.0 requests/2.23.0 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.7

File hashes

Hashes for oflibpytorch-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b8c127a5c83356bce1699b8e395955c680be943575bf1bf3fdcb7bbc7d32d4a4
MD5 fa26e17284677a42dcfa611dcb4bacfc
BLAKE2b-256 d2bf4d15a4951eb0c7187edb6dae9a20879464fe60cc1bfc66266a3302070b11

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page