DataLoader加载图像时Sizes of tensors must match except in dimension 0. Got 1and 3 in dimension 1的问题解决方法

  • Post author:
  • Post category:其他


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')

记录下这个小问题。



版权声明:本文为dcxhun3原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。