卷积输出尺寸
o u t x = ⌊ n − k + 2 × p s t r i d e ⌋ + 1 out_x = \lfloor \frac{n-k+2\times p}{stride}\rfloor+1 outx=⌊striden−k+2×p⌋+1
参数含义:
- n:特征图的宽或者高
- k:卷积核的尺寸
- p:padding的数值,一般指单侧填充几个单元
- stride:顾名思义
注意,分数部分是向下取整的。
做个练习吧,已知空洞卷积的公式如下:
atrous rate为2,stride为1,卷积核为3x3的卷积层,如果想让输入和输出的分辨率一样,p应该为多少呢?
聪明的小伙伴一定算出来了吧,就是p=2.
转置卷积输出尺寸
转置卷积俗称反卷积,但正规叫法是转置卷积,推荐朋友们以后也这么叫。
转置卷积用来增大特征图的分辨率的。
o
u
t
x
=
(
n
−
1
)
×
s
t
r
i
d
e
−
2
×
p
+
k
out_x = (n-1)\times stride -2\times p + k
outx=(n−1)×stride−2×p+k
参数含义同上。
import torch as t
from torch.nn import Module
from torch import nn
class Net(Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(3,3,3,padding=1)
self.conv2 = nn.Conv2d(3,3,3,padding=1)
self.maxpooling = nn.MaxPool2d(2,2)
self.trans_conv = nn.ConvTranspose2d(3, 16, 3, stride=2, padding=1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.maxpooling(x)
x = self.trans_conv(x)
return x
model = Net()
x = t.ones((1,3,12,12))
print(model(x).size())
可以发现转置卷积的输出其实并不完全是stride的倍数。但在实际的语义分割中,常常让输出分辨率成倍数增大,这个时候最好还是用双线性产值加conv的形式代替转置卷积吧,事实上一些state of the art 的语义分割网络也确实是这么做的。