SipMask-VIS/mmdet/models/anchor_heads/sipmask_head.py
def crop_split(masks00, masks01, masks10, masks11, boxes, masksG=None):
    """
    "Crop" predicted masks by zeroing out everything not in the predicted bbox.
    Vectorized by Chong (thanks Chong).

    Args:
        - masks should be a size [h, w, n] tensor of masks
        - boxes should be a size [n, 4] tensor of bbox coords in relative point form
    """

    h, w, n = masks00.size()
    rows = torch.arange(w, device=masks00.device, dtype=boxes.dtype).view(1, -1, 1).expand(h, w, n)
    cols = torch.arange(h, device=masks00.device, dtype=boxes.dtype).view(-1, 1, 1).expand(h, w, n)

    x1, x2 = boxes[:, 0], boxes[:, 2]  # sanitize_coordinates(boxes[:, 0], boxes[:, 2], w, padding, cast=False)
    y1, y2 = boxes[:, 1], boxes[:, 3]  # sanitize_coordinates(boxes[:, 1], boxes[:, 3], h, padding, cast=False)
    xc = (x1 + x2) / 2
    yc = (y1 + y2) / 2
    x1 = torch.clamp(x1, min=0, max=w - 1)
    y1 = torch.clamp(y1, min=0, max=h - 1)
    x2 = torch.clamp(x2, min=0, max=w - 1)
    y2 = torch.clamp(y2, min=0, max=h - 1)
    xc = torch.clamp(xc, min=0, max=w - 1)
    yc = torch.clamp(yc, min=0, max=h - 1)

    ##x1,y1,xc,yc
    crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < xc.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                cols < yc.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()

    masks00 = masks00 * crop_mask

    ##xc,y1,x2,yc
    crop_mask = (rows >= xc.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                cols < yc.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks01 = masks01 * crop_mask

    crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < xc.view(1, 1, -1)) & (cols >= yc.view(1, 1, -1)) & (
                cols < y2.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks10 = masks10 * crop_mask

    crop_mask = (rows >= xc.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= yc.view(1, 1, -1)) & (
                cols < y2.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks11 = masks11 * crop_mask

    masks = masks00 + masks01 + masks10 + masks11

    ########whole
    if masksG is not None:
        crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                    cols < y2.view(1, 1, -1))
        crop_mask = crop_mask.float()

        masksG = masksG * crop_mask
        return masks, masksG

    return masks

    "Crop" predicted masks by zeroing out everything not in the predicted bbox.
    Vectorized by Chong (thanks Chong).

    Args:
        - masks should be a size [h, w, n] tensor of masks
        - boxes should be a size [n, 4] tensor of bbox coords in relative point form
    
"Crop" predicted masks by zeroing out everything not in the predicted bbox.
Vectorized by Chong (thanks Chong).

masks should be a size [h, w, n] tensor of masks
boxes should be a size [n, 4] tensor of bbox coords in relative point form
" Crop " predicted masks by zeroing out everything not in the predicted bbox . Vectorized by Chong ( thanks Chong ) . masks should be a size [ h w n ] tensor of masks boxes should be a size [ n 4 ] tensor of bbox coords in relative point form
def crop_split(masks00, masks01, masks10, masks11, boxes, masksG=None):
    h, w, n = masks00.size()
    rows = torch.arange(w, device=masks00.device, dtype=boxes.dtype).view(1, -1, 1).expand(h, w, n)
    cols = torch.arange(h, device=masks00.device, dtype=boxes.dtype).view(-1, 1, 1).expand(h, w, n)
    x1, x2 = boxes[:, 0], boxes[:, 2]  
    y1, y2 = boxes[:, 1], boxes[:, 3]  
    xc = (x1 + x2) / 2
    yc = (y1 + y2) / 2
    x1 = torch.clamp(x1, min=0, max=w - 1)
    y1 = torch.clamp(y1, min=0, max=h - 1)
    x2 = torch.clamp(x2, min=0, max=w - 1)
    y2 = torch.clamp(y2, min=0, max=h - 1)
    xc = torch.clamp(xc, min=0, max=w - 1)
    yc = torch.clamp(yc, min=0, max=h - 1)
    crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < xc.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                cols < yc.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks00 = masks00 * crop_mask
    crop_mask = (rows >= xc.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                cols < yc.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks01 = masks01 * crop_mask
    crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < xc.view(1, 1, -1)) & (cols >= yc.view(1, 1, -1)) & (
                cols < y2.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks10 = masks10 * crop_mask
    crop_mask = (rows >= xc.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= yc.view(1, 1, -1)) & (
                cols < y2.view(1, 1, -1))
    crop_mask = crop_mask.float().detach()
    masks11 = masks11 * crop_mask
    masks = masks00 + masks01 + masks10 + masks11
    if masksG is not None:
        crop_mask = (rows >= x1.view(1, 1, -1)) & (rows < x2.view(1, 1, -1)) & (cols >= y1.view(1, 1, -1)) & (
                    cols < y2.view(1, 1, -1))
        crop_mask = crop_mask.float()
        masksG = masksG * crop_mask
        return masks, masksG
    return masks
def crop_split ( masks00 , masks01 , masks10 , masks11 , boxes , masksG = None ) : h , w , n = masks00 . size ( ) rows = torch . arange ( w , device = masks00 . device , dtype = boxes . dtype ) . view ( 1 , - 1 , 1 ) . expand ( h , w , n ) cols = torch . arange ( h , device = masks00 . device , dtype = boxes . dtype ) . view ( - 1 , 1 , 1 ) . expand ( h , w , n ) x1 , x2 = boxes [ : , 0 ] , boxes [ : , 2 ] y1 , y2 = boxes [ : , 1 ] , boxes [ : , 3 ] xc = ( x1 + x2 ) / 2 yc = ( y1 + y2 ) / 2 x1 = torch . clamp ( x1 , min = 0 , max = w - 1 ) y1 = torch . clamp ( y1 , min = 0 , max = h - 1 ) x2 = torch . clamp ( x2 , min = 0 , max = w - 1 ) y2 = torch . clamp ( y2 , min = 0 , max = h - 1 ) xc = torch . clamp ( xc , min = 0 , max = w - 1 ) yc = torch . clamp ( yc , min = 0 , max = h - 1 ) crop_mask = ( rows >= x1 . view ( 1 , 1 , - 1 ) ) & ( rows < xc . view ( 1 , 1 , - 1 ) ) & ( cols >= y1 . view ( 1 , 1 , - 1 ) ) & ( cols < yc . view ( 1 , 1 , - 1 ) ) crop_mask = crop_mask . float ( ) . detach ( ) masks00 = masks00 * crop_mask crop_mask = ( rows >= xc . view ( 1 , 1 , - 1 ) ) & ( rows < x2 . view ( 1 , 1 , - 1 ) ) & ( cols >= y1 . view ( 1 , 1 , - 1 ) ) & ( cols < yc . view ( 1 , 1 , - 1 ) ) crop_mask = crop_mask . float ( ) . detach ( ) masks01 = masks01 * crop_mask crop_mask = ( rows >= x1 . view ( 1 , 1 , - 1 ) ) & ( rows < xc . view ( 1 , 1 , - 1 ) ) & ( cols >= yc . view ( 1 , 1 , - 1 ) ) & ( cols < y2 . view ( 1 , 1 , - 1 ) ) crop_mask = crop_mask . float ( ) . detach ( ) masks10 = masks10 * crop_mask crop_mask = ( rows >= xc . view ( 1 , 1 , - 1 ) ) & ( rows < x2 . view ( 1 , 1 , - 1 ) ) & ( cols >= yc . view ( 1 , 1 , - 1 ) ) & ( cols < y2 . view ( 1 , 1 , - 1 ) ) crop_mask = crop_mask . float ( ) . detach ( ) masks11 = masks11 * crop_mask masks = masks00 + masks01 + masks10 + masks11 if masksG is not None : crop_mask = ( rows >= x1 . view ( 1 , 1 , - 1 ) ) & ( rows < x2 . view ( 1 , 1 , - 1 ) ) & ( cols >= y1 . view ( 1 , 1 , - 1 ) ) & ( cols < y2 . view ( 1 , 1 , - 1 ) ) crop_mask = crop_mask . float ( ) masksG = masksG * crop_mask return masks , masksG return masks
"Crop" predicted masks by zeroing out everything not in the predicted bbox.
" Crop " predicted masks by zeroing out everything not in the predicted bbox .
"""
    "Crop" predicted masks by zeroing out everything not in the predicted bbox.
    Vectorized by Chong (thanks Chong).

    Args:
        - masks should be a size [h, w, n] tensor of masks
        - boxes should be a size [n, 4] tensor of bbox coords in relative point form
    """ # sanitize_coordinates(boxes[:, 0], boxes[:, 2], w, padding, cast=False) # sanitize_coordinates(boxes[:, 1], boxes[:, 3], h, padding, cast=False) ##x1,y1,xc,yc ##xc,y1,x2,yc ########whole
masks00
masks01
masks10
masks11
boxes
masksG