@tf.function
def ncc_loss(static, moving):
    'Computes the normalized cross-correlation (NCC) loss.\n\n    Currently, only 4-D inputs are supported.\n\n    Parameters\n    ----------\n    static : tf.Tensor, shape (N, H, W, C)\n        The static image to which the moving image is aligned.\n    moving : tf.Tensor, shape (N, H, W, C)\n        The moving image, the same shape as the static image.\n\n    Returns\n    -------\n    loss : tf.Tensor, shape ()\n        Normalized cross-correlation loss between the static and the\n        moving images, averaged over the batch. Range is [-1.0, 1.0].\n        The best value is -1 (perfect match) and the worst is 1.\n\n    References\n    ----------\n    .. [1] `Wikipedia entry for the Cross-correlation\n           <https://en.wikipedia.org/wiki/Cross-correlation>`_\n    '
    eps = tf.constant(1e-09, 'float32')
    static_mean = tf.reduce_mean(static, axis=[1, 2], keepdims=True)
    moving_mean = tf.reduce_mean(moving, axis=[1, 2], keepdims=True)
    static_std = tf.math.reduce_std(static, axis=[1, 2], keepdims=True)
    moving_std = tf.math.reduce_std(moving, axis=[1, 2], keepdims=True)
    static_hat = ((static - static_mean) / (static_std + eps))
    moving_hat = ((moving - moving_mean) / (moving_std + eps))
    ncc = tf.reduce_mean((static_hat * moving_hat))
    loss = (- ncc)
    return loss
Computes the normalized cross-correlation (NCC) loss.

Currently, only 4-D inputs are supported.

Parameters
----------
static : tf.Tensor, shape (N, H, W, C)
    The static image to which the moving image is aligned.
moving : tf.Tensor, shape (N, H, W, C)
    The moving image, the same shape as the static image.

Returns
-------
loss : tf.Tensor, shape ()
    Normalized cross-correlation loss between the static and the
    moving images, averaged over the batch. Range is [-1.0, 1.0].
    The best value is -1 (perfect match) and the worst is 1.

References
----------
.. [1] `Wikipedia entry for the Cross-correlation
       <https://en.wikipedia.org/wiki/Cross-correlation>`_
@tf.function
def ncc_loss(static, moving):
    'Computes the normalized cross-correlation (NCC) loss.\n\n    Currently, only 4-D inputs are supported.\n\n    Parameters\n    ----------\n    static : tf.Tensor, shape (N, H, W, C)\n        The static image to which the moving image is aligned.\n    moving : tf.Tensor, shape (N, H, W, C)\n        The moving image, the same shape as the static image.\n\n    Returns\n    -------\n    loss : tf.Tensor, shape ()\n        Normalized cross-correlation loss between the static and the\n        moving images, averaged over the batch. Range is [-1.0, 1.0].\n        The best value is -1 (perfect match) and the worst is 1.\n\n    References\n    ----------\n    .. [1] `Wikipedia entry for the Cross-correlation\n           <https://en.wikipedia.org/wiki/Cross-correlation>`_\n    '
    eps = tf.constant(1e-09, 'float32')
    static_mean = tf.reduce_mean(static, axis=[1, 2], keepdims=True)
    moving_mean = tf.reduce_mean(moving, axis=[1, 2], keepdims=True)
    static_std = tf.math.reduce_std(static, axis=[1, 2], keepdims=True)
    moving_std = tf.math.reduce_std(moving, axis=[1, 2], keepdims=True)
    static_hat = ((static - static_mean) / (static_std + eps))
    moving_hat = ((moving - moving_mean) / (moving_std + eps))
    ncc = tf.reduce_mean((static_hat * moving_hat))
    loss = (- ncc)
    return loss