"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
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)
forward
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
conda, pip, source): conda -c pytorch
https://github.com/pytorch/pytorch/issues/42654
cc @pmeier
@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 hereimport 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 torchvisionor in the code asimport 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 )
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 )