niftynet.layer.grid_warper module

Grid warper layer and utilities adapted from https://github.com/deepmind/sonnet/blob/v1.13/sonnet/python/modules/spatial_transformer.py https://github.com/niftk/NiftyNet/blob/v0.2.0.post1/niftynet/layer/spatial_transformer.py

class GridWarperLayer(source_shape, output_shape, coeff_shape, name, **kwargs)[source]

Bases: niftynet.layer.base_layer.Layer

Grid warper interface class.

An object implementing the GridWarper interface generates a reference grid of feature points at construction time, and warps it via a parametric transformation model, specified at run time by an input parameter Tensor. Grid warpers must then implement a create_features function used to generate the reference grid to be warped in the forward pass (according to a determined warping model).

__init__(source_shape, output_shape, coeff_shape, name, **kwargs)[source]

Constructs a GridWarper module and initializes the source grid params.

source_shape and output_shape defines the size of the source and output signal domains.

For example, for an image of size width=W and height=H, {source,output}_shape=[H, W]; for a volume of size width=W, height=H and depth=D, {source,output}_shape=[H, W, D].

Parameters:
  • source_shape – Iterable of integers determining the size of the source signal domain.
  • output_shape – Iterable of integers determining the size of the destination resampled signal domain.
  • coeff_shape – Shape of coefficients parameterizing the grid warp. For example, a 2D affine transformation will be defined by the [6] parameters populating the corresponding 2x3 affine matrix.
  • name – Name of Module.
  • **kwargs – Extra kwargs to be forwarded to the create_features function, instantiating the source grid parameters.
Raises:
  • Error – If len(output_shape) > len(source_shape).
  • TypeError – If output_shape and source_shape are not both iterable.
layer_op(*args, **kwargs)[source]
coeff_shape

Returns number of coefficients of warping function.

psi

Returns a list of features used to compute the grid warp.

source_shape

Returns a tuple containing the shape of the source signal.

output_shape

Returns a tuple containing the shape of the output grid.

class AffineGridWarperLayer(source_shape, output_shape, constraints=None, name='affine_grid_warper')[source]

Bases: niftynet.layer.grid_warper.GridWarperLayer, niftynet.layer.base_layer.Invertible

Affine Grid Warper class.

The affine grid warper generates a reference grid of n-dimensional points and warps it via an affine transformation model determined by an input parameter Tensor. Some of the transformation parameters can be fixed at construction time via an AffineWarpConstraints object.

__init__(source_shape, output_shape, constraints=None, name='affine_grid_warper')[source]

Constructs an AffineGridWarper.

source_shape and output_shape are used to define shape of source and output signal domains, as opposed to the shape of the respective Tensors. For example, for an image of size width=W and height=H, {source,output}_shape=[H, W]; for a volume of size width=W, height=H and depth=D, {source,output}_shape=[H, W, D].

Parameters:
  • source_shape – Iterable of integers determining shape of source signal domain.
  • output_shape – Iterable of integers determining shape of destination resampled signal domain.
  • constraints – Either a double list of shape [N, N+1] defining constraints on the entries of a matrix defining an affine transformation in N dimensions, or an AffineWarpConstraints object. If the double list is passed, a numeric value bakes in a constraint on the corresponding entry in the transformation matrix, whereas None implies that the corresponding entry will be specified at run time.
  • name – Name of module.
Raises:
  • Error – If constraints fully define the affine transformation; or if input grid shape and constraints have different dimensionality.
  • TypeError – If output_shape and source_shape are not both iterable.
constraints
layer_op(inputs)[source]

Assembles the module network and adds it to the graph.

The internal computation graph is assembled according to the set of constraints provided at construction time.

inputs shape: batch_size x num_free_params

Parameters:inputs – Tensor containing a batch of transformation parameters.
Returns:A batch of warped grids.
Raises:Error – If the input tensor size is not consistent with the constraints passed at construction time.
inverse_op(name=None)[source]

Returns a layer to compute inverse affine transforms.

The function first assembles a network that given the constraints of the current AffineGridWarper and a set of input parameters, retrieves the coefficients of the corresponding inverse affine transform, then feeds its output into a new AffineGridWarper setup to correctly warp the output space into the source space.
Parameters:name – Name of module implementing the inverse grid transformation.
Returns:A sonnet module performing the inverse affine transform of a reference grid of points via an AffineGridWarper module.
Raises:tf.errors.UnimplementedError – If the function is called on a non 2D instance of AffineGridWarper.
class AffineWarpConstraints(constraints=((None, None, None), (None, None, None)))[source]

Bases: object

Affine warp constraints class.

AffineWarpConstraints allow for very succinct definitions of constraints on the values of entries in affine transform matrices.

__init__(constraints=((None, None, None), (None, None, None)))[source]

Creates a constraint definition for an affine transformation.

Parameters:
  • constraints – A doubly-nested iterable of shape [N, N+1]
  • constraints on the entries of a matrix that (defining) –
  • an affine transformation in N dimensions. (represents) –
  • numeric value bakes in a constraint on the corresponding (A) –
  • in the transformation matrix, whereas None implies that (entry) –
  • corresponding entry will be specified at run time. (the) –
Raises:
  • TypeError – If constraints is not a nested iterable.
  • ValueError – If the double iterable constraints has inconsistent dimensions.
num_free_params
constraints
num_dim
combine_with(additional_constraints)[source]

Combines two sets of constraints into a coherent single set.

classmethod no_constraints(num_dim=2)[source]

Empty set of constraints for a num_dim affine transform.

classmethod translation_2d(x=None, y=None)[source]

Assign constraints on translation components of affine transform in 2d.

classmethod translation_3d(x=None, y=None, z=None)[source]

Assign constraints on translation components of affine transform in 3d.

classmethod scale_2d(x=None, y=None)[source]

Assigns constraints on scaling components of affine transform in 2d.

classmethod scale_3d(x=None, y=None, z=None)[source]

Assigns constraints on scaling components of affine transform in 3d.

classmethod shear_2d(x=None, y=None)[source]

Assigns constraints on shear components of affine transform in 2d.

classmethod no_shear_2d()[source]
classmethod no_shear_3d()[source]

Assigns constraints on shear components of affine transform in 3d.