感受野計算
阿新 • • 發佈:2020-07-21
本文參考
在卷積神經網路中,感受野的定義是:卷積神經網路每一層輸出的特徵圖(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]))
列印如下:
""" 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]))