Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

FocalLoss mean reduction is multiplied by the number of channels when ignore_index is set

Offen Anfängerfreundlich
#9,214 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 3 Tagen

@mottopanikeiku arbeitet bereits daran.

Seit 22.10.2026.

  • #9215 von @mottopanikeiku — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
82/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python, pytorch

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

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Project-MONAI/MONAI

Alle Issues in Project-MONAI/MONAI

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.