RuntimeError: invalid argument 0:


(하민호) #1

I got error like this.

Traceback (most recent call last):
File “train11.py”, line 90, in
for data, target in train_bar:
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/tqdm/_tqdm.py”, line 941, in iter
for obj in iterable:
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 267, in next
return self._process_next_batch(batch)
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 301, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 55, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 135, in default_collate
return [default_collate(samples) for samples in transposed]
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 135, in
return [default_collate(samples) for samples in transposed]
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 112, in default_collate
return torch.stack(batch, 0, out=out)
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/functional.py”, line 66, in stack
return torch.cat(inputs, dim, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 1 in dimension 1 at /opt/conda/conda-bld/pytorch_1522182087074/work/torch/lib/TH/generic/THTensorMath.c:2897

What is the problem?


#2

The error message is a bit hard to interpret without the code.
It seems you are trying to load images using a DataLoader.
Some of these images seem to have 3 channels (color images), while others might have a single channel (BW images).
Since the dimension differ in dim1, they cannot be concatenated into a batch.
You could try to add img = img.convert('RGB') into your __getitem__ in your Dataset.


(하민호) #3

Yes! you’re right!

I use ImageNet figures. Almost figures are colored images, but some images are BW images.

Thanks a lot!!

I will add your advice into my code!!


(하민호) #4

I got error like this.

AttributeError: ‘torch.FloatTensor’ object has no attribute ‘convert’

What should i import??

I already imported “from PIL import Image”


#5

.convert is a method of PIL.Image, so you have to use it after you’ve loaded the image and before transforming it to a tensor.

Are you using a Dataset or a class like ImageFolder?
If you are using the latter, the transformation should already be performed in this line.

If you are using your own Dataset, you should add it into the __getitem__ method after the image was loaded:

class MyDataset(Dataset):
    def __init__(self, image_paths, transforms=transforms):
        self.image_paths = image_paths
        self.transforms = transforms

    def __getitem__(self, index):
        image = Image.open(self.image_paths[index])
        image = image.convert('RGB')
        if self.transforms:
            image = self.transforms(image)
        return image

I skipped the target part. Let me know, if this works for you!


(하민호) #6

thanks a lot! It will help me a lot!!


(Rounak Mohanty) #7

Hi @ptrblck,
I have a custom dataset that returns the image and a label with it.
I am getting the same error,
but its because I am returning the labels as well in the getitem method.

How do you suggest I handle this?

Thank You


(Rounak Mohanty) #8

Hi Guys,
I found a solution to the problem.
It was not because I was also returning the labels. It was because some images had 3 channels while some had only 1.
I fixed it by adding .convert(‘RGB’) to the image loading line.

Thanks