带dilation的卷积输出尺寸计算公式
K
′
=
k
e
r
n
e
l
+
(
k
e
r
n
e
l
−
1
)
⋅
(
d
i
l
a
t
i
o
n
−
1
)
K'=kernel+(kernel-1)\cdot(dilation-1)
K′=kernel+(kernel−1)⋅(dilation−1)
W
o
u
t
=
w
i
n
−
K
′
+
2
p
a
d
d
i
n
g
s
t
r
i
d
e
+
1
W_{out}=\frac{w_{in}-K'+2padding}{stride}+1
Wout=stridewin−K′+2padding+1
举例
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
def default_conv(in_channels,out_channels,kernel_size,stride,padding,dilation,bias=True):
return nn.Conv2d(in_channels = in_channels,
out_channels = out_channels,
kernel_size = kernel_size, # 3
stride = stride, # 1
padding = padding, # 5
dilation = dilation, # 5
bias=bias)
class ShizuoNet(nn.Module):
def __init__(self, conv=default_conv,n_feats=32):
super(ShizuoNet,self).__init__()
self.convA = conv(1,32,3,1,5,5)
def forward(self,x):
print(x.shape)
y = self.convA(x) # torch.Size([2, 1, 80, 80])
print(y.shape) # torch.Size([2, 32, 80, 80])
return y
def main():
net = ShizuoNet()
from torchsummary import summary
summary(net, (1, 80, 80))
if __name__ == "__main__":
main()