Exemplo n.º 1
0
def get_all_boxes_info(node_container):
    areas = np.array([((n.box[3] - n.box[1] + 1) *
                       (n.box[2] - n.box[0] + 1)).cpu().data.numpy()[0]
                      for n in node_container])
    boxes = np.vstack([n.box.cpu().data.numpy() for n in node_container])
    bbox_intersection = np.transpose(bbox_intersections_np(
        boxes, boxes))  # row-wise, box of each row is the denominator
    return areas, bbox_intersection
Exemplo n.º 2
0
def bbox_intersections(box_a, box_b):
    """ We resize both tensors to [A,B,2] without new malloc:
    [A,2] -> [A,1,2] -> [A,B,2]
    [B,2] -> [1,B,2] -> [A,B,2]
    Then we compute the area of intersect between box_a and box_b.
    Args:
      box_a: (tensor) bounding boxes, Shape: [A,4].
      box_b: (tensor) bounding boxes, Shape: [B,4].
    Return:
      (tensor) intersection area, Shape: [A,B].
    """
    if isinstance(box_a, np.ndarray):
        assert isinstance(box_b, np.ndarray)
        return bbox_intersections_np(box_a, box_b)
    A = box_a.size(0)
    B = box_b.size(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 + 1.0), min=0)
    return inter[:, :, 0] * inter[:, :, 1]