AI Compass
Compass

Convolution and CNNs

What a convolution kernel does, why the same operation finds edges and recognises cats, and how output size and cost are calculated.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

Lay a nine-square stencil over an image. Multiply every pixel under the stencil by the number in the corresponding square, add it all up, and write the result into a new image. Then shift the stencil one pixel and repeat.

That is a convolution. Depending on the numbers in the stencil, the result traces edges, blurs, sharpens, or highlights particular patterns.

Well-known kernels

KernelEffect
[[0,0,0],[0,1,0],[0,0,0]]Nothing, the image is unchanged
1/9 · [[1,1,1],[1,1,1],[1,1,1]]Blur
[[0,-1,0],[-1,5,-1],[0,-1,0]]Sharpen
[[-1,0,1],[-2,0,2],[-1,0,1]]Vertical edges (Sobel)

Convolution by hand

import cv2, numpy as np

img = cv2.imread("part.jpg", cv2.IMREAD_GRAYSCALE).astype(np.float32)

sharpen = np.array([[ 0, -1,  0],
                    [-1,  5, -1],
                    [ 0, -1,  0]], dtype=np.float32)
sharp = cv2.filter2D(img, -1, sharpen)

# The sum of the kernel values sets the brightness of the result:
# sum 1 leaves it unchanged, sum 0 gives a dark edge image.
print(sharpen.sum())    # 1.0

The three dials

DialEffect
Kernel sizeHow much context a filter sees. Usually 3, rarely 5 or 7.
StrideStep size. Stride 2 halves the output size.
PaddingAdd a border so the output keeps its size.
  • For an unchanged output size with kernel 3, always set padding 1.
  • Pooling reduces resolution without parameters; stride 2 does the same with them.
  • With multiple channels, a kernel always spans the full input channel depth.

The formulas

Discrete 2D convolution

S(i,j) = Σ_m Σ_n I(i+m, j+n) · K(m, n)

Each output value is the sum of products of kernel values and the image values underneath.

I
the input image
K
the kernel of size m by n
S
the output, also called a feature map

Output size, parameters and cost

H_out = ⌊(H + 2p − k) / s⌋ + 1 parameters = k² · C_in · C_out + C_out FLOPs ≈ 2 · H_out · W_out · k² · C_in · C_out

Output size follows from padding and stride, parameter count from kernel area times channels, cost from both together.

H, W
input height and width
k
kernel size
p
padding
s
stride
C_in, C_out
input and output channel counts

Worked through

A layer with H = W = 224, k = 3, p = 1, s = 1, C_in = 64, C_out = 128:

  • H_out = ⌊(224 + 2 − 3)/1⌋ + 1 = 224, resolution preserved.
  • Parameters: 9 × 64 × 128 + 128 = 73,856
  • FLOPs: 2 × 224 × 224 × 9 × 64 × 128 ≈ 7.4e9 per image

A single layer costs 7.4 GFLOPs. A fifty-layer network lands at several hundred GFLOPs per image, which gives the required compute directly. See Why GPUs.

Why separable convolutions save so much

A depthwise separable convolution splits the operation into one convolution per channel followed by a 1 by 1 convolution across channels.

Saving from separable convolution

ratio = 1/C_out + 1/k²

Cost falls to the reciprocal of the channel count plus the reciprocal of the kernel area.

k
kernel size
C_out
number of output channels

For k = 3 and C_out = 128 that is 1/128 + 1/9 = 0.119: just under an eighth of the cost. MobileNet and every architecture aimed at edge devices rests on this.

Related courses and sources

PaperFreeEN

An Image is Worth 16x16 Words

Images as a sequence of patches, processed like text. The paper that brought transformers into vision.

For anyone processing image and text in one model.

CoursePartly free7200 minEN

Deep Learning Specialization

Five courses from the basics of neural networks to sequence models. Thorough, with programming exercises, and in places older than current practice.

For anyone who can program and wants to work through the field completely.

DeepLearning.AIGo to offer
PaperFreeEN

Deep Residual Learning

The shortcut across layers that made hundred-layer networks trainable. Present in every architecture today.

For understanding how networks were able to get deep at all.

BookFreeEN

Dive into Deep Learning

A textbook with runnable code beside every derivation. Each chapter opens as a notebook you can recompute yourself.

For anyone who wants to compute along while reading; every chapter opens as a notebook.

CourseFree1500 minEN

Hugging Face computer vision course

From image preprocessing through convolutional networks to vision transformers, with runnable examples for detection and segmentation.

For development with image data; assumes Python and delivers runnable examples in exchange.

Hugging FaceGo to offer
CourseFree4200 minEN

Practical Deep Learning for Coders

Starts with a working model in the first hour and supplies the theory afterwards. The shortest route from basic Python to a model you trained yourself.

For impatient readers who know Python: your first model runs within the first hour.

PaperFreeEN

U-Net

Segmentation from few examples, developed in medical imaging. Still the first choice for segmentation.

For segmentation from few examples; still the first choice.

PaperFreeEN

Very Deep Convolutional Networks

The paper showing that depth with small filters wins. The architecture convolutional networks are usually explained with.

For getting into convolutional networks; the architecture they are usually explained with.

Was this page helpful?
Convolution and CNNs