3.1常用的神经网络层(一)图像相关层

图像相关层主要包括卷积层(Conv)、池化层(Pool)等,这些层在实际使用中可分为一维(1D)、二维(2D)和三维(3D),池化方式又分为平均池化(AvgPool)、最大池化(MaxPool)、自适应池化(AdaptiveAvgPool)等。卷积层除了常用的前向卷积外,还有逆卷积(TransposeConv)。

from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
import matplotlib.pyplot as plt
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as V
to_tensor=ToTensor()#image-->tensor
to_pil=ToPILImage()#而transforms.ToPILImage则是将Tensor或numpy.ndarray转化为PIL.Image
lena=Image.open('/home/yangyuting/Desktop/20180925183958930.jpeg')
lena = lena.convert('L')#将图像转换为灰度图
plt.figure("pic")
plt.imshow(lena)
plt.show()

对图像进行卷积操作后的图像:

#输入是一个batch,batch_size=1
input=to_tensor(lena).unsqueeze(0)
#锐化卷积核
kernel=t.ones(3,3)/-9
kernel[1][1]=1#卷积核为中心为1,周边为1/9的卷积核
conv=nn.Conv2d(1,1,(3,3),1,bias=False)#(in_channels, out_channels, kernel_size, stride=1)
conv.weight.data=kernel.view(1,1,3,3)#(batch_size,in_channels, kernel_size)
out=conv(V(input))
to_pil(out.data.squeeze(0))
plt.figure("pic")
plt.imshow(to_pil(out.data.squeeze(0)))
plt.show()

Linear:全连接层

#输入batch_size=2,维度=3
input=V(t.randn(2,3))
linear=nn.Linear(3,4)
h=linear(input)
print(h)

BatchNorm:批规范化层,分为1D,2D, 3 D

#4channel,初始化标准差为4,均值为0
bn=nn.BatchNorm1d(4)
bn.weight.data=t.ones(4)*4
bn.bias.data=t.zeros(4)

bn_out=bn(h)
bn_out.mean(0),bn_out.var(0,unbiased=False)

Dropout:dropout层,用来防止过拟合,同样分为1D,2D, 3 D

from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
import matplotlib.pyplot as plt
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as V

to_tensor=ToTensor()#image-->tensor
to_pil=ToPILImage()
lena=Image.open('/home/yangyuting/Desktop/20180925183958930.jpeg')
lena = lena.convert('L')
# plt.figure("pic")
# plt.imshow(lena)
# plt.show()

#输入是一个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))
# plt.figure("pic")
# plt.imshow(to_pil(out.data.squeeze(0)))
# plt.show()

pool=nn.AvgPool2d(2,2)
list(pool.parameters())

out=pool(V(input))
to_pil(out.data.squeeze(0))
# plt.figure("pic")
# plt.imshow(to_pil(out.data.squeeze(0)))
# plt.show()
#全连接层Linear
#输入batch_size=2,维度=3
input=V(t.randn(2,3))
linear=nn.Linear(3,4)
h=linear(input)
#print(h)
#4channel,初始化标准差为4,均值为0
bn=nn.BatchNorm1d(4)
bn.weight.data=t.ones(4)*4
bn.bias.data=t.zeros(4)

bn_out=bn(h)
bn_out.mean(0),bn_out.var(0,unbiased=False)
print(bn_out)
#每个元素以0.5的概率舍弃#有一半左右的数变为0
dropout=nn.Dropout(0.5)
O=dropout(bn_out)
print(O)

m = nn.Dropout (p=0.2)
input = t.randn (3, 4)
print(input)
output = m (input)
print(output)

输出:

tensor([[-3.9998, -3.9999,  3.9996,  3.9998],
        [ 3.9998,  3.9999, -3.9996, -3.9998]],
       grad_fn=<NativeBatchNormBackward>)
tensor([[-0.0000, -7.9997,  7.9993,  0.0000],
        [ 0.0000,  7.9997, -0.0000, -0.0000]], grad_fn=<MulBackward0>)
tensor([[-0.6623,  1.3807,  2.1652, -0.1931],
        [ 0.6697, -0.2446,  1.3638,  0.5675],
        [ 1.9312,  0.8611,  0.7551,  0.2626]])
tensor([[-0.8278,  1.7259,  2.7066, -0.2414],
        [ 0.8371, -0.0000,  1.7047,  0.7094],
        [ 2.4140,  0.0000,  0.9439,  0.0000]])

注意:input部分变为0以后,其他向量进行了缩放,也就是乘以1/(1-p)。因此会发现实验过程中,input 和output不是简单的部分置零。在图像里面如何解释还有待研究?

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值