Skip to content

Dice Losses computed for the whole batch #712

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

Closed
lassefschmidt opened this issue Jan 20, 2023 · 2 comments
Closed

Dice Losses computed for the whole batch #712

lassefschmidt opened this issue Jan 20, 2023 · 2 comments
Labels

Comments

@lassefschmidt
Copy link

lassefschmidt commented Jan 20, 2023

Hello there!

First of all, thank you for this great package!

Maybe I am mistaken, but when looking into the implementation of the dice loss I noticed that it computes the loss across the whole batch rather than over each image. At least for me this lead to extremely bouncy gradients when running loss.backward(). After going over some literature, I also always found that the loss should be computed across each image and then reduced (e.g. in most cases just the mean). In fact, computing it for the whole batch vs. for each image and then average gives very different results.

Luckily, the fix is very easy. In this script, change within the code of class DiceLoss the forward function line 72 to dims = (2) instead of dims = (0,2).

At least this gives the desired outputs for multiclass problems, no matter the batchsize (tested batch sizes 1 and 3 and 32 and all worked perfectly fine). I didn't test other cases though.

Best,
Lasse

@github-actions
Copy link

github-actions bot commented May 2, 2023

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label May 2, 2023
@github-actions
Copy link

github-actions bot commented May 9, 2023

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as completed May 9, 2023
nistring added a commit to nistring/segmentation_models.pytorch that referenced this issue May 13, 2025
The dice loss varies with batch sizes, and this issue remains unresolved.
qubvel-org#712 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant