pytorch七:常用的神经网络层

图像相关层主要包括卷积层(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

pytorch七:常用的神经网络层_第1张图片

 

#输入是一个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 = kernel.view(1,1,3,3)
out = conv(V(input))
to_pil(out.data.squeeze(0))

pytorch七:常用的神经网络层_第2张图片

 


你可能感兴趣的:(pytorch)