Skip to content
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

[Feature] Add support for the focal Tversky loss #2791

Merged
merged 5 commits into from
May 12, 2023
Merged

[Feature] Add support for the focal Tversky loss #2791

merged 5 commits into from
May 12, 2023

Conversation

zifuwanggg
Copy link

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

The focal Tversky loss was proposed in https://arxiv.org/abs/1810.07842. It has nearly 600 citations and has been shown to be extremely useful for highly imbalanced (medical) datasets. To add support for the focal Tversky loss, only few lines of changes are needed for the Tversky loss.

Modification

Add gamma as (optional) argument in the constructor of TverskyLoss. This parameter is then passed to tversky_loss to compute the focal Tversky loss.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMDet3D.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

Reopening of previous PR.

@zifuwanggg zifuwanggg changed the title Add support for the focal Tversky loss [Feature] Add support for the focal Tversky loss Mar 24, 2023
@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Patch coverage: 50.00% and project coverage change: -0.02 ⚠️

Comparison is base (ae78cb9) 88.13% compared to head (7a93cf5) 88.11%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2791      +/-   ##
==========================================
- Coverage   88.13%   88.11%   -0.02%     
==========================================
  Files         149      149              
  Lines        9183     9187       +4     
  Branches     1539     1540       +1     
==========================================
+ Hits         8093     8095       +2     
- Misses        835      836       +1     
- Partials      255      256       +1     
Flag Coverage Δ
unittests 88.11% <50.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmseg/models/losses/tversky_loss.py 91.37% <50.00%> (-3.07%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@xiexinch xiexinch left a comment

Choose a reason for hiding this comment

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

Hi @zifuwanggg,
Thanks for your contribution! We really appreciate it. Here are some comments that should be resolved, please take a view.

Comment on lines +81 to +83
gamma (float, in [1, inf]): The focal term. When `gamma` > 1,
the loss focuses more on less accurate predictions that
have been misclassified. Default: 1.0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might add the paper link https://arxiv.org/abs/1810.07842 to the docstring above.

Copy link
Author

Choose a reason for hiding this comment

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

I've added the reference in the docstring.

loss_name='loss_tversky'):
super(TverskyLoss, self).__init__()
self.smooth = smooth
self.class_weight = get_class_weight(class_weight)
self.loss_weight = loss_weight
self.ignore_index = ignore_index
assert (alpha + beta == 1.0), 'Sum of alpha and beta but be 1.0!'
assert gamma >= 1.0, 'gamma should be at least 1.0!'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since there is an assertion statement, we might add a unit test for it.

Copy link
Author

Choose a reason for hiding this comment

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

I've added an assertion test.

Comment on lines 35 to 36
if gamma > 1.0:
tversky_loss **= gamma
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should it be 1 / gamma?
At the official implementation https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py#L67, they calculate the FTL with gamma=1/(4/3)=0.75

Copy link
Author

Choose a reason for hiding this comment

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

Yes indeed. I've modified it to 1 / gamma.

@xiexinch xiexinch merged commit 38900d5 into open-mmlab:master May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants