code: def _niftyreg_resampling_grad(op, grad):
      grad_op = (
          get_niftyreg_module().niftyreg_image_resampling_gradient(
              op.inputs[0],
              op.inputs[1],
              interpolation=op.get_attr("interpolation"),
              boundary=op.get_attr("boundary"),
          )
      )
  
      chained_grad = None
  
      nof_modalities = op.inputs[0].shape.as_list()[1]
      if not nof_modalities is None and nof_modalities != 1:
          nof_dims = op.inputs[1].shape.as_list()[1]
  
          assert grad_op.shape.as_list()[1] == nof_modalities * nof_dims
  
          chained_grads = []
          for m in range(nof_modalities):
              mod_grad = grad_op[
                  :, (nof_dims * m) : ((m + 1) * nof_dims), ...
              ]
              out_mod_grad = tf.expand_dims(grad[:, m, ...], axis=1)
  
              out_grad = tf.tile(
                  out_mod_grad,
                  [1] + [nof_dims] + [1] * (len(grad_op.shape) - 2),
              )
  
              chained_grads.append(tf.multiply(mod_grad, out_grad))
  
          chained_grad = tf.reduce_sum(
              tf.stack(chained_grads, axis=0), axis=0
          )
  
      else:
          grad_rep = tf.tile(
              grad,
              [1] + [grad_op.shape[1]] + [1] * (len(grad_op.shape) - 2),
          )
          chained_grad = tf.multiply(grad_rep, grad_op)
  
      image_grad_op = get_niftyreg_module().niftyreg_image_resampling_image_gradient(
          op.inputs[0],
          op.inputs[1],
          grad,
          interpolation=op.get_attr("interpolation"),
          boundary=op.get_attr("boundary"),
      )
  
      return [image_grad_op, chained_grad]
  
file_path: NiftyNet/niftynet/contrib/niftyreg_image_resampling/niftyreg_image_resampling.py
imports: from __future__ import print_function, division
  import tensorflow
  from tensorflow.python.framework import ops
  from niftynet.layer.base_layer import Layer
  from niftynet.layer.layer_util import infer_spatial_rank
  from niftyreg_module_loader import get_niftyreg_module
label_desc: Use image node_id 2 for calling a global function with example usage: _niftyreg_resampling_grad(op, grad) and returns: list