Skip to content

Fix MS-SSIM size validation across scale counts - #9202

Open
Don-Yin wants to merge 1 commit into
Project-MONAI:devfrom
Don-Yin:fix-ms-ssim-size-gate
Open

Don-Yin wants to merge 1 commit into
Project-MONAI:devfrom
Don-Yin:fix-ms-ssim-size-gate

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 20, 2026

Copy link
Copy Markdown

Description

I corrected the minimum-size check in compute_ms_ssim to follow its stride-2 pooling. With L scales, the final SSIM calculation sees each spatial dimension divided by 2 ** (L - 1), not max(1, L - 1) ** 2.

The old check rejects valid four-scale inputs and lets undersized two-scale inputs reach a convolution error. With the default 11-pixel kernel:

  • An 88 × 88 image with four scales is valid: the SSIM inputs are 88 → 44 → 22 → 11. It now returns 1 for identical images instead of raising ValueError.
  • A 16 × 16 image with two scales is too small because its second scale is only 8 × 8. It now raises ValueError before convolution, with the correct minimum of 22 pixels.

I also updated the error message to identify the affected spatial dimension and report its exact minimum. The SSIM calculation and weights are unchanged; the default five-scale size threshold is unchanged.

Evidence

I checked 96 combinations of square image size and scale count using identical synthetic images and the default kernel. For the feasibility panel, I disabled only the old size precheck and ran the actual pooling and convolution operations. The old gate falsely accepts five combinations and falsely rejects two; the fixed gate agrees with feasibility in all 96 combinations. Outlined cells mark disagreements in the old gate.

MS-SSIM image-size and scale-count gate comparison

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.
  • In-line docstrings updated.

Validation

  • python -m unittest tests.metrics.test_compute_multiscalessim_metric tests.metrics.test_ssim_metric tests.losses.test_ssim_loss -v: 36 tests passed.
  • The added tests cover minimum sizes and undersized spatial dimensions in 2D and 3D with one through six scales, anisotropic kernels, and the two examples above.
  • black --check, isort --check-only, and ruff check passed for both changed files.

I used an AI coding assistant to help investigate and draft this; I reviewed and tested every change.

Use the stride-2 downsampling factor when checking the last SSIM scale.
Report the exact minimum size of the affected spatial dimension and add
2D and 3D boundary tests for one through six scales.

Signed-off-by: Don Yin <Don_Yin@outlook.com>
Assisted-by: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 20, 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: 9876ad4d-d096-4c5f-8ae9-f783640a1903
📥 Commits

Reviewing files that changed from the base of the PR and between bbebca6 and d4dc2b2.

📒 Files selected for processing: 2
  • monai/metrics/regression.py
  • tests/metrics/test_compute_multiscalessim_metric.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

compute_ms_ssim now checks spatial dimensions against the kernel size and number of scales, and reports the minimum required dimension when an input is too small. Tests cover minimum valid and undersized 2D and 3D inputs across one to six scales, plus default-kernel and two-scale cases.

Priority: ⬇️ Low

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

Merge Risk: ⚪ Minimal · up to d4dc2

The size-validation change is ready to merge after normal checks; no material issue remains identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: correcting MS-SSIM size validation across different scale counts.
Description check Passed The description explains the change, provides examples, lists affected change types, documents validation, and identifies new tests. The issue reference line from the template is missing, and several …
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 change, provides examples, lists affected change types, documents validation, and identifies new tests. The issue reference line from the template is missing, and several optional checklist items remain unchecked, but the description is otherwise complete.

✨ 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