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

[Fix] Add avg_non_ignore in cross entropy loss #1409

Merged
merged 21 commits into from
Mar 30, 2022

Conversation

MengzhangLI
Copy link
Contributor

Motivation

Right now, MMSegmentation do not support ignore_index in loss average over non-ignored elements in CE loss.

fix #578.

old pr: #1388.

In old PR: #578, the author fixed it while it left a bug if all labels in one image is ignore_index, avg_factor would be zero and ZeroDivisionError would be rasied in weight_reduce_loss function.

Usage

Just take fcn_unet_s5-d16_128x128_40k_chase_db1.py, its base config for example:

original config:

# model settings
...
        loss_decode=dict(
            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
...

image

Just add ignore_index in decoder head or auxiliary head and add avg_non_ignore=True:

# model settings
...
        loss_decode=dict(
            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0, avg_non_ignore=True),
...

image

cfg original avg_non_ignore=True avg_non_ignore=False
deeplabv3_r18-d8_512x1024_80k_cityscapes 76.70 78.27(ms+flip) 77.07 78.82(ms+flip) -
deeplabv3_r50-d8_512x512_80k_ade20k 42.42 43.28(ms+flip) 43.08 44.31(ms+flip) 42.40 43.65(ms+flip)

Copy link
Collaborator

@MeowZheng MeowZheng left a comment

Choose a reason for hiding this comment

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

ping @ZwwWayne and @RangiLyu take a look

@MengzhangLI MengzhangLI self-assigned this Mar 23, 2022
@codecov
Copy link

codecov bot commented Mar 23, 2022

Codecov Report

Merging #1409 (a4e4b0a) into master (98984bf) will decrease coverage by 0.13%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1409      +/-   ##
==========================================
- Coverage   90.39%   90.26%   -0.14%     
==========================================
  Files         133      136       +3     
  Lines        7906     7961      +55     
  Branches     1318     1326       +8     
==========================================
+ Hits         7147     7186      +39     
- Misses        536      550      +14     
- Partials      223      225       +2     
Flag Coverage Δ
unittests 90.26% <100.00%> (-0.14%) ⬇️

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

Impacted Files Coverage Δ
mmseg/models/losses/cross_entropy_loss.py 82.71% <100.00%> (+3.30%) ⬆️
mmseg/models/losses/utils.py 87.50% <100.00%> (+5.92%) ⬆️
mmseg/datasets/hrf.py 77.77% <0.00%> (-2.23%) ⬇️
mmseg/datasets/drive.py 77.77% <0.00%> (-2.23%) ⬇️
mmseg/datasets/chase_db1.py 77.77% <0.00%> (-2.23%) ⬇️
mmseg/datasets/pascal_context.py 75.00% <0.00%> (-1.48%) ⬇️
mmseg/datasets/isaid.py 93.93% <0.00%> (-0.18%) ⬇️
mmseg/core/utils/__init__.py 100.00% <0.00%> (ø)
mmseg/core/utils/dist_util.py 55.55% <0.00%> (ø)
mmseg/datasets/samplers/distributed_sampler.py 52.38% <0.00%> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0d51115...a4e4b0a. Read the comment docs.

@MeowZheng MeowZheng merged commit a82ebad into open-mmlab:master Mar 30, 2022
This was referenced Apr 5, 2022
mob5566 pushed a commit to mob5566/mmsegmentation that referenced this pull request Apr 13, 2022
* [Fix] Add avg_non_ignore in cross entropy loss

* [Fix] Add avg_non_ignore in cross entropy loss

* add docstring

* fix ut

* fix docstring and comments

* fix

* fix bce

* fix avg_factor in BCE and add more ut

* add avg_non_ignore

* add more ut

* fix part of ut

* fix part of ut

* test avg_non_ignore would not affect ce/bce when reduction none/sum

* test avg_non_ignore would not affect ce/bce when reduction none/sum/mean

* re-organize ut

* re-organize ut

* re-organize ut

* re-organize hardcode case

* fix parts of comments

* fix another parts of comments

* fix
ZhimingNJ pushed a commit to AetrexTechnology/mmsegmentation that referenced this pull request Jun 29, 2022
* [Fix] Add avg_non_ignore in cross entropy loss

* [Fix] Add avg_non_ignore in cross entropy loss

* add docstring

* fix ut

* fix docstring and comments

* fix

* fix bce

* fix avg_factor in BCE and add more ut

* add avg_non_ignore

* add more ut

* fix part of ut

* fix part of ut

* test avg_non_ignore would not affect ce/bce when reduction none/sum

* test avg_non_ignore would not affect ce/bce when reduction none/sum/mean

* re-organize ut

* re-organize ut

* re-organize ut

* re-organize hardcode case

* fix parts of comments

* fix another parts of comments

* fix
@MengzhangLI MengzhangLI deleted the fix_ignore_index_ce branch July 15, 2022 03:36
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.

3 participants