Skip to content

Slicing a MetaTensor: TypeError: only integer tensors of a single element can be converted to an index #5844

@jeanmonet

Description

@jeanmonet

The goal is to slice the tensor in half. For some reason that I cannot comprehend, it doesn't work on a particular tensor:

>>> type(x)
<class 'monai.data.meta_tensor.MetaTensor'>

>>> x.shape
torch.Size([128, 1024, 8, 64])

>>> x[slice(0, 64, None), ...]
*** TypeError: only integer tensors of a single element can be converted to an index

# OK with [0:16]:
>>> x[slice(0, 16, None), ...].shape
torch.Size([16, 1024, 8, 64])

# NOT OK with [0:32]:
>>> x[slice(0, 32, None), ...]
*** TypeError: only integer tensors of a single element can be converted to an index

>>> x[0:16].shape
torch.Size([16, 1024, 8, 64])

# ARGGGGGHHHH
>>> x[0:32].shape
*** TypeError: only integer tensors of a single element can be converted to an index

>>> x[0:17].shape
*** TypeError: only integer tensors of a single element can be converted to an index

# What if we convert to Numpy? (result: it works fine)
>>> x.detach().numpy()[0:64].shape
(64, 1024, 8, 64)

What could be going on here?

If it is of any use, the error occurs at this specific line: https://github.com/pashtari/factorizer/blob/fa7f03dfe8c390f0993cbc0d161922d2de819a1d/factorizer/factorization/operations.py#L331

To Reproduce

# This works fine, I cannot reproduce in this way
>>> from monai.data import MetaTensor
>>> x = MetaTensor(np.random.rand(128, 1024, 8, 64))
>>> x[slice(0, 64, None), ...].shape
torch.Size([128, 1024, 8, 64])

How is it possible that a specific tensor would cause such error?

Environment

Ubuntu 20.04
python 3.10

monai                     1.1.0              pyhd8ed1ab_0    conda-forge

pytorch                   1.13.1          py3.10_cuda11.7_cudnn8.5.0_0    pytorch
pytorch-cuda              11.7                 h67b0de4_1    pytorch
pytorch-lightning         1.8.1              pyhd8ed1ab_0    conda-forge
pytorch-mutex             1.0                        cuda    pytorch
torchmetrics              0.11.0             pyhd8ed1ab_0    conda-forge
torchvision               0.14.1              py310_cu117    pytorch

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions