RECEPTIVE FIELD

Ch 2 ▸ Convolution by hand ▸ Module 3/4

  1. 1One output
  2. 2Sliding the kernel
  3. 3What kernels find
  4. 4Moving the duck

What kernels find.

🔒 This module is locked

It builds on Sliding the kernel. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.

Go to Sliding the kernel

The slide is always the same. Only the nine weights change, and they decide what the output map shows.

Negative outputs

The blur kernel’s weights are all positive, and the duck’s pixels are all between 0 and 1, so every blur output is between 0 and 1 too, and the map can be drawn as a grayscale image. Kernels with negative weights can produce negative outputs, and a grayscale image has no way to show those. So the figures switch colors for them: red for outputs above zero, blue for outputs below zero, and the dark background color for zero. The stronger the color, the further from zero.

The edge kernel

The kernel from the worked examples is called a Sobel kernel, after Irwin Sobel, who described it in 1968. It finds places where brightness changes from left to right. Its version for that direction is written sobel_x in the figures:

[−101−202−101]\begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}

Read the weights column by column. The left column is negative, the right column is positive, and the middle column is zero. So the output is the right column of pixels minus the left column, with the middle row counted twice.

The bigger the jump in brightness, the further the output is from 0. The 4×4 example’s map was 28 on the left and 16 on the right, because the brightness jumps from 1 or 2 up to 8 or 9 in the first three columns, but only from 4 or 5 up to 8 or 9 in the last three.

In figure 2.3 the edge kernel runs over the duck. Most of the map is the dark zero color, because sky and water are flat. The strongest outputs, red and blue, sit around the duck’s eye, where black pixels meet white feathers on both sides. The left edge of the duck’s head, where the gray sky (0.56) meets the white head (1.00), shows up red: the output there is (1.00−0.56)×(1+2+1)≈1.78(1.00 - 0.56) \times (1 + 2 + 1) \approx 1.78. (The sky is exactly 5/9, which rounds to 0.56; with the rounded value you’d get 1.76.) The right edge of the head, where white meets sky again, shows up blue.

FIG 2.3A ▸ INPUT x32×32
The input x, 32×32. The highlighted squares are the nine pixels the kernel reads.
FIG 2.3B ▸ OUTPUT y30×30
NEGPOS
The output map y, 30×30, written one dot product at a time. This kernel has negative weights, so outputs can be negative: blue is below zero, red is above.
0/0 OUTPUTS · SWEEP 0.0 S
Kernel
15 STEPS/S
PYTHON ▸ conv.py
>>> out_size(n=32, k=3, p=0, s=1, d=1)
30 # ⌊(32 + 2·0 − 1·2 − 1) / 1⌋ + 1
>>> patch =
0.000.000.000.000.000.000.000.000.00
*
>>> (patch * w).sum()
–

Horizontal edges, like the top of the head, barely show at all. Where brightness changes only from top to bottom, the left and right columns of the patch hold the same values, so they cancel and the output is 0. Only the head’s corners, where the edge turns, light up. A kernel for horizontal edges is the same grid turned on its side.

A kernel looks for a pattern

The same reasoning works for any kernel. The output is large when bright pixels sit under large positive weights and dark pixels sit under negative weights. In other words, a kernel responds most strongly to patches that look like its own weights. The edge kernel’s weights are dark on the left and bright on the right, so it responds to patches that go from dark to bright. This is the same idea as the duck output’s weights in chapter 1, which looked like the duck, but the pattern is only 3×3, and it’s checked at every position.

Try the other kernels in figure 2.3 and read their weights in the listing:

You can also type your own weights into the listing.

Where the weights come from

These kernels were designed by hand, long before CNNs, and they’re useful for seeing what a convolution can do. A CNN doesn’t use them. Its kernels start out as random numbers, and training adjusts them, as it adjusts a dense layer’s weights, until the network gives good answers. Chapter 7 shows how. What the trained kernels end up looking for is whatever helps with the task, and chapter 9 looks inside a trained network to see.

Knowledge check ▸ 5 questions

Before you move on.

Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.

  1. 1The edge kernel −1, 0, 1 / −2, 0, 2 / −1, 0, 1 covers a patch where every pixel is 0.7. What is the output?

  2. 2The edge kernel covers a patch that is bright on the left and dark on the right. What sign is the output, and how is it drawn?

  3. 3Which edges does the edge kernel in figure 2.3 barely respond to?

  4. 4The identity kernel (all zeros except a 1 in the middle) covers the patch 0.1, 0.2, 0.3 / 0.4, 0.5, 0.6 / 0.7, 0.8, 0.9. What is the output?

  5. 5Where do a CNN’s kernel weights come from?

0 of 5 answered correctly.

🔒 Next module: Moving the duck