Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(losses): Keep fractional binary focal gradients finite (#5484)

A Aswanth Raj71074f77272d
diff --git a/changelog.d/5484.fixed.md b/changelog.d/5484.fixed.md
new file mode 100644
index 000000000..6d259d935
--- /dev/null
+++ b/changelog.d/5484.fixed.md
@@ -0,0 +1 @@
+Keep binary focal-loss gradients finite for large finite logits with a fractional focusing parameter (`0 < gamma < 1`).
diff --git a/kornia/losses/focal.py b/kornia/losses/focal.py
index 719041ccb..c4b848999 100644
--- a/kornia/losses/focal.py
+++ b/kornia/losses/focal.py
@@ -276,8 +276,8 @@ def binary_focal_loss_with_logits(
log_probs_neg = log_probs_neg * target_mask
log_probs_pos = log_probs_pos * target_mask
- pos_term: torch.Tensor = -log_probs_neg.exp().pow(gamma) * target * log_probs_pos
- neg_term: torch.Tensor = -log_probs_pos.exp().pow(gamma) * (1.0 - target) * log_probs_neg
+ pos_term: torch.Tensor = -(gamma * log_probs_neg).exp() * target * log_probs_pos
+ neg_term: torch.Tensor = -(gamma * log_probs_pos).exp() * (1.0 - target) * log_probs_neg
if alpha is not None:
pos_term = alpha * pos_term
neg_term = (1.0 - alpha) * neg_term
diff --git a/tests/losses/test_focal_loss.py b/tests/losses/test_focal_loss.py
index f4643ef23..00b7f9eb3 100644
--- a/tests/losses/test_focal_loss.py
+++ b/tests/losses/test_focal_loss.py
@@ -95,23 +95,25 @@ class TestBinaryFocalLossWithLogits(BaseTester):
).shape
assert actual_shape == expected_shape
- def test_dynamo(self, device, dtype, torch_optimizer):
+ @pytest.mark.parametrize("gamma", [0.5, 2.0])
+ def test_dynamo(self, device, dtype, torch_optimizer, gamma):
logits = torch.rand(2, 3, 2, dtype=dtype, device=device)
labels = torch.rand(2, 3, 2, dtype=dtype, device=device)
op = kornia.losses.binary_focal_loss_with_logits
op_optimized = torch_optimizer(op)
- args = (0.25, 2.0)
+ args = (0.25, gamma)
actual = op_optimized(logits, labels, *args)
expected = op(logits, labels, *args)
self.assert_close(actual, expected)
- def test_gradcheck(self, device, dtype):
+ @pytest.mark.parametrize("gamma", [0.5, 2.0])
+ def test_gradcheck(self, device, dtype, gamma):
logits = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
labels = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
- args = (0.25, 2.0)
+ args = (0.25, gamma)
op = kornia.losses.binary_focal_loss_with_logits
self.gradcheck(op, (logits, labels, *args))
@@ -143,6 +145,29 @@ class TestBinaryFocalLossWithLogits(BaseTester):
expected = torch.tensor([[0.0, 0.0]], dtype=dtype, device=device)
self.assert_close(actual, expected)
+ @pytest.mark.parametrize("gamma", [0.25, 0.5, 0.75, 2.0])
+ @pytest.mark.parametrize("alpha", [None, 0.25])
+ @pytest.mark.parametrize("reduction", ["none", "mean", "sum"])
+ def test_numeric_stability_backward(self, device, dtype, gamma, alpha, reduction):
+ logits = torch.tensor([[1000.0, -1000.0], [1000.0, -1000.0]], device=device, dtype=dtype, requires_grad=True)
+ labels = torch.tensor([[1.0, 0.0], [0.0, 1.0]], device=device, dtype=dtype)
+ # The focal objective tends to zero for correct predictions and |logit| for incorrect predictions.
+ expected = torch.tensor([[0.0, 0.0], [1000.0, 1000.0]], device=device, dtype=dtype)
+ expected_grad = torch.tensor([[0.0, 0.0], [1.0, -1.0]], device=device, dtype=dtype)
+ if alpha is not None:
+ factors = torch.tensor([[alpha, 1.0 - alpha], [1.0 - alpha, alpha]], device=device, dtype=dtype)
+ expected = expected * factors
+ expected_grad = expected_grad * factors
+ if reduction == "mean":
+ expected = expected.mean()
+ expected_grad = expected_grad / logits.numel()
+ elif reduction == "sum":
+ expected = expected.sum()
+
+ actual = kornia.losses.binary_focal_loss_with_logits(logits, labels, alpha, gamma, reduction)
+ self.assert_close(actual, expected)
+ self.assert_close(torch.autograd.grad(actual.sum(), logits)[0], expected_grad)
+
class TestFocalLoss(BaseTester):
@pytest.mark.parametrize("reduction", ["none", "mean", "sum"])