参考
如何计算感受野(Receptive Field)——原理
FOMORO AI -> 可视化计算感受野的网站,可以用来验证自己计算的结果
Python代码
这里使用的是从后向前的计算方法,简单且易于理解,也可以直接手算。
R
F
=
(
R
F
−
1
)
∗
S
t
r
a
d
e
+
K
e
r
n
e
l
S
i
z
e
w
h
e
r
e
,
K
e
r
n
e
l
S
i
z
e
=
K
e
r
n
e
l
S
i
z
e
+
(
K
e
r
n
e
l
S
i
z
e
−
1
)
(
D
i
l
a
t
i
o
n
−
1
)
RF = (RF - 1)*Strade+KernelSize\\ where,KernelSize = KernelSize+(KernelSize-1)(Dilation-1)
RF=(RF−1)∗Strade+KernelSizewhere,KernelSize=KernelSize+(KernelSize−1)(Dilation−1)
import sys
# VGG16为例
# k_size, stride, padding(1->"same",0->"valid"), dilation
convnet = [[3,1,1,1],[3,1,1,1],[2,2,0,2],[3,1,1,1],[3,1,1,1],[2,2,0,2],[3,1,1,1],[3,1,1,1],[3,1,1,1],[2,2,0,2],[3,1,1,1],[3,1,1,1],[3,1,1,1],[2,2,0,2],
[3,1,1,1],[3,1,1,1],[3,1,1,1],[2,2,0,2]]
layers_name = ["conv1_1","conv1_2","pool1","conv2_1","conv2_2","pool2","conv3_1","conv3_2","conv3_3","pool3",
"conv4_1","conv4_2","conv4_3","pool4","conv5_1","conv5_2","conv5_3","pool5"]
vn, vr = [], []
vn.append(224)
vr.append(1)
# l 为当前输出层,从 1 开始
for l in range(1, len(convnet)+1):
r = 1
for l_back in range(l, 0, -1):
k_size = convnet[l_back-1][0]
s = convnet[l_back-1][1]
d = convnet[l_back-1][3]
k_size = k_size + (k_size - 1) * (d - 1)
if convnet[l_back-1][2] == 1:
p = (k_size - 1) // 2 # 仅限于 s=1
# p = (k_size + vn[l_back-1]*(s-1) -s) // 2
elif convnet[l_back-1][2] == 0:
p = 0
else:
print("Padding is wrong!")
sys.exit()
# 计算感受野,从 top 往下层层迭代直到追溯回 input image
r = (r-1)*s + k_size
# 计算当前 l 层的输出尺寸
if l_back == l :
vn.append((vn[l-1]-k_size+2*p)//s + 1)
vr.append(r)
print("Input image:")
print("n:", vn[0], " r:", vr[0])
for l in range(1,len(convnet)+1):
print(layers_name[l-1] + ":")
print("Output Size:", vn[l], " Kernel Size: ", convnet[l-1][0],
" Stride: ", convnet[l-1][1], " Receptive Field:", vr[l])
输出结果
Input image:
n: 224 r: 1
conv1_1:
Output Size: 224 Kernel Size: 3 Stride: 1 Receptive Field: 3
conv1_2:
Output Size: 224 Kernel Size: 3 Stride: 1 Receptive Field: 5
pool1:
Output Size: 111 Kernel Size: 2 Stride: 2 Receptive Field: 7
conv2_1:
Output Size: 111 Kernel Size: 3 Stride: 1 Receptive Field: 11
conv2_2:
Output Size: 111 Kernel Size: 3 Stride: 1 Receptive Field: 23
pool2:
Output Size: 55 Kernel Size: 2 Stride: 2 Receptive Field: 27
conv3_1:
Output Size: 55 Kernel Size: 3 Stride: 1 Receptive Field: 35
conv3_2:
Output Size: 55 Kernel Size: 3 Stride: 1 Receptive Field: 43
conv3_3:
Output Size: 55 Kernel Size: 3 Stride: 1 Receptive Field: 51
pool3:
Output Size: 27 Kernel Size: 2 Stride: 2 Receptive Field: 59
conv4_1:
Output Size: 27 Kernel Size: 3 Stride: 1 Receptive Field: 75
conv4_2:
Output Size: 27 Kernel Size: 3 Stride: 1 Receptive Field: 91
conv4_3:
Output Size: 27 Kernel Size: 3 Stride: 1 Receptive Field: 107
pool4:
Output Size: 13 Kernel Size: 2 Stride: 2 Receptive Field: 123
conv5_1:
Output Size: 13 Kernel Size: 3 Stride: 1 Receptive Field: 155
conv5_2:
Output Size: 13 Kernel Size: 3 Stride: 1 Receptive Field: 187
conv5_3:
Output Size: 13 Kernel Size: 3 Stride: 1 Receptive Field: 219
pool5:
Output Size: 6 Kernel Size: 2 Stride: 2 Receptive Field: 251