Machine Learning from Scratch · Deep Learning
Convolution: How Networks See Edges
Slide a 3 × 3 filter over a tiny image and add up what it covers. Where dark meets bright, the numbers jump: the filter has found an edge.
convolution.py
# A convolution slides a small filter over an image. This filter lights up where dark meets bright: an edge.
image = [[0, 0, 9, 9, 9] for _ in range(5)] # 5 x 5 pixels: dark on the left, bright on the right
kernel = [[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]
output = []
for r in range(3):
row = []
for c in range(3):
total = sum([image[r + i][c + j] * kernel[i][j] for i in range(3) for j in range(3)])
row.append(total)
output.append(row)
print(output)
Step by step
- A 5 × 5 greyscale image: 0 is dark, 9 is bright. Dark on the left, bright on the right, so there is an edge between columns 1 and 2.
- The filter (kernel): −1 on the left, +1 on the right. It answers "is it brighter on the right than on the left?".
- The output grid: a 3 × 3 filter fits in 3 positions across and 3 down.
- Row 0 of the output.
row is created and points to a new list: [].- Column 0: the filter covers image columns 0 to 2.
- Multiply each covered pixel by the filter number on top of it, and add up: the right column (9s) gives +27, the left column (0s) gives 0. Total 27: an edge!
- 27 is added to the end of the list that
row points to. c changes from 0 to 1.- Column 1: covers columns 1 to 3. Left 0s, right 9s: 27 again. The edge is still inside the window.
- 27 is added to the end of the list that
row points to. c changes from 1 to 2.- Column 2: covers columns 2 to 4, all bright. Left 9s cancel right 9s: 0.
- 0 is added to the end of the list that
row points to. - Python moves to the next item of the loop:
for c in range(3). - Row 0 of the output is [27, 27, 0].
r changes from 0 to 1.row now points to a different list: [].c changes from 2 to 0.total changes from 0 to 27.- 27 is added to the end of the list that
row points to. c changes from 0 to 1.- Line runs:
total = sum([image[r + i][c + j] * kernel[i][j] for i in range(3) for j in range(3)]). - 27 is added to the end of the list that
row points to. c changes from 1 to 2.total changes from 27 to 0.- 0 is added to the end of the list that
row points to. - Python moves to the next item of the loop:
for c in range(3). - Row 1: the image is the same all the way down, so the same numbers.
r changes from 1 to 2.row now points to a different list: [].c changes from 2 to 0.total changes from 0 to 27.- 27 is added to the end of the list that
row points to. c changes from 0 to 1.- Line runs:
total = sum([image[r + i][c + j] * kernel[i][j] for i in range(3) for j in range(3)]). - 27 is added to the end of the list that
row points to. c changes from 1 to 2.total changes from 27 to 0.- 0 is added to the end of the list that
row points to. - Python moves to the next item of the loop:
for c in range(3). - a list is added to the end of the list that
output points to. - Python moves to the next item of the loop:
for r in range(3). - The output lights up (27) exactly where the edge is and stays 0 on the flat bright area. That is a feature map. A CNN layer has dozens of learned filters, each producing a map, and stacks layers to see bigger patterns.