Dataloader error by using my data

I get some pictures and want to read them by dataloader. But when I use “enumerate” to get data and labels,I get an error: RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 888 and 666 in dimension 2 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586
The number “666” and “888” is relation with bitch_size , but i don’t know the connection.

  • List item
    Traceback (most recent call last):
    File “datatest.py”, line 34, in
    _,data,lab = enumerate(val_loader)
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 286, in next
    return self._process_next_batch(batch)
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 307, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
    RuntimeError: Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 138, in default_collate
    return [default_collate(samples) for samples in transposed]
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 138, in
    return [default_collate(samples) for samples in transposed]
    File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 115, in default_collate
    return torch.stack(batch, 0, out=out)
    RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 888 and 666 in dimension 2 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586

  • List item
    from future import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import torchvision
from torchvision import datasets, models, transforms
import time
import os
data_transforms = {
‘train’: transforms.Compose([
transforms.Resize(500),
transforms.ToTensor(),
#transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
‘val’: transforms.Compose([
transforms.Resize(500),
transforms.ToTensor(),
#transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}

data_dir = ‘…/Coke’
image_train = datasets.ImageFolder(os.path.join(data_dir, ‘train’),data_transforms[‘train’])
image_val = datasets.ImageFolder(os.path.join(data_dir, ‘val’),data_transforms[‘val’])

train_loader = torch.utils.data.DataLoader(image_train,batch_size=8,shuffle=True,num_workers=4)
val_loader = torch.utils.data.DataLoader(image_val,batch_size=5,shuffle=True,num_workers=4)

_,data,lab = enumerate(val_loader)

Your code looks good and I’m wondering what size mismatch might occur.
The error message states that the dimension 2 has different sizes, which would be the height of the image tensors.
Since you are resizing them to 500 I’m not sure, what’s causing this issue.
Could you a few images somewhere so that we could debug it?

1 Like

Thanks for your answer. I get the pictures by phone , its size is 3120 X 4160(W x H).I want to resize them to a rectangle.Are there any error or special method?