class_docstr: resample inputs according to ``sample_coords``
class_docstr_tok: ["resample","inputs","according","to","``","sample_coords","``"]
docstr: This layer resamples 2D or 3D data given the coordinates.
  
  In terms of 3D inputs,
  
  when the shape of ``inputs`` is ``[batch, x, y, z, num_channels]``,
  the shape of ``sample_coords`` can be
  ``[1, d0, d1, ..., 3]`` or ``[batch, d0, d1, ..., 3]``.
  
  The output shape would be ``[batch, d0, d1, ..., num_channels]``.
  
  Similarly, in 2D,
  
  when the shape of ``inputs`` is ``[batch, x, y, num_channels]``,
  the shape of ``sample_coords`` can be
  ``[1, d0, d1, ..., 2]`` or ``[batch, d0, d1, ..., 2]``.
  
  The output shape would be ``[batch, d0, d1, ... num_channels]``
  
  (If the shape of ``inputs`` is not fully specified, ``sample_coords``
  must be checked before using this function, to make sure the
  coordinates are pointing to locations within the inputs.)
  
  (Resampling 2D inputs is implemented by calling
  ``tf.contrib.resampler.resampler``. The interpretaion of coordinates is
  different in between this function and
  ``tf.contrib.resampler.resampler``:
  using ``self.layer_op(inputs, sample_coords)`` for 2D data
  is equivalent to (apart from the batch size broadcasting feature)::
  
      tf.contrib.resampler.resampler(
          tf.transpose(inputs, [0, 2, 1, 3]), sample_coords)
  
  (No gradient is computed for ``NEAREST`` method, and
   some of the padding modes.)
docstr_tok: ["This","layer","resamples","2D","or","3D","data","given","the","coordinates",".","In","terms","of","3D","inputs",",","when","the","shape","of","``","inputs","``","is","``","[","batch",",","x",",","y",",","z",",","num_channels","]","``",",","the","shape","of","``","sample_coords","``","can","be","``","[","1",",","d0",",","d1",",","...",",","3","]","``","or","``","[","batch",",","d0",",","d1",",","...",",","3","]","``",".","The","output","shape","would","be","``","[","batch",",","d0",",","d1",",","...",",","num_channels","]","``",".","Similarly",",","in","2D",",","when","the","shape","of","``","inputs","``","is","``","[","batch",",","x",",","y",",","num_channels","]","``",",","the","shape","of","``","sample_coords","``","can","be","``","[","1",",","d0",",","d1",",","...",",","2","]","``","or","``","[","batch",",","d0",",","d1",",","...",",","2","]","``",".","The","output","shape","would","be","``","[","batch",",","d0",",","d1",",","...","num_channels","]","``","(","If","the","shape","of","``","inputs","``","is","not","fully","specified",",","``","sample_coords","``","must","be","checked","before","using","this","function",",","to","make","sure","the","coordinates","are","pointing","to","locations","within","the","inputs",".",")","(","Resampling","2D","inputs","is","implemented","by","calling","``","tf.contrib.resampler.resampler","``",".","The","interpretaion","of","coordinates","is","different","in","between","this","function","and","``","tf.contrib.resampler.resampler","``",":","using","``","self.layer_op","(","inputs",",","sample_coords",")","``","for","2D","data","is","equivalent","to","(","apart","from","the","batch","size","broadcasting","feature",")",":",":","tf.contrib.resampler.resampler","(","tf.transpose","(","inputs",",","[","0",",","2",",","1",",","3","]",")",",","sample_coords",")","(","No","gradient","is","computed","for","``","NEAREST","``","method",",","and","some","of","the","padding","modes",".",")"]
code: def layer_op(self, inputs, sample_coords):
      """
      This layer resamples 2D or 3D data given the coordinates.
  
      In terms of 3D inputs,
  
      when the shape of ``inputs`` is ``[batch, x, y, z, num_channels]``,
      the shape of ``sample_coords`` can be
      ``[1, d0, d1, ..., 3]`` or ``[batch, d0, d1, ..., 3]``.
  
      The output shape would be ``[batch, d0, d1, ..., num_channels]``.
  
      Similarly, in 2D,
  
      when the shape of ``inputs`` is ``[batch, x, y, num_channels]``,
      the shape of ``sample_coords`` can be
      ``[1, d0, d1, ..., 2]`` or ``[batch, d0, d1, ..., 2]``.
  
      The output shape would be ``[batch, d0, d1, ... num_channels]``
  
      (If the shape of ``inputs`` is not fully specified, ``sample_coords``
      must be checked before using this function, to make sure the
      coordinates are pointing to locations within the inputs.)
  
      (Resampling 2D inputs is implemented by calling
      ``tf.contrib.resampler.resampler``. The interpretaion of coordinates is
      different in between this function and
      ``tf.contrib.resampler.resampler``:
      using ``self.layer_op(inputs, sample_coords)`` for 2D data
      is equivalent to (apart from the batch size broadcasting feature)::
  
          tf.contrib.resampler.resampler(
              tf.transpose(inputs, [0, 2, 1, 3]), sample_coords)
  
      (No gradient is computed for ``NEAREST`` method, and
       some of the padding modes.)
      """
  
      # check the input dims
      try:
          batch_inputs = int(inputs.shape[0])
          batch_sample_coords = int(sample_coords.shape[0])
      except (TypeError, ValueError):
          tf.logging.fatal(
              "Unknown input shape, at least batch size "
              "needs to be specified."
          )
          raise
  
      if (
          batch_inputs != batch_sample_coords
          and batch_sample_coords > 1
      ):
          tf.logging.fatal(
              "\nOnly the following two cases are currently supported:\n"
              "    - batch size of inputs == batch size of sample_coords\n"
              "    - batch size of sample_coords == 1\n"
              "In the second case, sample_coords will be applied to each of "
              "the batch component of the inputs."
          )
          raise ValueError
  
      # input_spatial_rank = infer_spatial_rank(inputs)
      # if input_spatial_rank != 2 and input_spatial_rank != 3:
      #     tf.logging.fatal('Only 2D or 3D inputs are supported.')
      #     raise ValueError
  
      try:
          coords_n_dim = int(sample_coords.shape[-1])
      except (TypeError, ValueError):
          tf.logging.fatal(
              "The last dim of the coordinates must have 2 or 3 elements."
          )
          raise
  
      if infer_spatial_rank(inputs) != coords_n_dim:
          tf.logging.fatal(
              "sample_coords.shape[-1] must be the same as the spatial rank "
              "of the inputs."
          )
          raise ValueError
  
      # currently converting everything to floats
      if inputs.dtype not in SUPPORTED_INPUT_DTYPE:
          inputs = tf.to_float(inputs)
      if sample_coords.dtype not in SUPPORTED_INPUT_DTYPE:
          sample_coords = tf.to_float(sample_coords)
  
      # use fast resampling layer if available
      if self.FastResamplerLayer is not None:
          return self.FastResamplerLayer.layer_op(inputs, sample_coords)
      # otherwise compatibility resampling layer is used
      if self.interpolation == "LINEAR":
          return self._resample_linear(inputs, sample_coords)
      if self.interpolation == "NEAREST":
          return self._resample_nearest(inputs, sample_coords)
      if self.interpolation == "BSPLINE":
          return self._resample_bspline(inputs, sample_coords)
      if self.interpolation == "IDW":
          return self._resample_inv_dst_weighting(inputs, sample_coords)
      tf.logging.fatal("interpolation method not implmented")
      raise NotImplementedError
  
file_path: NiftyNet/niftynet/layer/resampler.py
imports: from __future__ import absolute_import, division, print_function
  import tensorflow
  from niftynet.layer.base_layer import Layer
  from niftynet.layer.layer_util import infer_spatial_rank
  from niftynet.utilities.util_common import look_up_operations
label_desc: Use image node_id 2 for calling the ResamplerLayer obj's underlying member method code with example usage: obj.layer_op(inputs, sample_coords) and returns: self, self, self, self, self