Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fixed the issue of NaN gradients by adding epsilon in focal loss (#924)

adi199936131d769ea3
diff --git a/kornia/losses/focal.py b/kornia/losses/focal.py
index cfbac4455..25fc006d4 100644
--- a/kornia/losses/focal.py
+++ b/kornia/losses/focal.py
@@ -196,8 +196,9 @@ def binary_focal_loss_with_logits(
probs = torch.sigmoid(input)
target = target.unsqueeze(dim=1)
- loss_tmp = -alpha * torch.pow((1. - probs), gamma) * target * torch.log(probs + eps) \
- - (1 - alpha) * torch.pow(probs, gamma) * (1. - target) * torch.log(1. - probs + eps)
+ loss_tmp = - alpha * torch.pow((1. - probs + eps), gamma) * target * torch.log(probs + eps) \
+ - (1 - alpha) * torch.pow(probs + eps, gamma) * (1. - target) * torch.log(1. - probs + eps)
+
loss_tmp = loss_tmp.squeeze(dim=1)
if reduction == 'none':