Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix -inf issue in focal loss

edgarriba4992bf3091ba
diff --git a/torchgeometry/losses/focal.py b/torchgeometry/losses/focal.py
index b21fd18df..bf55fe7c4 100644
--- a/torchgeometry/losses/focal.py
+++ b/torchgeometry/losses/focal.py
@@ -81,9 +81,9 @@ class FocalLoss(nn.Module):
device=input.device, dtype=input.dtype)
# compute the actual focal loss
- prob = input_soft * target_one_hot
+ prob = input_soft * target_one_hot + self.eps
focal = -torch.log(prob) * self.alpha * (1. - prob) ** self.gamma
- loss_tmp = 1. - torch.sum(focal, dim=1)
+ loss_tmp = torch.sum(focal, dim=1)
loss = -1
if self.reduction == 'none':