Convolutions and vision
How does a network learn to see edges, shapes, and objects?
Lesson complete
If you like:
A one-hundred-by-one-hundred pixel image has ten thousand values. Connecting each one to a thousand neurons takes ten million weights, in the first layer alone.
No voice: the lesson advances on its own with subtitles.
The voice for this lesson is being recorded: for now it advances on its own with subtitles.
Review modeComment on this stepI finished the lesson
What you will achieve
- Explain what a convolution kernel does and compute by hand one output value of a three-by-three kernel.
- Understand why sharing weights makes it possible to process images with very few parameters.
- Describe how stacked layers go from edges to parts to objects, and what limits these networks.
Key ideas
- Connecting every pixel to every neuron does not scale: a small image already needs millions of weights.
- A kernel is a small grid of weights that sweeps across the image; at each position it multiplies, adds, and produces an output pixel.
- The same kernel is reused across the image (shared weights): few parameters and the same detector at any position.
- Kernel values are learned with backpropagation; a layer has many kernels, and each produces a feature map.
- Stacking layers builds a hierarchy from edges to parts to objects. ImageNet-trained networks tend to lean on textures, and vision networks can be fragile to subtle changes.
What many people believe
Common belief A vision network “sees” the image like a person: it recognizes objects and understands the scene.
What is true It detects statistical patterns in pixel numbers. It is very good at what it saw in training, but it tends to lean on textures and can be wrong with changes a person would not even notice.
Check yourself
Four questions. No grade, no account: they just show whether the idea stuck.
Under the hoodFor those who want the math and the code. You do not need it to follow the course.
The operation
Given an image and a kernel , the output (without padding) at position is
Technically this is cross-correlation; mathematical convolution flips the kernel, but in deep learning the kernel is learned and the distinction does not matter (Goodfellow et al., 2016, ch. 9). With an image, no padding (), and stride 1, the output is per side; with stride and padding (rows and columns of zeros around the border): (Dumoulin and Visin, 2016). Quiz example: with gives ; with the size stays .
Parameters: fully connected versus convolutional
A fully connected layer from inputs to neurons has weights (plus biases). A convolutional layer with kernels of over channels has
parameters, independent of the image size. With , and 16 kernels: .
The hook’s comparison (10 million versus 10) sets a single kernel against a thousand neurons, and one kernel produces a single map. With 1,000 kernels on the same image (no padding) there are parameters, a thousand times fewer than ; but the output is 1,000 maps of (9,604,000 values) and million multiplications, more than the dense layer’s 10 million. Sharing weights saves parameters (and the data needed to fit them), not operations: it does not change the order of the forward-pass running time, only the memory for the parameters (Goodfellow et al., 2016, §9.2).
Where the idea comes from
Fukushima’s neocognitron (1980) already combined layers of local feature detectors with layers tolerant to shifts, but it was trained with a self-organizing rule, without supervision: it lacked an end-to-end supervised algorithm such as backpropagation (LeCun, Bengio, and Hinton, 2015; Goodfellow et al., 2016, §9.10). LeCun et al. (1989) trained with backpropagation a network with shared weights to recognize handwritten zip code digits provided by the U.S. Postal Service: 9,298 already-segmented digits (7,291 for training, 2,007 for testing) in images. Its 64,660 connections were controlled by 9,760 independent parameters, and the error was 0.14% on the training set (10 mistakes) and 5.0% on the test set (102 mistakes); a fully connected network with 40 hidden units, tried as a comparison, reached 8.1% on the test set. That network did not use pooling, but convolutions that jumped two pixels at a time. LeNet-5 (LeCun et al., 1998) has 7 layers, a input, kernels, 340,908 connections, and about 60,000 free parameters; its “sub-sampling” averages each block (with a trainable coefficient and bias) instead of taking the maximum.
The lesson’s kernel
The lesson’s edge kernels are those of the Sobel operator (OpenCV documentation, “Sobel Derivatives”). The vertical-edge kernel is
Its magnitude estimates edge strength (same source). Here the kernels are chosen by hand to show the operation; in a trained network they are learned, and in the first layers they tend to resemble oriented-edge detectors and color blobs (Krizhevsky et al., 2012, fig. 3; Yosinski et al., 2014).
Receptive field and hierarchy
Each layer sees a larger region of the original image. With at the last layer, the receptive field satisfies (Araujo, Norris, and Sim, 2019): two stacked convolutions “see” ; with a pooling in between (, pooling, ) they see , and a unit of the lesson’s example network, after its two poolings, sees pixels. That is why early layers detect local patterns (edges, colors) and deep layers detect broad combinations.
In an AlexNet-type network, Zeiler and Fergus (2014) visualized what excites each layer: layer 2 responds to corners and edge/color conjunctions; layer 3, to textures and more complex patterns; layer 4, to class-specific parts (dog faces); layer 5, to whole objects with significant pose variation. LeCun, Bengio, and Hinton (2015) summarize it as edges, motifs, parts, and objects, and stress that these layers of features are not designed by human engineers: they are learned from data. What a person does design is the architecture (how many layers, what kernel size, where the pooling goes).
Pooling and shifts
max pooling with stride 2 halves the map per side and makes the output approximately invariant to small shifts (Goodfellow et al., 2016, §9.3; LeCun, Bengio, and Hinton, 2015). It is only approximate: in the scene, shifting the image by one pixel changes the edge map by 56% (sum of absolute differences over the sum of absolute values) and after pooling by 19%; with 400 random-stripe images, the mean change drops from 0.97 to 0.50 and in 50 of the 400 pooling does not reduce it (script verify-data.ts). Azulay and Weiss (2019) show that in full networks shifting or rescaling the image a little can change the prediction a lot. In the scene, pooling takes the maximum of the absolute value; in a real network the activation (for example ReLU) comes first.
The limits, with numbers
- Textures. Geirhos et al. (2019) showed ImageNet-trained networks images with the shape of one object and the texture of another. When forced to choose, ResNet-50 answered by texture 77.9% of the time (VGG-16, 82.8%; GoogLeNet, 68.8%; AlexNet, 57.1%); people chose shape in 95.9% of their correct decisions. With the same architecture, training on a “stylized” version of ImageNet changed that bias: it depends on the data and the task, not only on convolution.
- Nearly invisible changes. Szegedy et al. (2014) found hardly perceptible perturbations that change the class a network predicts. Goodfellow, Shlens, and Szegedy (2015) took GoogLeNet’s answer on a photo of a panda from “panda” (57.7% confidence) to “gibbon” (99.3%) by adding a perturbation of magnitude 0.007, the smallest bit of an 8-bit image.
- Data unlike the training data. Recht et al. (2019) rebuilt ImageNet test sets with the same procedure and model accuracy dropped by 11% to 14%. Beery et al. (2018): with wildlife camera traps, good performance where it was trained and poor generalization to new places. Hendrycks and Dietterich (2019): ImageNet-C applies 75 alterations (15 types at 5 intensities, including brightness, contrast, fog, and noise), and relative robustness barely improved from AlexNet to ResNet.
- Scenes. Goodfellow et al. (2016, §9.10) note that the human visual system does much more than recognize objects (it understands entire scenes, relationships between objects, and 3D geometry) and that applications of convolutional networks to those problems were then in their infancy.
Reusing an already trained model
Yosinski et al. (2014) measured what can be reused: first-layer features (such as Gabor filters and color blobs) do not depend on the task or the data, while last-layer features do. He, Girshick, and Dollár (2018, technical report) compared, in object detection on COCO, starting from a model pre-trained on ImageNet versus training from scratch: pre-training speeds up convergence, but with enough iterations training from scratch matched the accuracy, even with 10% of the data; only with very little data (fewer than about 10,000 COCO images) did pre-training clearly improve the result.
In code
import numpy as np
def conv2d(img, K, b=0.0):
k = K.shape[0]
h, w = img.shape[0] - k + 1, img.shape[1] - k + 1
out = np.zeros((h, w))
for i in range(h):
for j in range(w):
out[i, j] = b + np.sum(K * img[i:i + k, j:j + k])
return out
Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
img = np.zeros((6, 6)); img[:, 3:] = 1 # half dark, half bright: a vertical edge
print(conv2d(img, Kx)) # values 4 in the edge columns, 0 elsewhere
What comes next
Backpropagation from lesson 8 trains the kernels like any other weight. Convolutional networks came to dominate vision from 2012 on (lesson 13); LeCun, Bengio, and Hinton (2015) describe them as the dominant approach for almost all recognition and detection tasks. For sequential data such as language, another idea is needed (lesson 12).
Decision lens
What this idea means if your job is to decide, not to code.
- A vision system performs well on images similar to its training data: ask for it to be tested with your cameras, your lighting, and your real cases.
- If the vendor talks about “99% accuracy,” ask on which set of images and with what variety: performance drops when conditions change.
- These networks can fail in ways a person does not understand (tiny changes, backgrounds, textures). For critical decisions, design human review and adversarial test cases.
- Starting from an already trained vision model usually speeds up training and helps most when you have little data of your own; with lots of data, training from scratch can match it. Ask whether they use a pretrained one and how they adapted it.
Terms in this lesson
- Pixel The smallest dot of a digital image. In grayscale it is a number from 0 (black) to 255 (white); in color it is three numbers (red, green, and blue).
- Convolution An operation in which a small kernel sweeps an image and, at each position, multiplies and sums pixels by weights to produce an output pixel.
- Kernel (filter) The small grid of weights of a convolution. In a convolutional network, its values are learned during training.
- Feature map The output image of a kernel: it shows where the pattern that kernel detects appears.
- Pooling A size reduction that summarizes each block of a map (for example, with its maximum value).
- Convolutional network (CNN) A neural network that uses convolutions, activations, and pooling to process grid-structured data such as images.
- Overfitting When a model learns the noise in its training data and therefore fails on new data. It shows as a large gap between training and validation error.
Full transcript
Sources
- LeCun, Y., Boser, B., Denker, J. S., Henderson, D., Howard, R. E., Hubbard, W., Jackel, L. D. (1989). Backpropagation Applied to Handwritten Zip Code Recognition (Neural Computation 1(4), 541–551)
- LeCun, Y., Bottou, L., Bengio, Y., Haffner, P. (1998). Gradient-Based Learning Applied to Document Recognition (Proceedings of the IEEE 86(11), 2278–2324; sección II: LeNet-5)
- Fukushima, K. (1980). Neocognitron: A self-organizing neural network model for a mechanism of pattern recognition unaffected by shift in position (Biological Cybernetics 36(4), 193–202)
- LeCun, Y., Bengio, Y., Hinton, G. (2015). Deep learning (Nature 521, 436–444; sección «Convolutional neural networks»)
- Goodfellow, I., Bengio, Y., Courville, A. (2016). Deep Learning, cap. 9: Convolutional Networks (§9.2 parámetros compartidos, §9.3 pooling, §9.10 base neurocientífica, §9.11 historia)
- Krizhevsky, A., Sutskever, I., Hinton, G. E. (2012). ImageNet Classification with Deep Convolutional Neural Networks (fig. 3 y §6.1: los kernels aprendidos de la primera capa)
- Yosinski, J., Clune, J., Bengio, Y., Lipson, H. (2014). How transferable are features in deep neural networks?
- Zeiler, M. D., Fergus, R. (2014). Visualizing and Understanding Convolutional Networks (ECCV 2014)
- Dumoulin, V., Visin, F. (2016). A guide to convolution arithmetic for deep learning
- Araujo, A., Norris, W., Sim, J. (2019). Computing Receptive Fields of Convolutional Neural Networks (Distill 4(11))
- OpenCV (autora original del tutorial: Huamán, A.) (2026). Sobel Derivatives (tutorial de la documentación de OpenCV 4.x, consultada en 2026: kernels de Sobel y magnitud del gradiente)
- Azulay, A., Weiss, Y. (2019). Why do deep convolutional networks generalize so poorly to small image transformations? (JMLR 20(184), 2019)
- Geirhos, R., Rubisch, P., Michaelis, C., Bethge, M., Wichmann, F. A., Brendel, W. (2019). ImageNet-trained CNNs are biased towards texture; increasing shape bias improves accuracy and robustness
- Szegedy, C., Zaremba, W., Sutskever, I., Bruna, J., Erhan, D., Goodfellow, I., Fergus, R. (2014). Intriguing properties of neural networks
- Goodfellow, I. J., Shlens, J., Szegedy, C. (2015). Explaining and Harnessing Adversarial Examples
- Recht, B., Roelofs, R., Schmidt, L., Shankar, V. (2019). Do ImageNet Classifiers Generalize to ImageNet?
- Beery, S., Van Horn, G., Perona, P. (2018). Recognition in Terra Incognita (ECCV 2018)
- Hendrycks, D., Dietterich, T. (2019). Benchmarking Neural Network Robustness to Common Corruptions and Perturbations (ICLR 2019)
- He, K., Girshick, R., Dollár, P. (2018). Rethinking ImageNet Pre-training (informe técnico en arXiv)
CommentsThis lesson’s public conversation: questions, facts to check, ideas.
Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub