Learning Objectives
- Trace the three major self-supervised families — contrastive (SimCLR), teacher-student (DINO), masked reconstruction (MAE) — and state what each one optimises
- Implement an InfoNCE loss from scratch and explain why a batch of 512 works but a batch of 32 fails
- Explain why MAE's 75% masking ratio is not arbitrary and how it differs from BERT's 15% for text
- Use DINOv2 or MAE ImageNet checkpoints for linear probing and zero-shot retrieval
The Problem
Supervised ImageNet has 1.3M labelled images, which cost an estimated $10M to annotate. Medical and industrial datasets are smaller and even more expensive to label. Every vision team asks: can we pretrain on cheap unlabelled data — YouTube frames, web crawls, webcam footage, satellite sweeps — and then fine-tune on a small labelled set?
Self-supervised learning is the answer. A modern self-supervised ViT trained on LAION or JFT reaches or beats supervised ImageNet accuracy when fine-tuned. It also transfers better to downstream tasks (detection, segmentation, depth) than supervised pretraining. DINOv2 (Meta, 2023) and MAE (Meta, 2022) are the current production defaults for transferable vision features.
The conceptual shift is that the pretext task — the thing the model is trained to do — does not have to be the downstream task. What matters is that it forces the model to learn useful features. Predict the colour of grayscale images, rotate images and ask the model to classify the rotation, mask patches and reconstruct them — all have worked. The three approaches that scale are contrastive learning, teacher-student distillation, and masked reconstruction.
The Concept
Three families
flowchart LR
A["Contrastive<br/>SimCLR, MoCo, CLIP"] --> AT["positive pairs<br/>(same image, 2 augs)<br/>pulled together,<br/>negatives pushed apart"]
B["Teacher-student<br/>DINO, BYOL, iBOT"] --> BT["student predicts<br/>teacher's output;<br/>teacher is EMA of student"]
C["Masked reconstruction<br/>MAE, BEiT, SimMIM"] --> CT["mask 75% of patches;<br/>reconstruct pixel or<br/>token targets"]
style A fill:#dbeafe,stroke:#2563eb
style B fill:#fef3c7,stroke:#d97706
style C fill:#dcfce7,stroke:#16a34a
Contrastive learning (SimCLR)
Take one image, apply two random augmentations, get two views. Feed both through the same encoder plus a projection head. Minimise a loss that says "these two embeddings should be close" and "this embedding should be far from every other image's embeddings in the batch."
Loss for positive pair (z_i, z_j) among 2N views per batch:
L_ij = -log( exp(sim(z_i, z_j) / tau) / sum_k in batch \ {i} exp(sim(z_i, z_k) / tau) )
sim = cosine similarity
tau = temperature (0.1 standard)
This is the InfoNCE loss. It requires many negatives per positive, so batch size matters — SimCLR needs 512-8192. MoCo introduced a momentum queue of past batches to decouple negative count from batch size.
Teacher-student (DINO)
Two networks with the same architecture: student and teacher. The teacher is an exponential moving average (EMA) of the student's weights. Both see augmented views of the image. The student's output is trained to match the teacher's — no explicit negatives.
loss = CE( student_output(view_1), teacher_output(view_2) )
+ CE( student_output(view_2), teacher_output(view_1) )
teacher_weights = m * teacher_weights + (1 - m) * student_weights (m ≈ 0.996)
Why it does not collapse to "predict a constant": the teacher's output is centred (subtract per-dimension mean) and sharpened (divide by small temperature). Centering prevents one dimension from dominating; sharpening prevents output collapse to uniform.
DINO is what DINOv2 scales up, on 142M curated images. The resulting features are the current SOTA for zero-shot visual retrieval and dense prediction.
Masked reconstruction (MAE)
Mask 75% of patches of a ViT input. Pass only the visible 25% through the encoder. A small decoder receives the encoder's output plus mask tokens at masked positions, and is trained to reconstruct the pixels of the masked patches.
Encoder: visible 25% of patches -> features
Decoder: features + mask tokens at masked positions -> reconstructed pixels
Loss: MSE between reconstructed and original pixels on masked patches only
Key design choices that make MAE work:
- 75% mask ratio — high. Forces the encoder to learn semantic features; reconstructing 25% would be near-trivial (neighbouring pixels are so correlated that a CNN could nail it).
- Asymmetric encoder/decoder — the big ViT encoder only sees visible patches; a small decoder (8-layer, 512-dim) handles reconstruction. 3x faster pretraining than naive BEiT.
- Pixel-space reconstruction target — simpler than BEiT's tokenised target and works better on ViT.
After pretraining, discard the decoder. The encoder is the feature extractor.
Why 75% and not 15%
BERT masks 15% of tokens. MAE masks 75%. The difference is information density.
- Natural language has high entropy per token. Predicting 15% of tokens is still hard because each masked position has many plausible completions.
- Image patches have low entropy — an unmasked neighbourhood often determines the masked patch's pixels almost exactly. To make prediction require semantic understanding, you have to mask aggressively.
75% is high enough that simple spatial extrapolation cannot solve the task; the encoder must represent the image content.
Linear-probe evaluation
After self-supervised pretraining, the standard evaluation is a linear probe: freeze the encoder, train a single linear classifier on top on ImageNet labels. Reports top-1 accuracy.
- SimCLR ResNet-50: ~71% (2020)
- DINO ViT-S/16: ~77% (2021)
- MAE ViT-L/16: ~76% (2022)
- DINOv2 ViT-g/14: ~86% (2023)
Linear probe is a pure measure of feature quality; fine-tuning typically adds 2-5 points but also mixes in the effect of head retraining.
Interactive figure coming later.
Build It
Step 1: Two-view augmentation pipeline
import torch
import torchvision.transforms as T
two_view_train = lambda: T.Compose([
T.RandomResizedCrop(96, scale=(0.2, 1.0)),
T.RandomHorizontalFlip(),
T.ColorJitter(0.4, 0.4, 0.4, 0.1),
T.RandomGrayscale(p=0.2),
T.ToTensor(),
])
class TwoViewDataset(torch.utils.data.Dataset):
def __init__(self, base):
self.base = base
self.aug = two_view_train()
def __len__(self):
return len(self.base)
def __getitem__(self, i):
img, _ = self.base[i]
v1 = self.aug(img)
v2 = self.aug(img)
return v1, v2
Each getitem returns two augmented views of the same image; labels are not needed.
Step 2: InfoNCE loss
import torch.nn.functional as F
def info_nce(z1, z2, tau=0.1):
"""
z1, z2: (N, D) L2-normalised embeddings of paired views
"""
N, D = z1.shape
z = torch.cat([z1, z2], dim=0) # (2N, D)
sim = z @ z.T / tau # (2N, 2N)
mask = torch.eye(2 * N, dtype=torch.bool, device=z.device)
sim = sim.masked_fill(mask, float("-inf"))
targets = torch.cat([torch.arange(N, 2 * N), torch.arange(0, N)]).to(z.device)
return F.cross_entropy(sim, targets)
L2-normalise embeddings before calling. tau=0.1 is the SimCLR default; lower makes the loss sharper and requires more negatives.
Step 3: Sanity check InfoNCE
z1 = F.normalize(torch.randn(16, 32), dim=-1)
z2 = z1.clone()
loss_same = info_nce(z1, z2, tau=0.1).item()
z2_random = F.normalize(torch.randn(16, 32), dim=-1)
loss_random = info_nce(z1, z2_random, tau=0.1).item()
print(f"InfoNCE with identical pairs: {loss_same:.3f}")
print(f"InfoNCE with random pairs: {loss_random:.3f}")
Identical pairs should give a low loss (close to 0 for a large batch and cold temperature). Random pairs should give log(2N-1) = ~log(31) = ~3.4 with a 16-pair batch.
Step 4: MAE-style masking
def random_mask_indices(num_patches, mask_ratio=0.75, seed=0):
g = torch.Generator().manual_seed(seed)
n_keep = int(num_patches * (1 - mask_ratio))
perm = torch.randperm(num_patches, generator=g)
visible = perm[:n_keep]
masked = perm[n_keep:]
return visible.sort().values, masked.sort().values
num_patches = 196
visible, masked = random_mask_indices(num_patches, mask_ratio=0.75)
print(f"visible: {len(visible)} / {num_patches}")
print(f"masked: {len(masked)} / {num_patches}")
Simple, fast, and deterministic for a given seed. Real MAE implementations batch this and keep per-sample masks.
Use It
DINOv2 is the production standard in 2026:
import torch
from transformers import AutoImageProcessor, AutoModel
processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
model = AutoModel.from_pretrained("facebook/dinov2-base")
model.eval()
# Per-image embeddings for zero-shot retrieval
with torch.no_grad():
inputs = processor(images=[pil_image], return_tensors="pt")
outputs = model(**inputs)
embedding = outputs.last_hidden_state[:, 0] # CLS token
The resulting 768-dim embedding is the backbone of modern image retrieval, dense correspondence, and zero-shot transfer pipelines. Fine-tuning on a downstream task rarely needs more than a linear head.
For image-text embeddings, SigLIP or OpenCLIP is the equivalent; for MAE-style fine-tuning, the timm repo ships every MAE checkpoint.
Ship It
This lesson produces:
outputs/prompt-ssl-pretraining-picker.md— a prompt that picks SimCLR / MAE / DINOv2 given dataset size, compute, and downstream task.outputs/skill-linear-probe-runner.md— a skill that writes the linear-probe evaluation for any frozen encoder + labelled dataset.
Exercises
- (Easy) Verify that InfoNCE loss drops when you decrease temperature for well-aligned embeddings and rises when you decrease temperature for random embeddings. Produce a plot
tau in [0.05, 0.1, 0.2, 0.5]vs loss. - (Medium) Implement a DINO-style centre buffer. Show that without the centring, the student collapses to a constant vector within a few epochs.
- (Hard) Train MAE on CIFAR-100 using the TinyUNet from Lesson 10 as the backbone. Report linear-probe accuracy at 10, 50, and 200 epochs. Show that a MAE-pretrained linear probe beats a from-scratch supervised linear probe on the same 1,000-image subset.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Self-supervised | "Label-free" | A pretext task that produces useful representations from unlabelled data |
| Pretext task | "The fake task" | The objective used during SSL (reconstruct patches, match views); discarded after pretraining |
| Linear probe | "Frozen encoder + linear head" | Standard SSL evaluation: train only a linear classifier on top of frozen features |
| InfoNCE | "Contrastive loss" | softmax over cosine similarities; positive pair is the target class, all others are negatives |
| EMA teacher | "Moving-average teacher" | Teacher whose weights are an exponential moving average of the student's; used by BYOL, MoCo, DINO |
| Mask ratio | "% of patches hidden" | Fraction of patches masked during MAE; 75% for vision, 15% for text |
| Representation collapse | "Constant output" | SSL failure where the encoder outputs a constant vector for all inputs; prevented by centring, sharpening, or negatives |
| DINOv2 | "Production SSL backbone" | Meta's 2023 self-supervised ViT; strongest general-purpose image features in 2026 |
Further Reading
- SimCLR (Chen et al., 2020) — contrastive learning reference
- DINO (Caron et al., 2021) — teacher-student with momentum, centring, sharpening
- MAE (He et al., 2022) — masked autoencoder pretraining for ViT
- DINOv2 (Oquab et al., 2023) — scaling self-supervised ViT to production features
Code
Take-away files
prompt-ssl-pretraining-picker.md
--- name: prompt-ssl-pretraining-picker description: Pick SimCLR / MAE / DINOv2 given dataset size, compute, and downstream task phase: 4 lesson: 17 --- You are a self-supervised pretraining selector. ## Inputs - `unlabelled_images`: how many available - `backbone`: ResNet | ViT - `downstream_task`: classification | detection | segmentation | retrieval - `compute_gpu_hours`: approximate training budget ## Precedence Evaluate rules top-down; first match wins. Earlier rules short-circuit later ones. All numeric boundaries are non-overlapping: a rule that says `< 1,000,000` never fires for the exact value 1,000,000 — that goes to the next band. ## Decision 1. `compute_gpu_hours < 200` -> **do not run SSL from scratch**. No SSL recipe converges in that budget. Emit `method: none, use_pretrained: DINOv2, reason: compute_budget_too_small`. 2. `unlabelled_images < 100,000` -> **do not run SSL**. A pretrained checkpoint dominates anything you can train here. Emit `method: none, use_pretrained: DINOv2`. 3. `downstream_task == retrieval` -> **DINOv2**. Linear separability of DINOv2 features is the strongest across backbones; this rule overrides every backbone rule that follows. 4. `downstream_task in [detection, segmentation]` and `backbone == ViT` -> **MAE**. Dense reconstruction targets align with dense prediction. This rule overrides rule 6. 5. `downstream_task in [detection, segmentation]` and `backbone == ResNet` -> **DenseCL** (contrastive with dense projection head) or **PixPro**; if neither is available in your stack, fall back to **MoCo v3** and document the mismatch. 6. `backbone == ResNet` (remaining classification cases) -> **MoCo v3**. 7. `backbone == ViT` and `unlabelled_images >= 100,000,000` and `compute_gpu_hours >= 5,000` -> **DINOv2-style**. Downgrade to MAE if compute falls below 5,000 GPU hours. 8. `backbone == ViT` and `1,000,000 <= unlabelled_images < 100,000,000` and `compute_gpu_hours >= 1,000` -> **MAE**. 9. `backbone == ViT` and `100,000 <= unlabelled_images < 1,000,000` -> **use a pretrained DINOv2 checkpoint**; do not re-pretrain from scratch. Emit `method: none, use_pretrained: DINOv2`. ## Output ``` [pretraining] method: SimCLR | MoCo v3 | DINO | DINOv2 | MAE | DenseCL | PixPro | none use_pretrained: <checkpoint name if method == none> epochs: <int if method != none> batch: <int> aug: <list> eval: linear_probe | kNN | fine-tune [warnings] - <compute headroom> - <batch size floor for contrastive methods> - <downstream mismatch when a fallback was selected> ``` ## Rules - Never recommend SimCLR with batch size < 1024; at smaller batches, MoCo's queue structure trains faster and lands at similar quality. - When `compute_gpu_hours` is provided, always include a one-line sanity check against the picked method's known GPU-hour ranges; flag insufficient budget explicitly. - Do not mix "emit a method" and "use pretrained" in the same row. If rule 1, 2, or 9 fires, the method is `none` and the pretrained checkpoint is the output. - If a fallback path in rule 5 was taken (ResNet + dense task), note the theoretical mismatch so the reader knows why a dense-specific variant would have been preferable.
skill-linear-probe-runner.md
---
name: skill-linear-probe-runner
description: Write the complete linear-probe evaluation for any frozen encoder and labelled dataset
version: 1.0.0
phase: 4
lesson: 17
tags: [self-supervised, evaluation, linear-probe, pytorch]
---
# Linear Probe Runner
Evaluate a frozen encoder's features by training a single linear classifier on top. The standard evaluation for every self-supervised paper.
## When to use
- Comparing self-supervised checkpoints.
- Tracking feature quality over pretraining epochs.
- Deciding whether a pretrained encoder is good enough for a downstream task without fine-tuning.
## Inputs
- `encoder`: frozen `nn.Module` returning a fixed-dim feature per image.
- `feature_dim`: dimensionality of the encoder output.
- `train_dataset`: labelled dataset (image, class_id).
- `val_dataset`: held-out set.
- `num_classes`: task classes.
- `epochs`: typically 100 for ImageNet-scale, 50 for smaller datasets.
## Steps
1. Set encoder to eval mode and `requires_grad=False` on every parameter.
2. Feature-extract both train and val sets once. Store as numpy arrays or a memory-mapped file.
3. Train a `nn.Linear(feature_dim, num_classes)` on the cached features with SGD + cosine schedule.
4. Standard hyperparameters: `lr=0.1`, `momentum=0.9`, `weight_decay=0`, `batch_size=1024`. Linear probe is surprisingly sensitive to `lr` — sweep if accuracy is poor.
5. Report top-1 accuracy on val at the end of training.
## Output template
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.optim import SGD
from torch.optim.lr_scheduler import CosineAnnealingLR
def extract(encoder, loader, device="cpu"):
encoder.eval()
feats, labels = [], []
with torch.no_grad():
for x, y in loader:
f = encoder(x.to(device)).cpu()
feats.append(f)
labels.append(y)
return torch.cat(feats), torch.cat(labels)
def linear_probe(encoder, feature_dim, train_loader, val_loader,
num_classes, epochs=50, lr=0.1, device="cpu"):
for p in encoder.parameters():
p.requires_grad = False
f_train, y_train = extract(encoder, train_loader, device)
f_val, y_val = extract(encoder, val_loader, device)
head = nn.Linear(feature_dim, num_classes).to(device)
opt = SGD(head.parameters(), lr=lr, momentum=0.9, weight_decay=0)
sched = CosineAnnealingLR(opt, T_max=epochs)
ds = torch.utils.data.TensorDataset(f_train, y_train)
train_iter = DataLoader(ds, batch_size=1024, shuffle=True)
best_val = 0.0
for ep in range(epochs):
head.train()
for x, y in train_iter:
x, y = x.to(device), y.to(device)
loss = F.cross_entropy(head(x), y)
opt.zero_grad(); loss.backward(); opt.step()
sched.step()
head.eval()
with torch.no_grad():
acc = (head(f_val.to(device)).argmax(-1).cpu() == y_val).float().mean().item()
best_val = max(best_val, acc)
return best_val
```
## Report
```
[linear probe]
encoder: <name + pretrain checkpoint>
feature_dim: <int>
epochs: <int>
best_val_top1: <float>
```
## Rules
- Never update encoder weights during linear probe; that would be a fine-tune, not a probe.
- Precompute features once; retraining the encoder on every epoch wastes 100x compute.
- Use SGD with cosine schedule and no weight decay; Adam sometimes underperforms here.
- Sweep learning rates at least once per encoder family; the optimum varies across SSL methods.