From 1e0a554b5e263d00698f3215a0783598e1863c0c Mon Sep 17 00:00:00 2001 From: Arthur Dujardin Date: Mon, 29 Sep 2025 11:51:00 +0200 Subject: [PATCH 1/2] Support decollate for numpy scalars Signed-off-by: Arthur Dujardin --- monai/data/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/monai/data/utils.py b/monai/data/utils.py index 14217e9103..59136ecdbf 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -625,6 +625,8 @@ def decollate_batch(batch, detach: bool = True, pad=True, fill_value=None): type(batch).__module__ == "numpy" and not isinstance(batch, Iterable) ): return batch + if getattr(batch, "ndim", -1) == 0 and hasattr(batch, "item"): + return batch.item() if detach else batch if isinstance(batch, torch.Tensor): if detach: batch = batch.detach() From 2473d221c7fe3ad242fd09972fd0fc1e0b2dd314 Mon Sep 17 00:00:00 2001 From: Arthur Dujardin Date: Mon, 29 Sep 2025 11:58:09 +0200 Subject: [PATCH 2/2] Remove redundant decollate condition for torch/numpy scalars Signed-off-by: Arthur Dujardin --- monai/data/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/monai/data/utils.py b/monai/data/utils.py index 59136ecdbf..70a2f3ff9b 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -625,13 +625,12 @@ def decollate_batch(batch, detach: bool = True, pad=True, fill_value=None): type(batch).__module__ == "numpy" and not isinstance(batch, Iterable) ): return batch + # if scalar tensor/array, return the item itself. if getattr(batch, "ndim", -1) == 0 and hasattr(batch, "item"): return batch.item() if detach else batch if isinstance(batch, torch.Tensor): if detach: batch = batch.detach() - if batch.ndim == 0: - return batch.item() if detach else batch out_list = torch.unbind(batch, dim=0) # if of type MetaObj, decollate the metadata if isinstance(batch, MetaObj):