卷积操作中的dilation的理解:
https://blog.csdn.net/weixin_42363544/article/details/123920699
空洞卷积,膨胀卷积
在Pytorch中,dilation = 1等同于没有dilation的标准卷积。
当dilation为1时,卷积核中的每个元素都紧挨着;
而当dilation为2时,卷积核中的每个元素之间都会间隔一个元素。
通过调整dilation的大小,可以控制卷积操作提取特征的范围和感受野的大小,从而影响网络的性能和特征提取能力。
Conv1d卷积
Conv1d卷积的Pytorch官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
import torch.nn as nn
import torch
m = nn.Conv1d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50)
print(input.shape)
output = m(input)
print(output.shape)
输出结果:
首先,让我们来理解一下这个1D卷积操作的参数和输入。
卷积层 nn.Conv1d(16, 33, 3, stride=2) 的参数解释如下:
输入通道数:16
输出通道数:33
卷积核大小:3
步长(stride):2
输入张量 input 的形状是 (20, 16, 50),这表示有20个样本,每个样本有16个通道,每个通道的长度是50。
卷积操作后,输出张量的形状可以通过以下方式计算:
输出长度 = (输入长度 - 卷积核大小) // 步长 + 1
在这里,输入长度是50,卷积核大小是3,步长是2,所以输出长度 = (50 - 3) // 2 + 1 = 24
因此,输出张量的形状将是 (样本数, 输出通道数, 输出长度),即 (20, 33, 24)。
结合上图的公式,dilation默认是1
Conv2d卷积
Pytorch官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html
import torch.nn as nn
import torch
# With square kernels and equal stride
m = nn.Conv2d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50, 100)
print("1:")
print(input.shape)
output = m(input)
print(output.shape)
# non-square kernels and unequal stride and with padding
m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
input = torch.randn(20, 16, 50, 100)
print("2:")
print(input.shape)
output = m(input)
print(output.shape)
# non-square kernels and unequal stride and with padding and dilation
m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
input = torch.randn(20, 16, 50, 100)
print("3:")
print(input.shape)
output = m(input)
print(output.shape)
输出结果:
计算过程如下:
Conv3d卷积
官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html
import torch.nn as nn
import torch
# With square kernels and equal stride
m = nn.Conv3d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 10, 50, 100)
print("1:")
print(input.shape)
output = m(input)
print(output.shape)
# non-square kernels and unequal stride and with padding
m = nn.Conv3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(4, 2, 0))
input = torch.randn(20, 16, 10, 50, 100)
print("2:")
print(input.shape)
output = m(input)
print(output.shape)
输出结果:
计算过程: