Elastic deformations for N-D images.
Project description
Elastic deformations for N-dimensional images (Python, SciPy, NumPy, TensorFlow, PyTorch)
This library implements elastic grid-based deformations for N-dimensional images.
The elastic deformation approach is described in
- Ronneberger, Fischer, and Brox, "U-Net: Convolutional Networks for Biomedical Image Segmentation" (https://arxiv.org/abs/1505.04597)
- Çiçek et al., "3D U-Net: Learning Dense Volumetric Segmentation from Sparse Annotation" (https://arxiv.org/abs/1606.06650)
The procedure generates a coarse displacement grid with a random displacement for each grid point. This grid is then interpolated to compute a displacement for each pixel in the input image. The input image is then deformed using the displacement vectors and a spline interpolation.
In addition to the normal, forward deformation, this package also provides a
function that can backpropagate the gradient through the deformation. This makes
it possible to use the deformation as a layer in a convolutional neural network.
For convenience, TensorFlow and PyTorch wrappers are provided in elasticdeform.tf
and elasticdeform.torch
.
Installation
pip install elasticdeform
or
pip install git+https://github.com/gvtulder/elasticdeform
This library requires Python 3 and NumPy development headers.
On Windows, try to install the precompiled binaries directly using pip install elasticdeform
.
If that does not work, these precompiled packages might be an alternative option.
Examples
This basic example deforms an image with a random 3 x 3 deformation grid:
import numpy, imageio, elasticdeform
X = numpy.zeros((200, 300))
X[::10, ::10] = 1
# apply deformation with a random 3 x 3 grid
X_deformed = elasticdeform.deform_random_grid(X, sigma=25, points=3)
imageio.imsave('test_X.png', X)
imageio.imsave('test_X_deformed.png', X_deformed)
Multiple inputs
If you have multiple images, e.g., an image and a segmentation image, you can deform both simultaneously by providing a list of inputs. You can specify a different spline order for each input.
# apply deformation to inputs X and Y
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y])
# apply deformation to inputs X and Y,
# with a different interpolation for each input
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y], order=[3, 0])
Multi-channel images
By default, a deformation will be applied to every dimension of the input. If you
have multi-channel images, you can use the axis
parameter to specify which axes
should be deformed. The same deformation will be applied for each channel.
For example, to deform an RGB image across the first two dimensions, run:
X_deformed = elasticdeform.deform_random_grid(X, axis=(0, 1))
When deforming multiple inputs, you can provide a tuple of axes for each input:
X = numpy.random.rand(3, 200, 300)
Y = numpy.random.rand(200, 300)
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y], axis=[(1, 2), (0, 1)])
Cropping
If you intend to crop a small subpatch from the deformed image, you can provide the crop dimensions to the deform function. It will then compute only the cropped output pixels, while still computing the deformation grid based on the full image dimensions. This saves computation time.
X = numpy.random.rand(200, 300)
# define a crop region
crop = (slice(50, 150), slice(0, 100))
# generate a deformation grid
displacement = numpy.random.randn(2, 3, 3) * 25
# deform full image
X_deformed = elasticdeform.deform_grid(X, displacement)
# compute only the cropped region
X_deformed_crop = elasticdeform.deform_grid(X, displacement, crop=crop)
# the deformation is the same
numpy.testing.assert_equal(X_deformed[crop], X_deformed_crop)
Rotate and zoom
The deformation functions accept rotate
and zoom
parameters, which allows you
to combine the elastic deformation with rotation and scaling. This can be useful
as data augmentation step. The rotation and zoom are applied to the output
coordinates, using the center pixel of the output patch as the origin.
# apply deformation with a random 3 x 3 grid,
# rotate by 30 degrees and rescale with a factor 1.5
X_deformed = elasticdeform.deform_random_grid(X, sigma=25, points=3,
rotate=30, zoom=1.5)
Note that the output shape remains the same. The mapping of the input to the output is rotated within the given output frame.
Rotate and zoom can be combined with the crop
argument. In that case, the
scaling and rotation is performed relative to the center of the cropped output.
For more advanced transformations, it is also possible to provide an affine transformation matrix directly.
Gradient
The deform_grid_gradient
function can be used to backpropagate the gradient of
the output with respect to the input. Call deform_grid_gradient
with the
parameters that were used for the forward step.
X = numpy.random.rand(200, 300)
# generate a deformation grid
displacement = numpy.random.randn(2, 3, 3) * 25
# perform forward deformation
X_deformed = elasticdeform.deform_grid(X, displacement)
# obtain the gradient w.r.t. X_deformed (e.g., with backpropagation)
dX_deformed = numpy.random.randn(*X_deformed.shape)
# compute the gradient w.r.t. X
dX = elasticdeform.deform_grid_gradient(dX_deformed, displacement)
Note: The gradient function will assume that the input has the same size as the
output. If you used the crop
parameter in the forward phase, it is necessary to
provide the gradient function with the original, uncropped input shape in the
X_shape
parameter.
TensorFlow wrapper
The elasticdeform.tf
module provides a wrapper for deform_grid
in TensorFlow.
The function uses TensorFlow Tensors as input and output, but otherwise uses
the same parameters.
import numpy
import elasticdeform.tf as etf
displacement_val = numpy.random.randn(2, 3, 3) * 5
X_val = numpy.random.rand(200, 300)
dY_val = numpy.random.rand(200, 300)
# construct TensorFlow input and top gradient
displacement = tf.Variable(displacement_val)
X = tf.Variable(X_val)
dY = tf.Variable(dY_val)
# the deform_grid function is similar to the plain Python equivalent,
# but it accepts and returns TensorFlow Tensors
X_deformed = etf.deform_grid(X, displacement, order=3)
# the gradient w.r.t. X can be computed in the normal TensorFlow manner
[dX] = tf.gradients(X_deformed, X, dY)
PyTorch wrapper
The elasticdeform.torch
module provides a wrapper for deform_grid
in PyTorch.
The function uses PyTorch Tensors as input and output, but otherwise uses
the same parameters.
import numpy
import elasticdeform.torch as etorch
displacement_val = numpy.random.randn(2, 3, 3) * 5
X_val = numpy.random.rand(200, 300)
dY_val = numpy.random.rand(200, 300)
# construct PyTorch input and top gradient
displacement = torch.tensor(displacement_val)
X = torch.tensor(X_val, requires_grad=True)
dY = torch.tensor(dY_val)
# the deform_grid function is similar to the plain Python equivalent,
# but it accepts and returns PyTorch Tensors
X_deformed = etorch.deform_grid(X, displacement, order=3)
# the gradient w.r.t. X can be computed in the normal PyTorch manner
X_deformed.backward(dY)
print(X.grad)
License information
This library was written by Gijs van Tulder at the Biomedical Imaging Group Rotterdam, Erasmus MC, Rotterdam, the Netherlands
It is inspired by a similar, Python-based implementation by Florian Calvet. This C-based implementation gives the same results, but is faster and has a gradient implementation.
This C implementation includes a modified version of the NI_GeometricTransform
from SciPy's ndimage library.
This code is made available under the BSD license. See LICENSE.txt
for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for elasticdeform-0.5.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebd7b358ec0902fb1d1a9e01f671e32a56503fdd29280f3f91f901a52d2e31ae |
|
MD5 | 74a4d10676ad5ca3bf47f62db6cbc0eb |
|
BLAKE2b-256 | fba65c5449d9780f7f61b8e7e8e7b4accc4f4e20dba2bd1d035377a818eebd38 |
Hashes for elasticdeform-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb28fb05bc093fef6c69e96948e48667ae413c36471f3ab77a840a0c77d3fe20 |
|
MD5 | a8a954b4e11941bdcc92b7350e1704c0 |
|
BLAKE2b-256 | 33979e54225df4e0f7e4a6eb7f3fc4c312eb1701e9ced1ada62b3ca17bf8c0b0 |
Hashes for elasticdeform-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22997c880ddbb4ad8e168ee6827c2b55d8df5fcf2cb46636a8e7569f71310a0c |
|
MD5 | a409e27d0f4cc87d259556033e55c7b7 |
|
BLAKE2b-256 | 731c5c6833462d016b4354c76e3c869c899e0242019c6f5bdcd3517cd23a2d29 |
Hashes for elasticdeform-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c388e2b93b0deac9c9e51228552eb053941401180b7a646b4ca66c6e8e05bf5b |
|
MD5 | b9fe04d8c67cf767302a0d12ff2bd5a4 |
|
BLAKE2b-256 | f8cf1da8e415847ab0f7321c68c15553136039a47f786109fbee0fd1248aa8f1 |
Hashes for elasticdeform-0.5.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c843cc252bf1a4854584777df8991e25b7b6c33980668d7e875db8fbe160ab2 |
|
MD5 | 78b78d8e4b4a34ef69a8be82d171ac39 |
|
BLAKE2b-256 | 65010d2b43d5695e7a78e103929930a38dc291224690a9c10d9f21cf39a0f76c |
Hashes for elasticdeform-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31c912fb565f474392c22a24fb3222f5377f7654182f951984888c8a6aae5949 |
|
MD5 | de640ee354b4367c4025df3ac87c4995 |
|
BLAKE2b-256 | 003ed3b7e718fb108f0ee8bcdbaa00079e339df02d5a7a5e6014f977e990ff0a |
Hashes for elasticdeform-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 058a88864398de0433203dd97afb6f6c327ba2e111e906227e389807e0bf9cc5 |
|
MD5 | dceff437ac1b5ab957cb5e0d4439a271 |
|
BLAKE2b-256 | 44be088d3a6bd505fa6d68ce550f0e73ebd76685b04362f0fb260be3527e6dbc |
Hashes for elasticdeform-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f10a1e4f1923f2b20460d692385b0bfc980298872dff779cb0f1c9f041b355ac |
|
MD5 | a7d0790b34cd0d09315cf1033403db82 |
|
BLAKE2b-256 | 95ed6b6862bc3defaec433d9e47be318a302086c43a15ca257a88ca992ca758d |
Hashes for elasticdeform-0.5.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d07b1a94cb4d3fd0d51f36eb365b57d1f07cfc5f73497c4da94ba279eba7e2f7 |
|
MD5 | 6816ef00a9947d63ac1408c76857878b |
|
BLAKE2b-256 | badb0e3d450201d0e1ad51fcd84df6810235753d7ac9a3a1ff6c3c9bfaae5c10 |
Hashes for elasticdeform-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09272deb0cf3c1aabb9cc5335bee9d94110e5a28486a9078efaf229c7b219a18 |
|
MD5 | b75bb6ca8357b1d5edfd27ed722b95a8 |
|
BLAKE2b-256 | 9b4a48f220326e0778ffc450d6d6cbe19d8c23442978c3a834f9ce723ee582f5 |
Hashes for elasticdeform-0.5.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98ee332015fa83b3ea0ed1a4182ff57d44672a8ea452fda0b490075613d76d47 |
|
MD5 | a42b4d7aa7ff89da74ddb3ab06d76a74 |
|
BLAKE2b-256 | f4306906241b6788a51ece61a4b2498f2fcebdde16890a0b7782baa698534ec6 |
Hashes for elasticdeform-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02aa1173aa91b2df357a9ca48766fd47b316f61c9acac537c1cd12e5a1a0bd80 |
|
MD5 | 4a0540c27b9514cd98fdf1eaf30e7b66 |
|
BLAKE2b-256 | 53fa3fe6de314e1b7993c2bfeff5a0d1bf0df6a913bfa9a33e7fca3e28df2469 |
Hashes for elasticdeform-0.5.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1896f5fbca215fc4c3d693e3f63f9be464ce98967e730eee187557854a3926b |
|
MD5 | 9c9e3a455622a4e6d9cecf3f835748d1 |
|
BLAKE2b-256 | 9744763a99daaf3f90a7763f5c07b6cb52930266d481748cca01d8b00d57efb6 |
Hashes for elasticdeform-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db020f08cdb4151be55a2b0b7343da4e1935d8f6ad7268495f8863da6d7cc4bb |
|
MD5 | ba10a9c5bcd5ea8d55b87f198d0c3e16 |
|
BLAKE2b-256 | 8242b7d5abc7dd568b77b8f35c4e4e7647fc2a5a2973c91353022b59a5565494 |
Hashes for elasticdeform-0.5.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c0b160ca230577f130fb36b6b696bb081a07a0443bcdf89c9d159679ee34613 |
|
MD5 | fe097b71aada9483f039ad5816717437 |
|
BLAKE2b-256 | 70df50b8b75e7b35b62c7881a797f44f0bed0d4e33776f9c1b5c6ef60b38720d |
Hashes for elasticdeform-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c429727b26f5edaa7516d66db1021c095d6c8aaef67cfa33e149860a90f5d53a |
|
MD5 | 3106665f1a2cdfbcd5d5bb3a60346b49 |
|
BLAKE2b-256 | 4931421b69917b021106bbc47c1c0d43100a29ef887dd7a36f3b9bc39b10ff09 |