Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions torchvision/ops/giou_loss.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import torch
from torch import Tensor


def _upcast(t: Tensor) -> Tensor:
# Protects from numerical overflows in multiplications by upcasting to the equivalent higher type
if t.dtype not in (torch.float32, torch.float64):
return t.float()
return t


def generalized_box_iou_loss(
Expand Down Expand Up @@ -34,6 +42,8 @@ def generalized_box_iou_loss(
https://arxiv.org/abs/1902.09630
"""

boxes1 = _upcast(boxes1)
boxes2 = _upcast(boxes2)
x1, y1, x2, y2 = boxes1.unbind(dim=-1)
x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1)

Expand Down