Project-MONAI / Project-MONAI/MONAI
GlobalMutualInformationLoss returns NaN for constant B-spline inputs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.7k
- Forks
- 1.6k
- Avg merge
- 5d 1h
- Merged PRs (30d)
- 20
Description
Describe the bug
GlobalMutualInformationLoss(kernel_type="b-spline") returns a non-finite loss for valid constant prediction or target tensors. Constant tensors have zero intensity range, so the B-spline bin width becomes zero and normalization produces NaNs.
Two related boundary failures exist in the same path:
num_bins <= 4cannot accommodate the implementation's two-bin padding on each side, leaving a zero or negative interior denominator.- In float16, the cubic B-spline distance polynomial can overflow before masking.
To Reproduce
import torch
from monai.losses import GlobalMutualInformationLoss
pred = torch.zeros((1, 1, 8, 8), requires_grad=True)
target = torch.ones_like(pred)
loss = GlobalMutualInformationLoss(kernel_type="b-spline")
result = loss(pred, target)
print(result, torch.isfinite(result))
On the current dev revision, result is non-finite.
Expected behavior
Valid constant tensors should produce a finite loss and finite gradients. B-spline configurations with too few bins should fail at construction with a clear ValueError.
Screenshots
Not applicable.
Environment
MONAI version: 1.6.0rc1+46.g3ee058bd
MONAI revision: 3ee058bdd16dd4a566d23d3f84687c3c35268a36
Python version: 3.13.13
PyTorch version: 2.12.0
NumPy version: 2.4.6
System: macOS 27.0 arm64
CUDA available: False
Additional context
A focused fix and regression coverage are proposed in #9022.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the GlobalMutualInformationLoss entry point and reproduce the constant-tensor case from the issue. Check the B-spline path for zero ranges, too few bins, and float16 behavior, then verify finite loss and gradients, construction-time ValueError handling, and regression coverage as proposed in #9022.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100