Skip to main content

Compute one dimensional CPAB transformations in Numpy and Pytorch

Project description


https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/logo.png

Finite-dimensional spaces of simple, fast, and highly-expressive diffeomorphisms derived from parametric, continuously-defined, velocity fields in Numpy and Pytorch

PyPI Status PyPI Version License Actions https://img.shields.io/pypi/dm/cpab?style=flat-square Top Language Github Issues

Getting Started

The following code transforms a regular grid using a diffeomorphic curve parametrized theta:

https://mybinder.org/badge_logo.svg
# Import cpab library
import cpab

# Transformation instance
T = cpab.Cpab(tess_size=5, backend="numpy", device="cpu", zero_boundary=True, basis="qr")

# Generate grid
grid = T.uniform_meshgrid(100)

# Transformation parameters
theta = T.identity(epsilon=1)

# Transform grid
grid_t = T.transform_grid(grid, theta)
https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/figures/visualize_deformgrid.png

In this example, the tesselation is composed of 5 intervals, and the zero_boundary condition set to True constraints the velocity at the tesselation boundary (in this case, at x=0 and x=1). The regular grid has 100 equally spaced points.

T.visualize_tesselation()
https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/figures/visualize_tesselation.png

The velocity field is formed by a continuous piecewise affine function defined over 5 intervals. The parameters theta represent a basis of the null space for all continuous piecewise affine functions composed of 5 intervals. In this case, we have used the QR decomposition to build the basis. See the API documentation for more details about the transformation options.

Taking into account the zero velocity constraints at the boundary, only 4 dimensions or degree of freedom are left to play with, and that indeed is the dimensionality of theta, a vector of 4 values.

T.visualize_velocity(theta)
https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/figures/visualize_velocity.png

We can visualize the generated transformation based on the parameters theta:

T.visualize_deformgrid(theta)
https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/figures/visualize_deformgrid.png

In addition, for optimization tasks, it is useful to obtain the gradient of the transformation with respect to parameters theta. The gradient function can be obtained in closed-form solution. There are 4 different functions, one per dimension in theta:

T.visualize_gradient(theta)
https://raw.githubusercontent.com/imartinezl/cpab/master/docs/source/_static/figures/visualize_gradient.png

Citations

This repository was presented at the International Conference of Machine Learning (ICML 2022).

@inproceedings{martinez2022closed,
    title={Closed-Form Diffeomorphic Transformations for Time Series Alignment},
    author={Martinez, I{\~n}igo and Viles, Elisabeth and Olaizola, Igor G},
    booktitle={International Conference on Machine Learning},
    pages={15122--15158},
    year={2022},
    organization={PMLR}
}

Furthermore, please cite previous articles by Freifeld et al. that are essential to this work.

@article{freifeld2017transformations,
    title={Transformations based on continuous piecewise-affine velocity fields},
    author={Freifeld, Oren and Hauberg, S{\o}ren and Batmanghelich, Kayhan and Fisher, Jonn W},
    journal={IEEE transactions on pattern analysis and machine intelligence},
    volume={39},
    number={12},
    pages={2496--2509},
    year={2017},
    publisher={IEEE}
}

@inproceedings{freifeld2015highly,
    title={Highly-expressive spaces of well-behaved transformations: Keeping it simple},
    author={Freifeld, Oren and Hauberg, Soren and Batmanghelich, Kayhan and Fisher, John W},
    booktitle={Proceedings of the IEEE International Conference on Computer Vision},
    pages={2911--2919},
    year={2015}
}

Installation

As the compiled cpab package is hosted on the Python Package Index (PyPI) you can easily install it with pip. To install cpab, run this command in your terminal of choice:

$ pip install cpab

or, alternatively:

$ python -m pip install cpab

If you want to get cpab’s latest version, you can refer to the repository hosted at github:

python -m pip install https://github.com/imartinezl/cpab/archive/master.zip

Environment Setup

Requirements

cpab builds on numpy, torch, scipy, ninja, and matplotlib libraries.

Python 3

To find out which version of python you have, open a terminal window and try the following command:

$ python3 --version
Python 3.6.9

If you have python3 on your machine, then this command should respond with a version number. If you do not have python3 installed, follow these instructions.

Pip

pip is the reference Python package manager. It’s used to install and update packages. In case pip is not installed in your OS, follow these procedure.

Virtual Environment

venv creates a “virtual” isolated Python installation and installs packages into that virtual installation. It is always recommended to use a virtual environment while developing Python applications. To create a virtual environment, go to your project’s directory and run venv.

$ python3 -m venv env

Before you can start installing or using packages in your virtual environment you’ll need to activate it.

$ source env/bin/activate

Source Code

cpab is developed on GitHub, where the code is always available.

You can either clone the public repository:

$ git clone git://github.com/imartinezl/cpab.git

Or, download the tarball:

$ curl -OL https://github.com/imartinezl/cpab/tarball/main
# optionally, zipball is also available (for Windows users).

Once you have a copy of the source, you can embed it in your own Python package, or install it into your site-packages easily:

$ cd cpab
$ python -m pip install .

MIT License

Copyright (c) 2021 Iñigo Martínez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

cpab-0.0.26.tar.gz (46.0 kB view details)

Uploaded Source

Built Distribution

cpab-0.0.26-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file cpab-0.0.26.tar.gz.

File metadata

  • Download URL: cpab-0.0.26.tar.gz
  • Upload date:
  • Size: 46.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for cpab-0.0.26.tar.gz
Algorithm Hash digest
SHA256 10899d3eb6586a9963cb75100066616a5fbd07b087fe0d760a500b936beca30e
MD5 11756ba63b1fe75d6b693ad2a5be8274
BLAKE2b-256 201ae96e0954eaa3b1c8aff183c7f7d5b7e74e79cf1c6711558909a8d6e4df5f

See more details on using hashes here.

File details

Details for the file cpab-0.0.26-py3-none-any.whl.

File metadata

  • Download URL: cpab-0.0.26-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for cpab-0.0.26-py3-none-any.whl
Algorithm Hash digest
SHA256 8450bc0d8c31d729907463f19079f09e635b1f060610b81defea536960f33412
MD5 9ef11afcca07d13fe31f6b73d7f600cd
BLAKE2b-256 8a94982bd597d74782b068d737cdb2f0c6df3b7d244d868965997fe3a959e78f

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