感受野计算

本文参考

在卷积神经网络中,感受野的定义是:卷积神经网络每一层输出的特征图(feature map)上的像素点在原始图像上映射的区域大小。

"""
striden = strides= stride1*stride2*...*striden-1
rn = rn-1 + (fn - 1)*strides

# 卷积核大小/stride/padding
# 卷积核大小/stride/padding/dilation
"""

net_struct = {
    'alexnet': {
        'net': [[11, 4, 1, 0], [3, 2, 1, 0], [5, 1, 1, 2], [3, 2, 1, 0], [3, 1, 1, 1], [3, 1, 1, 1], [3, 1, 1, 1],
                [3, 2, 1, 0]],
        'name': ['conv1', 'pool1', 'conv2', 'pool2', 'conv3', 'conv4', 'conv5', 'pool5']},
    'dilated_conv': {'net': [[3, 1, 1, 0], [3, 1, 2, 0], [3, 1, 4, 0]],
                     'name': ['dilated1', 'dilated2', 'dilated4']},

    'vgg16':{
        'net':[[3,1,1],[3,1,1],[2,2,0],[3,1,1],[3,1,1],[2,2,0],[3,1,1],[3,1,1],[3,1,1],
                        [2,2,0],[3,1,1],[3,1,1],[3,1,1],[2,2,0],[3,1,1],[3,1,1],[3,1,1],[2,2,0]],
        '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']}
}


def calc_respective_fields(net):
    layers = net['net']
    layers_num = len(layers)
    result = []
    rf = 1
    strides = 1
    for i in range(layers_num):
        length = len(layers[i])
        if 3 == length:
            f, s, p = layers[i]
            d = 1
        elif 4 == length:
            f, s, p, d = layers[i]
        else:
            print("len(layers[i]) should = 3 or 4!\n ")
            exit(-1)

        # 扩大卷积
        f = (f - 1) * d + 1
        rf = rf + (f - 1) * strides
        strides *= s
        result.append([rf, strides])
    return result


if __name__ == '__main__':

    net = net_struct['vgg16']
    print('vgg16')
    result = calc_respective_fields(net)
    for i in range(len(result)):
        print('vgg16 %s layer output respective field %s strides %s' % (net['name'][i], result[i][0], result[i][1]))

打印如下:

vgg16
vgg16 conv1_1 layer output respective field 3 strides 1
vgg16 conv1_2 layer output respective field 5 strides 1
vgg16 pool1 layer output respective field 6 strides 2
vgg16 conv2_1 layer output respective field 10 strides 2
vgg16 conv2_2 layer output respective field 14 strides 2
vgg16 pool2 layer output respective field 16 strides 4
vgg16 conv3_1 layer output respective field 24 strides 4
vgg16 conv3_2 layer output respective field 32 strides 4
vgg16 conv3_3 layer output respective field 40 strides 4
vgg16 pool3 layer output respective field 44 strides 8
vgg16 conv4_1 layer output respective field 60 strides 8
vgg16 conv4_2 layer output respective field 76 strides 8
vgg16 conv4_3 layer output respective field 92 strides 8
vgg16 pool4 layer output respective field 100 strides 16
vgg16 conv5_1 layer output respective field 132 strides 16
vgg16 conv5_2 layer output respective field 164 strides 16
vgg16 conv5_3 layer output respective field 196 strides 16
vgg16 pool5 layer output respective field 212 strides 32
posted @ 2020-07-21 09:24  无左无右  阅读(329)  评论(0编辑  收藏  举报