code: def crop_and_resize(
      image, boxes, box_ind, crop_size, pad_border=True
  ):
      """
      Aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.
  
      Args:
          image: NCHW
          boxes: nx4, x1y1x2y2
          box_ind: (n,)
          crop_size (int):
      Returns:
          n,C,size,size
      """
      assert isinstance(crop_size, int), crop_size
      boxes = tf.stop_gradient(boxes)
  
      # TF's crop_and_resize produces zeros on border
      if pad_border:
          # this can be quite slow
          image = tf.pad(
              image, [[0, 0], [0, 0], [1, 1], [1, 1]], mode="SYMMETRIC"
          )
          boxes = boxes + 1
  
      @under_name_scope()
      def transform_fpcoor_for_tf(boxes, image_shape, crop_shape):
          """
          The way tf.image.crop_and_resize works (with normalized box):
          Initial point (the value of output[0]): x0_box * (W_img - 1)
          Spacing: w_box * (W_img - 1) / (W_crop - 1)
          Use the above grid to bilinear sample.
  
          However, what we want is (with fpcoor box):
          Spacing: w_box / W_crop
          Initial point: x0_box + spacing/2 - 0.5
          (-0.5 because bilinear sample (in my definition) assumes floating point coordinate
           (0.0, 0.0) is the same as pixel value (0, 0))
  
          This function transform fpcoor boxes to a format to be used by tf.image.crop_and_resize
  
          Returns:
              y1x1y2x2
          """
          x0, y0, x1, y1 = tf.split(boxes, 4, axis=1)
  
          spacing_w = (x1 - x0) / tf.cast(crop_shape[1], tf.float32)
          spacing_h = (y1 - y0) / tf.cast(crop_shape[0], tf.float32)
  
          imshape = [
              tf.cast(image_shape[0] - 1, tf.float32),
              tf.cast(image_shape[1] - 1, tf.float32),
          ]
          nx0 = (x0 + spacing_w / 2 - 0.5) / imshape[1]
          ny0 = (y0 + spacing_h / 2 - 0.5) / imshape[0]
  
          nw = (
              spacing_w
              * tf.cast(crop_shape[1] - 1, tf.float32)
              / imshape[1]
          )
          nh = (
              spacing_h
              * tf.cast(crop_shape[0] - 1, tf.float32)
              / imshape[0]
          )
  
          return tf.concat([ny0, nx0, ny0 + nh, nx0 + nw], axis=1)
  
      image_shape = tf.shape(image)[2:]
  
      boxes = transform_fpcoor_for_tf(
          boxes, image_shape, [crop_size, crop_size]
      )
      image = tf.transpose(image, [0, 2, 3, 1])  # nhwc
      ret = tf.image.crop_and_resize(
          image,
          boxes,
          tf.cast(box_ind, tf.int32),
          crop_size=[crop_size, crop_size],
      )
      ret = tf.transpose(ret, [0, 3, 1, 2])  # ncss
      return ret
  
file_path: tensorpack/examples/FasterRCNN/modeling/model_box.py
imports: import numpy
  import tensorflow
  from collections import namedtuple
  from tensorpack.tfutils.scope_utils import under_name_scope
  from config import config
label_desc: Use image node_id 4 for calling a global function with example usage: crop_and_resize(image, boxes, box_ind, crop_size, pad_border) and returns: ret, tf