-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Modify Dice, Jaccard and Tversky losses #8138
Open
zifuwanggg
wants to merge
7
commits into
Project-MONAI:dev
Choose a base branch
from
zifuwanggg:8094-modify-dice-loss
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
087cf74
Modify Dice, Jaccard and Tversky losses
zifuwanggg cfd2d1e
Merge remote-tracking branch 'upstream/dev' into 8094-modify-dice-loss
zifuwanggg 3f74183
Add helper function
zifuwanggg ea8a240
Merge branch 'dev' into 8094-modify-dice-loss
KumoLiu 8c3a746
Merge branch 'Project-MONAI:dev' into 8094-modify-dice-loss
zifuwanggg 3e4f714
Fix mypy error
zifuwanggg f3ab679
Fix mypy error
zifuwanggg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import warnings | ||
|
||
import torch | ||
import torch.linalg as LA | ||
|
||
|
||
def compute_tp_fp_fn( | ||
input: torch.Tensor, | ||
target: torch.Tensor, | ||
reduce_axis: list[int], | ||
ord: int, | ||
soft_label: bool, | ||
decoupled: bool = True, | ||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
""" | ||
Adapted from: | ||
https://github.com/zifuwanggg/JDTLosses | ||
""" | ||
if torch.unique(target).shape[0] > 2 and not soft_label: | ||
warnings.warn("soft labels are used, but `soft_label == False`.") | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little worried that |
||
|
||
# the original implementation that is erroneous with soft labels | ||
if ord == 1 and not soft_label: | ||
tp = torch.sum(input * target, dim=reduce_axis) | ||
# the original implementation of Dice and Jaccard loss | ||
if decoupled: | ||
fp = torch.sum(input, dim=reduce_axis) - tp | ||
fn = torch.sum(target, dim=reduce_axis) - tp | ||
# the original implementation of Tversky loss | ||
else: | ||
fp = torch.sum(input * (1 - target), dim=reduce_axis) | ||
fn = torch.sum((1 - input) * target, dim=reduce_axis) | ||
else: | ||
pred_o = LA.vector_norm(input, ord=ord, dim=reduce_axis) | ||
ground_o = LA.vector_norm(target, ord=ord, dim=reduce_axis) | ||
difference = LA.vector_norm(input - target, ord=ord, dim=reduce_axis) | ||
|
||
if ord > 1: | ||
pred_o = torch.pow(pred_o, exponent=ord) | ||
ground_o = torch.pow(ground_o, exponent=ord) | ||
difference = torch.pow(difference, exponent=ord) | ||
|
||
tp = (pred_o + ground_o - difference) / 2 | ||
fp = pred_o - tp | ||
fn = ground_o - tp | ||
|
||
return tp, fp, fn |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clarifies a little bit I feel, the same should be done with the other modified losses.