2
$\begingroup$

I have input ($n=224$), strides ($s=4$), filter size ($k=11$) and no padding which gives me a fractional conv output:

$$\texttt{conv output} = (n-k+2p)/s + 1 = 54.25$$

My question is how keras.layers.Conv2D is generating conv output when the above formula results in a fraction?

For the above parameters keras.layers.Conv2D is giving conv output as $54$. Why is it taking the floor of the fractional conv output? I read that when it is in fraction we need to adjust $n$, $s$, $k$ to make that output an integer.

$\endgroup$

1 Answer 1

1
$\begingroup$

In a given dimension, for a given input size ($n$), filter size ($k$), stride ($s$) and padding ($p$), the formula for the number of convolutional windows is

$$\lfloor (n - k + p + s)/s\rfloor,$$

which in your case is

$$\lfloor (224 - 11 + 0 + 4)/4\rfloor = \lfloor 54.25\rfloor = 54.$$

The question is why the floor is taken (as opposed to the ceiling for example). The reason is that the stride of $s = 4$ forces a distance of 4 pixels between each convolutional window. Without padding ($p = 0$), there is no space left for a 55th convolutional window at the right-most part while maintaining a distance of 4 pixels.

This is illustrated well here,$^\dagger$ in a 2D convolutional example where (horizontally) $n = 5$, $k = 2$, $s = 2$, $p = 0$:

kernel size

Here the lack of padding means only two steps can be taken in the horizontal dimension.

Filling in the formula gives the same answer:

$$\lfloor (5 - 2 + 0 + 2)/2\rfloor = \lfloor 2.5\rfloor = 2.$$


$^\dagger$: Zhang, A., Lipton, Z., Li, M., & Smola, A. (2023). Dive into Deep Learning. Cambridge University Press. https://D2L.ai

$\endgroup$
1
  • $\begingroup$ Thanks @Frans Rodenburg for the neat explanation and the reference book. $\endgroup$
    – Shri
    Commented Jul 10 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.