π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix pos_weight in focal loss (#2323)
Royc5fb8e4c12f0
diff --git a/kornia/losses/focal.py b/kornia/losses/focal.pyindex cdee97727..b0555ca5b 100644--- a/kornia/losses/focal.py+++ b/kornia/losses/focal.py@@ -200,10 +200,10 @@ def binary_focal_loss_with_logits()if pos_weight is None:- pos_weight = torch.ones(input.shape[-1], device=input.device, dtype=input.dtype)+ pos_weight = torch.ones([input.shape[1], *([1] * len(input.shape[2:]))], device=input.device, dtype=input.dtype)KORNIA_CHECK_IS_TENSOR(pos_weight)- KORNIA_CHECK(input.shape[-1] == pos_weight.shape[0], "Expected pos_weight equals number of classes.")+ KORNIA_CHECK(input.shape[1] == pos_weight.shape[0], "Expected pos_weight equals number of classes.")probs_pos = input.sigmoid()probs_neg = (-input).sigmoid()diff --git a/test/losses/test_losses.py b/test/losses/test_losses.pyindex ef71f9990..5e464ed57 100644--- a/test/losses/test_losses.py+++ b/test/losses/test_losses.py@@ -59,7 +59,7 @@ class TestBinaryFocalLossWithLogits:reduction="mean",pos_weight=Noneif pos_weight is None- else torch.full([logits.size(-1)], pos_weight, dtype=dtype, device=device),+ else torch.full([logits.size(1), 1], pos_weight, dtype=dtype, device=device),).shape== ())