Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン 初心者向け
#9,214 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

@mottopanikeiku がすでに取り組んでいます。

2026年10月22日 から。

  • #9215 @mottopanikeiku による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

まず 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Project-MONAI/MONAI のほかの issue

Project-MONAI/MONAI の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。