vision icon indicating copy to clipboard operation
vision copied to clipboard

RuntimeError: each element in list of batch should be of equal size

Open ZhiyuanChen opened this issue 6 years ago • 9 comments

🐛 Bug

"python3.7/site-packages/torch/utils/data/_utils/collate.py", line 82, in default_collate
    raise RuntimeError('each element in list of batch should be of equal size')
RuntimeError: each element in list of batch should be of equal size

To Reproduce

Steps to reproduce the behavior:

model = models.resnet50()

transform = transforms.Compose([
    transforms.Resize((480, 640)),
    transforms.ToTensor(),
])

train_dataset = datasets.CocoDetection(
    root=args.train_dataset, annFile=args.train_annotation, transform=transform)

train_loader = DataLoader(train_dataset, batch_size=64)

for (img, anno) in train_loader:
    out = model(img)

Expected behavior

forward

Environment

Please copy and paste the output from our environment collection script (or fill out the checklist below manually).

You can get the script and run it with:

wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
  • PyTorch Version (e.g., 1.0): 1.6
  • OS (e.g., Linux): Ubuntu
  • How you installed PyTorch (conda, pip, source): conda -c pytorch
  • Build command you used (if compiling from source):
  • Python version: 3.7
  • CUDA/cuDNN version: 10.2
  • GPU models and configuration: V100
  • Any other relevant information:

Additional context

https://github.com/pytorch/pytorch/issues/42654

cc @pmeier

ZhiyuanChen avatar Aug 27 '20 05:08 ZhiyuanChen

@zyc-ai the problem here is that the targets of COCODetection dataset are not of the same length (as variable number of targets per image) and thus default data loader collate function fails. You can for example setup the dataloader with a custom collate function like here

import torch

# ...

def collate_fn(batch):
    return tuple(zip(*batch))

train_loader = DataLoader(train_dataset, batch_size=64, collate_fn=collate_fn)

for (img, anno) in train_loader:
    x = torch.stack(img)
    out = model(x)

See https://github.com/pytorch/vision/tree/master/references/detection for more complete detection task example.

vfdev-5 avatar Aug 27 '20 08:08 vfdev-5

Closing following @vfdev-5 answer

fmassa avatar Aug 31 '20 12:08 fmassa

@zyc-ai the problem here is that the targets of COCODetection dataset are not of the same length (as variable number of targets per image) and thus default data loader collate function fails. You can for example setup the dataloader with a custom collate function like here

import torch

# ...

def collate_fn(batch):
    return tuple(zip(*batch))

train_loader = DataLoader(train_dataset, batch_size=64, collate_fn=collate_fn)

for (img, anno) in train_loader:
    x = torch.stack(img)
    out = model(x)

See https://github.com/pytorch/vision/tree/master/references/detection for more complete detection task example.

@vfdev-5 Thank you for answering.

The reason why this is an issue is that it will NOT raise such exception in the previous version (PyTorch 1.5). And with this issue, current demo on pytorch.org no longer works.

ZhiyuanChen avatar Sep 01 '20 06:09 ZhiyuanChen

@zyc-ai could you please detail precisely in which versions of pytorch and torchvision your code and which exactly

current demo on pytorch.org no longer works.

you are talking about ?

vfdev-5 avatar Sep 01 '20 07:09 vfdev-5

@zyc-ai could you please detail precisely in which versions of pytorch and torchvision your code and which exactly

This code works on PyTorch 1.5, and sometimes works on PyTorch 1.6. I'm not quite sure about the version of torchvision, I only specify the pytorch version to conda which handle the version of torchvision.

current demo on pytorch.org no longer works.

you are talking about ?

refers to the example on https://pytorch.org/docs/stable/torchvision/datasets.html#coco , I don't have the chance to test it, but since there isn't a collate_fn defined in example, it shouldn't work.

ZhiyuanChen avatar Sep 01 '20 07:09 ZhiyuanChen

You can fetch torchvision version with pip list | grep torchvision or in the code as import torchvision; print(torchvision.__version__). It would be nice to have precise versions of pytorch/torchvision to reproduce your issue. Thanks !

refers to the example on https://pytorch.org/docs/stable/torchvision/datasets.html#coco , I don't have the chance to test it, but since there isn't a collate_fn defined in example, it shouldn't work.

The example on the link only shows dataset usage only and not the dataloader, that's why there is no collate_fn. But I agree that it would suffer from the same problem if dataloader is contructed without collate_fn. For CocoCaption user needs to encode the sentences into tensors. It is up to user to specify the algorithm of encoding in target_transform. Same is applied for CocoDetection.

vfdev-5 avatar Sep 01 '20 08:09 vfdev-5

You can fetch torchvision version with pip list | grep torchvision or in the code as import torchvision; print(torchvision.__version__). It would be nice to have precise versions of pytorch/torchvision to reproduce your issue. Thanks !

I know that... It's just I have downgraded from PyTorch 1.6, so the previous env no longer exists (I thought it should be a PyTorch issue But considering it was PyTorch 1.6, I would assume TorchVision should be 0.7.0?

ZhiyuanChen avatar Sep 01 '20 13:09 ZhiyuanChen

I encountered the same problem, but because my sample is in map format, my solution is to use the collate_fn parameter in the DataLoader to solve this problem data_loader = torch.utils.data.DataLoader( batch_size=batch_size, dataset=data, shuffle=shuffle, num_workers=0, collate_fn=lambda x: x )

HarryYuan007 avatar Oct 12 '20 07:10 HarryYuan007

Comment the checking operation in site-packages/torch/utils/data/_utils/collate.py (line 80 81)

#if not all(len(elem) == elem_size for elem in it):
        #    raise RuntimeError('each element in list of batch should be of equal size')

That works for me with torch1.8 on Windows10.

haolsun avatar Mar 31 '21 06:03 haolsun