Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix -inf issue in focal loss

edgarriba7d6a3262a94f
diff --git a/torchgeometry/losses/focal.py b/torchgeometry/losses/focal.py
index b21fd18df..adf4a0e39 100644
--- a/torchgeometry/losses/focal.py
+++ b/torchgeometry/losses/focal.py
@@ -81,7 +81,7 @@ 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)