Team Ai
Apppublic

Shawn87377/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
0likes
box_ops.cpython-38.pyc30 linesDownload Raw Back to __pycache__
1U

2E$�c�3�@sLdZddlZddlmZdd�Zdd�Zdd	�Zd4d�Zdd
�Zdd�Z	dS)z35Utilities for bounding box manipulation and GIoU.6�N)�box_areacCsL|�d�\}}}}|d||d||d||d|g}tj|dd�S)N�����g�?��dim��unbind�torch�stack)�x�x_c�y_c�w�h�b�r�//data/arXiv/demo/Demo/xdecoder/utils/box_ops.py�box_cxcywh_to_xyxy	s78�rcCsD|�d�\}}}}||d||d||||g}tj|dd�S)Nr�rr�r9�x0�y0�x1�y1rrrr�box_xyxy_to_cxcywhs�rcCs4|�d�\}}}}||||||g}tj|dd�S)Nrrrrrrr�box_xywh_to_xyxysrc10Cs�t|�}t|�}t�|dd�ddd�f|dd�dd�f�}t�|dd�ddd�f|dd�dd�f�}||jdd�}|dd�dd�df|dd�dd�df}|dd�df||}||}	|	|fS)Nrr��min�)rr�maxr�clamp)11�boxes1�boxes2�area1�area2�lt�rb�wh�inter�union�iourrr�box_ious..,r*cCs|dd�dd�f|dd�dd�fk��s0t�|dd�dd�f|dd�dd�fk��s`t�t||�\}}t�|dd�ddd�f|dd�dd�f�}t�|dd�ddd�f|dd�dd�f�}||jdd�}|dd�dd�df|dd�dd�df}||||S)z�12    Generalized IoU from https://giou.stanford.edu/13 14    The boxes should be in [x0, y0, x1, y1] format15 16    Returns a [N, M] pairwise matrix, where N = len(boxes1)17    and M = len(boxes2)18    Nrrrr)�all�AssertionErrorr*rrrr)r r!r)r(r$r%r&�arearrr�generalized_box_iou-s00..,r.cCs�|��dkrtjd|jd�S|jdd�\}}tjd|tjd�}tjd|tjd�}t�||�\}}||�d�}|�	d��19d�d}|�|��d	��	d��
d�d}||�d�}|�	d��20d�d}	|�|��d	��	d��
d�d}21t�||22||	gd�S)23z�Compute the bounding boxes around the provided masks24 25    The masks should be in format [N, H, W] where N is the number of masks, (H, W) are the spatial dimensions.26 27    Returns a [N, 4] tensors, with the boxes in xyxy format28    r)r�)�device�����N)�dtyperrg�חA)�numelr�zerosr0�shape�arange�float�meshgrid�	unsqueeze�flattenr�masked_fill�boolrr	)�masksrr
�yr29Zx_mask�x_max�x_minZy_mask�y_max�y_minrrr�masks_to_boxesEs""rC)30�__doc__rZtorchvision.ops.boxesrrrrr*r.rCrrrr�<module>s