图像相关层主要包括卷积层(Conv)、池化层(Pool)等,这些层在使用中可分为一维(1D)、二维(2D)、三维(3D),池化方式又分为平均池化(AvgPool)、最大值池化(MaxPool)、自适应池化(AdaptiveAvgPool)等。卷积层除了常用的前向卷积外,还有逆卷积。
from PIL import Image
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor() #img->tensor
to_pil = ToPILImage() #tensor->img
img = Image.open('templ.png')
img = img.convert('L')
img
#输入是一个batch,batch.size=1
input = to_tensor(lena).unsqueeze(0)
#边缘算子(二阶微分,拉普拉斯考虑对角线的算子)
kernel = t.ones(3,3)
kernel[1,1] = -8
conv = nn.Conv2d(1,1,(3,3),bias = False)
conv.weight.data = ker