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.

Machine Learning · beginner · 10 min · 44 steps · free

What you'll learn

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

  1. 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.
  2. The filter (kernel): −1 on the left, +1 on the right. It answers "is it brighter on the right than on the left?".
  3. The output grid: a 3 × 3 filter fits in 3 positions across and 3 down.
  4. Row 0 of the output.
  5. row is created and points to a new list: [].
  6. Column 0: the filter covers image columns 0 to 2.
  7. 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!
  8. 27 is added to the end of the list that row points to.
  9. c changes from 0 to 1.
  10. Column 1: covers columns 1 to 3. Left 0s, right 9s: 27 again. The edge is still inside the window.
  11. 27 is added to the end of the list that row points to.
  12. c changes from 1 to 2.
  13. Column 2: covers columns 2 to 4, all bright. Left 9s cancel right 9s: 0.
  14. 0 is added to the end of the list that row points to.
  15. Python moves to the next item of the loop: for c in range(3).
  16. Row 0 of the output is [27, 27, 0].
  17. r changes from 0 to 1.
  18. row now points to a different list: [].
  19. c changes from 2 to 0.
  20. total changes from 0 to 27.
  21. 27 is added to the end of the list that row points to.
  22. c changes from 0 to 1.
  23. Line runs: total = sum([image[r + i][c + j] * kernel[i][j] for i in range(3) for j in range(3)]).
  24. 27 is added to the end of the list that row points to.
  25. c changes from 1 to 2.
  26. total changes from 27 to 0.
  27. 0 is added to the end of the list that row points to.
  28. Python moves to the next item of the loop: for c in range(3).
  29. Row 1: the image is the same all the way down, so the same numbers.
  30. r changes from 1 to 2.
  31. row now points to a different list: [].
  32. c changes from 2 to 0.
  33. total changes from 0 to 27.
  34. 27 is added to the end of the list that row points to.
  35. c changes from 0 to 1.
  36. Line runs: total = sum([image[r + i][c + j] * kernel[i][j] for i in range(3) for j in range(3)]).
  37. 27 is added to the end of the list that row points to.
  38. c changes from 1 to 2.
  39. total changes from 27 to 0.
  40. 0 is added to the end of the list that row points to.
  41. Python moves to the next item of the loop: for c in range(3).
  42. a list is added to the end of the list that output points to.
  43. Python moves to the next item of the loop: for r in range(3).
  44. 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.

Common mistakes

Best practices

Key takeaways

Check yourself

A 3 × 3 filter slides over a 6 × 6 image (no padding). What size is the output?

Answer: 4 × 4. It fits in 6 − 3 + 1 = 4 positions each way.

Where does an edge-detecting filter give a big output?

Answer: Where brightness changes. Flat areas cancel out; changes do not.

·

More in Deep Learning