Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Binary focal loss: Use pre-computed probs and increase readability (#1848)

Kilian Lieretea19dff0506d
diff --git a/kornia/losses/focal.py b/kornia/losses/focal.py
index a79fe5076..e37b39c5f 100644
--- a/kornia/losses/focal.py
+++ b/kornia/losses/focal.py
@@ -210,11 +210,11 @@ def binary_focal_loss_with_logits(
KORNIA_CHECK(input.shape[-1] == pos_weight.shape[0], "Expected pos_weight equals number of classes.")
probs_pos = input.sigmoid()
- probs_neg = torch.sigmoid(-input)
+ probs_neg = (-input).sigmoid()
loss_tmp = (
- -alpha * pos_weight * probs_neg.pow(gamma) * target * input.sigmoid().log()
- - (1 - alpha) * torch.pow(probs_pos, gamma) * (1.0 - target) * (-input).sigmoid().log()
+ -alpha * pos_weight * probs_neg.pow(gamma) * target * probs_pos.log()
+ - (1 - alpha) * probs_pos.pow(gamma) * (1.0 - target) * probs_neg.log()
)
if reduction == 'none':