import torch


def intersect(box_a, box_b):
  """
  Compute the intersection of two sets of boxes
  Args:
    box_a: (tensor) Shape(num_objects, 4) with tl_x, tl_y, br_x, br_y
      tl = topleft
      br = bottomright
    box_b: (tensor) Shape(num_priors, 4) with tl_x, tl_y, br_x, br_y
  Return:
    intersection: (tensor) Shape: [box_a.size(0), box_b.size(0)]
  """
  A = box_a.shape[0]
  B = box_b.shape[0]
  max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
                     box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
  min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
                     box_b[:, :2].unsqueeze(0).expand(A, B, 2))
  inter = torch.clamp((max_xy - min_xy), min=0)
  return inter[:, :, 0] * inter[:, :, 1]