o
    ϯi                     @   s6   d dl Z 		dde jde jdedede jf
d	d
ZdS )    NnoneHz>boxes1boxes2	reductionepsreturnc                 C   s  | j dd\}}}}|j dd\}}	}
}||k sJ d||k s(J dt||}t||	}t||
}t||}t|}||k||k@ }|| ||  || ||   ||< || ||  |
| ||	   | }|||  }t||}t||	}t||
}t||}|| ||  }||| ||   }d| }|dkr| dkr| }|S d|  }|S |d	kr| }|S )
a  
    Generalized Intersection over Union Loss (Hamid Rezatofighi et. al)
    https://arxiv.org/abs/1902.09630

    Gradient-friendly IoU loss with an additional penalty that is non-zero when the
    boxes do not overlap and scales with the size of their smallest enclosing box.
    This loss is symmetric, so the boxes1 and boxes2 arguments are interchangeable.

    Args:
        boxes1, boxes2 (Tensor): box locations in XYXY format, shape (N, 4) or (4,).
        reduction: 'none' | 'mean' | 'sum'
                 'none': No reduction will be applied to the output.
                 'mean': The output will be averaged.
                 'sum': The output will be summed.
        eps (float): small number to prevent division by zero
    )dimzbad box: x1 larger than x2zbad box: y1 larger than y2   meanr   g        sum)	unbindalltorchmaxmin
zeros_likenumelr   r   )r   r   r   r   x1y1x2y2x1gy1gx2gy2gxkis1ykis1xkis2ykis2intsctkmaskunionkioukxc1yc1xc2yc2area_cmioukloss r,   G/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/nn/giou_loss.py	giou_loss   s8   
$$r.   )r   r   )r   Tensorstrfloatr.   r,   r,   r,   r-   <module>   s   