Registration#

Similarity losses for image registration: the negative mutual information and the negative normalized mutual information of two signals, estimated from a kernel (Parzen-window) joint histogram. The functions take flat signals, 2D images or 3D volumes; each module stores the target once and compares other signals with it. The Convention block of mutual_information_loss() states the rules that all of them share.

Porting from scikit-learn and scikit-image#

The losses use the natural logarithm, as scikit-learn’s mutual_info_score does. normalized_mutual_information_loss() negates Studholme’s \(\mathrm{NMI} = (H(X) + H(Y)) / H(X, Y)\), which is scikit-image’s normalized_mutual_information. scikit-learn’s normalized_mutual_info_score divides the mutual information by the arithmetic mean of the two entropies, its default, and equals \(2 - 2 / \mathrm{NMI}\) for the same joint histogram with nonzero joint entropy. If both signals are constant, scikit-image returns NaN (\(0 / 0\)) and scikit-learn returns 1.0, while kornia gives a finite value below -1 that changes with the dtype, num_bins and the number of samples.

Both libraries count a hard histogram. scikit-image’s bins defaults to 100, kornia’s num_bins to 64. With the default kernel and window_radius, kornia’s histogram is a hard one when every value sits on a bin centre, as on integer images whose values span 0, ..., K (both ends present) with num_bins=K + 1.

Functions#

kornia.losses.mutual_information_loss(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the mutual-information loss of two flat signals (*, N).

loss = mutual_information_loss(input, target) returns the negative mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per leading index.

\[\text{loss} = -(H(X) + H(Y) - H(X, Y))\]
Convention:
  • The loss is in nats: minimising it maximises the mutual information. It is symmetric in input and target. Registration maps the losses onto scikit-learn and scikit-image.

  • input and target are (*, N) tensors of one shape and one floating dtype; a single target is not broadcast over a batch of inputs. There is no reduction (Losses and metrics lists the defaults of the other losses).

  • Each signal is min-max normalised per sample, over its mask, so no data range is assumed. A positive rescaling and a shift of one sample leave its loss unchanged up to roundoff when both its original and transformed ranges exceed the absolute threshold torch.finfo(dtype).eps. One outlier squeezes the other values of its sample into a few bins. A sample whose range is at most that threshold counts as constant: its MI is 0 and it gets a zero gradient.

  • The joint histogram has num_bins bins per signal, and each value spreads over the bins within window_radius bin widths of it, weighted by kernel_function, a MIKernel member. With continuous values and the default window_radius, this soft histogram scores an image against itself below its entropy. Values compare only at equal num_bins, window_radius and kernel.

  • With MIKernel.xu (the default) and MIKernel.truncated_gaussian, input and target both get gradients. MIKernel.rectangular is piecewise constant: its loss has no gradient and serves for evaluation only.

  • input_mask and target_mask are boolean (N,) masks, common to the batch. The histogram counts the positions inside both masks, while each signal is normalised over its own mask: a value inside one mask only is not counted, but it changes the loss when it is that signal’s minimum or maximum. Values outside both masks have no effect. Masks of another shape, or with no position in common, raise an error.

  • For float16 and bfloat16 inputs, min-max normalisation, kernel evaluation, histogram accumulation and entropy arithmetic use float32, including under autocast, and the final loss is cast back to the input dtype. The empty-bin floor uses the histogram’s computation dtype rather than the input dtype. The constant-signal threshold remains torch.finfo(input dtype).eps.

Parameters:
  • input (Tensor) – flat signals (*, N).

  • target (Tensor) – flat signals with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (N,) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (N,) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per leading index, with shape *, in the input dtype.

See also

Example

>>> input = torch.rand(2, 3, 64)
>>> target = torch.rand(2, 3, 64)
>>> loss = mutual_information_loss(input, target)
kornia.losses.mutual_information_loss_2d(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the mutual-information loss of two images (*, H, W).

loss = mutual_information_loss_2d(input, target) returns the negative mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per image.

\[\text{loss} = -(H(X) + H(Y) - H(X, Y))\]
Convention:

See the Convention block of mutual_information_loss(); this function applies it to input.flatten(-2) and target.flatten(-2). Every axis before the last two is a batch axis, so a (B, C, H, W) input gives one loss per image and channel, (B, C).

Parameters:
  • input (Tensor) – images (*, H, W).

  • target (Tensor) – images with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (H, W) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (H, W) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per image, with shape *, in the input dtype.

See also

kornia.losses.mutual_information_loss_3d(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the mutual-information loss of two volumes (*, D, H, W).

loss = mutual_information_loss_3d(input, target) returns the negative mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per volume.

\[\text{loss} = -(H(X) + H(Y) - H(X, Y))\]
Convention:

See the Convention block of mutual_information_loss(); this function applies it to input.flatten(-3) and target.flatten(-3). Every axis before the last three is a batch axis, so a (B, C, H, W) tensor is read as B volumes of depth C and gives (B,).

Parameters:
  • input (Tensor) – volumes (*, D, H, W).

  • target (Tensor) – volumes with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (D, H, W) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (D, H, W) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per volume, with shape *, in the input dtype.

See also

kornia.losses.normalized_mutual_information_loss(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the normalized mutual-information loss of two flat signals (*, N).

loss = normalized_mutual_information_loss(input, target) returns the negative normalized mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per leading index.

\[\text{loss} = -\frac{H(X) + H(Y)}{H(X, Y)}\]
Convention:
  • The loss is the negative of Studholme’s normalized mutual information, which lies in \([1, 2]\): the loss lies in [-2, -1] and is -1 when one signal is constant and the other is not. Two constant signals give a finite value below -1 that changes with the dtype, num_bins and the number of samples, because histogram cells are floored at machine epsilon. With continuous values and the default window_radius, even an image against itself scores above -2. Registration maps this normalisation onto scikit-image and scikit-learn.

  • Everything else is as in the Convention block of mutual_information_loss().

Parameters:
  • input (Tensor) – flat signals (*, N).

  • target (Tensor) – flat signals with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (N,) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (N,) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per leading index, with shape *, in the input dtype.

See also

Example

>>> input = torch.rand(2, 3, 64)
>>> target = torch.rand(2, 3, 64)
>>> loss = normalized_mutual_information_loss(input, target)
kornia.losses.normalized_mutual_information_loss_2d(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the normalized mutual-information loss of two images (*, H, W).

loss = normalized_mutual_information_loss_2d(input, target) returns the negative normalized mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per image.

\[\text{loss} = -\frac{H(X) + H(Y)}{H(X, Y)}\]
Convention:

See the Convention block of normalized_mutual_information_loss(); the flattening and the batch axes are those of mutual_information_loss_2d().

Parameters:
  • input (Tensor) – images (*, H, W).

  • target (Tensor) – images with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (H, W) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (H, W) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per image, with shape *, in the input dtype.

See also

kornia.losses.normalized_mutual_information_loss_3d(input, target, input_mask=None, target_mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the normalized mutual-information loss of two volumes (*, D, H, W).

loss = normalized_mutual_information_loss_3d(input, target) returns the negative normalized mutual information of input and target from their kernel (Parzen-window) joint histogram, one value per volume.

\[\text{loss} = -\frac{H(X) + H(Y)}{H(X, Y)}\]
Convention:

See the Convention block of normalized_mutual_information_loss(); the flattening and the batch axes are those of mutual_information_loss_3d().

Parameters:
  • input (Tensor) – volumes (*, D, H, W).

  • target (Tensor) – volumes with the shape of input.

  • input_mask (Tensor | None, optional) – boolean region of interest (D, H, W) of input, common to the batch; None keeps every position. Default: None

  • target_mask (Tensor | None, optional) – boolean region of interest (D, H, W) of target, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Return type:

Tensor

Returns:

the loss per volume, with shape *, in the input dtype.

See also

Modules#

class kornia.losses.MILossFromRef(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the mutual-information loss of signals (*, N) against a reference (module form).

criterion = MILossFromRef(reference_signal, mask) stores the reference. loss = criterion(other_signal) is mutual_information_loss() with input=other_signal and target=reference_signal.

Convention:
  • See the Convention block of mutual_information_loss().

  • At construction the reference is restricted to its mask, min-max normalised and stored as the buffer signal, next to a copy of the mask as the buffer mask; later in-place changes to the reference tensor or to the mask tensor are not seen. The cache is not detached: a reference that requires grad receives a gradient from the first backward pass, and a second backward pass through the same module raises. Half-precision references are normalised and cached in float32. The final loss and constant-signal threshold follow the original reference dtype, including after .to(dtype); converting the cache itself to a lower precision can round its stored bin positions.

Parameters:
  • reference_signal (Tensor) – reference signals (*, N).

  • mask (Tensor | None, optional) – boolean region of interest (N,) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Shape:
  • other_signal: signals with the shape of reference_signal.

  • other_mask: boolean region of interest (N,) of other_signal, or None.

  • Output: the loss per leading index, in the dtype of the reference.

See also

Example

>>> reference = torch.rand(2, 64)
>>> criterion = MILossFromRef(reference)
>>> loss = criterion(torch.rand(2, 64))
class kornia.losses.MILossFromRef2D(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1)[source]#

Compute the mutual-information loss of images (*, H, W) against a reference (module form).

criterion = MILossFromRef2D(reference_signal, mask) stores the reference. loss = criterion(other_signal) is mutual_information_loss_2d() with input=other_signal and target=reference_signal.

Convention:

See the Convention block of MILossFromRef; the flattening and the batch axes are those of mutual_information_loss_2d().

Parameters:
  • reference_signal (Tensor) – reference images (*, H, W).

  • mask (Tensor | None, optional) – boolean region of interest (H, W) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1

Shape:
  • other_signal: images with the shape of reference_signal.

  • other_mask: boolean region of interest (H, W) of other_signal, or None.

  • Output: the loss per image, in the dtype of the reference.

See also

class kornia.losses.MILossFromRef3D(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1)[source]#

Compute the mutual-information loss of volumes (*, D, H, W) against a reference (module form).

criterion = MILossFromRef3D(reference_signal, mask) stores the reference. loss = criterion(other_signal) is mutual_information_loss_3d() with input=other_signal and target=reference_signal.

Convention:

See the Convention block of MILossFromRef; the flattening and the batch axes are those of mutual_information_loss_3d().

Parameters:
  • reference_signal (Tensor) – reference volumes (*, D, H, W).

  • mask (Tensor | None, optional) – boolean region of interest (D, H, W) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1

Shape:
  • other_signal: volumes with the shape of reference_signal.

  • other_mask: boolean region of interest (D, H, W) of other_signal, or None.

  • Output: the loss per volume, in the dtype of the reference.

See also

class kornia.losses.NMILossFromRef(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1.0)[source]#

Compute the normalized mutual-information loss of signals (*, N) against a reference (module form).

criterion = NMILossFromRef(reference_signal, mask) stores the reference. loss = criterion(other_signal) is normalized_mutual_information_loss() with input=other_signal and target=reference_signal.

Convention:

See the Convention blocks of normalized_mutual_information_loss() for the loss and of MILossFromRef for the stored reference.

Parameters:
  • reference_signal (Tensor) – reference signals (*, N).

  • mask (Tensor | None, optional) – boolean region of interest (N,) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1.0

Shape:
  • other_signal: signals with the shape of reference_signal.

  • other_mask: boolean region of interest (N,) of other_signal, or None.

  • Output: the loss per leading index, in the dtype of the reference.

See also

Example

>>> reference = torch.rand(2, 64)
>>> criterion = NMILossFromRef(reference)
>>> loss = criterion(torch.rand(2, 64))
class kornia.losses.NMILossFromRef2D(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1)[source]#

Compute the normalized mutual-information loss of images (*, H, W) against a reference (module form).

criterion = NMILossFromRef2D(reference_signal, mask) stores the reference. loss = criterion(other_signal) is normalized_mutual_information_loss_2d() with input=other_signal and target=reference_signal.

Convention:

See the Convention block of NMILossFromRef; the flattening and the batch axes are those of normalized_mutual_information_loss_2d().

Parameters:
  • reference_signal (Tensor) – reference images (*, H, W).

  • mask (Tensor | None, optional) – boolean region of interest (H, W) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1

Shape:
  • other_signal: images with the shape of reference_signal.

  • other_mask: boolean region of interest (H, W) of other_signal, or None.

  • Output: the loss per image, in the dtype of the reference.

See also

class kornia.losses.NMILossFromRef3D(reference_signal, mask=None, kernel_function=MIKernel.xu, num_bins=64, window_radius=1)[source]#

Compute the normalized mutual-information loss of volumes (*, D, H, W) against a reference (module form).

criterion = NMILossFromRef3D(reference_signal, mask) stores the reference. loss = criterion(other_signal) is normalized_mutual_information_loss_3d() with input=other_signal and target=reference_signal.

Convention:

See the Convention block of NMILossFromRef; the flattening and the batch axes are those of normalized_mutual_information_loss_3d().

Parameters:
  • reference_signal (Tensor) – reference volumes (*, D, H, W).

  • mask (Tensor | None, optional) – boolean region of interest (D, H, W) of the reference, common to the batch; None keeps every position. Default: None

  • kernel_function (MIKernel, optional) – kernel that spreads each value over the bins. Default: MIKernel.xu

  • num_bins (int, optional) – number of bins per signal. Default: 64

  • window_radius (float, optional) – radius of the kernel in bin widths. Default: 1

Shape:
  • other_signal: volumes with the shape of reference_signal.

  • other_mask: boolean region of interest (D, H, W) of other_signal, or None.

  • Output: the loss per volume, in the dtype of the reference.

See also

Kernels#

class kornia.losses.MIKernel(value)[source]#

Choose the kernel that spreads a sample over the histogram bins of the mutual-information losses.

Pass a member as the kernel_function of a mutual-information loss. The members are not callable: a member’s value is the kernel f(d, window_radius=1.0), the weight that a sample at distance d from a bin centre, in bin widths, gives that bin.

Convention:
  • Every kernel is 0 outside |d| <= window_radius. With u = |d| / window_radius:

    • xu (Xu et al., 2008), the default: \(1 - 0.1 u - 1.8 u^2\) for \(u < 0.5\) and \(1.9 - 3.7 u + 1.8 u^2\) up to \(u = 1\). At window_radius=1 the weights of a sample between the first and the last bin centre sum to 1.

    • rectangular: 1 on the closed support, so a sample counts fully in every bin within window_radius. Its loss has no gradient (evaluation only, see mutual_information_loss()).

    • truncated_gaussian: the normal density with standard deviation window_radius, cut at one standard deviation.

See also

  • mutual_information_loss(): takes a member as kernel_function, as do the other mutual-information losses and their module forms.