v9.1.1
Menu

Conv2D Output Size

easyFREECNNsDeep LearningCV: Deep Learning

Implement a calculation for the output dimensions of a 2D convolution operation, a fundamental component in Convolutional Neural Networks (CNNs). This operation is crucial for image processing tasks, where understanding the output size is essential for subsequent layers.

In CNNs, the 2D convolution operation involves sliding a kernel over an input image, performing a dot product at each position to generate a feature map. The output size of this operation depends on several factors, including the input size HinH_{in} and WinW_{in}, the kernel size KK, the padding PP, and the stride SS.

To calculate the output dimensions, follow these steps:

  1. Determine the input size HinH_{in} and WinW_{in}.
  2. Specify the kernel size KK, padding PP, and stride SS.
  3. Apply the formula to both height and width.
Hout=⌊Hin+2P−KS⌋+1H_{out} = \lfloor\frac{H_{in} + 2P - K}{S}\rfloor + 1

This technique is widely used in image classification tasks.

Example

Input
conv_output_size(32, 32, 3, 1, 1)
Output
(32, 32)
Reasoning
  • Inputs: a 32×3232 \times 32 feature map (Hin=Win=32H_{in} = W_{in} = 32), a 3×33 \times 3 kernel (K=3K = 3), one ring of zero padding (P=1P = 1) and stride S=1S = 1.
  • Padding adds PP rows on top and on bottom, so the padded height is 32+2⋅1=3432 + 2 \cdot 1 = 34.
  • A 33-tall kernel fits into 34−3=3134 - 3 = 31 shifts below its first position; with S=1S = 1 every shift is used, giving ⌊31/1⌋+1=32\lfloor 31 / 1 \rfloor + 1 = 32 positions.
  • Width follows the same arithmetic with WinW_{in} in place of HinH_{in}, so Wout=32W_{out} = 32 as well, giving (32, 32), height first.
  • Notice that P=1P = 1 with K=3K = 3 exactly cancels the K−1=2K - 1 = 2 pixels the kernel would otherwise lose, so the size is preserved; without padding it would shrink to 30×3030 \times 30.
  • The floor only matters when S>1S > 1 and the shifts don't divide evenly: a leftover strip shorter than one stride adds no output.

Constraints

  • Return (H_out, W_out)

Learn this in

More from CV: Deep Learning

solution.py

Test results

Sign in to run your code against the test cases.