pytorch报错:Got 3 and 1 in dimension 1 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586

  File "/home/fuxueping/sdb/Caffe_Project_Train/github/InsightFace_Pytorch-
master_new/Learner.py", line 295, in train
    for batch_idx, (batch_sample) in progress_bar:
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-
packages/tqdm/_tqdm.py", line 872, in __iter__
    for obj in iterable:
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-
packages/torch/utils/data/dataloader.py", line 287, in __next__
    return self._process_next_batch(batch)
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-
packages/torch/utils/data/dataloader.py", line 308, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-
packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-
packages/torch/utils/data/dataloader.py", line 136, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in batch[0]}
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 136, in 
    return {key: default_collate([d[key] for d in batch]) for key in batch[0]}
  File "/home/fuxueping/anaconda2/envs/pytorch_yolov3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 116, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension
 0. Got 3 and 1 in dimension 1 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586

错误出现原因:图像中混有灰度图(单通道图像),读取图像时没有进行处理;

处理方法:

neg_img = Image.open(neg_img)
if neg_img.getbands()[0] == 'L':
     neg_img = neg_img.convert('RGB')

 

你可能感兴趣的:(pytorch)