Skip to content

fix(losses): bin each sample from its own range in b-spline GlobalMutualInformationLoss - #9186

Open
tritsystem wants to merge 1 commit into
Project-MONAI:devfrom
tritsystem:fix/gmi-bspline-per-sample-range
Open

tritsystem wants to merge 1 commit into
Project-MONAI:devfrom
tritsystem:fix/gmi-bspline-per-sample-range

Conversation

@tritsystem

Copy link
Copy Markdown
Contributor

File: monai/losses/image_dissimilarity.py. This loss is used to compare how similar two images are (common in image registration -- lining up a scan with a reference scan). For one kernel option ("b-spline"), it needs to find the min and max brightness value in the image to build a histogram. Instead of looking at each image's own min/max, the code was computing min/max across the whole batch -- so if you feed it 4 images at once, one bright image can shift the histogram for all the others. I tested this directly: the same image scored -1.2792 alone, but -0.1958 when a brighter image shared its batch -- 85% of the signal gone, just from batch composition. That's a real correctness bug, not just a style issue.

Fix: per-sample torch.amax/torch.amin over the non-batch dimensions. A batch of 1 gives the same results as before. The Gaussian kernel is unaffected.

Types of changes

  • Non-breaking change (arguably a behaviour change for batch > 1, since that's the bug being fixed)
  • New tests added to cover the changes
  • In-line docstrings updated
  • Documentation updated

Fixes # .

…ualInformationLoss

parzen_windowing_b_spline took torch.max/torch.min over the whole batch, so
a sample's loss depended on the intensity range of the other images in the
batch. Use a per-sample amax/amin, as ITK's Mattes MI does for a single
image pair. Batch-of-1 results are unchanged.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: tritsystem <gbranaa4@gmail.com>
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bf2d619e-c611-4692-95ff-a01c69eb4af5
📥 Commits

Reviewing files that changed from the base of the PR and between 6aa4abf and d0710d9.

📒 Files selected for processing: 2
  • monai/losses/image_dissimilarity.py
  • tests/losses/image_dissimilarity/test_global_mutual_information_loss.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

B-spline windowing now computes minimum and maximum intensity values independently for each batch item. A regression test checks that per-sample losses for mixed-range images match losses computed for each image separately.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Merge Risk: ⚪ Minimal · up to d0710

The B-spline path now bins each sample using its own intensity range, addressing batch-composition-dependent results. No actionable merge-blocking risk is established; normal CI should run the loss tests.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the per-sample B-spline range fix in GlobalMutualInformationLoss.
Description check Passed The description explains the bug, the fix, and the added tests. The issue number is blank, and it does not say whether the listed test commands were run; these omissions do not make the description la…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the bug, the fix, and the added tests. The issue number is blank, and it does not say whether the listed test commands were run; these omissions do not make the description largely incomplete.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant