今天调试代码时遇到了错误:
RuntimeError: Given groups=1, weight of size [4, 1, 2, 2], expected input[4, 3, 128, 256] to have 1 channels, but got 3 channels instead
Given groups=1, weight of size [4, 1, 2, 2]
的意思是卷积核的输入通道数为1,输出通道数为4,大小为2*2。我们期望的输入是1个通道的,但实际的输入是3个通道的(这里实际输入是[4, 3, 128, 256]
)。
于是我查看了输入图像的size,果然是三通道的,可以通过以下方式修改:
from torchvision import transforms
transform = transforms.Compose([
transforms.Grayscale(1) # 加入这一行代码即可,将图像改为一通道的
])
bug消,bug消