Skip to content

Explicitly set reduction for Focal loss #1165

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 1 commit into from
Jun 4, 2025

Conversation

simonreise
Copy link
Contributor

@simonreise simonreise commented May 27, 2025

Focal loss have a reduction parameter, but it is not set as a self.reduction value.

This does not affect loss functionality, but it can be misleading, because by default __init__ function of _Loss sets self.reduction to "mean".

For example:

>>> loss = smp.losses.FocalLoss(..., reduction="none")
>>> loss.reduction
'mean'

This can make checks like this fail, while they should pass

if hasattr(loss, "reduction") and loss.reduction != "none":
    raise ValueError("basic loss must have reduction = none")

This is a purely cosmetical addition that should not affect anything but can improve user experience

Copy link
Collaborator

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thanks, makes sense!

Copy link

codecov bot commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
segmentation_models_pytorch/losses/focal.py 0.00% 1 Missing ⚠️
Files with missing lines Coverage Δ
segmentation_models_pytorch/losses/focal.py 27.02% <0.00%> (-0.76%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@qubvel qubvel merged commit 8a15d0a into qubvel-org:main Jun 4, 2025
16 of 17 checks passed
@simonreise simonreise deleted the explicit-reduction-focal-loss branch June 4, 2025 11:58
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