RuntimeError: invalid argument 0: Sizes of tensors must match except
in dimension 0. Got 1 and 3 in dimension 1 at
/opt/conda/conda-bld/pytorch_1579040055865/work/aten/src/TH/generic/THTensor.cpp:612
通常在pytorch环境中,DataLoader加载图像时,用的是 Image库读取图像,代码如下:
from PIL import Image
Image.open(image_path)
有时候会遇到上述错误,遇到这种情况时,需要统一限制数据通道为3或者1,在我这次情况是,需要1,于是用convert(‘L’)将读入的图像统一转成1通道,代码如下
from PIL import Image
Image.open(image_path).convert('L')
如果是需要通道为3,则转换代码如下:
Image.open(image_path).convert('RGB')
记录下这个小问题。