Skip to main content
Open In Colab This section was written by Kaushik Kachireddy (pull request #82), with help from an AI coding agent (Claude Code) on the code. It reproduces the ideas of Chapter 30 of Foundations of Computer Vision by Antonio Torralba, Phillip Isola, and William T. Freeman. Representation learning maps raw pixels to a compact embedding that exposes the factors behind an image, such as the shape and the color of an object. This section rebuilds the book’s demonstrations on its toy dataset of colored shapes:
  1. The dataset (Figure 30.4): three shapes in eight colors, with random size, position and rotation.
  2. Autoencoders (Figure 30.5): an encoder, a bottleneck and a decoder. You inspect the embedding through reconstructions, nearest neighbors and a probe of each layer.
  3. K-means (Figure 30.11): clustering as a discrete representation.
  4. Contrastive learning (Figure 30.14): the InfoNCE loss, where the choice of data augmentation decides whether the embedding encodes color or shape.
The book generates the colored shapes from rules, and this section generates them with the same rules instead of loading a file. Contrastive learning is also the idea behind CLIP, the next section, which applies it to pairs of images and captions.

The colored-shapes dataset

Each image shows one of three shapes (circle, square or triangle) in one of eight colors, with random size, position and rotation. Shape and color vary independently of each other, and a good representation keeps the two apart. Output from cell 2

Autoencoders

An autoencoder passes each image through a low-dimensional bottleneck and back, and is trained to reconstruct its input by minimizing Ex ∥g(f(x))−x∥2\mathbb{E}_x\,\lVert g(f(x))-x\rVert^2 over the encoder ff and the decoder gg. The bottleneck f(x)f(x) has to keep what the decoder needs to redraw the image, so it becomes a compact representation of the image. You train a small convolutional autoencoder and then look at what its embedding captures.

Reconstructions and nearest neighbors

The reconstructions show that the bottleneck keeps the shape, the color and the rough pose of each object. The nearest neighbors of an image in the embedding space usually share both its shape and its color, so the embedding has organized the data by the two factors that generated it.
Output from cell 5

What each layer encodes

A 1-nearest-neighbor probe measures what a layer keeps. It labels each test image with the label of the closest training image in that layer’s features, once for shape and once for color. Shape accuracy rises with depth, from 78% on the raw pixels to 98% after the sixth convolution, while color accuracy falls from 79% to 65%. The deeper layers keep the more abstract factor and discard low-level appearance, which is the crossing the book reports in Figure 30.5b. The crossing appears only with the book’s architecture, six convolutional layers that keep some spatial detail, and a long training run. A shallower model, or one that downsamples each image to a single vector, does not show it.
Output from cell 7

K-means clustering

Clustering gives a discrete representation: each point is summarized by the index of its nearest code vector. K-means minimizes ∑i∥zai−x(i)∥2\sum_i \lVert z_{a_i} - x^{(i)}\rVert^2 by block coordinate descent. It alternates two steps: it assigns each point to its nearest mean, then moves each mean to the average of the points assigned to it. The figure shows the first iterations on a two-dimensional dataset with k=5k=5 (Figure 30.11).
Output from cell 9

Contrastive learning and the choice of augmentation

Contrastive learning pulls together the embeddings of two augmented views of the same image, a positive pair, and pushes apart the embeddings of different images. The InfoNCE loss does this: L=−log⁡ef(x)⊤f(x+)/τef(x)⊤f(x+)/τ+∑ief(x)⊤f(xi−)/τ,\mathcal{L} = -\log \frac{e^{f(x)^\top f(x^+)/\tau}}{e^{f(x)^\top f(x^+)/\tau} + \sum_i e^{f(x)^\top f(x_i^-)/\tau}}, where x+x^+ is the other view of xx, the xi−x_i^- are views of other images in the batch, and τ\tau is a temperature. The embedding becomes invariant to whatever the augmentation changes, so the choice of augmentation decides which factor survives. Here the embedding has two dimensions (M=2M=2) and lies on the unit circle, so you can plot it directly:
  • Cropping alone (TcT_c) changes which part of the shape is visible but keeps the color, and the embedding groups the images by color.
  • Color jitter (TsT_s) destroys the color but keeps the shape, and the embedding groups the images by shape.
Output from cell 11 Output from cell 11

Summary

A representation describes an image by the factors that produced it, here shape and color, and each method in this section exposes those factors differently. The autoencoder learns an embedding without labels. Its nearest neighbors share shape and color, and its deeper layers trade color for shape. K-means describes each point by a cluster index, which is a discrete representation. Contrastive learning makes the choice explicit: the augmentation you pick sets the invariance you get, so cropping keeps color and color jitter keeps shape. Modern self-supervised encoders depend on the same choice, and so does CLIP, where the two views of a pair are an image and its caption.
Found an error on this page? Report an issue.