计算各种网络结构的卷积层和返回的感受野

计算各种网络结构的卷积层和返回的感受野的代码

在卷积神经网络中,感受野的定义是卷积神经网络每一层输出的特征图(Feature Map)上的像素点在原始图像上映射的区域大小。
input field size = (output field size - 1)* stride - 2*padding + kernel size

# -*- coding: UTF-8 -*-

net_struct = {'alexnet': {'net':[[11,4,0],[3,2,0],[5,1,2],[3,2,0],[3,1,1],[3,1,1],[3,1,1],[3,2,0]],
                   'name':['conv1','pool1','conv2','pool2','conv3','conv4','conv5','pool5']},
       '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']},
       'zf-5':{'net': [[7,2,3],[3,2,1],[5,2,2],[3,2,1],[3,1,1],[3,1,1],[3,1,1]],
               'name': ['conv1','pool1','conv2','pool2','conv3','conv4','conv5']}}

imsize = 224

def outFromIn(isz,net,layernum):#从前往后算输出维度
    totstride = 1
    insize = isz
    for layer in range(layernum):
        fsize,stride,pad = net[layer]
        outsize = (insize - fsize + 2*pad) / stride +1
        insize = outsize
        totstride = totstride * stride
    return outsize,totstride

def inFromOut(net,layernum):#从后往前算感受野 返回该层元素在原始图片中的感受野
    RF = 1
    for layer in reversed(range(layernum)):
        fsize,stride,pad = net[layer]
        RF = ((RF - 1)*stride) + fsize
    return RF

if __name__ == '__main__':
    print "layer output sizes given image = %dx%d" % (imsize,imsize)
    for net in net_struct.keys():
        print '*********net structure name is %s********'%net
        for i in range(len(net_struct[net]['net'])):
            p = outFromIn(imsize,net_struct[net]['net'],i+1)
            rf = inFromOut(net_struct[net]['net'],i+1)
            print "Layer Name = %s,Output size = %3d,Stride = %3d,RF size = %3d "%(net_struct[net]['name'][i],p[0],p[1],rf)

输出结果

Layer Name = conv1_1,Output size = 224,Stride =   1,RF size =   3 
Layer Name = conv1_2,Output size = 224,Stride =   1,RF size =   5 
Layer Name = pool1,Output size = 112,Stride =   2,RF size =   6 
Layer Name = conv2_1,Output size = 112,Stride =   2,RF size =  10 
Layer Name = conv2_2,Output size = 112,Stride =   2,RF size =  14 
Layer Name = pool2,Output size =  56,Stride =   4,RF size =  16 
Layer Name = conv3_1,Output size =  56,Stride =   4,RF size =  24 
Layer Name = conv3_2,Output size =  56,Stride =   4,RF size =  32 
Layer Name = conv3_3,Output size =  56,Stride =   4,RF size =  40 
Layer Name = pool3,Output size =  28,Stride =   8,RF size =  44 
Layer Name = conv4_1,Output size =  28,Stride =   8,RF size =  60 
Layer Name = conv4_2,Output size =  28,Stride =   8,RF size =  76 
Layer Name = conv4_3,Output size =  28,Stride =   8,RF size =  92 
Layer Name = pool4,Output size =  14,Stride =  16,RF size = 100 
Layer Name = conv5_1,Output size =  14,Stride =  16,RF size = 132 
Layer Name = conv5_2,Output size =  14,Stride =  16,RF size = 164 
Layer Name = conv5_3,Output size =  14,Stride =  16,RF size = 196 
Layer Name = pool5,Output size =   7,Stride =  32,RF size = 212 
*********net structure name is zf-5********
Layer Name = conv1,Output size = 112,Stride =   2,RF size =   7 
Layer Name = pool1,Output size =  56,Stride =   4,RF size =  11 
Layer Name = conv2,Output size =  28,Stride =   8,RF size =  27 
Layer Name = pool2,Output size =  14,Stride =  16,RF size =  43 
Layer Name = conv3,Output size =  14,Stride =  16,RF size =  75 
Layer Name = conv4,Output size =  14,Stride =  16,RF size = 107 
Layer Name = conv5,Output size =  14,Stride =  16,RF size = 139 
*********net structure name is alexnet********
Layer Name = conv1,Output size =  54,Stride =   4,RF size =  11 
Layer Name = pool1,Output size =  26,Stride =   8,RF size =  19 
Layer Name = conv2,Output size =  26,Stride =   8,RF size =  51 
Layer Name = pool2,Output size =  12,Stride =  16,RF size =  67 
Layer Name = conv3,Output size =  12,Stride =  16,RF size =  99 
Layer Name = conv4,Output size =  12,Stride =  16,RF size = 131 
Layer Name = conv5,Output size =  12,Stride =  16,RF size = 163 
Layer Name = pool5,Output size =   5,Stride =  32,RF size = 195 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值