Skip to content

Commit 958dac7

Browse files
arthurdjnKumoLiuericspod
authored
fix: support decollate for numpy scalars (#8470)
Fixes #8471 ### Description This PR supports numpy scalars (e.g. in the form of `np.array(1)` ) in the `decollate_batch` function (fix issue #8471). ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [x] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Arthur Dujardin <arthurdujardin.dev@gmail.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent b66fb18 commit 958dac7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

monai/data/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,12 @@ def decollate_batch(batch, detach: bool = True, pad=True, fill_value=None):
597597
type(batch).__module__ == "numpy" and not isinstance(batch, Iterable)
598598
):
599599
return batch
600+
# if scalar tensor/array, return the item itself.
601+
if getattr(batch, "ndim", -1) == 0 and hasattr(batch, "item"):
602+
return batch.item() if detach else batch
600603
if isinstance(batch, torch.Tensor):
601604
if detach:
602605
batch = batch.detach()
603-
if batch.ndim == 0:
604-
return batch.item() if detach else batch
605606
out_list = torch.unbind(batch, dim=0)
606607
# if of type MetaObj, decollate the metadata
607608
if isinstance(batch, MetaObj):

0 commit comments

Comments
 (0)