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

Created on 27 Aug 2020  路  8Comments  路  Source: pytorch/vision

馃悰 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

datasets question

Most helpful comment

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 )

All 8 comments

@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.

Closing following @vfdev-5 answer

@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.

@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 ?

@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.

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.

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?

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 )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JingyunLiang picture JingyunLiang  路  26Comments

mcleonard picture mcleonard  路  26Comments

fmassa picture fmassa  路  34Comments

davidsteinar picture davidsteinar  路  23Comments

dssa56 picture dssa56  路  60Comments