Skip to main content

πŸ“ 2D Convolution (Conv2D)

Description​

< What is it? >​

A 2D convolution layer slides each output filter over a padded input, multiplies the filter and local input window element by element, sums across all input channels, and optionally adds one bias per output channel.

For multi-channel input, one output filter has shape (C_in, K_h, K_w) and contains one 2D kernel slice per input channel. The slices are applied to their corresponding channels and summed to produce one output channel. In deep-learning usage, filter and kernel are often used interchangeably.

For an NCHW input of shape (N, C_in, H, W) and filters of shape (C_out, C_in, K_h, K_w), the output shape is (N, C_out, H_out, W_out).

< What is NCHW? >​

NCHW is a tensor layout used in CNNs and deep-learning frameworks. It gives the dimension order for a batch of images.

LetterMeaningTypical size
NNumber of images in the batche.g., 32
CChannels: C_in for the layer input and C_out for its outpute.g., 3 β†’ 64
HHeight of each image or feature mape.g., 224
WWidth of each image or feature mape.g., 224

For example, an NCHW tensor with shape (32, 3, 224, 224) means:

32 images Γ— 3 channels Γ— 224 height Γ— 224 width

C_in is the number of channels entering the layer, such as 3 for an RGB image. C_out is the number of learned filters and therefore the number of output feature maps. For example, a layer with C_in = 3 and C_out = 64 maps RGB input to 64 output channels; later layers usually use the previous layer’s C_out as their C_in.

Key points​

< Tensor shapes >​

  • x: input tensor (N, C_in, H, W)
  • w: filter tensor (C_out, C_in, K_h, K_w)
  • K_h, K_w: kernel height and width; K_h = 3, K_w = 3 means a 3 Γ— 3 kernel, while K_h = 3, K_w = 5 means a non-square 3 Γ— 5 kernel
  • b: optional bias of length C_out
  • s_h, s_w: height and width strides
  • p_h, p_w: height and width zero-padding

< Output shape >​

Hout=⌊H+2phβˆ’KhshβŒ‹+1,Wout=⌊W+2pwβˆ’KwswβŒ‹+1H_{\text{out}} = \left\lfloor \frac{H + 2p_h - K_h}{s_h} \right\rfloor + 1, \qquad W_{\text{out}} = \left\lfloor \frac{W + 2p_w - K_w}{s_w} \right\rfloor + 1

Favorites​

πŸ‚