@@ -131,11 +131,27 @@ def sliding_window_inference(
131131 kwargs: optional keyword args to be passed to ``predictor``.
132132
133133 Note:
134- - input must be channel-first and have a batch dim, supports N-D sliding window.
134+ - Inputs must be channel-first and have a batch dim (NCHW / NCDHW).
135+ - If your data is NHWC/NDHWC, please apply `EnsureChannelFirst` / `EnsureChannelFirstd` upstream.
135136
136137 """
137- buffered = buffer_steps is not None and buffer_steps > 0
138138 num_spatial_dims = len (inputs .shape ) - 2
139+
140+ # Only perform strict shape validation if roi_size is a sequence (explicit dimensions).
141+ # If roi_size is an integer, it is broadcast to all dimensions, so we cannot
142+ # infer the expected dimensionality to enforce a strict check here.
143+ if not isinstance (roi_size , Sequence ):
144+ roi_dims = len (roi_size )
145+ if num_spatial_dims != roi_dims :
146+ raise ValueError (
147+ f"inputs must have { roi_dims + 2 } dimensions for { roi_dims } D roi_size "
148+ f"(Batch, Channel, { ', ' .join (['Spatial' ] * roi_dims )} ), "
149+ f"but got inputs shape { inputs .shape } .\n "
150+ "If you have channel-last data (e.g. B, D, H, W, C), please use "
151+ "monai.transforms.EnsureChannelFirst or EnsureChannelFirstd upstream."
152+ )
153+ # -----------------------------------------------------------------
154+ buffered = buffer_steps is not None and buffer_steps > 0
139155 if buffered :
140156 if buffer_dim < - num_spatial_dims or buffer_dim > num_spatial_dims :
141157 raise ValueError (f"buffer_dim must be in [{ - num_spatial_dims } , { num_spatial_dims } ], got { buffer_dim } ." )
0 commit comments