Skip to content

Instantly share code, notes, and snippets.

@svecon
Last active November 13, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svecon/c7622b0fc0f6ed1bac6a5e644eaea1f4 to your computer and use it in GitHub Desktop.
Save svecon/c7622b0fc0f6ed1bac6a5e644eaea1f4 to your computer and use it in GitHub Desktop.
Calculate receptive field and strides for multiple convolutions
# https://medium.com/@nikasa1889/a-guide-to-receptive-field-arithmetic-for-convolutional-neural-networks-e0f514068807#1780
import math
def receptive_field(n, layers):
"""
Parameters
----------
n: int, input size
layers: array of triplets: (k,s,p)
k: kernel size
s: stride
p: padding
Returns
-------
n: output size
j: jump size
r: size of receptive field
start: center position of the receptive field of the first output feature
"""
r = 1
j = 1
start = 0.5
for k, s, p in layers:
n = math.floor( (n - k + p)/s ) + 1
r = r + (k-1)*j
start = start + ( (k-1)/2 - p)*j
j = j*s
return int(n), j, r, start
# Example:
layers = [
(32, 4, 28), # k, s, p
(32, 4, 28),
(1, 4, 0), # maxpool
(16, 8, 8),
(16, 8, 8),
]
receptive_field(1024, layers)
#> (0, 4096, 8796, -640.5)
@srcolinas
Copy link

srcolinas commented Apr 17, 2019

In line 24 you have p instead of 2*p, which implies that you are using the total padding (i.e. the sum of the paddings applied to each of the borders of the same axis); however, line 26 suggest that you are using the "padding left" value. In particular this function is not exactly equivalent to the one you mention in the Medium article. Which one is right? @svecon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment