FocalLoss mean reduction is multiplied by the number of channels when ignore_index is set
Maintainer antworten meist innerhalb von 3 Tagen
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 82/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Bereich
- machine-learning
Rechercherichtung
Lies zuerst monai/losses/focal_loss.py im Bereich der Mean-Reduktion von FocalLoss und vergleiche die Anzahl der maskierten Elemente mit der Reduktion von AsymmetricFocalLoss. Füge einen Regressionstest für ignore_index mit mehreren Loss-Kanälen hinzu, einschließlich eines ungenutzten Ignore-Labels. Fertig ist die Änderung, wenn ignorierte Voxel denselben Mittelwert ergeben wie nach ihrem Entfernen und ein ungenutztes Ignore-Label den Loss nicht verändert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
The text below was written with AI assistance (Claude and GPT-6 Astra).
With ignore_index set, FocalLoss(reduction="mean") divides the summed loss by mask.sum() (https://github.com/Project-MONAI/MONAI/blob/6920610c310200d683a3b00987a05893ccf1ca5f/monai/losses/focal_loss.py#L240-L245). The mask has one channel, but the loss has one value per voxel and channel, so the result is the masked mean times the number of loss channels. Just setting ignore_index to a label that never occurs changes the loss from x to C * x, and the gradients scale with it. The "sum" reduction and AsymmetricFocalLoss (mask.expand_as(loss).sum()) are not affected.
I found this by reading and testing the code.
To Reproduce
import torch
from monai.losses import FocalLoss
torch.manual_seed(0)
logits = torch.randn(2, 4, 16, 16, 8)
labels = torch.randint(0, 4, (2, 1, 16, 16, 8))
plain = FocalLoss(to_onehot_y=True, use_softmax=True)(logits, labels)
masked = FocalLoss(to_onehot_y=True, use_softmax=True, ignore_index=255)(logits, labels) # no voxel is 255
print(plain.item(), masked.item(), (masked / plain).item())
Output on dev (6920610):
0.30678269267082214 1.2271307706832886 4.0
On MONAI's MNI152 2 mm T1 test volume with its 6-label structural segmentation (small UNet logits), the ratio is 6.0. With the top 40% of axial slices set to 255, FocalLoss(ignore_index=255) gives 1.795147, while the same loss on only the labelled slices gives 0.299191.
Expected behavior
Ignoring voxels should give the same mean as removing them, and an unused ignore_index should not change the loss.
Environment
MONAI 1.6.1rc0+50.g6920610c (dev), PyTorch 2.14.1+cpu, NumPy 2.5.4, Python 3.12.13, Linux.
Additional context
I have a one-line fix that counts the mask once per loss channel, as AsymmetricFocalLoss already does, plus a regression test, and can open a PR.
I made this report and directed the investigation, with AI assistance from Claude and GPT-6 Astra.
- Vorherrschende Sprache
- Python
- Sterne
- 8.8k
- Forks
- 1.7k
- Ø Merge
- 5 T. 4 Std.
- Gemergte PRs (30 T.)
- 12
Entwicklungsumgebung
- Enthält ein Dockerfile oder eine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus Project-MONAI/MONAI
-
partition_dataset with ratios leaves some items out of every partitionEvtl. vergeben @mottopanikeiku hat das vor 1 Tag übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Project-MONAI/MONAI#9216 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
DiNTS: missing graph-break regression test (follow-up to #9145)Evtl. vergeben @nilapate hat das vor 15 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
Project-MONAI/MONAI#9159 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
ProbMapProducer raises IndexError on numpy 2.x: SlidingPatchWSIDataset emits float locationsEvtl. vergeben @nilapate hat das vor 25 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Project-MONAI/MONAI#9135 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 3 Tagen
-
RandGridPatch crashes for documented 0 or None patch_size entriesEvtl. vergeben @Rajioba1 hat das vor 82 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Project-MONAI/MONAI#9046 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Incorrect type hint in `pad_func`Evtl. vergeben @Harshal875 hat das vor 87 Tagen übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
Project-MONAI/MONAI#9026 ·
Maintainer antworten meist innerhalb von 3 Tagen
Alle Issues in Project-MONAI/MONAI
Ähnliche Issues
-
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 85/100
WLAN-Pi/wlanpi-profiler#341 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Memory leak: the shared `validation_aborted_error` keeps every traceback it is raised throughEvtl. vergeben @Fire162 hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
graphql-python/graphql-core#287 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 67/100
roman-corgi/ctc-dip#160 ·
Maintainer antworten meist innerhalb von 1 Tag
-
type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
render_findings.py reads and writes in the host's default encoding, so findings.html breaks on a cp1252 Windows hostEvtl. vergeben @Aaqibhafeezkhan hat das heute übernommen. Offenbug good first issue mentored python size: XS up-for-grabs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
dheerajjha/mcp-migrate#366 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag