complete_box_iou_loss / complete_box_iou: NaN gradients for zero-height boxes, NaN for point boxes
🐛 Describe the bug
complete_box_iou_loss and complete_box_iou compute CIoU's aspect-ratio term as atan(w_gt / h_gt) - atan(w_pred / h_pred) without eps, although eps is documented as preventing division by zero.
A predicted box with zero height, which is what clamping a box to the image border produces, gives a normal-looking loss but NaN gradients for every coordinate:
import torch
import torchvision
pred = torch.tensor([[10.0, 80.0, 50.0, 80.0]], requires_grad=True) # zero height
target = torch.tensor([[10.0, 20.0, 50.0, 80.0]])
loss = torchvision.ops.complete_box_iou_loss(pred, target, reduction="sum")
loss.backward()
print(loss.item(), pred.grad) # 1.2832..., tensor([[nan, nan, nan, nan]])
A point box (zero width and height) makes the loss itself NaN, and puts NaN into the complete_box_iou matrix. Passing a larger eps doesn't help, because it isn't used in that division. generalized_box_iou_loss and distance_box_iou_loss are finite on the same boxes.
I know the docstrings ask for x1 < x2 and y1 < y2, but these are predictions, so the caller can't guarantee that during training.
Possible fix: torch.atan2(w, h + eps) in both places. For positive sizes it equals atan(w / h), it has no division to overflow, and eps covers the point box. I have it on a branch with tests, if it's useful: https://github.com/tritsystem/vision/compare/main...fix/ciou-degenerate-box-nan (two changed lines plus two tests that fail before and pass after; on 2000 random valid box pairs the loss changes by at most 1.2e-7 in float32, and complete_box_iou still scripts). One limit: a point box in float16 still gives a non-finite gradient.
AI disclosure: I used Claude Code as my assistant. It found this with a script that runs losses at singular inputs and checks the gradients, then wrote the fix and the tests. My take: zero-height boxes corrupt training, so the fix replaces the base function so there's no division by zero.
Versions
torch 2.11.0+cu128, torchvision 0.26.0+cu128, Python 3.12, Windows 11, CPU and CUDA (RTX 5060). The same code is on main (e334535).
Reproduced on Windows 11, Python 3.12, torch 2.15.0+cpu, torchvision 0.29.2+cpu, CPU: loss 1.2832, gradient tensor([[nan, nan, nan, nan]]). So it isn’t specific to CUDA.
@tritsystem, are you planning to open a PR from your branch? If you’d rather not, I’d be happy to take it, using your atan2(w, h + eps) approach with tests, and credit you in the PR. No problem either way.
Thanks for reproducing this on Windows and CPU, and sorry I hadn't replied sooner; it was a busy week. I tried to open the PR from my account and GitHub refused (a permissions error on PR creation), so please go ahead. The fix and test are in one commit, rebased on current main: https://github.com/tritsystem/vision/tree/fix/ciou-zero-size-boxes. It uses atan2(w, h + eps) in complete_box_iou_loss and complete_box_iou, with tests for zero-height and point boxes. Take it as is or adapt it; a mention is plenty.
Thanks again @tritsystem! Opened #9750 with your fix, credited to you.