code: def sample(img, coords):
      """
      Args:
          img: bxhxwxc
          coords: bxh2xw2x2. each coordinate is (y, x) integer.
              Out of boundary coordinates will be clipped.
      Return:
          bxh2xw2xc image
      """
      shape = img.get_shape().as_list()[1:]  # h, w, c
      batch = tf.shape(img)[0]
      shape2 = coords.get_shape().as_list()[1:3]  # h2, w2
      assert None not in shape2, coords.get_shape()
      max_coor = tf.constant(
          [shape[0] - 1, shape[1] - 1], dtype=tf.float32
      )
  
      coords = tf.clip_by_value(
          coords, 0.0, max_coor
      )  # borderMode==repeat
      coords = tf.cast(coords, tf.int32)
  
      batch_index = tf.range(batch, dtype=tf.int32)
      batch_index = tf.reshape(batch_index, [-1, 1, 1, 1])
      batch_index = tf.tile(
          batch_index, [1, shape2[0], shape2[1], 1]
      )  # bxh2xw2x1
      indices = tf.concat([batch_index, coords], axis=3)  # bxh2xw2x3
      sampled = tf.gather_nd(img, indices)
      return sampled
  
file_path: tensorpack/examples/SpatialTransformer/mnist-addition.py
imports: import argparse
  import numpy
  import os
  import cv2
  from tensorpack import tfv1
  from tensorpack import *
  from tensorpack.dataflow import dataset
  from tensorpack.tfutils import gradproc, optimizer, summary
label_desc: Use image node_id 1 for calling a global function with example usage: sample(img, coords) and returns: sampled