Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 26 additions & 0 deletions test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,12 @@ def test_giou_loss(self, dtype, device):
assert_iou_loss(ops.generalized_box_iou_loss, box1s, box2s, 2.5, device=device, reduction="sum")
assert_iou_loss(ops.generalized_box_iou_loss, box1s, box2s, 1.25, device=device, reduction="mean")

# Test reduction value
# reduction value other than ["none", "mean", "sum"] should raise a ValueError
with pytest.raises(ValueError, match="Invalid"):
ops.generalized_box_iou_loss(box1s, box2s, reduction="xyz")


@pytest.mark.parametrize("device", cpu_and_gpu())
@pytest.mark.parametrize("dtype", [torch.float32, torch.half])
def test_empty_inputs(self, dtype, device):
Expand All @@ -1412,6 +1418,9 @@ def test_ciou_loss(self, dtype, device):
assert_iou_loss(ops.complete_box_iou_loss, box1, box4, 1.2500, device=device)
assert_iou_loss(ops.complete_box_iou_loss, box1s, box2s, 1.2250, device=device, reduction="mean")
assert_iou_loss(ops.complete_box_iou_loss, box1s, box2s, 2.4500, device=device, reduction="sum")

with pytest.raises(ValueError, match="Invalid"):
ops.complete_box_iou_loss(box1s, box2s, reduction="xyz")

@pytest.mark.parametrize("device", cpu_and_gpu())
@pytest.mark.parametrize("dtype", [torch.float32, torch.half])
Expand All @@ -1431,6 +1440,9 @@ def test_distance_iou_loss(self, dtype, device):
assert_iou_loss(ops.distance_box_iou_loss, box1, box4, 1.2500, device=device)
assert_iou_loss(ops.distance_box_iou_loss, box1s, box2s, 1.2250, device=device, reduction="mean")
assert_iou_loss(ops.distance_box_iou_loss, box1s, box2s, 2.4500, device=device, reduction="sum")

with pytest.raises(ValueError, match="Invalid"):
ops.distance_box_iou_loss(box1s, box2s, reduction="xyz")

@pytest.mark.parametrize("device", cpu_and_gpu())
@pytest.mark.parametrize("dtype", [torch.float32, torch.half])
Expand Down Expand Up @@ -1553,6 +1565,20 @@ def test_jit(self, alpha, gamma, reduction, device, dtype, seed):

tol = 1e-3 if dtype is torch.half else 1e-5
torch.testing.assert_close(focal_loss, scripted_focal_loss, rtol=tol, atol=tol)

# Raise ValueError for anonymous reduction mode
@pytest.mark.parametrize("device", cpu_and_gpu())
@pytest.mark.parametrize("dtype", [torch.float32, torch.half])
def test_reduction_mode(self, device, dtype, reduction="xyz"):
if device == "cpu" and dtype is torch.half:
pytest.skip("Currently torch.half is not fully supported on cpu")
torch.random.manual_seed(0)
inputs, targets = self._generate_diverse_input_target_pair(device=device, dtype=dtype)
with pytest.raises(ValueError, match="Invalid"):
ops.sigmoid_focal_loss(inputs, targets, 0.25, 2, reduction)





class TestMasksToBoxes:
Expand Down
11 changes: 9 additions & 2 deletions torchvision/ops/ciou_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ def complete_box_iou_loss(
alpha = v / (1 - iou + v + eps)

loss = diou_loss + alpha * v
if reduction == "mean":

# Check reduction option and return loss accordingly
if reduction == "none":
pass
elif reduction == "mean":
loss = loss.mean() if loss.numel() > 0 else 0.0 * loss.sum()
elif reduction == "sum":
loss = loss.sum()

else:
raise ValueError(
f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
)
return loss
9 changes: 8 additions & 1 deletion torchvision/ops/diou_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ def distance_box_iou_loss(

loss, _ = _diou_iou_loss(boxes1, boxes2, eps)

if reduction == "mean":
# Check reduction option and return loss accordingly
if reduction == "none":
pass
elif reduction == "mean":
loss = loss.mean() if loss.numel() > 0 else 0.0 * loss.sum()
elif reduction == "sum":
loss = loss.sum()
else:
raise ValueError(
f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
)
return loss


Expand Down
7 changes: 7 additions & 0 deletions torchvision/ops/focal_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def sigmoid_focal_loss(
Loss tensor with the reduction option applied.
"""
# Original implementation from https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/focal_loss.py

# Check reduction option
if reduction != "none" or reduction != "mean" or reduction != "sum":
raise ValueError(
f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
)

if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(sigmoid_focal_loss)
p = torch.sigmoid(inputs)
Expand Down
10 changes: 8 additions & 2 deletions torchvision/ops/giou_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ def generalized_box_iou_loss(

loss = 1 - miouk

if reduction == "mean":
# Check reduction option and return loss accordingly
if reduction == "none":
pass
elif reduction == "mean":
loss = loss.mean() if loss.numel() > 0 else 0.0 * loss.sum()
elif reduction == "sum":
loss = loss.sum()

else:
raise ValueError(
f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
)
return loss