这是陈云的《深度学习框架Pytorch入门与实践》4.2.1节图像相关层的代码:
from PIL import Image
from torchvision.transforms import ToTensor,ToPILImage
import torch as t
from torch import nn
from torch.autograd import Variable as V
to_tensor=ToTensor()
to_pil=ToPILImage()
lena=Image.open('C:/Users/Desktop/lena.png')
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))
但是结果报错:
File "D:\py\lib\site-packages\torch\nn\modules\conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [1, 1, 3, 3], expected input[1, 3, 300, 300] to have 1 channels, but got 3 channels instead
这怎么解决啊。。。。。