CoolFace
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
box_ops.py93 linesDownload Raw Back to utils
1# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved2"""3Utilities for bounding box manipulation and GIoU.4"""5import torch6from torchvision.ops.boxes import box_area7 8 9def box_cxcywh_to_xyxy(x):10    x_c, y_c, w, h = x.unbind(-1)11    b = [(x_c - 0.5 * w), (y_c - 0.5 * h),12         (x_c + 0.5 * w), (y_c + 0.5 * h)]13    return torch.stack(b, dim=-1)14 15 16def box_xyxy_to_cxcywh(x):17    x0, y0, x1, y1 = x.unbind(-1)18    b = [(x0 + x1) / 2, (y0 + y1) / 2,19         (x1 - x0), (y1 - y0)]20    return torch.stack(b, dim=-1)21 22def box_xywh_to_xyxy(x):23    x0, y0, x1, y1 = x.unbind(-1)24    b = [x0, y0, (x0 + x1), (y0 + y1)]25    return torch.stack(b, dim=-1)26 27 28# modified from torchvision to also return the union29def box_iou(boxes1, boxes2):30    area1 = box_area(boxes1)31    area2 = box_area(boxes2)32 33    lt = torch.max(boxes1[:, None, :2], boxes2[:, :2])  # [N,M,2]34    rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])  # [N,M,2]35 36    wh = (rb - lt).clamp(min=0)  # [N,M,2]37    inter = wh[:, :, 0] * wh[:, :, 1]  # [N,M]38 39    union = area1[:, None] + area2 - inter40 41    iou = inter / union42    return iou, union43 44 45def generalized_box_iou(boxes1, boxes2):46    """47    Generalized IoU from https://giou.stanford.edu/48 49    The boxes should be in [x0, y0, x1, y1] format50 51    Returns a [N, M] pairwise matrix, where N = len(boxes1)52    and M = len(boxes2)53    """54    # degenerate boxes gives inf / nan results55    # so do an early check56    assert (boxes1[:, 2:] >= boxes1[:, :2]).all()57    assert (boxes2[:, 2:] >= boxes2[:, :2]).all()58    iou, union = box_iou(boxes1, boxes2)59 60    lt = torch.min(boxes1[:, None, :2], boxes2[:, :2])61    rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:])62 63    wh = (rb - lt).clamp(min=0)  # [N,M,2]64    area = wh[:, :, 0] * wh[:, :, 1]65 66    return iou - (area - union) / area67 68 69def masks_to_boxes(masks):70    """Compute the bounding boxes around the provided masks71 72    The masks should be in format [N, H, W] where N is the number of masks, (H, W) are the spatial dimensions.73 74    Returns a [N, 4] tensors, with the boxes in xyxy format75    """76    if masks.numel() == 0:77        return torch.zeros((0, 4), device=masks.device)78 79    h, w = masks.shape[-2:]80 81    y = torch.arange(0, h, dtype=torch.float)82    x = torch.arange(0, w, dtype=torch.float)83    y, x = torch.meshgrid(y, x)84 85    x_mask = (masks * x.unsqueeze(0))86    x_max = x_mask.flatten(1).max(-1)[0]87    x_min = x_mask.masked_fill(~(masks.bool()), 1e8).flatten(1).min(-1)[0]88 89    y_mask = (masks * y.unsqueeze(0))90    y_max = y_mask.flatten(1).max(-1)[0]91    y_min = y_mask.masked_fill(~(masks.bool()), 1e8).flatten(1).min(-1)[0]92 93    return torch.stack([x_min, y_min, x_max, y_max], 1)