Skip to content

Modify Jaccard, Dice and Tversky losses #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions segmentation_models_pytorch/losses/_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,7 @@ def soft_jaccard_score(
dims=None,
) -> torch.Tensor:
assert output.size() == target.size()
if dims is not None:
intersection = torch.sum(output * target, dim=dims)
cardinality = torch.sum(output + target, dim=dims)
else:
intersection = torch.sum(output * target)
cardinality = torch.sum(output + target)

union = cardinality - intersection
jaccard_score = (intersection + smooth) / (union + smooth).clamp_min(eps)
jaccard_score = soft_tversky_score(output, target, 1.0, 1.0, smooth, eps, dims)
return jaccard_score


Expand All @@ -177,13 +169,7 @@ def soft_dice_score(
dims=None,
) -> torch.Tensor:
assert output.size() == target.size()
if dims is not None:
intersection = torch.sum(output * target, dim=dims)
cardinality = torch.sum(output + target, dim=dims)
else:
intersection = torch.sum(output * target)
cardinality = torch.sum(output + target)
dice_score = (2.0 * intersection + smooth) / (cardinality + smooth).clamp_min(eps)
dice_score = soft_tversky_score(output, target, 0.5, 0.5, smooth, eps, dims)
return dice_score


Expand All @@ -196,15 +182,28 @@ def soft_tversky_score(
eps: float = 1e-7,
dims=None,
) -> torch.Tensor:
"""Tversky loss

References:
https://arxiv.org/pdf/2302.05666
https://arxiv.org/pdf/2303.16296

"""
assert output.size() == target.size()
if dims is not None:
intersection = torch.sum(output * target, dim=dims) # TP
fp = torch.sum(output * (1.0 - target), dim=dims)
fn = torch.sum((1 - output) * target, dim=dims)
difference = torch.norm(output - target, p=1, dim=dims)
output_sum = torch.sum(output, dim=dims)
target_sum = torch.sum(target, dim=dims)
intersection = (output_sum + target_sum - difference) / 2 # TP
fp = output_sum - intersection
fn = target_sum - intersection
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have this moved out of the if/else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @qubvel, thanks for reviewing!

Since dim=None are default in torch.norm and torch.sum, should we just remove the if/else?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the if/else, and changed torch.norm in my previous commit to torch.linalg.vector_norm, since torch.norm is deprecated and may be removed in a future PyTorch release.

else:
intersection = torch.sum(output * target) # TP
fp = torch.sum(output * (1.0 - target))
fn = torch.sum((1 - output) * target)
difference = torch.norm(output - target, p=1)
output_sum = torch.sum(output)
target_sum = torch.sum(target)
intersection = (output_sum + target_sum - difference) / 2 # TP
fp = output_sum - intersection
fn = target_sum - intersection

tversky_score = (intersection + smooth) / (
intersection + alpha * fp + beta * fn + smooth
Expand Down