-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[Fix] Added ignore_index and one hot encoding for dice loss #3237
Conversation
Added ignore_index param to forward(), also implemented one hot encoding to ensure the dims of target match pred.
Changed as the syntax a | b (equal to typing.Union[a, b]) only works above python 3.9.
Hmm, I'm not entirely sure why the pr_stage_test failed after adding the type hints... |
It's caused by the update of MMEngine... |
Hi @yeedrag, We'd like to express our appreciation for your valuable contributions to the mmsegmentation. Your efforts have significantly aided in enhancing the project's quality. If you're on WeChat, we'd also love for you to join our community there. Just add our assistant using the WeChat ID: openmmlabwx. When sending the friend request, remember to include the remark "mmsig + Github ID". Thanks again for your awesome contribution, and we're excited to have you as part of our community! |
Also, with my current pr, I haven't implemented actually using ignore_index to calculate the loss. I think I can implement it, but should I just commit the implementation in this pr or should I make a new one? Thanks. |
Might commit to this pr. |
Could you help to update some code? if self.activate:
if self.use_sigmoid:
pred = pred.sigmoid()
else:
pred = pred.softmax(dim=1) The current implementation only supports |
So I add softmax and other_act? |
Currently, we only need to add softmax, if you have a better solution, you can support adding other activation methods. |
Also added unit tests.
Done! if self.activate:
if self.use_sigmoid:
pred = pred.sigmoid()
elif pred.shape[1] != 1:
# softmax does not work when there is only 1 class
pred = pred.softmax(dim=1) I'm not sure whether to use an assert or a warning when there's only 1 class so I currently just used an elif. |
…ab#3237) Added ignore_index param to forward(), also implemented one hot encoding to ensure the dims of target matches pred. 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 Please describe the motivation of this PR and the goal you want to achieve through this PR. Attempted to solve the problems mentioned by open-mmlab#3172 ## Modification Please briefly describe what modification is made in this PR. Added ignore_index into forward function (although the dice loss itself does not actually take account for it for some reason). Added _expand_onehot_labels_dice, which takes the target with shape [N, H, W] into [N, num_classes, H, W]. ## 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. This is my first time contributing to open-source code, so I might have made some stupid mistakes. Please don't hesitate to point it out.
…ab#3237) Added ignore_index param to forward(), also implemented one hot encoding to ensure the dims of target matches pred. 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 Please describe the motivation of this PR and the goal you want to achieve through this PR. Attempted to solve the problems mentioned by open-mmlab#3172 ## Modification Please briefly describe what modification is made in this PR. Added ignore_index into forward function (although the dice loss itself does not actually take account for it for some reason). Added _expand_onehot_labels_dice, which takes the target with shape [N, H, W] into [N, num_classes, H, W]. ## 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. This is my first time contributing to open-source code, so I might have made some stupid mistakes. Please don't hesitate to point it out.
…ab#3237) Added ignore_index param to forward(), also implemented one hot encoding to ensure the dims of target matches pred. 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 Please describe the motivation of this PR and the goal you want to achieve through this PR. Attempted to solve the problems mentioned by open-mmlab#3172 ## Modification Please briefly describe what modification is made in this PR. Added ignore_index into forward function (although the dice loss itself does not actually take account for it for some reason). Added _expand_onehot_labels_dice, which takes the target with shape [N, H, W] into [N, num_classes, H, W]. ## 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. This is my first time contributing to open-source code, so I might have made some stupid mistakes. Please don't hesitate to point it out.
Added ignore_index param to forward(),
also implemented one hot encoding to ensure the dims of target matches pred.
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
Please describe the motivation of this PR and the goal you want to achieve through this PR.
Attempted to solve the problems mentioned by #3172
Modification
Please briefly describe what modification is made in this PR.
Added ignore_index into forward function (although the dice loss itself does not actually take account for it for some reason).
Added _expand_onehot_labels_dice, which takes the target with shape [N, H, W] into [N, num_classes, H, W].
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
This is my first time contributing to open-source code, so I might have made some stupid mistakes. Please don't hesitate to point it out.