-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Modify Dice, Jaccard and Tversky losses #8138
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
Changes from all commits
Commits
Show all changes
15 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 60c7b36
Merge branch 'dev' into 8094-modify-dice-loss
zifuwanggg a778e58
Add test cases
zifuwanggg aeef0af
Modify args description and remove check
zifuwanggg 2f77b1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 58c5396
Fix code format
zifuwanggg f07925e
Merge branch 'dev' into 8094-modify-dice-loss
zifuwanggg 185d2e1
DCO Remediation Commit for Zifu Wang <[email protected]>
zifuwanggg a6c7cf3
Merge branch 'dev' into 8094-modify-dice-loss
KumoLiu 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
| # 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 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]: | ||
| """ | ||
| Args: | ||
| input: the shape should be BNH[WD], where N is the number of classes. | ||
| target: the shape should be BNH[WD] or B1H[WD], where N is the number of classes. | ||
| reduce_axis: the axis to be reduced. | ||
| ord: the order of the vector norm. | ||
| soft_label: whether the target contains non-binary values (soft labels) or not. | ||
| If True a soft label formulation of the loss will be used. | ||
| decoupled: whether the input and the target should be decoupled when computing fp and fn. | ||
| Only for the original implementation when soft_label is False. | ||
|
|
||
| Adapted from: | ||
| https://github.com/zifuwanggg/JDTLosses | ||
| """ | ||
|
|
||
| # 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) | ||
| # the new implementation that is correct with soft labels | ||
| # and it is identical to the original implementation with hard labels | ||
| 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 | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.