Skip to content

Commit aada4f2

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

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

monai/inferers/utils.py

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import itertools
1515
from collections.abc import Callable, Iterable, Mapping, Sequence
16-
from typing import Any
16+
from typing import Any, Optional
1717

1818
import numpy as np
1919
import torch
@@ -33,12 +33,62 @@
3333
optional_import,
3434
)
3535

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

3940
__all__ = ["sliding_window_inference"]
4041

4142

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