Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.71k stars 1.04k forks source link

collate/stack a list of tensors > E: expected Tensor as element 1 in argument 0, but got int #7333

Closed inikishev closed 8 months ago

inikishev commented 9 months ago

Describe the bug My dataset returns a tensor and a number representing the class. PyTorch Dataloader works with that, but monai.data.Dataloader says the error:

collate/stack a list of tensors
> E: expected Tensor as element 1 in argument 0, but got int, type ['MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int', 'MetaTensor', 'int'] in collate([metatensor([[[[-0.5960, -0.5960, -0.5960,  ..., -0.5960, -0.5960, -0.5960],

And I also tried one-hot encoding labels, that again works with pytorch DataLoader then it says something like tensor sizes don't match (obviously they dont, first tensor is a 3d image and second is 1d one-hot vector).

To Reproduce dataset that returns a (tensor, int)

Expected behavior works like pytorch dataloader

Screenshots If applicable, add screenshots to help explain your problem. image

Environment

Ensuring you use the relevant python executable, please paste the output of:

python -c "import monai; monai.config.print_debug_info()"
================================
Printing MONAI config...
================================
MONAI version: 1.3.0
Numpy version: 1.26.0
Pytorch version: 2.1.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 865972f7a791bf7b42efbcd87c8402bd865b329e
MONAI __file__: D:\miniconda3\envs\pytorch311\Lib\site-packages\monai\__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 5.2.0
scikit-image version: 0.20.0
scipy version: 1.11.3
Pillow version: 10.0.1
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.16.1
tqdm version: 4.66.1
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.4
pandas version: 2.1.1
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.
clearml version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

================================
Printing system config...
================================
System: Windows
Win32 version: ('10', '10.0.22631', 'SP0', 'Multiprocessor Free')
Win32 edition: CoreSingleLanguage
Platform: Windows-10-10.0.22631-SP0
Processor: Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
Machine: AMD64
Python version: 3.11.5
Process name: python.exe
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: [popenfile(path='C:\\Program Files\\WindowsApps\\Microsoft.LanguageExperiencePackru-RU_22621.47.187.0_neutral__8wekyb3d8bbwe\\windows\\System32\\ru-RU\\d085649eb0e86d25984170bc5853bb6f\\KernelBase.dll.mui', fd=-1), popenfile(path='C:\\Program Files\\WindowsApps\\Microsoft.LanguageExperiencePackru-RU_22621.47.187.0_neutral__8wekyb3d8bbwe\\windows\\System32\\ru-RU\\ba801c41aa4aa1ad2184fabd9e61fcb4\\msvfw32.dll.mui', fd=-1), popenfile(path='C:\\Program Files\\WindowsApps\\Microsoft.LanguageExperiencePackru-RU_22621.47.187.0_neutral__8wekyb3d8bbwe\\windows\\System32\\ru-RU\\2cbeec04ec887bf07719b55cd4a612fe\\avicap32.dll.mui', fd=-1), popenfile(path='C:\\Windows\\System32\\ru-RU\\tzres.dll.mui', fd=-1), popenfile(path='C:\\Program Files\\WindowsApps\\Microsoft.LanguageExperiencePackru-RU_22621.47.187.0_neutral__8wekyb3d8bbwe\\windows\\System32\\ru-RU\\95e569d0b454ef648523801596491dac\\kernel32.dll.mui', fd=-1)]
Num physical CPUs: 4
Num logical CPUs: 8
Num usable CPUs: 8
CPU usage (%): [7.7, 6.0, 6.4, 5.5, 6.7, 4.6, 3.3, 16.2]
CPU freq. (MHz): 2496
Load avg. in last 1, 5, 15 mins (%): [0.0, 0.0, 0.0]
Disk usage (%): 39.1
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 39.8
Available memory (GB): 23.9
Used memory (GB): 15.9

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 11.8
cuDNN enabled: True
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None
cuDNN version: 8700
Current device: 0
Library compiled for CUDA architectures: ['sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'sm_37', 'sm_90', 'compute_37']
GPU 0 Name: NVIDIA GeForce GTX 1650
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 14
GPU 0 Total memory (GB): 4.0
GPU 0 CUDA capability (maj.min): 7.5
KumoLiu commented 8 months ago

Hi @stunlocked1, you can use the MONAI dataset. You can refer to this tutorial. https://github.com/Project-MONAI/tutorials/tree/main/3d_classification Thanks!