1. torch.nn.Conv2d
是PyTorch里面实现卷积的函数
Args:
in_channels (int): Number of channels in the input image
out_channels (int): Number of channels produced by the convolution
kernel_size (int or tuple): Size of the convolving kernel
stride (int or tuple, optional): Stride of the convolution. Default: 1
padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional): If True, adds a learnable bias to the output. Default: True
in_channels (int): 输入通道
out_channels (int): 输出通过
kernel_size (int or tuple): 卷积核(滤波器)的大小
stride (int or tuple, optional): 步长,可以是整数,也可以是tuple
padding (int or tuple, optional): 周围补零Default: 0
dilation (int or tuple, optional): Spacing between kernel elements. Default: 1(不懂)
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1(不懂)
bias (bool, optional): If True, adds a learnable bias to the output. Default: True(偏差)
2. 实现卷积层、池化层
import torch as t
from torch.autograd import Variable as V
from torch import nn
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage
import matplotlib.pyplot as plt
to_tensor = ToTensor() # img -> Tensor
to_pil = ToPILImage() # 将Numpy的ndarray或者Tensor转化成PILImage类型
Christmas_Trees = Image.open('C:/Users/shh/Pictures/Christmas_Trees.png') # 图像的通道得是1
Christmas_Trees.show()
#输入是一个batch,batch_size = 1
input = to_tensor(Christmas_Trees).unsqueeze(0) # unsqueeze(0)在第0维增加1 (1*通道数*长*宽) 1*1*1800*2880
#锐化卷积核
kernel = t.ones(8,8)/-64.
kernel[1][1] = 1
conv = nn.Conv2d(1,1,(8,8),stride=1,bias = False)# 参数含义依次为:输入通道、输出通道、卷积核大小、步长、偏差
conv.weight.data = kernel.view(1,1,8,8)
out = conv(V(input))
to_pil(out.data.squeeze(0)).show() #显示卷积之后的图像
Christmas_Trees.save('C:/Users/shh/Pictures/Christmas_Trees1.png','PNG') #图像存下来
pool = nn.AvgPool2d(4,4) # 步长默认(4,4)
out = pool(V(input))
to_pil(out.data.squeeze(0)).show() #显示池化之后的图像
Christmas_Trees.save('C:/Users/shh/Pictures/Christmas_Trees2.png','PNG')
原始图像:
卷积之后的图像:
池化之后的图像:
原始图像的像素为:1800*2880
池化,经过下采样,pooling窗口为4*4
,步长默认为4*4
,所以图片像素变为450*720
3. 其他几个常用的层
- Linear:全连接层
- BatchNorm:批规范层
- Dropout:dropout层,用于防止过拟合
import torch as t
from torch.autograd import Variable as V
from torch import nn
#输入 batch_size=2,维度为3
input = V(t.randn(2,3))
linear = nn.Linear(3,4) # 全连接层
h = linear(input)
# 4 channel ,初始化标准差为4, 均值为0
bn = nn.BatchNorm1d(4) # BatchNorm:批规范化层 ,1D
# 用于解决多层神经网络中间层的协方差偏移(Internal Covariate Shift)问题
# BatchNorm1d的参数为input的第1维(从第0维开始)
bn.weight.data = t.ones(4)*4 # 方差
bn.bias.data = t.zeros(4) #标准差
bn_out = bn(h) #输入为4个样本组成的一个batch数据 ,注意输出的均值和方差
#方差是标准差是平方,计算无偏方差分母会减1
#使用unbiased = False ,分母不减1
#print(bn_out.mean(0),bn_out.var(0,unbiased = False))
#输出 :tensor([ 0.0000e+00, 0.0000e+00, -4.7684e-07, 0.0000e+00],
# grad_fn=<MeanBackward0>) tensor([15.9903, 15.9914, 15.9979, 15.9846], grad_fn=<VarBackward1>)
dropout = nn.Dropout(0.5) # dropout层,用来防止过拟合,每个元素以0.5的概率舍弃
o = dropout(bn_out)
print(o)
# 输出 tensor([[-7.9998, -7.9998, -7.9585, 7.9999],
# [ 0.0000, 7.9998, 0.0000, -7.9999]], grad_fn=<MulBackward0>)