def batch_iou(boxes1, boxes2):
  """Calculates the overlap between proposal and ground truth boxes.

  Some `boxes2` may have been padded.  The returned `iou` tensor for these
  boxes will be -1.

  Args:
    boxes1: a tensor with a shape of [batch_size, N, 4]. N is the number of
      proposals before groundtruth assignment. The last dimension is the pixel
      coordinates in [ymin, xmin, ymax, xmax] form.
    boxes2: a tensor with a shape of [batch_size, MAX_NUM_INSTANCES, 4]. This
      tensor might have paddings with a negative value.

  Returns:
    iou: a tensor with as a shape of [batch_size, N, MAX_NUM_INSTANCES].
  """
  with tf.name_scope('BatchIOU'):
    y1_min, x1_min, y1_max, x1_max = tf.split(
        value=boxes1, num_or_size_splits=4, axis=2)
    y2_min, x2_min, y2_max, x2_max = tf.split(
        value=boxes2, num_or_size_splits=4, axis=2)

    # Calculates the intersection area.
    intersection_xmin = tf.maximum(x1_min, tf.transpose(x2_min, [0, 2, 1]))
    intersection_xmax = tf.minimum(x1_max, tf.transpose(x2_max, [0, 2, 1]))
    intersection_ymin = tf.maximum(y1_min, tf.transpose(y2_min, [0, 2, 1]))
    intersection_ymax = tf.minimum(y1_max, tf.transpose(y2_max, [0, 2, 1]))
    intersection_area = tf.maximum(
        (intersection_xmax - intersection_xmin), 0) * tf.maximum(
            (intersection_ymax - intersection_ymin), 0)

    # Calculates the union area.
    area1 = (y1_max - y1_min) * (x1_max - x1_min)
    area2 = (y2_max - y2_min) * (x2_max - x2_min)
    # Adds a small epsilon to avoid divide-by-zero.
    union_area = area1 + tf.transpose(area2,
                                      [0, 2, 1]) - intersection_area + 1e-8

    # Calculates IoU.
    iou = intersection_area / union_area

    # Fills -1 for padded ground truth boxes.
    padding_mask = tf.logical_and(
        tf.less(intersection_xmax, 0), tf.less(intersection_ymax, 0))
    iou = tf.where(padding_mask, -tf.ones_like(iou), iou)

    return iou
Calculates the overlap between proposal and ground truth boxes.

  Some `boxes2` may have been padded.  The returned `iou` tensor for these
  boxes will be -1.

  Args:
    boxes1: a tensor with a shape of [batch_size, N, 4]. N is the number of
      proposals before groundtruth assignment. The last dimension is the pixel
      coordinates in [ymin, xmin, ymax, xmax] form.
    boxes2: a tensor with a shape of [batch_size, MAX_NUM_INSTANCES, 4]. This
      tensor might have paddings with a negative value.

  Returns:
    iou: a tensor with as a shape of [batch_size, N, MAX_NUM_INSTANCES].
  
inference_api/src/main/object_detection/core/post_processing.py