@vfdev-5, I modified your code such that it gives me the same error.
import torch
from torch.utils.data import Dataset, DataLoader

sz = 1000

images = []
for i in range(sz):
    size = torch.randint(10, 20, size=(2, ), dtype=torch.long)
    img = torch.rand(tuple(size.numpy()))
    images.append(img)
    

class VarSizeImages(Dataset):
    
    def __len__(self):
        return sz
    
    def __getitem__(self, idx):
        global images
        img = images[idx]
        label = torch.randint(0, 2, size=(1, ), dtype=torch.long).item()
        return img, label
        
def my_collate(batch):
    return [(dp[0], torch.tensor(dp[1])) for dp in batch]

ds = VarSizeImages()

loader = DataLoader(ds, batch_size=10, shuffle=True, num_workers=1, pin_memory=True, collate_fn=my_collate)

for batch in loader:
    pass

The main difference is that I don’t generate random images, but read it from the list.
I can’t understand why this code doesn’t work.