import torch


def intersection(anchors, targets):
    """
    Compute the sizes of the intersections of `anchors` by `targets`.
    Assume both anchors and targets are in tl br format
    """
    ancs, tgts = anchors, targets
    a, t = ancs.size(0), tgts.size(0)
    ancs, tgts = ancs.unsqueeze(1).expand(
        a, t, 4), tgts.unsqueeze(0).expand(a, t, 4)
    top_left_i = torch.max(ancs[..., :2], tgts[..., :2])
    bot_right_i = torch.min(ancs[..., 2:], tgts[..., 2:])

    sizes = torch.clamp(bot_right_i - top_left_i, min=0)
    return sizes[..., 0] * sizes[..., 1]