Project-MONAI / Project-MONAI/monai-deploy-app-sdk
MonaiSegInferenceOperator: padding_mode='reflect' crashes on small (e.g. pediatric-scale) inputs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 138
- Forks
- 70
- PR merge metrics
- No merged PRs in 30d
Description
Summary
Follow-up testing on #547 (thanks @bluna301 for the mode/padding_mode argument support, approved by @MMelQin) — as discussed in the Aug 6, 2025 WG call, testing the updated MonaiSegInferenceOperator sliding-window inference on small pediatric-scale CT volumes with different padding settings.
padding_mode="reflect" raises a RuntimeError when the input volume is smaller than the ROI by more than the input's own dimension size along any axis — a scenario common in pediatric imaging, where volumes are meaningfully smaller than adult-scale CTs. constant, replicate, and circular all handle the same input correctly; only reflect fails.
Repro
import torch
from monai.inferers import sliding_window_inference
from monai.networks.nets import SegResNet
# Pediatric-scale volume, smaller than a typical adult ROI, forcing the
# "pad input up to roi_size" path that padding_mode governs.
img = torch.rand((1, 1, 48, 48, 32))
model = SegResNet(spatial_dims=3, in_channels=1, out_channels=2).eval()
with torch.no_grad():
sliding_window_inference(
inputs=img,
roi_size=(96, 96, 96),
sw_batch_size=1,
predictor=model,
overlap=0.25,
mode="constant",
padding_mode="reflect", # fails; "constant"/"replicate"/"circular" all pass
)
Result:
RuntimeError: Argument #4: Padding size should be less than the corresponding
input dimension, but got: padding (32, 32) at dimension 4 of input [1, 1, 48, 48, 32]
Full matrix tested (both mode values x all four padding_mode values): 6/8 pass, the 2 failures are both padding_mode="reflect" (with mode="constant" and mode="gaussian").
Root cause
This is a torch.nn.functional.pad constraint: reflect-padding requires the pad amount on each side to be strictly less than the corresponding input dimension. When roi_size exceeds the input by more than the input's own extent along an axis (exactly the pediatric-vs-adult-ROI scenario), the reflect padding needed exceeds what PyTorch allows.
Suggested handling
Not proposing a specific fix here, opening for discussion first — options as I see them:
- Validate at operator construction/call time and raise a clearer error naming the exact constraint (better than the raw PyTorch traceback) when
padding_mode="reflect"is selected with an ROI/input combination that will violate it. - Fall back to
padding_mode="constant"with a logged warning when the reflect constraint can't be satisfied. - Document the limitation in the operator's docstring / #547's argument description so it's a known tradeoff rather than a surprise crash.
Happy to submit a PR for whichever direction the team prefers.
Environment: torch 2.8.0+cpu, monai 1.6.0, Python 3.13, Windows.
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 by reproducing the failure through monai.inferers.sliding_window_inference with the pediatric-sized input and roi_size shown in the issue, then trace how MonaiSegInferenceOperator passes padding_mode. Compare the reflect behavior with constant, replicate, and circular padding. Done means the team-selected handling is implemented and the same small-input matrix no longer produces an unexpected raw RuntimeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100