3
Á3W^{  ã               @   sH   d dl Z d dlmZ dd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )é    N)Ú_lazy_importc             C   s   t ƒ }|j| ||ƒS )aå  
    Performs non-maximum suppression (NMS) on the boxes according
    to their intersection-over-union (IoU).

    NMS iteratively removes lower scoring boxes which have an
    IoU greater than iou_threshold with another (higher scoring)
    box.

    Parameters
    ----------
    boxes : Tensor[N, 4])
        boxes to perform NMS on. They
        are expected to be in (x1, y1, x2, y2) format
    scores : Tensor[N]
        scores for each one of the boxes
    iou_threshold : float
        discards all overlapping
        boxes with IoU < iou_threshold

    Returns
    -------
    keep : Tensor
        int64 tensor with the indices
        of the elements that have been kept
        by NMS, sorted in decreasing order of scores
    )r   Únms)ÚboxesÚscoresÚiou_thresholdZ_C© r   ú</tmp/pip-build-7wfi2gtz/torchvision/torchvision/ops/boxes.pyr      s    r   c             C   s^   | j ƒ dkr tjdtj| jd�S | jƒ }|j| ƒ|d  }| |dd…df  }t|||ƒ}|S )aþ  
    Performs non-maximum suppression in a batched fashion.

    Each index value correspond to a category, and NMS
    will not be applied between elements of different categories.

    Parameters
    ----------
    boxes : Tensor[N, 4]
        boxes where NMS will be performed. They
        are expected to be in (x1, y1, x2, y2) format
    scores : Tensor[N]
        scores for each one of the boxes
    idxs : Tensor[N]
        indices of the categories for each one of the boxes.
    iou_threshold : float
        discards all overlapping boxes
        with IoU < iou_threshold

    Returns
    -------
    keep : Tensor
        int64 tensor with the indices of
        the elements that have been kept by NMS, sorted
        in decreasing order of scores
    r   )ZdtypeÚdeviceé   N)r   )ZnumelÚtorchÚemptyZint64r	   ÚmaxZtor   )r   r   Zidxsr   Zmax_coordinateÚoffsetsZboxes_for_nmsÚkeepr   r   r   Úbatched_nms$   s    r   c             C   sd   | dd…df | dd…df  | dd…df | dd…df   }}||k||k@ }|j ƒ jdƒ}|S )a5  
    Remove boxes which contains at least one side smaller than min_size.

    Arguments:
        boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format
        min_size (int): minimum size

    Returns:
        keep (Tensor[K]): indices of the boxes that have both sides
            larger than min_size
    Né   r   é   r
   )ZnonzeroZsqueeze)r   Zmin_sizeÚwsZhsr   r   r   r   Úremove_small_boxesL   s    Br   c             C   sn   | j ƒ }| dddd…f }| dddd…f }|\}}|jd|d�}|jd|d�}tj||f|d�}|j| jƒS )zþ
    Clip boxes so that they lie inside an image of size `size`.

    Arguments:
        boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format
        size (Tuple[height, width]): size of the image

    Returns:
        clipped_boxes (Tensor[N, 4])
    .r   Nr   r
   )Úminr   )Údim)r   Úclampr   ÚstackZreshapeÚshape)r   Úsizer   Zboxes_xZboxes_yÚheightÚwidthZclipped_boxesr   r   r   Úclip_boxes_to_image^   s    r   c             C   s@   | dd…df | dd…df  | dd…df | dd…df   S )aF  
    Computes the area of a set of bounding boxes, which are specified by its
    (x1, y1, x2, y2) coordinates.

    Arguments:
        boxes (Tensor[N, 4]): boxes for which the area will be computed. They
            are expected to be in (x1, y1, x2, y2) format

    Returns:
        area (Tensor[N]): area for each box
    Nr   r   r   r
   r   )r   r   r   r   Úbox_areas   s    r   c       	      C   sÈ   t | ƒ}t |ƒ}tj| dd…ddd…f |dd…dd…f ƒ}tj| dd…ddd…f |dd…dd…f ƒ}|| jdd�}|dd…dd…df |dd…dd…df  }||dd…df | |  }|S )ad  
    Return intersection-over-union (Jaccard index) of boxes.

    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.

    Arguments:
        boxes1 (Tensor[N, 4])
        boxes2 (Tensor[M, 4])

    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    Nr   r   )r   r
   )r   r   r   r   r   )	Zboxes1Zboxes2Zarea1Zarea2ÚltÚrbZwhZinterZiour   r   r   Úbox_iou„   s    ..,r!   )	r   Ztorchvision.extensionr   r   r   r   r   r   r!   r   r   r   r   Ú<module>   s   (