Skip to content

Commit c52922c

Browse files
林旻佑林旻佑
authored andcommitted
Fix: add y-informed fallback when num_classes is None to robustly normalize NHWC with large C (refs #8366)
Signed-off-by: 林旻佑 <[email protected]>
1 parent 0fea070 commit c52922c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

monai/metrics/meandice.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,15 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl
314314
# --- Normalize layout to channel-first (N, C, spatial...) ---
315315
# Prefer a strong signal when available.
316316
if self.num_classes is not None:
317-
y_pred, _ = ensure_channel_first(y_pred, channel_hint=self.num_classes)
317+
y_pred, _ = ensure_channel_first(y_pred, channel_hint=self.num_classes)
318318
else:
319+
# First pass: heuristic only.
319320
y_pred, _ = ensure_channel_first(y_pred)
321+
# Fallback: if implausible vs y's layout, retry with a hint from y's last dim.
322+
if y.ndim == y_pred.ndim:
323+
plausible = {1, y.shape[1], y.shape[-1]}
324+
if y_pred.shape[1] not in plausible:
325+
y_pred, _ = ensure_channel_first(y_pred, channel_hint=int(y.shape[-1]))
320326

321327
# Infer channels after normalization (or use provided).
322328
n_ch = self.num_classes or y_pred.shape[1]

0 commit comments

Comments
 (0)