Sobes.tech
Junior — Senior

What are stride and padding? How do they affect the output size of a convolution?

sobes.tech AI

Answer from AI

Stride — this parameter determines how many pixels (or elements) the convolution filter shifts over the input image each time. For example, stride=1 means the filter moves by 1 pixel, stride=2 — by 2 pixels, and so on.

Padding — this involves adding extra pixels (usually zeros) around the edges of the input image before applying convolution. This is done to control the size of the output and preserve information at the borders.

Impact on output size:

  • Larger stride reduces the output size because the filter skips more positions.
  • Padding increases the input size, allowing to preserve or increase the output size.

Formula for calculating the output size (for one dimension):

Output = floor((Input + 2 * Padding - KernelSize) / Stride) + 1

Where:

  • Input — size of the input image along the axis
  • KernelSize — size of the filter
  • Padding — number of pixels added on each side
  • Stride — convolution step

For example, with input=7, kernel=3, padding=1, stride=1: Output = floor((7 + 2*1 - 3)/1) + 1 = floor(7) + 1 = 7

Thus, the output size is preserved.