#卷基层除了常用的向前卷积外,还有逆卷积
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
to_tensor = ToTensor() #img->tensor
to_pil = ToPILImage()
lena = Image.open('/home/travis/下载/lena.png')
lena
#输入是一个batch,batch_size=1
input = to_tensor(lena).unsqueeze(0)
#锐化卷积核
kernel = t.ones(3, 3)/-9.
kernel[1][1] =1
conv = nn.Conv2d(1, 1, (3, 3), 1, bias = False)
conv.weight.data = kernel.view(1, 1, 3, 3)
out = conv(V(input))
to_pil(out.data.squeeze(0))
RuntimeError: Given groups=1, weight of size [1, 1, 3, 3], expected input[1, 3, 550, 850] to have 1 channels, but got 3 channels instead
解决方法:
换一张灰度图片