Convolution

Mathematical operation: applies a small matrix called a convolution kernel or filter K over a given image X, element-wise multiplying each overlapping set of values with the kernel and then summing the results

Y=f(X,K)

Each output represents the sum of the product of the input and the pixel.

Used in Computer vision. For instance Y=f(X,K) produces an image Y of size h×w, with h=hk+1 and w=wk+1

Types of kernels

Choosing the right kernel size for convolution:

Conv2d

def convolution_batch_torch_conv2d(images, kernel, stride = 1, padding = 0):
    # Convert kernel to PyTorch tensor, if needed
    kernel = torch.from_numpy(kernel)
    kernel = kernel.view(1, 1, kernel.shape[0], kernel.shape[1])
    kernel = kernel.float()

    # Flip the kernel (optional)
    kernel = torch.flip(torch.flip(kernel, [2]), [3])

    # Create a convolutional layer
    conv = torch.nn.Conv2d(in_channels = images.shape[1], \
                           out_channels = 1, \
                           kernel_size = kernel.shape[2:], \
                           stride = stride, \
                           padding = padding)

    # Assign the kernel to the layer
    conv.weight = torch.nn.Parameter(kernel)
    conv.bias = torch.nn.Parameter(torch.tensor([0.0]))

    # Perform convolution
    output = conv(images)

    return output
Yi,j=1k2m=1kn=1kXi+m1,j+n1Km,n+b

Formatted as a 4D tensor of size Ns×c×h×w, where:

For RGB (not grayscale images), follow #Higher dimension convolution.

Chaining convolutions

nn.Conv2d(1, 32, kernel_size = 3, stride = 1, padding = 1)

The first two integer values correspond to the number of channels of input images X and the number of channels in kernel.

The convolution layer below then expects grayscale images X with only one channel and will produce images Y with 32 channels.

This image Y with 32 channels can be visualised as 32 different convolution operations on 32 different kernels.

These filters are initialised (see Constant initialisation), and the Convolutional Neural Networks will learn the best filters on its own.

nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)

By stacking multiple convolutional layers, the network can learn hierarchical features, where lower layers detect simple patterns and higher layers detect more complex structures.

Padding

Convolution produces a new image Y whose size and resolution has been reduced, as it sums several pixels together but only produces one pixel. Larger kernels result with even smaller outputs.

Hence, we have to pad the image - add extra pixels on the outer part of the original image. This artificially increases the size of the original mimage X such that the output image Y matches in size with X.

Valid padding

By default, with no padding applied to the input data. Convolution is thus only performed on the valid parts of input, with the output size being smaller than the input size.

Same padding

We add a padding size p of zeros so that Y is same-sized as X

When using a padding with size p, we need to redefine h and w as:

Thus, the padding size p=k12 if we want to ensure Y has the same dimensions as X.

Interpolation padding

Similar to same padding, but we use the value of the closest pixel instead of zeros. This supports zooming into the original image, which means we can resize the image and then apply interpolation techniques before convolution such that the output Y has the same size as X.

2025-03-06_01-39-59_Convolution_Interpolation padding.png

image = np.pad(image, ((padding, padding), (padding, padding)), 'constant')

Stride

Controls the movement or step size of the convolution filter as it slides over the input image

Larger stride results in a smaller output feature map, and the converse is true.

Stride can help reduce the spatial dimensions of the feature map, reducing the number of parameters and computation.

A stride of 2 means we are sliding to every other pixel.

Dilation

Spacing between values of original image multiplying the kernel. By default d=1

For d>1, the pixel values are spread apart by d pixels

2025-03-06_01-53-08_Convolution_Dilation example.png

Convolution formula

The resulting image Y will have a size h×w×c, where:

h=h+2pd(k1)1s+1w=w+2pd(k1)1s+1

Higher dimension convolution

With a 2D kernel:

For pixels Yi,j,l,

i[1,h],j[1,w],l[1,3]:Yi,j,l=1k2m=1kn=1kXi+m1,j+n1,lKm,n+b

This preserves the original number of channels of original image X, producing new RGB image Y

For a kernel as a tensor with its own number of channels kd1:

For pixels Yi,j,l calculated using the convolution operation on each channel separately,

i[1,h],j[1,w],l[1,kd]:Yi,j,l=m=1kn=1kl=13Xi+m1,j+n1,lKm,n,l+b

This produces a new image Y, with number of channels matching kernel K i.e. h×w×kd