torch_ecg.utils.compute_receptive_field

torch_ecg.utils.compute_receptive_field(kernel_sizes: Sequence[int] | int = 1, strides: Sequence[int] | int = 1, dilations: Sequence[int] | int = 1, input_len: int | None = None, fs: Real | None = None) int | float[source]

Compute the receptive field of several types of Module.

Computes the (generic) receptive field of feature map of certain channel, from certain flow (if not merged, different flows, e.g. shortcut, must be computed separately), for convolutions, (non-global) poolings. “generic” refers to a general position, rather than specific positions, like on the edges, whose receptive field is definitely different.

In CNNs, for any element of some layer, its receptive field refers to all the elements (from all the previous layers) that may affect the calculation of during the forward propagation [Zhang et al.[1]]. (See this url)

The receptive field is computed as follows. Let the layers has kernel size, stride, dilation \((k_n, s_n, d_n)\) respectively. Let each feature map has receptive field length \(r_n\), and the difference of receptive fields of adjacent positions (layers) be \(f_n\). By convention, \((r_0, f_0) = (1, 1)\). Then one has

\[\begin{split}\begin{eqnarray} r_{n+1} & = & r_n + d_n(k_n-1)f_n, \\ f_{n+1} & = & s_n f_n. \end{eqnarray}\end{split}\]

Hence

\[\begin{split}\begin{eqnarray} f_{n} & = & \prod\limits_{i=0}^{n-1} s_i, \\ r_{n} & = & 1 + \sum\limits_{i=0}^{n-1} d_i(k_i-1) \prod\limits_{i=0}^{j-1} s_j, \end{eqnarray}\end{split}\]

with empty products equaling 1 by convention.

Parameters:
  • kernel_sizes (int or Sequence[int], default 1) – The sequence of kernel size for all the layers in the flow.

  • strides (int or Sequence[int], default 1) – The sequence of strides for all the layers in the flow

  • dilations (int or Sequence[int], default 1) – The sequence of strides for all the layers in the flow

  • input_len (int, optional) – Length of the first feature map in the flow.

  • fs (numbers.Real, optional) – Sampling frequency of the input signal. If is not None, then the receptive field is returned in seconds.

Returns:

receptive_field – (Length of) the receptive field, in samples if fs is None, otherwise in seconds.

Return type:

int or float

Examples

>>> compute_receptive_field([11,2,7,7,2,5,5,5,2],[1,2,1,1,2,1,1,1,2])
90
>>> compute_receptive_field([11,2,7,7,2,5,5,5,2],[1,2,1,1,2,1,1,1,2],fs=500)
0.18
>>> compute_receptive_field([11,2,7,7,2,5,5,5,2],[1,2,1,1,2,1,1,1,2],[2,1,2,4,1,8,8,8,1])
484
>>> compute_receptive_field([11,2,7,7,2,5,5,5,2],[1,2,1,1,2,1,1,1,2],[4,1,4,8,1,16,32,64,1])
1984

The above example exhibits the receptive fields of the output feature maps of the 3 branches of the multi-scopic net, using its original hyper-parameters, (note the 3 max pooling layers).

References