Beginner level · Module 2: Convolutional Networks for Images · Lesson 1 of 3
Images and Convolutions
See images as grids of numbers and understand how a convolution filter detects patterns such as edges.
In this lesson you will
- Represent images as tensors of pixels and channels
- Calculate a convolution by hand and explain filters
Key concepts
pixel and channeltensor shapeconvolution filterfeature map
The lesson
A digital image is a grid of pixels. A greyscale image has one number per pixel; a colour image has three channels (red, green, blue). In PyTorch an image batch has the shape batch × channels × height × width.
A convolution slides a small filter, such as 3 × 3, across the image, multiplying and adding at each position. The result, a feature map, is large where the image matches the filter's pattern — an edge, a corner, a texture.
In a CNN the filter values are not designed by hand; they are learned during training.
Step by step
- Load an image and print its tensor shape.
- Apply an edge filter by hand to a 3 × 3 patch.
- Apply the filter to a whole image with PyTorch.
- Visualise the resulting feature map.
How a convolution filter finds an edge
The same 3 × 3 vertical-edge filter applied to three image patches gives very different responses.
| Item | Working | Amount |
|---|---|---|
| Bright left, dark right: rows [9, 9, 1] | 3 × (9×1 + 9×0 + 1×(−1)) | 24 |
| Flat patch: rows [5, 5, 5] | 3 × (5×1 + 5×0 + 5×(−1)) | 0 |
| Dark left, bright right: rows [1, 9, 9] | 3 × (1×1 + 9×0 + 9×(−1)) | −24 |
Explanation
At each position, the filter multiplies the nine pixels by its nine weights and adds the results. Because the left column has +1 and the right column −1, the output measures how much brighter the left side is than the right.
A strong positive or negative value means a vertical edge; zero means no edge. A CNN learns many such filters from data — edges first, then textures and shapes in deeper layers.
Check your understanding
Each patch has three identical rows, so each total is three times the result for one row.
Practice activity
Calculate three convolution outputs by hand and check them with PyTorch.
Expected result: Correct hand calculations matching the code.
Check your understanding
-
How many channels does a colour image have?
Show answer
Three (red, green, blue).
-
What does a feature map show?
Show answer
Where in the image the filter's pattern appears.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.