Skip to content

Commit 8491fdb

Browse files
林旻佑林旻佑
authored andcommitted
Fix #8366: Add strict shape validation to sliding_window_inference
Signed-off-by: 林旻佑 <[email protected]>
1 parent e267705 commit 8491fdb

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

monai/inferers/utils.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,56 @@
3333
optional_import,
3434
)
3535

36-
tqdm, _ = optional_import("tqdm", name="tqdm")
36+
37+
tqdm, _ = optional_import("tqdm", name="tqdm")
3738
_nearest_mode = "nearest-exact"
3839

3940
__all__ = ["sliding_window_inference"]
4041

4142

43+
44+
"""
45+
Enforce channel-first layout (NCHW/NCDHW) without guessing.
46+
47+
Args:
48+
t: Input tensor to validate.
49+
name: Name of the tensor for error messages.
50+
num_classes: Optional expected channel count at dim=1.
51+
allow_binary_two_channel: If True and num_classes==2, accept C=2.
52+
53+
Raises:
54+
ValueError: If tensor shape doesn't match channel-first layout or
55+
num_classes constraint.
56+
57+
Note:
58+
- Accepts only 4D (NCHW) or 5D (NCDHW) tensors with channel at dim=1.
59+
- Non-tensors and tensors with other dimensionalities are silently ignored.
60+
- Users must apply EnsureChannelFirst/EnsureChannelFirstd upstream for
61+
channel-last data.
62+
"""
63+
if not isinstance(t, torch.Tensor):
64+
return
65+
if t.ndim not in (4, 5):
66+
return
67+
68+
c = int(t.shape[1])
69+
layout = "NCHW" if t.ndim == 4 else "NCDHW"
70+
layout_last = "NHWC" if t.ndim == 4 else "NDHWC"
71+
72+
if num_classes is not None:
73+
ok = (c == num_classes) or (num_classes == 1 and c == 1)
74+
if allow_binary_two_channel and num_classes == 2:
75+
ok = ok or (c == 2)
76+
if not ok:
77+
raise ValueError(
78+
f"{name}: expected {layout} with C(dim=1)==num_classes, "
79+
f"but got shape={tuple(t.shape)} (C={c}) and num_classes={num_classes}. "
80+
f"If your data is {layout_last}, please apply EnsureChannelFirst/EnsureChannelFirstd upstream."
81+
)
82+
# No guessing when num_classes is None; we simply require channel at dim=1.
83+
# If callers provided NHWC/NDHWC, they must convert upstream.
84+
85+
4286
def sliding_window_inference(
4387
inputs: torch.Tensor | MetaTensor,
4488
roi_size: Sequence[int] | int,
@@ -131,11 +175,29 @@ def sliding_window_inference(
131175
kwargs: optional keyword args to be passed to ``predictor``.
132176
133177
Note:
134-
- input must be channel-first and have a batch dim, supports N-D sliding window.
178+
- Inputs must be channel-first and have a batch dim (NCHW / NCDHW).
179+
- If your data is NHWC/NDHWC, please apply `EnsureChannelFirst` / `EnsureChannelFirstd` upstream.
135180
136181
"""
137-
buffered = buffer_steps is not None and buffer_steps > 0
138182
num_spatial_dims = len(inputs.shape) - 2
183+
184+
# Only perform strict shape validation if roi_size is a sequence (explicit dimensions).
185+
# If roi_size is an integer, it is broadcast to all dimensions, so we cannot
186+
# infer the expected dimensionality to enforce a strict check here.
187+
if not isinstance(roi_size, int):
188+
roi_dims = len(roi_size)
189+
if num_spatial_dims != roi_dims:
190+
raise ValueError(
191+
f"inputs must have {roi_dims + 2} dimensions for {roi_dims}D roi_size "
192+
f"(Batch, Channel, {', '.join(['Spatial'] * roi_dims)}), "
193+
f"but got inputs shape {inputs.shape}.\n"
194+
"If you have channel-last data (e.g. B, D, H, W, C), please use "
195+
"monai.transforms.EnsureChannelFirst or EnsureChannelFirstd upstream."
196+
)
197+
# -----------------------------------------------------------------
198+
# ---- Strict validation: do NOT guess or permute layouts ----
199+
200+
buffered = buffer_steps is not None and buffer_steps > 0
139201
if buffered:
140202
if buffer_dim < -num_spatial_dims or buffer_dim > num_spatial_dims:
141203
raise ValueError(f"buffer_dim must be in [{-num_spatial_dims}, {num_spatial_dims}], got {buffer_dim}.")

tests/inferers/test_sliding_window_inference.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,26 @@ def compute_dict(data):
372372
for rr, _ in zip(result_dict, expected_dict):
373373
np.testing.assert_allclose(result_dict[rr].cpu().numpy(), expected_dict[rr], rtol=1e-4)
374374

375+
def test_strict_shape_validation(self):
376+
"""Test strict shape validation to ensure inputs match roi_size dimensions."""
377+
device = "cpu"
378+
roi_size = (16, 16, 16)
379+
sw_batch_size = 4
380+
381+
def predictor(data):
382+
return data
383+
384+
# Case 1: Input has fewer dimensions than expected (e.g., missing Batch or Channel)
385+
# 3D roi_size requires 5D input (B, C, D, H, W), giving 4D here.
386+
inputs_4d = torch.randn((1, 16, 16, 16), device=device)
387+
with self.assertRaisesRegex(ValueError, "inputs must have 5 dimensions"):
388+
sliding_window_inference(inputs_4d, roi_size, sw_batch_size, predictor)
389+
390+
# Case 2: Input is 3D (missing Batch AND Channel)
391+
inputs_3d = torch.randn((16, 16, 16), device=device)
392+
with self.assertRaisesRegex(ValueError, "inputs must have 5 dimensions"):
393+
sliding_window_inference(inputs_3d, roi_size, sw_batch_size, predictor)
394+
375395

376396
class TestSlidingWindowInferenceCond(unittest.TestCase):
377397
@parameterized.expand(TEST_CASES)

0 commit comments

Comments
 (0)