Last active
July 16, 2018 19:54
-
-
Save ruslangrimov/106e7b00c30cf4a221123dacb36f5dcf to your computer and use it in GitHub Desktop.
Get receptive field size of a neuron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Code from https://medium.com/mlreview/a-guide-to-receptive-field-arithmetic-for-convolutional-neural-networks-e0f514068807 | |
''' | |
import math | |
# [kernel_size, stride, pad_size], ... | |
convnet = [[8, 1, 0], [2, 2, 0], | |
[5, 1, 0], [2, 2, 0], | |
[3, 1, 0], [2, 2, 0], | |
[2, 1, 0], [2, 2, 0] | |
] | |
layer_names = ['conv1', 'pool1', 'conv2', 'pool2', 'conv3', 'pool3', 'conv4', 'pool4'] | |
imsize = 150 | |
def outFromIn(conv, layerIn): | |
n_in = layerIn[0] | |
j_in = layerIn[1] | |
r_in = layerIn[2] | |
start_in = layerIn[3] | |
k = conv[0] | |
s = conv[1] | |
p = conv[2] | |
n_out = math.floor((n_in - k + 2*p)/s) + 1 | |
actualP = (n_out-1)*s - n_in + k | |
pR = math.ceil(actualP/2) | |
pL = math.floor(actualP/2) | |
j_out = j_in * s | |
r_out = r_in + (k - 1)*j_in | |
start_out = start_in + ((k-1)/2 - pL)*j_in | |
return n_out, j_out, r_out, start_out | |
def printLayer(layer, layer_name): | |
print(layer_name + ":") | |
print("\t n features: %s \n \t jump: %s \n \t receptive size: %s \t start: %s " % (layer[0], layer[1], layer[2], layer[3])) | |
layerInfos = [] | |
#first layer is the data layer (image) with n_0 = image size; j_0 = 1; r_0 = 1; and start_0 = 0.5 | |
print ("-------Net summary------") | |
currentLayer = [imsize, 1, 1, 0] | |
printLayer(currentLayer, "input image") | |
for i in range(len(convnet)): | |
currentLayer = outFromIn(convnet[i], currentLayer) | |
layerInfos.append(currentLayer) | |
printLayer(currentLayer, layer_names[i]) | |
print ("------------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment