TL;DR
DINO is a way to pretrain a Vision Transformer (ViT) on ImageNet images with no labels at all. Two copies of the network see different random crops of the same picture. The student is trained to match the output distribution of the teacher, and the teacher is a slow-moving average of the student itself. The learned features contain the outline of objects, and a plain nearest-neighbour search on them classifies ImageNet at 78.3 percent top-1 with a small ViT.
Builds on: Self-Supervised Learning, Knowledge Distillation, Vision Transformer (ViT), Exponential Moving Average · Leads to: Image Segmentation
Why it mattered
Before this paper, the success of Transformers in language came from self-supervised pretraining, but in vision the best Transformers still came from supervised training on labelled images. The paper showed that the same self-supervised recipe, applied to a ViT, produces features that are useful far beyond the pretraining task. The features are used directly for image search: a DINO ViT trained on the Google Landmarks v2 dataset scored higher on the revisited Oxford and Paris retrieval benchmarks than supervised ViT features, and it was competitive on the copy detection test for near-duplicate images. The same features were used for video object segmentation on DAVIS 2017 without any fine-tuning, and the attention maps of the last layer separate objects from background without masks. The training objective is also short enough to write out in about twenty lines of pseudocode, which the paper includes.
The problem
Self-supervised methods for convolutional networks had already become strong, but they were tuned around the convnet architecture. Applying them to a ViT tended to produce weak features: the ViT did well only with supervised training or with heavy fine-tuning. The paper asked whether self-supervised learning gives ViTs properties that supervised ViTs and convnets do not have, and whether those properties show up in a simple evaluation.
The obstacle is collapse. If the training target is produced by the same network that is being trained, the easiest solution is to output the same thing for every image. Contrastive methods avoid this by using negative pairs, and other methods add predictors, batch normalisation or clustering constraints. The question for this paper is which of those parts are really needed when the target comes from a moving average of the student.
History
Contrastive methods such as A Simple Framework for Contrastive Learning of Visual Representations (SimCLR) and Momentum Contrast for Unsupervised Visual Representation Learning had shown that self-supervised features could match supervised ones for convolutional networks, but they relied on large numbers of negative examples or on memory banks. Methods such as BYOL had shown that negatives are not needed if a predictor and a moving-average target are used. Self- supervised pretraining had made big gains for Transformers in language, which raised the question of whether vision Transformers could be pretrained the same way. The paper answered yes with a method that is closer to distillation than to contrastive learning. It was published at the International Conference on Computer Vision in 2021, and the released code and models made it easy for other groups to reuse.
How it works
Here is one training step traced through with the basic setting of two global crops and several local crops. The numbers are illustrative, with an output dimension of 3 rather than the 65536 used in the paper.
flowchart TD A[One image] --> B[Two global crops and several local crops] B --> C[Student sees all crops] B --> D[Teacher sees the two global crops only] D --> E[Teacher output centred and sharpened] C --> F[Student output with its own temperature] E --> G[Cross-entropy between teacher and student] F --> G G --> H[Gradient step on the student] H --> I[Teacher weights move slowly toward the student]
- Make views. Take one photo and cut it into random crops. Two large crops (the global views) are sent to both networks. Several small crops (the local views) are sent to the student only. This is the Data Augmentation step, with multi-crop. Every crop is passed through a ViT, and its class token goes through a projection head to give the output vector.
- Get the student's output. The student turns each crop's feature into a vector of numbers, then divides by a temperature (0.1 in the paper) and applies a softmax. Example: logits divided by give , and the softmax is about .
- Get the teacher's output. The teacher, which has the same architecture, sees the two global crops. Its raw output is centred by subtracting a running mean , then sharpened with a low temperature. Example: raw logits , centre , temperature . The centred, scaled values are , and the softmax is about . Most of the mass now sits on one dimension.
- Match them. The loss is the cross-entropy from the teacher's distribution to the student's, summed over pairs of views where the teacher view is global and the student view is any other crop. The teacher's output is treated as a fixed target (its gradient is stopped), so only the student learns. Using the numbers above, the cross-entropy is about . The lowest the loss can go is the teacher's own entropy, reached when the student matches the teacher exactly.
- Update the student. A normal gradient step with stochastic gradient descent (AdamW in the paper) moves the student.
- Update the teacher and the centre. The teacher's weights are an exponential moving average of the student's weights, with a decay that rises from 0.996 to 1 over training. The centre is updated the same way from the batch mean of the teacher outputs. Neither update uses a gradient.
Two checks help. After step 3 the teacher's distribution should be peaked but not one-hot. Over many training steps (not just one) the loss should go down, while the teacher's output should not collapse to a single dimension.
The math
Let be the student network and the teacher network, both with outputs. For an image , the output probability on dimension with temperature is the softmax:
A similar formula with temperature gives from the teacher. This is the Softmax Temperature formula from step 2: a lower temperature makes the distribution sharper.
The distillation loss for one pair of views is the Cross-Entropy Loss between the two distributions, :
Here is a global view and is any other view of the same image, so the sum over the set of views is . In the walkthrough, step 4, this is one term.
Teacher centering and sharpening are written as one operation on the teacher's raw output :
where is the batch size and is a rate close to 1. Subtracting is the centring, and the small is the sharpening. The paper shows that the two push in opposite directions: centring alone drives the output toward a uniform distribution, and sharpening alone drives it toward one dimension. Used together they keep the output between the two failure modes.
The teacher weights follow a moving average of the student weights, with decay that follows a cosine schedule from 0.996 to 1:
This is the same kind of exponential moving average used in Momentum Contrast. There it keeps a queue of keys consistent; here it provides a slowly changing target for the student.
Results
On ImageNet, a DINO ViT-S/16 reaches 77.0 percent top-1 with a linear classifier and 74.5 percent with a 20-nearest-neighbour vote, the ViT-S/8 variant reaches 78.3 percent with the vote, and the ViT-B/8 variant reaches 80.1 percent linearly. A ResNet-50 trained with DINO reaches 75.3 percent linearly and 67.5 percent with the vote. The paper also reports that the features transfer to retrieval, copy detection and video segmentation. The limit is compute: smaller patches (/8) give the best numbers but cost much more time per image than /16.
Worked examples
Teacher raw logits are , the centre is , and the teacher temperature is .
- Subtract the centre: .
- Divide by the temperature: .
- Exponentiate: .
- Sum: .
- Divide: .
Interpretation: the first dimension gets almost all the probability. Centring has removed the bias that would otherwise favour dimension 1, and sharpening has made the distribution peaked. Both steps are needed; drop either and the output drifts toward collapse.
Use the student logits with , and the teacher distribution from the previous example.
- Divide the student logits by : .
- Softmax: , , , sum , so the student distribution is .
- Cross-entropy . The logs of the student's probabilities are .
- Multiply each by the teacher's probability: , , .
- Add the three terms: . The cross-entropy is the negative of the sum of , so the loss is about .
Interpretation: the student puts 0.27 on dimension 2 where the teacher puts 0.02, so the loss is still high. Gradient descent moves the student toward the teacher's peaked output. The loss can never go below the teacher's own entropy.
The paper measures how good the features are without training anything on top. On ImageNet, a ViT-S/16 trained with DINO gets 74.5 percent top-1 with a weighted 20-nearest-neighbour vote over stored training features, against 77.0 percent for a trained linear classifier. The same pretrained network, with its weights frozen, can be used for retrieval.
- Freeze the DINO network and compute one feature for every training image.
- For a query image, compute its feature in one forward pass.
- Find the 20 stored features with the highest cosine similarity, and let each one vote for its label, weighted by similarity.
- Predict the label with the most votes.
Interpretation: no labels are used for pretraining, and no classifier is trained on the features. The gap between the two numbers (74.5 and 77.0) is small, so the nearest-neighbour vote recovers most of the class information that a trained linear classifier finds. Try it: the paper's ViT-S/8 gets 78.3 percent with this same vote, higher than the /16 model's 74.5. Explain in one sentence why a smaller patch size can help the nearest-neighbour vote.
Answer: smaller patches give more tokens, so the features carry finer spatial detail, and similar objects are more likely to end up close together in feature space.
Common mistakes and tradeoffs
It is tempting to think DINO learns from labels in a disguised form. In fact no label is used anywhere. The only supervision is the principle that two views of one image should give the same output, because the image is the same object. That is why the features group similar objects together without ever being told their names.
It is tempting to think the teacher is a separate pretrained model that must be obtained first. In fact the teacher is built from the student during training, as a moving average of the student's weights. It is never trained by gradients of its own.
It is tempting to think that centring alone or sharpening alone is enough to avoid collapse. In fact each one causes a different kind of collapse: centring alone pushes the output toward uniform, and sharpening alone pushes it toward one dimension. The combination keeps the output between the two.
It is tempting to think that more views always help. Extra local crops add training signal but cost time and memory, so the gain has to be weighed against the compute budget.
Tradeoffs. Smaller patches (/8 instead of /16) help features and retrieval, and they cost a lot of speed. DINO is a good choice when you want general-purpose frozen features and have no labels. It is a worse choice when you have a large labelled dataset and the target task is well defined, because supervised training is simpler and often just as good for that one task.
Reading guide
The paper is organised around the method, the evaluation protocols, and the ablations. Map each part to the lesson:
- Method (self-distillation with no labels). The loss, the teacher update and the centring rule. Read it with the How it works walkthrough and The math section above.
- Evaluation protocols. The k-NN and linear protocols used in the main tables.
- Main results. ImageNet comparisons, retrieval, copy detection, DAVIS segmentation and transfer.
- Ablations. The component studies, the teacher's role, and the collapse experiments. The Vision Transformer (ViT) design sets the patch sizes used throughout.
Recap
- Self-supervised learning builds a training target from the data itself rather than from labels.
- Knowledge distillation trains a student to match a teacher's output, and DINO uses a teacher that is built from the student during training.
- The teacher is an exponential moving average of the student, which makes it a stable target.
- Centring and sharpening of the teacher output prevent collapse without negatives or a predictor.
- Multi-crop training, with a few global and many local crops, gives better features at moderate extra cost.
- The features contain object segmentation and support strong nearest-neighbour retrieval.
What to read next: Image Segmentation for the dense task the attention maps hint at, and Approximate Nearest Neighbor Search for the retrieval setting the paper evaluates.
Check yourself
QuestionIn DINO, which network receives only the two global crops, and why does that matter?Show answer
The teacher receives only the two global crops, while the student receives all the crops, including the small local ones. This sets up local-to-global correspondence: the student has to predict what the teacher says about a large view from a small view of the same image. The local crops are cheap and add training signal.
QuestionA teacher's raw logits are , the centre is , and . What is the sharpened teacher distribution?Show answer
Centre the logits: . Divide by : . Softmax: and , sum . The distribution is .
QuestionWhy does the paper use centring and sharpening together rather than one of them?Show answer
Centring subtracts a running mean, which stops one dimension from dominating, but it pushes the output toward a uniform distribution. Sharpening, a low teacher temperature, makes the output peaked, but it pushes toward one dimension. Each operation alone produces a different collapse, and the two effects balance when both are applied.
QuestionThe paper reports 74.5 percent for DINO ViT-S/16 with a 20-nearest-neighbour vote and 77.0 percent with a linear classifier. A team wants a fast image search over a product catalogue and has no labels. What should they do, and what does the gap tell them?Show answer
They can pretrain or reuse a DINO ViT on the catalogue images without labels, compute one feature per item, and search by cosine similarity, which needs no classifier training. The gap is small, so most of the class information is easy to read from the features. They should still check retrieval quality on their own data, because the paper's numbers are for ImageNet classes and landmark retrieval, not for their catalogue.
QuestionWhy does DINO use the teacher's output as the target, rather than the student's own output?Show answer
If the student were matched to its own output, the easiest solution would be to output the same thing for every image, which is collapse. The teacher is a slowly moving average of the student's weights, so its output changes gradually and gives a stable target. The centring and sharpening then keep that target informative.
Sources
- Caron, M., Touvron, H., Misra, I., Jégou, H., Mairal, J., Bojanowski, P., and Joulin, A. (2021). Emerging Properties in Self-Supervised Vision Transformers. ICCV 2021. arXiv:2104.14294. arxiv.org/abs/2104.14294
- Official code and pretrained models: github.com/facebookresearch/dino