You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
decollate batch different behavior with numpy and torch for scalars #8471
Description
I noticed a different behavior with decollate_batch when using numpy vs torch scalars: the function allow decollating batches containing torch scalars but not numpy scalars. It becomes an issue when trying to decollate images containing numpy scalar metadata (for example images loaded with LoadImaged).
To Reproduce
importnumpyasnpimporttorchfrommonai.dataimportdecollate_batchdecollated=decollate_batch({"value": np.array(1)})
# TypeError: iteration over a 0-d array# But this worksdecollated=decollate_batch({"value": torch.tensor(1)})
In practice I encountered this issue with scalars saved inside meta tensor metadata:
frommonai.dataimportMetaTensorct=MetaTensor(torch.randn(2, 1, 64, 64, 64), meta={"value": np.array(1)})
decollated=decollate_batch({"ct": ct})
# TypeError: iteration over a 0-d array# But this worksct=MetaTensor(torch.randn(2, 1, 64, 64, 64), meta={"ok": torch.tensor(1)})
decollated=decollate_batch({"ct": ct})
Expected behavior
I would expect that using numpy or torch would result in the same behavior, i.e. being able to decollate a batch.
Why this bug?
Because the special case for numpy arrays is not handled in the decollate_batch (while the torch case is implemented) and the code fails here
changed the title [-]decollate batch different behavior with numpy and torch[/-][+]decollate batch different behavior with numpy and torch for scalars[/+]on Jun 3, 2025
Activity
[-]decollate batch different behavior with numpy and torch[/-][+]decollate batch different behavior with numpy and torch for scalars[/+]fix: support decollate for numpy scalars (Project-MONAI#8470)
fix: support decollate for numpy scalars (Project-MONAI#8470)
fix: support decollate for numpy scalars (Project-MONAI#8470)
fix: support decollate for numpy scalars (Project-MONAI#8470)