RuntimeError: invalid argument 0:

username: minhoha
cooked: <p>I got error like this.</p>
<p>Traceback (most recent call last):<br>
File “train11.py”, line 90, in <br>
for data, target in train_bar:<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/tqdm/_tqdm.py”, line 941, in <strong>iter</strong><br>
for obj in iterable:<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 267, in <strong>next</strong><br>
return self._process_next_batch(batch)<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 301, in _process_next_batch<br>
raise batch.exc_type(batch.exc_msg)<br>
RuntimeError: Traceback (most recent call last):<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 55, in _worker_loop<br>
samples = collate_fn([dataset[i] for i in batch_indices])<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 135, in default_collate<br>
return [default_collate(samples) for samples in transposed]<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 135, in <br>
return [default_collate(samples) for samples in transposed]<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 112, in default_collate<br>
return torch.stack(batch, 0, out=out)<br>
File “/home/mhha/.conda/envs/pytorchmh2/lib/python3.5/site-packages/torch/functional.py”, line 66, in stack<br>
return torch.cat(inputs, dim, out=out)<br>
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</p>
<p>What is the problem?</p>

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

username: minhoha
cooked: <p>Yes! you’re right!</p>
<p>I use ImageNet figures. Almost figures are colored images, but some images are BW images.</p>
<p>Thanks a lot!!</p>
<p>I will add your advice into my code!!</p>

username: minhoha
cooked: <p>I got error like this.</p>
<p>AttributeError: ‘torch.FloatTensor’ object has no attribute ‘convert’</p>
<p>What should i import??</p>
<p>I already imported “from PIL import Image”</p>

username: ptrblck
cooked: <p><code>.convert</code> is a method of <code>PIL.Image</code>, so you have to use it after you’ve loaded the image and before transforming it to a <code>tensor</code>.</p>
<p>Are you using a <code>Dataset</code> or a class like <code>ImageFolder</code>?<br>
If you are using the latter, the transformation should already be performed in <a href="https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py#L126">this line</a>.</p>
<p>If you are using your own <code>Dataset</code>, you should add it into the <code>__getitem__</code> method after the image was loaded:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>I skipped the target part. Let me know, if this works for you!</p>

username: minhoha
cooked: <p>thanks a lot! It will help me a lot!!</p>

username: Rounak_Mohanty
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,<br>
I have a custom dataset that returns the image and a label with it.<br>
I am getting the same error,<br>
but its because I am returning the labels as well in the <strong>getitem</strong> method.</p>
<p>How do you suggest I handle this?</p>
<p>Thank You</p>

username: Rounak_Mohanty
cooked: <p>Hi Guys,<br>
I found a solution to the problem.<br>
It was not because I was also returning the labels. It was because some images had 3 channels while some had only 1.<br>
I fixed it by adding .convert(‘RGB’) to the image loading line.</p>
<p>Thanks</p>

username: wangxinyue11
cooked: <p>I also encountered the same problem, I used Dataset_folder to load the dataset, how do you solve it?</p>
<p>RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 2 and 1 in dimension 1 at /opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/TH/generic/THTensor.cpp:711</p>

username: wangxinyue11
cooked: <p>Can you post your code?</p>

username: kun_yang
cooked: <p>I encountered a error when I start the trainning.The error is :<br>
Traceback (most recent call last):<br>
File “tools/train.py”, line 142, in <br>
main()<br>
File “tools/train.py”, line 138, in main<br>
meta=meta)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/apis/train.py”, line 111, in train_detector<br>
meta=meta)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/apis/train.py”, line 305, in <em>non_dist_train<br>
runner.run(data_loaders, cfg.workflow, cfg.total_epochs)<br>
File “/home/work/anaconda3/lib/python3.6/site-packages/mmcv/runner/runner.py”, line 371, in run<br>
epoch_runner(data_loaders[i], **kwargs)<br>
File “/home/work/anaconda3/lib/python3.6/site-packages/mmcv/runner/runner.py”, line 275, in train<br>
self.model, data_batch, train_mode=True, **kwargs)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/apis/train.py”, line 75, in batch_processor<br>
losses = model(**data)<br>
File “/home/work/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/home/work/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py”, line 150, in forward<br>
return self.module(*inputs[0], **kwargs[0])<br>
File “/home/work/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/core/fp16/decorators.py”, line 49, in new_func<br>
return old_func(*args, **kwargs)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/models/detectors/base.py”, line 137, in forward<br>
return self.forward_train(img, img_meta, **kwargs)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/models/detectors/cascade_rcnn.py”, line 231, in forward_train<br>
feats=[lvl_feat[j][None] for lvl_feat in x])<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/core/bbox/samplers/base_sampler.py”, line 75, in sample<br>
assign_result.add_gt</em>(gt_labels)<br>
File “/cache/user-job-dir/codes/mmdetection/mmdet/core/bbox/assigners/assign_result.py”, line 192, in add_gt_<br>
self.labels = torch.cat([gt_labels, self.labels])<br>
RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 1 and 2 at /pytorch/aten/src/THC/generic/THCTensorMath.cu:62<br>
How can i solve the problem?</p>

username: ptrblck
cooked: <p>Could you check the dimensions of <code>gt_labels</code> and <code>self.labels</code>?<br>
It seems <code>self.labels</code> uses two dimensions, while <code>gt_labels</code> has only one.<br>
I’m not familiar with your use case, but you might be able to <code>squeeze()</code> one dimension of <code>self.labels</code>.</p>

username: kun_yang
cooked: <p>Thanks,i’ve sove it!</p>

username: hedinghong
cooked: <p>How can you slove?  I am facing a same problem</p>