Skip to content
BIASED

Self-supervised learning

Pretext tasks, contrastive learning (SimCLR, NT-Xent), MoCo and masked image modelling; the linear probe measures what the learned representations are worth.

From labels to a pretext

The topic Semantic segmentation closed the convolutional module: every network there learned from labels — a class per image, a box, or a class per pixel. Labels are the expensive part of a dataset; pixel masks are the most expensive of all. Unlabelled photographs cost almost nothing.

Self-supervised learning trains a network on images alone. The training target is computed from the image itself. Afterward the features are reused on the task that has labels. A supervised ImageNet backbone works, but it learns only what the thousand class names ask for. The question of this topic is whether images alone can train a backbone whose features are as useful.

An encoder ff maps an image xx to a representation h=f(x)∈Rdh = f(x) \in \mathbb R^d. A pretext task is a prediction problem solved only as a means to good hh. What hh is worth is measured later, on a labelled protocol.

Pretext tasks and shortcuts

The first pretext tasks transform the image in a known way and ask which transformation was applied.

Rotation. Each image is rotated by 0∘0^\circ, 90∘90^\circ, 180∘180^\circ or 270∘270^\circ, and the network classifies the angle. To tell which way is up it must find the object and its parts.

Jigsaw. The image is cut into a 3×33 \times 3 grid, the tiles are shuffled by one of a fixed set of permutations, and the network classifies which permutation it was. Of 9!=362 8809! = 362\,880 orders the authors keep a subset of 10001000 that differ in many positions.

Both invite shortcuts: low-level cues that solve the pretext without understanding content. Jigsaw tiles can be ordered by matching edges or by colour fringes that lenses add near the borders. Authors cut tiles with random gaps and jitter colour channels for that reason. A shortcut solves the pretext and teaches nothing. Pretext features came close to supervised pretraining on some transfer tasks, but stayed well below it under a linear probe on ImageNet, and each task had to be designed by hand.

SimCLR and NT-Xent

Contrastive learning compares images instead of predicting a property of one. Two random augmentations of the same image are a positive pair; views of different images are negatives. Train so that a view is closer to its partner than to every other view in the batch. Features must keep what survives the augmentations — the content — and may discard what the augmentations change.

SimCLR is the simplest complete form. For every image of a batch it draws two augmentations t(x)t(x), t′(x)t'(x), runs both through a shared encoder ff to get hh, h′h', then through a small projection head gg to get zz, z′z'. After training, gg is thrown away and hh is reused.

With NN images the batch has 2N2N views. Let zˉ=z/∥z∥\bar z = z / \|z\| and sik=zˉi⊤zˉks_{ik} = \bar z_i^\top \bar z_k. The NT-Xent loss for view ii, with partner index p(i)p(i) and temperature τ>0\tau > 0, is

ℓi=−log⁡exp⁡(si,p(i)/τ)∑k≠iexp⁡(sik/τ),L=12N∑i=12Nℓi.\ell_i = -\log \frac{\exp(s_{i,p(i)} / \tau)}{\sum_{k \ne i} \exp(s_{ik} / \tau)}, \qquad L = \frac{1}{2N}\sum_{i=1}^{2N} \ell_i.

Read it as classification: the logits are the similarities to the other 2N−12N - 1 views, divided by τ\tau; the class to predict is the partner. The gradient pulls the positive with force (1−qi,p(i))/τ(1 - q_{i,p(i)})/\tau and pushes each negative with force qik/τq_{ik}/\tau — hard negatives that look similar get most of the push.

The head is discarded because the loss makes zz invariant to the augmentations, and that invariance destroys colour or orientation a downstream task may need. With a nonlinear head, a linear classifier on hh was more than ten points better than one on zz on ImageNet.

Temperature and batch size

The temperature sets how sharply the softmax distinguishes candidates. The ratio of gradients on two negatives with similarities ss and s′s' is

qikqik′=exp⁡ ⁣(s−s′τ).\frac{q_{ik}}{q_{ik'}} = \exp\!\Big(\frac{s - s'}{\tau}\Big).

A small τ\tau makes the hardest negatives dominate; a large τ\tau spreads the gradient evenly. Neither extreme is good. With ResNet-50 and batches of 40964096, SimCLR’s linear-probe accuracy peaked near τ=0.1\tau = 0.1.

Batch size sets the number of negatives. If all similarities were equal, every qikq_{ik} would be 1/(2N−1)1/(2N - 1) and the loss would be log⁡(2N−1)\log(2N - 1) — 6.2366.236 for N=256N = 256. InfoNCE with KK candidates also bounds mutual information by I≥log⁡K−ℓI \ge \log K - \ell, so the ceiling cannot exceed log⁡K\log K. Larger batches helped most when training was short; a batch of 40964096 needs many accelerators.

Augmentations define what the features ignore. SimCLR’s decisive composition is random crop plus colour distortion: crops alone share a colour histogram, a shortcut that distortion removes. Contrastive learning wants stronger colour distortion than supervised training.

MoCo and learning without negatives

SimCLR draws negatives from the current batch, so more negatives need a bigger batch. MoCo keeps a queue of encoded keys from recent batches — K=65 536K = 65\,536 by default — and compares the query with its own key and the queue by the same InfoNCE loss, using ordinary batches of 256256.

Keys in the queue must stay comparable. MoCo encodes them with a separate encoder whose weights follow the query as a slow moving average,

θk←m θk+(1−m) θq,m=0.999,\theta_k \leftarrow m\,\theta_k + (1 - m)\,\theta_q, \qquad m = 0.999,

and only θq\theta_q is trained by backpropagation. With m=0.999m = 0.999 the key encoder averages roughly the last 10001000 versions of the query.

Negatives prevent collapse: without them, “views of the same image should agree” is minimized by a constant encoder. BYOL drops negatives anyway and avoids collapse with an online predictor and a momentum target. SimSiam shows that even the momentum is unnecessary if one side has a predictor and the other a stop-gradient; removing the stop-gradient alone collapses accuracy to chance.

MAE and the linear probe

Masked image modelling returns to prediction of the image itself. The masked autoencoder (MAE) cuts an image into patches — using the Vision Transformer of the next topic — hides a random 75%75\% of them, encodes only the visible 25%25\%, and asks a small decoder to reconstruct the hidden pixels. The loss is MSE on the hidden patches only.

Images are redundant, so a small hole can be filled from neighbours; 75%75\% forces a model of objects and scenes, and the encoder processes a quarter of the patches, so pretraining is several times faster. MAE features excel after fine-tuning but score lower under a linear probe than contrastive features of the same network: one number does not rank methods.

The standard protocol is the linear probe: freeze the encoder, train only a linear classifier on hh. Fine-tuning, few-label probes and transfer to detection or segmentation complement it. A random untrained encoder and raw pixels are the floor; a probe that beats both shows that pretraining, not architecture alone, put the information there.

Digits, and what comes next

On the bundled 8×88 \times 8 digits, SimCLR trained without labels on 10781078 training images, then a logistic regression on frozen features. With five labels per class the probe reached 0.9420.942 on SimCLR’s hh, against 0.8250.825 on raw pixels and 0.8060.806 on a random encoder. With all labels the gain shrank to about two points. Mild augmentations of tiny images left the head little to discard, so hh and zz were nearly tied — unlike the double-digit gap on ImageNet.

Self-supervised learning replaced hand-designed pretext tasks with contrastive and masked objectives. What it leaves open is the encoder that MAE needs: one that sees an image as a set of patches and can drop three quarters of them. A convolutional grid cannot do that. The next topic builds that encoder — the Vision Transformer.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.