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. A siamese network gives the shared encoder, and contrastive loss gives the push-apart rule. Together they learn features that later classify with few labels.
This page covers the SimCLR recipe only: strong crops, projection head, and NT-Xent over a large batch.
The one idea you need first.
An embedding is a short vector that stands for a 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 has 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 crop, color jitter, and blur. Encode both with shared backbone plus small projection head to get and . 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 views. For one anchor let the mate score and seven negatives score . With , mate term is . Negative terms are , , , , , , , summing to . Denominator is . Loss is . Only a higher mate similarity or weaker negatives lowers it.
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.