Convolutional Neural Networks (CNNs) Explained Simply
CNNs are the neural networks built for images. Learn how convolution, filters, ReLU and pooling let a computer "see" — with clear diagrams, a runnable NumPy demo, and real Keras code.
In Lesson 15 you built an ordinary neural network, and in Lesson 16 you saw how it learns. Those networks are great for tables of numbers — but hand them a photo and they struggle badly. Convolutional Neural Networks (CNNs) are the fix. They're the architecture behind face unlock, medical scan analysis, self-driving car vision, and almost every "the computer recognised the image" story you've heard. This lesson explains exactly how they work — no heavy math, just clear pictures.
Why regular neural networks struggle with images
A small 200×200 colour photo has 200 × 200 × 3 = 120,000 numbers. If you feed that into a normal (fully-connected) network, the very first hidden layer needs a weight for every pixel connected to every neuron — millions of weights before you've done anything useful. Two problems follow:
- Too many parameters. The network is huge, slow, and overfits easily (remember Lesson 12).
- It ignores structure. A cat's ear is a cat's ear whether it's top-left or bottom-right. A plain network treats every pixel position as unrelated, so it has to re-learn "what an ear looks like" in every location. That's wasteful.
CNNs solve both by using a small, reusable pattern-detector that scans the whole image. That detector is called a filter (or kernel).
The big idea: convolution
A filter is just a tiny grid of numbers — often 3×3. You slide it across the image, and at each position you multiply the overlapping pixels by the filter values and add them up. That single number goes into a new grid called a feature map. Slide, compute, record — across the whole image.
Here's the magic: the network learns the filter values itself during training. One filter might learn to detect vertical edges, another horizontal edges, another a patch of "fur" texture. Because the same filter is reused across the whole image, it detects its pattern anywhere — and it needs only nine weights, not millions. That's the two problems solved at once.
The three building blocks
A CNN stacks three simple operations, over and over:
| Layer | What it does | In one sentence |
|---|---|---|
| Convolution | Slides filters over the input to build feature maps. | Finds patterns (edges, textures, shapes). |
| ReLU | Replaces negative values with zero. | Keeps the strong signals, drops the rest. |
| Pooling | Shrinks each feature map (e.g. keep the max of every 2×2 block). | Smaller, faster, and position-tolerant. |
Pooling deserves a note. "Max pooling" looks at a small block — say 2×2 — and keeps only the largest value. It halves the width and height, cutting computation, and it makes the network care that a feature exists nearby rather than its exact pixel. That's why a CNN still recognises a cat when it shifts a few pixels.
Putting it together: the CNN pipeline
Early layers detect simple things (edges, colours). Their outputs feed later layers that combine those into bigger patterns (eyes, wheels, letters). By the end, a normal fully-connected layer takes those high-level features and makes the final decision.
See a filter in action (runnable)
You don't need a deep-learning library to feel what convolution does. This tiny NumPy example slides a known edge-detection filter over a simple image and shows how it lights up the boundary. Run it and read the output grid — the high numbers trace the edge.
import numpy as np
# A 6x6 "image": left half dark (0), right half bright (1)
image = np.array([
[0,0,0,1,1,1],
[0,0,0,1,1,1],
[0,0,0,1,1,1],
[0,0,0,1,1,1],
[0,0,0,1,1,1],
[0,0,0,1,1,1],
])
# A 3x3 vertical-edge filter
kernel = np.array([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1],
])
# Slide the kernel across the image (valid convolution)
h, w = image.shape
kh, kw = kernel.shape
feature_map = np.zeros((h - kh + 1, w - kw + 1))
for r in range(feature_map.shape[0]):
for c in range(feature_map.shape[1]):
patch = image[r:r+kh, c:c+kw] # the 3x3 window
feature_map[r, c] = np.sum(patch * kernel)
print(feature_map)
# The column where dark meets bright shows a strong 3 —
# the filter "found" the vertical edge.
That's the entire secret. A CNN just does this thousands of times with filters it learned, stacking the results into ever-more-abstract features.
What real CNN code looks like
In practice nobody writes the sliding loop by hand — libraries like Keras (TensorFlow) or PyTorch do it, on the GPU, and handle the learning. Here's a complete small CNN in Keras so you recognise the shape of real code. (This one needs pip install tensorflow to run; read it as a map, not a must-run.)
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Conv2D(32, (3,3), activation="relu", input_shape=(28,28,1)),
layers.MaxPooling2D((2,2)), # shrink by half
layers.Conv2D(64, (3,3), activation="relu"),
layers.MaxPooling2D((2,2)),
layers.Flatten(), # grid -> single list
layers.Dense(64, activation="relu"),
layers.Dense(10, activation="softmax")# 10 classes
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model.summary() # prints the layers you just read about
Read it top to bottom and you'll see the exact pipeline from the diagram: two Conv + Pool blocks, a Flatten, then Dense layers for the decision. Everything you learned in Lessons 15 and 16 still applies — this network learns by the same backpropagation and gradient descent.
Where CNNs are used
| Field | What a CNN does |
|---|---|
| Healthcare | Spot tumours or fractures in X-rays, MRIs and CT scans. |
| Phones | Face unlock, photo tagging, background blur. |
| Self-driving cars | Detect lanes, signs, pedestrians and other vehicles. |
| Agriculture | Identify crop disease from leaf photos. |
Remember this: a CNN doesn't "see" a cat. It detects edges, which combine into shapes, which combine into features, which a final layer scores as "cat". Intelligence built from thousands of tiny pattern-detectors.
When I first ran that NumPy edge-detector and watched a column of 3s appear exactly where dark met bright, convolution finally clicked for me — it stopped being a scary word and became "a filter that lights up when it finds its pattern." I'd encourage you to change the image and the kernel numbers yourself; that five-minute experiment taught me more than any diagram.
Key takeaways
- Regular networks are too big and ignore image structure; CNNs fix both with small, reusable filters.
- Convolution slides a filter over the image to make a feature map; the filter values are learned.
- ReLU keeps strong signals, pooling shrinks the maps and adds position-tolerance.
- Stacked Conv + Pool blocks build features from edges → shapes → objects, then a dense layer decides.
- Real CNNs are written in Keras/PyTorch, but they do exactly what the hand-coded loop does — just faster and learned.
Continue the series: ← Lesson 16: How Neural Networks Learn · Next: Lesson 18 — Recurrent Neural Networks (RNNs) and LSTMs →
Frequently Asked Questions
What is a convolutional neural network (CNN)?
A CNN is a type of neural network designed for images. Instead of connecting every pixel to every neuron, it slides small learnable filters across the image to detect patterns like edges and shapes, making it far more efficient and accurate on visual data.
What is a filter or kernel in a CNN?
A filter (or kernel) is a tiny grid of numbers, often 3x3, that slides over the image. At each position it multiplies the overlapping pixels by its values and sums them to produce one number in a feature map. The network learns the best filter values during training.
What does pooling do in a CNN?
Pooling shrinks each feature map by summarising small blocks — max pooling keeps the largest value in every 2x2 block. This reduces computation and makes the network tolerant to small shifts, so it still recognises an object even if it moves a few pixels.
Do I need TensorFlow or PyTorch to learn CNNs?
Not to understand them. You can hand-code a convolution in a few lines of NumPy to see how filters work. For real projects you use Keras (TensorFlow) or PyTorch because they run on the GPU and handle training, but the underlying idea is identical.