Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 73 ยท Convolutional Networks

Convolution, Pooling and Feature Maps

Build a convolutional layer in NumPy with many filters and channels, add max and average pooling, count parameters and visualize feature maps.

The Convolution Operation slid one kernel over one grayscale image. Real convolutional layers do more. A color image has three channels, and a layer applies dozens of different filters at once, each producing its own feature map. The stack of feature maps is the layer's output, and it is the input to the next layer.

In this lesson you will extend the operation to many channels and filters, count how few parameters that needs, write a batched convolution layer in NumPy, add pooling to shrink the maps, and look at what the feature maps of a real handwritten digit contain. You will also see how deep layers end up "seeing" a large part of the image even though every kernel is tiny: that is the receptive field.

A convolutional layer, piece by piece

Input A stack of channels, such as red, green and blue.
Filters Each filter spans every input channel.
Feature maps One output map per filter.
Shared weights Each filter is reused at every position.
1 / 2

Channels and filters

An image batch has shape (N, C, H, W): N images, C channels, H rows, W columns. A convolutional layer owns a weight tensor of shape (F, C, kh, kw): F filters, each of which has one kh by kw kernel per input channel.

To compute one output map, a filter slides across the image. At each position it multiplies its patch in every channel by the matching kernel, then adds all of those products (over channels as well as over the window) into a single number, plus one bias per filter. With F filters you get F output maps, so the output shape is (N, F, out_h, out_w).

The parameter count shows why this is so efficient. It depends only on the kernel, never on the image size:

TEXT
parameters = F * (C * kh * kw + 1)
Python Counting parameters: convolution versus dense

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 convolution needs 448 parameters (16 filters times 27 weights plus 16 biases), whatever the image size. A dense layer producing the same output shape needs over 50 million for a 32 by 32 image, and the number grows with the square of the image side. This sharing of weights across positions is called parameter sharing, and it is the main reason convolutional networks can handle large images.

A convolution layer in NumPy

Last lesson you used sliding_window_view to expose every patch. Adding batch and channel dimensions is a small step. For padding we use np.pad; for stride we take every s-th window. A single einsum then sums over the channel and kernel axes:

Python A batched conv layer

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 shapes follow the size formula from the previous lesson, with the filter count F = 4 in the channel position. The last line confirms that one output cell is nothing more than a dot product of a 3-channel patch with a 3-channel filter, plus the bias.

Stacking layers works because the output's channel count becomes the next layer's C. A network might go 3 -> 16 -> 32 -> 64 channels, trading spatial size (pooling and strides shrink it) for depth (more filters). Early filters look for edges and colors; later ones combine them into textures and shapes.

Pooling

A pooling layer replaces each small window of a feature map by one number. It has no weights to learn. The usual choice is max pooling with a 2 by 2 window and stride 2, which halves the height and width and keeps the strongest response in each window. Average pooling keeps the mean instead.

Pooling does two jobs. It cuts the amount of data (and computation) by four per map. And it gives a little tolerance to position: if a feature moves by a pixel but stays inside the same window, the pooled output does not change.

For non-overlapping windows, a reshape does it all, with no loops: split each spatial axis into (blocks, 2) and reduce over the "2" axes.

Python Max and average pooling

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.

On the 4 by 4 map, max pooling gives [[4, 2], [2, 7]] and average pooling gives [[2.5, 1.0], [1.25, 4.75]]. The pixel moving from column 2 to column 3 stays inside the same 2 by 2 window, so the pooled maps are identical. Moving it on to column 4 crosses a window boundary, and the pooled map changes. Pooling gives tolerance, not perfect invariance.

Modern networks often replace pooling by a convolution with stride 2, which learns how to downsample, and finish with a global average pool that averages each entire map into one number before the final classifier. The idea is the same: shrink the spatial size, keep the channels.

Receptive field

Each output cell depends on a patch of the input, called its receptive field. A single 3 by 3 convolution sees 3 by 3 pixels. Put a second 3 by 3 convolution on top, and each of its cells sees a 3 by 3 area of the first map, which together cover 5 by 5 input pixels. Layer by layer, the field grows, and downsampling (stride or pooling) makes it grow faster.

The recurrence tracks two numbers: the receptive field rf and the jump j (the distance in input pixels between neighboring cells). For a layer with kernel k and stride s:

TEXT
rf = rf + (k - 1) * j
j  = j * s
Python Receptive field of a stack of layers

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.

After seven small layers, one output cell already sees a 24 by 24 patch: small kernels, big view. That is why deep convolutional stacks can recognize whole objects, even though every kernel is only 3 by 3.

Looking at feature maps

To end, push a real handwritten digit through a conv layer. The scikit-learn load_digits data set is bundled with the library (no download), with 8 by 8 grayscale images. We use four hand-made 3 by 3 filters that respond to vertical, horizontal and the two diagonal strokes, apply a ReLU (keep only positive responses), and max-pool.

Python Feature maps of a handwritten digit

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.

Each row of the figure is one stage. The conv row can have negative values; ReLU zeroes them; pooling shrinks each map to 4 by 4. Each map highlights the strokes of the 7 that run in its own direction, and the printed strongest responses show which filters fire hardest on this digit. In a trained network the filters are learned instead of hand-made, and the later layers combine such maps into more complex detectors. In the next lesson, Training a CNN From Scratch, we will derive how those filters get their gradients.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A conv layer has 8 filters, 3 input channels and 5 by 5 kernels. How many parameters does it have, including biases?

    Each filter has 3 * 5 * 5 = 75 weights plus 1 bias = 76. Times 8 filters = 608.
  2. 2. What is the shape of the output of a conv layer with F filters on an input of shape (N, C, H, W)?

    Each filter produces one feature map per image, so the channel axis becomes F. The input channels are summed away inside each filter.
  3. 3. Which statement about max pooling is correct?

    Pooling has no weights, shrinks height and width, and leaves the channel count alone. A feature that moves within a window gives the same pooled output.
  4. 4. Two stacked 3 by 3 convolutions with stride 1 give each output cell a receptive field of:

    rf = 1 + (3-1) + (3-1) = 5. Each added 3 by 3 layer extends the field by 2 pixels when the jump is 1.

Summary

  • A conv layer has weights of shape (F, C, kh, kw) and a bias per filter, and maps (N, C, H, W) to (N, F, out_h, out_w).
  • Parameter sharing makes the parameter count independent of image size: F * (C * kh * kw + 1).
  • sliding_window_view with einsum gives a compact, loop-free batched convolution.
  • Max pooling keeps the strongest response per window; average pooling keeps the mean. Neither has weights, and both shrink the maps and add shift tolerance.
  • The receptive field grows with depth and with strides, so small kernels can still see large regions.
  • Feature maps show where in the image each filter fires; a trained network learns its own filters.

Next: Training a CNN From Scratch.