非对称卷积的特征图尺寸计算
此处只例举输入图像是正方形的情况。设输入图像尺寸为WxW,卷积核尺寸为ExF,步幅为S,Padding为P,卷积后的特征图尺寸为:
矩形卷积
如果输入图像是正方形,尺寸为WxW,卷积核尺寸为FxF,步幅为S,Padding使用P,经过该卷积层后输出的特征图尺寸为NxN:
如果输入图像是矩形,尺寸为WxH,卷积核的尺寸为FxF,步幅为S,图像深度(通道数)为C,Padding使用P,则:
非对称卷积的验证程序如下:
import torch
import torch.nn as nn
import torch
a = torch.randn(1, 32, 640, 640) # 创建一个1x32x640x640的输入,BNWH格式
conv3x1 = torch.nn.Conv2d(32, 32, (3, 1),stride=2,padding=(1,0)) # 3x1卷积
conv1x3 = torch.nn.Conv2d(32, 32, (1, 3),stride=1,padding=(0,1)) # 1x3卷积
conv3x3 = torch.nn.Conv2d(32, 32, (3, 3),stride=2,padding=1) # 1x3卷积
conv1x1 = torch.nn.Conv2d(32, 32, (1, 1),stride=2,padding=0) # 1x1卷积
out = conv3x1(a)
print(out.shape) # 进行了3x1后的output的形状
out = conv1x3(out) # 对1x3后的结果输出形状
print(out.shape)
out = conv3x3(a)
print(out.shape)
out = conv1x1(a)
print(out.shape)
"""
torch.Size([1, 32, 320, 320])
torch.Size([1, 32, 320, 320])
torch.Size([1, 32, 320, 320])
torch.Size([1, 32, 320, 320])
"""