SimCLR Contrastive Pretraining
Show the net two random crops of one photo and teach it they match, while crops from other photos do not. Matching without labels builds strong features.
Why Does This Exist?
Labeling millions of photos costs too much, yet unlabeled photos are free. SimCLR turns each photo into its own lesson: two views of photo must agree more than views of different photos do. A siamese network supplies the shared encoder and contrastive loss supplies the push-apart rule. Together they learn features that later classify with few labels.
This page covers the SimCLR recipe only: strong crops, the projection head, and NT-Xent over a large batch.
The one idea you need first: an embedding is a short vector that stands for one view. Cosine similarity says how alike two embeddings are, from to .
Think of It Like This
Two blurred photos of one dog in a lineup
You hold two blurry snapshots of one dog and face a lineup of many dogs. Your job is to point at the pair that came from the same animal. Ears, spots, and posture survive the blur, so you match them and ignore the rest.
SimCLR is that game played millions of times. Crops are the blur, the lineup is the batch, and the prize is a feature reader that knows dogs from cats before it ever sees a label. Where it stops: a real lineup holds one match, while SimCLR treats every other photo as a non-match even when two dogs look alike.
How It Actually Works
From each photo make two random views with cropping, color jitter, and blur. Encode both with a shared backbone plus a small projection head to get and , where is the deep encoder kept for later, is the throwaway head used only for training, and is the projected embedding. For a batch of photos there are views. The NT-Xent loss for pair is
In plain words, is the temperature that sharpens scores, the top term rewards the true mate, and the bottom sum runs over all other views. Small makes hard negatives count more.
Worked example
Say , so 8 views. For one anchor let the mate score 0.92 and seven negatives score . With the mate term is . The negative terms are , , , , , , , summing to 9.99. The denominator is . The loss is . Only a higher mate similarity or weaker negatives lowers it.
Code
import math
mate, tau = 0.92, 0.5negs = [0.10, 0.20, 0.05, 0.30, 0.12, 0.18, 0.25]num = math.exp(mate / tau)den = num + sum(math.exp(n / tau) for n in negs)print(round(-math.log(num / den), 4))# -> 0.9506The mate term of 6.30 beats the negative total of 9.99 only weakly, so the loss stays near 0.95.
Watch Out For
Weak crops that make the task trivial
Small crops with no color change let the net match by average brightness. Accuracy looks fine yet transfer flops. The symptom is high pair accuracy with poor image classification later. Use strong random crops plus color jitter and Gaussian blur together.
Judging the projection head features as final
SimCLR trains for the loss but downstream uses before . Reading off throws away about two accuracy points. Always freeze , drop , and probe or fine-tune from .
The Quick Version
- SimCLR learns from two augmented views of one photo treated as a positive pair.
- NT-Xent with temperature 0.5 contrasts the mate against all other views in the batch.
- Strong crops and a nonlinear projection head carry most of the gain.
- Large batches supply the negatives, so small batches need memory tricks.
- Use the backbone output, not the projection output, for later tasks.