Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 40 ยท Convolutional Networks

How Convolution Works

Understand the convolution operation behind CNNs: kernels, sliding windows, feature maps and edge detection, built from scratch in NumPy before using Keras Conv2D.

A dense layer looks at every input value at once and learns one weight per connection. For an image of 28 by 28 pixels that is already 784 inputs for every neuron, and the layer has no idea that pixel 1 and pixel 2 are neighbors. Convolution fixes both problems. It slides a small grid of weights, called a kernel or filter, across the image and computes the same weighted sum at every position.

In this lesson you will build a 2D convolution in plain NumPy so that nothing about Conv2D is magic when you meet it in Keras. NumPy stands in for TensorFlow here, because the arithmetic is identical.

TensorFlow is not installed in the in-browser Python runner. The runnable blocks below use NumPy to show the exact math. Blocks that use keras are read-only.

The sliding window

Take a 3 by 3 kernel. Place it over the top-left corner of the image, multiply each kernel weight by the pixel under it, add the nine products, and write the result into the output. Then move one pixel right and repeat. When you reach the end of the row, move down one row.

Convolution in four steps

Place the kernel Align it with a patch
Multiply Weight times pixel
Add up One number per spot
Slide Repeat everywhere

The same kernel weights are reused at every position.

1 / 2

The output grid is called a feature map. A large value means "the pattern this kernel looks for is strongly present here".

Convolution in NumPy

The loop below is deliberately slow and obvious. Strictly speaking, deep learning libraries compute cross-correlation (the kernel is not flipped), but everyone calls it convolution, and so does Keras.

Python 2D convolution from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The image has a dark left half and a bright right half. The output is large only in the columns where the kernel straddles the border, so this kernel is a vertical edge detector. Flip the kernel's sign and the response flips too.

Hand-made kernels, learned kernels

Classic image processing used kernels designed by people: blur, sharpen, edge detection. A CNN does something different. It starts with random kernel values and lets training (see Training and Evaluation) adjust them until they detect whatever helps the task. Early layers usually end up with edge and color detectors; deeper layers combine them into textures and shapes.

Python Three kernels, three views

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Output size

A kernel of size k on an input of size n fits in n - k + 1 positions, so the feature map shrinks. A 28 by 28 image and a 3 by 3 kernel give 26 by 26. The next lesson covers padding and strides, which let you control this. Notice also how few weights there are: a 3 by 3 kernel has 9 weights no matter how big the image is. That is weight sharing, and it is why CNNs need far fewer parameters than dense networks on images.

Multiple channels and many filters

Color images have three channels. A kernel for an RGB image is really a 3 by 3 by 3 block: one 3 by 3 slice per channel. Each position still produces a single number, because the products from all channels are summed together. A layer then uses many filters, say 32. Each filter produces its own feature map, and stacking them gives an output with 32 channels.

Python Channels and filters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The parameter count 3*3*3*4 + 4 = 112 is exactly what Keras reports for Conv2D(4, 3) on a 3-channel input.

The same thing in Keras

This is read-only, because TensorFlow does not run in the browser here.

Python
import keras
from keras import layers

x = keras.random.uniform((1, 8, 8, 3))
conv = layers.Conv2D(filters=4, kernel_size=3)
y = conv(x)
print(y.shape)              # (1, 6, 6, 4)
print(conv.count_params())  # 112

Keras uses the shape (batch, height, width, channels) by default ("channels last"). The first dimension, the batch, never changes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A 3x3 kernel is applied to a 10x10 single-channel image with no padding and stride 1. What is the output size?

    The kernel fits in 10 - 3 + 1 = 8 positions along each axis.
  2. 2. Why do CNNs need fewer parameters than dense layers on images?

    Weight sharing means one small kernel is applied everywhere instead of one weight per pixel connection.
  3. 3. How many weights (without bias) does one 3x3 filter have on an RGB input?

    The filter spans all channels: 3 x 3 x 3 = 27.
  4. 4. What is a feature map?

    Each filter yields one feature map whose values say how strongly its pattern appears at each location.

Summary

  • A convolution slides a small kernel over the input and computes a weighted sum at each position.
  • The result is a feature map; many filters give many channels.
  • Weight sharing keeps the parameter count small and respects the neighbor structure of images.
  • Without padding the output shrinks by k - 1 along each axis.
  • Keras Conv2D learns the kernel values; you choose their number and size.

Next: Padding, Strides and Pooling