a
    PSic                     @   sx   d dl mZ d dlZddlmZ ddlmZmZ dejeje	e
ejd	d
dZdejeje
eejejf dddZdS )    )TupleN   )_log_api_usage_once   )_loss_inter_union_upcast_non_floatnoneHz>)boxes1boxes2	reductionepsreturnc                 C   sz   t j st j stt t| } t|}t| ||\}}|dkrf| dkrX|	 n
d|
  }n|dkrv|
 }|S )a  
    Gradient-friendly IoU loss with an additional penalty that is non-zero when the
    distance between boxes' centers isn't zero. Indeed, for two exactly overlapping
    boxes, the distance IoU is the same as the IoU loss.
    This loss is symmetric, so the boxes1 and boxes2 arguments are interchangeable.

    Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
    ``0 <= x1 < x2`` and ``0 <= y1 < y2``, and The two boxes should have the
    same dimensions.

    Args:
        boxes1 (Tensor[N, 4]): first set of boxes
        boxes2 (Tensor[N, 4]): second set of boxes
        reduction (string, optional): Specifies the reduction to apply to the output:
            ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: No reduction will be
            applied to the output. ``'mean'``: The output will be averaged.
            ``'sum'``: The output will be summed. Default: ``'none'``
        eps (float, optional): small number to prevent division by zero. Default: 1e-7

    Returns:
        Tensor: Loss tensor with the reduction option applied.

    Reference:
        Zhaohui Zheng et. al: Distance Intersection over Union Loss:
        https://arxiv.org/abs/1911.08287
    meanr   g        sum)torchjitis_scripting
is_tracingr   distance_box_iou_lossr   _diou_iou_lossnumelr   r   )r
   r   r   r   loss_ r   U/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/ops/diou_loss.pyr   	   s    $"r   )r
   r   r   r   c                 C   s   t | |\}}|||  }| jdd\}}}}	|jdd\}
}}}t||
}t||}t||}t|	|}|| d || d  | }|| d }|	| d }|
| d }|| d }|| d || d  }d| ||  }||fS )N)dimr   r   )r   unbindr   minmax)r
   r   r   Zintsctunionioux1y1x2y2x1gy1gx2gy2gZxc1Zyc1Zxc2Zyc2diagonal_distance_squaredx_py_px_gy_gcenters_distance_squaredr   r   r   r   r   <   s     r   )r   r	   )r	   )typingr   r   utilsr   _utilsr   r   Tensorstrfloatr   r   r   r   r   r   <module>   s&     6 