1, 函数定义
nn.Conv2d(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True))
2, 参数:
in_channel: 输入数据的通道数,例RGB图片通道数为3;
out_channel: 输出数据的通道数,这个根据模型调整;
kennel_size: 卷积核大小,可以是int,或tuple;kennel_size=2,意味着卷积大小(2,2), kennel_size=(2,3),意味着卷积大小(2,3)即非正方形卷积
stride:步长,默认为1,与kennel_size类似,stride=2,意味着步长上下左右扫描皆为2, stride=(2,3),左右扫描步长为2,上下为3;
padding: 零填充
3, 代码
import torch
import torch.nn as nn
from torch.autograd import Variable
r = torch.randn(5, 8, 10, 5) # batch, channel , height , width
print(r.shape)
r2 = nn.Conv2d(8, 14, (3, 2), (2,1)) # in_channel, out_channel ,kennel_size,stride
print(r2)
r3 = r2(r)
print(r3.shape)
torch.Size([5, 8, 10, 5])
Conv2d(8, 14, kernel_size=(3, 2), stride=(2, 1))
torch.Size([5, 14, 4, 4])
4, 分析计算过程
卷积公式:
h = (h - kennel_size + 2padding) / stride + 1
w = (w - kennel_size + 2padding) / stride + 1
r = ([5, 8, 10, 5]),其中h=10,w=5,对于卷积核长分别是 h:3,w:2 ;对于步长分别是h:2,w:1;padding默认0;
h = (10 - 3 + 20)/ 2 +1 = 7/2 +1 = 3+1 =4
w =(5 - 2 + 20)/ 1 +1 = 3/1 +1 = 3/1+1 =4
batch = 5, out_channel = 14
故: y= ([5, 14, 4, 4])
参考