Package: torch-cubic-spline-grids
torch-cubic-spline-gridsCubic spline interpolation on multidimensional grids in PyTorch.The primary goal of this package is to provide learnable, continuous
parametrisations of 1-4D spaces.Overviewtorch_cubic_spline_gridsprovides a set of PyTorch components called grids.Grids are defined bytheir dimensionality (1d, 2d, 3d, 4d...)the number of points covering each dimension (resolution)the number of values stored on each grid point (n_channels)how we interpolate between values on grid pointsAll grids in this package consist of uniformly spaced points covering the full
extend of each dimension.First stepsLet's make a simple 2D grid with one value on each grid point.importtorchfromtorch_cubic_spline_gridsimportCubicBSplineGrid2dgrid=CubicBSplineGrid2d(resolution=(5,3),n_channels=1)grid.ndimis2grid.resolutionis(5, 3)(or(h, w))grid.n_channelsis1grid.data.shapeis(1, 5, 3)(or(c, h, w))In words, the grid extends over two dimensions(h, w)with 5 points
inhand3points inw.
There is one value stored at each point on the 2D grid.
The grid data is stored in a tensor of shape(c, *grid_resolution).We can obtain the value (interpolant) at any continuous point on the grid.
The grid coordinate system extends from[0, 1]along each grid dimension.
The interpolant is obtained by sequential application of
cubic spline interpolation along each dimension of the grid.coords=torch.rand(size=(10,2))# values in [0, 1]interpolants=grid(coords)interpolants.shapeis(10, 1)OptimisationValues at each grid point can be optimised by minimising a loss function associated with grid interpolants.
In this way the continuous space of the grid can be made to more accurately model a 1-4D space.The image above shows the values of 6 control points on a 1D grid being optimised such
that interpolating between them with cubic B-spline interpolation approximates a single oscillation of a sine wave.Notebooks are available for this1D exampleand a similar2D example.Types of gridstorch_cubic_spline_gridsprovides grids which can be interpolated withcubic
B-splineinterpolation orcubic Catmull-Rom splineinterpolation.splinecontinuityinterpolating?cubic B-splineC2NoCatmull-Rom splineC1YesIf your need the resulting curve to intersect the data on the grid you should
use the cubic Catmull-Rom spline gridsCubicCatmullRomGrid1dCubicCatmullRomGrid2dCubicCatmullRomGrid3dCubicCatmullRomGrid4dIf you require continuous second derivatives then the cubic B-spline grids are more
suitable.CubicBSplineGrid1dCubicBSplineGrid2dCubicBSplineGrid3dCubicBSplineGrid4dRegularisationThe number of points in each dimension should be chosen such that interpolating on the
grid can approximate the underlying phenomenon being modelled without overfitting.
A low resolution grid provides a regularising effect by smoothing the model.Installationtorch_cubic_spline_gridsis available on PyPIpipinstalltorch-cubic-spline-gridsRelated workThis is a PyTorch implementation of the wayWarpmodels continuous deformation
fields and locally variable optical parameters in cryo-EM images.
The approach is described inDimitry Tegunov's paper:Many methods in Warp are based on a continuous parametrization of 1- to
3-dimensional spaces.
This parameterization is achieved by spline interpolation between points on a coarse,
uniform grid, which is computationally efficient.
A grid extends over the entirety of each dimension that needs to be modeled.
The grid resolution is defined by the number of control points in each dimension
and is scaled according to physical constraints
(for example, the number of frames or pixels) and available signal.
The latter provides regularization to prevent overfitting of sparse data with too many
parameters.
When a parameter described by the grid is retrieved for a point in space (and time),
for example for a particle (frame), B-spline interpolation is performed at that point
on the grid.
To fit a grid’s parameters, in general, a cost function associated with the
interpolants at specific positions on the grid is optimized.For a fantastic introduction to splines I recommendFreya Holmer's YouTube video.The Continuity of Splines - YouTube