FocalLoss mean reduction is multiplied by the number of channels when ignore_index is set
メンテナーはふだん 3 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
まず monai/losses/focal_loss.py の FocalLoss の mean reduction 付近を読み、マスクされた要素数を AsymmetricFocalLoss の reduction と比較します。複数の loss チャネルを使い、未使用の ignore ラベルも含めて ignore_index の回帰テストを追加します。無視されたボクセルを除去した場合と同じ平均になり、未使用の ignore ラベルによって loss が変わらなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 8.8k
- フォーク
- 1.7k
- 平均マージ
- 5日 3時間
- マージ済み PR(30日)
- 13
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Project-MONAI/MONAI のほかの issue
-
partition_datasetオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Project-MONAI/MONAI#9220 ·
メンテナーはふだん 3 日以内に返信
-
partition_dataset with ratios leaves some items out of every partition対応中かも @mottopanikeiku が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Project-MONAI/MONAI#9216 ·
メンテナーはふだん 3 日以内に返信
-
DiNTS: missing graph-break regression test (follow-up to #9145)対応中かも @nilapate が 16 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
Project-MONAI/MONAI#9159 ·
メンテナーはふだん 3 日以内に返信
-
ProbMapProducer raises IndexError on numpy 2.x: SlidingPatchWSIDataset emits float locations対応中かも @nilapate が 27 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Project-MONAI/MONAI#9135 · コメント 1 件 ·
メンテナーはふだん 3 日以内に返信
-
RandGridPatch crashes for documented 0 or None patch_size entries対応中かも @Rajioba1 が 83 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Project-MONAI/MONAI#9046 ·
メンテナーはふだん 3 日以内に返信
Project-MONAI/MONAI の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Arekkazu/sgpmp-backend#744 ·
メンテナーはふだん 1 日以内に返信
-
agent/reviewer
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
projectbluefin/utah-packages#634 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue help wanted
難易度 2/5 1〜2日 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
home-assistant/core#188613 ·
メンテナーはふだん 1 日以内に返信
-
Websiteオープンgood first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 91/100
TanbirRamim/open-source-radar#314 ·
メンテナーはふだん 1 日以内に返信