Skip to content
Kuza Ndoto Academy

Convolutional Neural Networks

IntermediateChapter 6 · Page 20 of 266 min readUpdated 15 Nov 2026

A convolutional neural network (CNN) is a neural network designed for grid-like data such as images. Instead of treating every pixel separately, it slides small filters across the image to detect edges, textures and shapes. CNNs power face unlock, medical image analysis and apps that diagnose crop diseases from a photo.

Why ordinary networks struggle with images

To a computer, an image is a grid of numbers. A greyscale image is a 2-D grid of pixel brightness values (0 = black, 255 = white); a colour image has three grids, one each for red, green and blue. A small 224 × 224 colour photo has 224 × 224 × 3 = 150,528 numbers.

A dense layer of just 1,000 neurons connected to every one of those numbers would need over 150 million weights. Worse, it treats each pixel position separately, so a cat in the top-left corner and a cat in the bottom-right look like completely different inputs. CNNs fix both problems.

Convolution: sliding a filter

A filter (or kernel) is a small grid of weights, typically 3 × 3. It slides across the image; at each position, it multiplies its weights with the pixels underneath and adds them up. The grid of results is a feature map that shows where the filter's pattern appears.

This filter detects vertical edges (bright on the left, dark on the right):

 1  0  -1
 1  0  -1
 1  0  -1

Let's slide it across a 4 × 6 image whose left half is bright (9) and right half is dark (0):

import numpy as np

image = np.array([[9, 9, 9, 0, 0, 0]] * 4)     # 4 rows, 6 columns
kernel = np.array([[1, 0, -1]] * 3)            # 3 x 3 vertical-edge filter

out = np.zeros((2, 4), dtype=int)
for i in range(2):
    for j in range(4):
        patch = image[i:i + 3, j:j + 3]        # the 3 x 3 window under the filter
        out[i, j] = np.sum(patch * kernel)
print(out)
[[ 0 27 27 0] [ 0 27 27 0]]

Check the first value: the window covers columns 0 to 2, all 9s, so (9 + 9 + 9) − (9 + 9 + 9) = 0: no edge. One step to the right, the window covers columns 1 to 3: the left column adds 27 and the right column (all 0s) subtracts nothing, giving 27. The large values mark exactly where the bright-to-dark edge is.

999000 999000 999000 999000 image (4 × 6) 10-1 10-1 10-1 kernel (3 × 3) multiply, sum 027270 027270 feature map (2 × 4) gold window: columns 1-3 give (9+9+9) - (0+0+0) = 27
Figure 1. The NumPy example as a picture. The gold window over columns 1 to 3 produces the gold-outlined 27 in the feature map; the strong responses line up with the edge.
  • The output size is (input size − filter size) / stride + 1. Here: (6 − 3) / 1 + 1 = 4 columns and (4 − 3) / 1 + 1 = 2 rows.
  • Stride is how far the filter moves each step. Padding adds a border of zeros so the output can keep the input's size (padding="same" in Keras).
  • The same small set of weights is reused at every position (weight sharing). That's why CNNs need far fewer weights, and why they detect a pattern wherever it appears.

The crucial point: in a CNN, nobody designs the filters by hand. The network learns the filter weights by backpropagation, just like any other weights (see Neural Networks). A layer has many filters, each producing its own feature map.

Note

Strictly speaking, deep learning libraries compute cross-correlation (the filter is not flipped), but everyone calls it convolution. Since the filters are learned, the difference doesn't matter in practice.

Pooling: shrinking the feature maps

A pooling layer shrinks each feature map, keeping the important information while reducing computation. Max pooling with a 2 × 2 window keeps the largest value in each block:

fmap = np.array([[1, 3, 2, 0],
                 [4, 2, 1, 5],
                 [0, 1, 3, 2],
                 [2, 6, 1, 1]])

pooled = fmap.reshape(2, 2, 2, 2).max(axis=(1, 3))   # max of each 2 x 2 block
print(pooled)
[[4 5] [6 3]]

The top-left block holds 1, 3, 4 and 2, so it becomes 4; the top-right block (2, 0, 1, 5) becomes 5, and so on. A 4 × 4 map becomes 2 × 2. Pooling also makes the network a little more tolerant of small shifts in the image.

1320 4215 0132 2611 feature map (4 × 4) max of each 2 × 2 block 45 63 pooled (2 × 2)
Figure 2. Each coloured 2 × 2 block keeps only its largest value (shown in bold), matching the NumPy output.

A typical CNN architecture

  1. Convolution + ReLU layers detect simple features: edges and colour blobs.
  2. Pooling shrinks the maps.
  3. More convolution + pooling blocks combine simple features into complex ones: edges into corners, corners into shapes, shapes into eyes or leaves.
  4. Flatten the final maps into a list of numbers.
  5. Dense layers and a softmax output make the final classification.

A CNN in Keras

This classic example classifies handwritten digits from the MNIST dataset (70,000 greyscale 28 × 28 images). It needs an extra installation (pip install tensorflow) and downloads the dataset the first time it runs:

import keras
from keras import layers

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1) / 255.0    # add a channel axis, scale to 0-1
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0

model = keras.Sequential([
    keras.Input(shape=(28, 28, 1)),
    layers.Conv2D(32, kernel_size=3, activation="relu"),   # 28x28 becomes 26x26x32
    layers.MaxPooling2D(pool_size=2),                      # 13x13x32
    layers.Conv2D(64, kernel_size=3, activation="relu"),   # 11x11x64
    layers.MaxPooling2D(pool_size=2),                      # 5x5x64
    layers.Flatten(),                                      # 1600 values
    layers.Dropout(0.5),
    layers.Dense(10, activation="softmax"),                # one output per digit
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=128, epochs=5, validation_split=0.1)

print(model.evaluate(x_test, y_test, verbose=0))

This model has only 34,826 parameters: 320 in the first convolution (3 × 3 × 1 × 32 weights + 32 biases), 18,496 in the second, and 16,010 in the dense layer. After five epochs, test accuracy is typically around 0.98 to 0.99; your exact number will differ because training starts from random weights.

Transfer learning

Training a large CNN from scratch needs huge datasets and GPUs. In practice, people start from a network already trained on millions of images (such as ResNet or MobileNet, available in keras.applications) and retrain only the last layers on their own smaller dataset, for example a few thousand photos of healthy and diseased cassava leaves. This is transfer learning, and it often gives excellent results with modest data.

Common mistake

Training and testing on photos that are too similar, such as several shots of the same leaf split across train and test, or a dataset where every diseased photo was taken in one field and every healthy photo in another. The model may learn the background instead of the disease and fail in a real farm. Make sure the test images reflect real conditions.

Key points

  • CNNs slide small learned filters across images to detect local patterns.
  • Weight sharing means far fewer weights and pattern detection anywhere in the image.
  • Pooling shrinks feature maps; stacked layers build simple features into complex ones.
  • Transfer learning lets you build strong image models with modest data.
Up next · Deep Learning & GenAIHow Large Language Models WorkThe technology behind chatbots.
→

Tip: use the ← and → keys to move between pages.