解决方法
在dataloader前的dataset函数里,加.convert(‘RGB’),eg:
image = Image.open(image_file).convert('RGB')
解决过程
论文复现过程中尝试将未使用的数据集应用到源代码里,出现如上报错。检查后发现错误出现在dataloader里,考虑是输入图片的问题,通过如下代码判断新加的数据集是灰度图(1.灰度图只有一个通道2.直接查看像素值),因此推断dataloader只接受三通道RGB图,将图片转化为RGB图即可。
打印图片像素值
img_path = '这里写要查看的图像地址'
from PIL import Image
from torchvision import transforms
img = Image.open(img_path)
change = transforms.ToTensor()
img = change(img)
print(img)
查看图片像素值
import cv2
img_path = '这里写要查看的图像地址'
img_c = cv2.imread(img_path,cv2.IMREAD_UNCHANGED)
cv2.imshow('photo',img_c)
key = cv2.waitKey(0)
有博客的解决方案是在transforms.Compose()里加入如下程序段,我的程序并未好转,如果是灰度图,RGB转换与repeat转换的三通道图像是一致的,这可能是程序存在其他的原因。
transforms.Lambda(lambda x: x.repeat(3, 1, 1))