VerifiedarXiv:2104.1429430 min
Vision · Self-supervised learning

Emerging Properties in Self-Supervised Vision Transformers

A network learns image features by predicting the output of a slowly averaged copy of itself, with no labels.

DINO trains a Vision Transformer, the student, to match the softmax output of a teacher whose weights are a moving average of the student's own. Two crops of the same image go in, the student sees one and the teacher the other, and two small operations on the teacher's output keep the model from giving every image the same answer.

Explaining the paperEmerging Properties in Self-Supervised Vision TransformersCaron, Touvron, Misra, Jégou, Mairal, Bojanowski, Joulin · ICCV 2021 · arXiv:2104.14294 ↗

The paper behind the attention maps that outline objects nobody labelled, and a k-NN classifier on frozen features that reaches 78.3% on ImageNet.

The question and the two findings

In 2020 the Vision Transformer (ViT) matched convolutional networks on image classification, but it needed more training data and compute to get there, and its features showed nothing a convnet's did not. Language Transformers had taken a different route: BERT and GPT were pretrained on raw text with a self-supervised objective (predict hidden or next words) and fine-tuned afterwards. Self-supervised means the training signal is made from the data itself, with no human labels. Caron and colleagues at Facebook AI Research and Inria asked whether the same would help ViTs: pretrain on ImageNet's 1.28 million images with the labels thrown away, and see what the features can do.

They report two properties that supervised ViTs and convnets do not show as clearly:

  1. The attention of the last layer outlines objects. Take the [CLS] token (an extra token whose output summarizes the image) and look at how much it attends to each image patch: the high-attention patches trace the foreground object, although no segmentation label was ever used.
  2. The features work as a nearest-neighbour index. Store the frozen features of the ImageNet training set, classify a test image by a vote of its 20 most similar training images, and a ViT-S/8 gets 78.3% top-1, close to the 79.7% of a linear classifier trained on the same features.

The method they built to get there is DINO, for self-distillation with no labels. It has four parts, and the sections below take them in order: a student network trained to match a teacher's softmax output (distillation); many crops of each image, with the teacher seeing only the large ones (multi-crop); a teacher whose weights are a moving average of the student's (the momentum teacher); and two operations on the teacher's output, centering and sharpening, that stop the trivial solution. After that come the evaluations, the attention maps, and which parts the ablations say matter.

The network: a ViT and a projection head

The network gg is two pieces, g=h∘fg = h \circ f. The backbone ff is a standard ViT (or a ResNet-50; DINO works with both). A ViT cuts a 224×224 image into non-overlapping N×NN\times N patches, flattens each and maps it linearly to a vector, adds a position embedding, prepends the learnable [CLS] vector, and runs the sequence through a stack of Transformer blocks. With 16×16 patches that is 14×14=19614 \times 14 = 196 patch tokens plus [CLS], 197 tokens; with 8×8 patches it is 785. The paper names models by size and patch: ViT-S/16 is the small model (12 blocks, width 384, 6 attention heads, 21M parameters) with 16-pixel patches; ViT-B is 12 blocks, width 768, 12 heads, 85M parameters. ViT-S was chosen because it resembles a ResNet-50 in size (21M vs 23M parameters) and supervised accuracy (79.8% vs 79.3%).

The output of ff is the [CLS] token's final vector, 384 numbers for ViT-S. This is the feature every evaluation uses. During training it passes through the projection head hh: a 3-layer MLP (384 → 2048 → 2048 → 256, GELU activations), an ℓ2\ell_2 normalization of the 256-dimensional result, and a weight-normalized linear layer from 256 to K=65,536K = 65{,}536 outputs with no bias. That last layer alone has 256×65,536≈16.8256 \times 65{,}536 \approx 16.8 million weights, and the whole head about 22.3 million, more than the ViT-S backbone it sits on. It is discarded after training.

Nothing in the network uses batch normalization. ViTs normalize with LayerNorm, and DINO keeps the head free of BatchNorm too (the paper measured 69.7% k-NN without it and 68.6% with it at 100 epochs). A BatchNorm-free system does not need to synchronize statistics across GPUs, which is one of the things that makes earlier self-supervised methods slow to train.

Distillation, and the output it matches

Knowledge distillation trains a student network gθsg_{\theta_s} to reproduce the outputs of a teacher network gθtg_{\theta_t}. Both produce KK unnormalized scores (logits) for an input xx, and a softmax with a temperature turns them into a probability distribution:

Ps(x)(i)=exp⁡(gθs(x)(i)/τs)∑k=1Kexp⁡(gθs(x)(k)/τs)P_s(x)^{(i)} = \frac{\exp(g_{\theta_s}(x)^{(i)}/\tau_s)}{\sum_{k=1}^{K}\exp(g_{\theta_s}(x)^{(k)}/\tau_s)}
(1)

The superscript (i)(i) picks one of the KK entries. The temperature τs>0\tau_s > 0 divides every logit before the exponential: a small temperature stretches the gaps between logits and concentrates the probability on the largest ones; a large temperature shrinks the gaps and flattens the distribution toward uniform. The teacher's PtP_t is computed the same way with its own temperature τt\tau_t. With the teacher fixed, the student minimizes the cross-entropy between the two distributions:

min⁡θs  H(Pt(x), Ps(x))\min_{\theta_s}\; H\big(P_t(x),\, P_s(x)\big)
(2)
H(a,b)=−∑i=1Ka(i)log⁡b(i)H(a, b) = -\sum_{i=1}^{K} a^{(i)} \log b^{(i)}

The paper writes H(a,b)=−alog⁡bH(a,b) = -a \log b, with the sum over the KK entries implied. Cross-entropy is smallest when Ps=PtP_s = P_t, so the gradient pushes the student's distribution toward the teacher's, entry by entry. Its gradient with respect to the student's logits has the form (Ps−Pt)/τs(P_s - P_t)/\tau_s, which is the whole signal the student receives.

In Hinton's distillation both sides use the same raised temperature, so the student learns the teacher's relative preferences among wrong classes. DINO uses different temperatures on purpose. The student runs at τs=0.1\tau_s = 0.1, the teacher at τt\tau_t between 0.04 and 0.07, so the teacher's target is more peaked than anything the student produces at its own temperature. A four-entry example with logits (0.30, 0.25, 0.10, −0.05)(0.30,\ 0.25,\ 0.10,\ -0.05), the scale this head works at: with the code's default normalized last layer, every logit is a cosine similarity in [−1,1][-1, 1]:

τ=0.1: (0.56, 0.34, 0.08, 0.02)τ=0.04: (0.77, 0.22, 0.01, 0.00)\begin{aligned} \tau = 0.1 &:\ (0.56,\ 0.34,\ 0.08,\ 0.02) \\ \tau = 0.04 &:\ (0.77,\ 0.22,\ 0.01,\ 0.00) \end{aligned}

The entropy drops from 0.96 to 0.56 nats (the maximum for four entries is ln⁡4=1.39\ln 4 = 1.39). At temperature 1 the same logits would give (0.29, 0.27, 0.24, 0.20)(0.29,\ 0.27,\ 0.24,\ 0.20), close to uniform, so a head whose outputs are bounded by 1 needs temperatures well below 1 to produce a peaked distribution. The paper calls the low teacher temperature sharpening; the collapse section shows it is one of the two things that keep training from failing.

Views: multi-crop and the loss

Distillation needs a teacher output to match, and with no labels the only thing to agree on is the image itself. DINO feeds the two networks different random views of the same image and asks the student's output on one view to match the teacher's on another. A view is a random crop, resized, then flipped, color-jittered, blurred or solarized with some probability (the augmentations of BYOL). Two views of one image share their content and differ in everything the augmentations change, so matching them rewards features that describe what is in the image and ignore where the crop landed or what the color balance was.

The views come from multi-crop, a scheme from SwAV. Each image yields two global crops, each covering a large part of the image (the code draws the area fraction from 0.4 to 1) and resized to 224×224, and several local crops covering a small part (0.05 to 0.4) resized to 96×96. A local crop costs much less than a global one: at patch size 16 it is 6×6=366\times 6 = 36 patches plus [CLS], 37 tokens against 197. The teacher sees only the two global crops; the student sees all of them. With VV the set of all crops, the loss is

min⁡θs∑x∈{x1g, x2g}  ∑x′∈Vx′≠xH(Pt(x), Ps(x′))\min_{\theta_s} \sum_{x \in \{x^g_1,\, x^g_2\}}\ \ \sum_{\substack{x' \in V \\ x' \neq x}} H\big(P_t(x),\, P_s(x')\big)
(3)

Every teacher crop is paired with every student crop except itself. With 2 global and LL local crops that is 2(L+2)−2=2L+22(L+2) - 2 = 2L + 2 cross-entropy terms per image; the official code averages them instead of summing, which only rescales the loss. For the 10 local crops of the paper's best ViT-S/16 run, each image contributes 22 terms: the two global crops predict each other, and each of the 10 local crops is asked to predict both global ones. Because the teacher only sees crops covering most of the image, a student looking at a 96-pixel piece of it is trained to output what the teacher says about the whole image. The paper calls this local-to-global correspondence.

In Figure 1 the teal rectangles are the global crops and the amber ones the local crops. Click a cell in the pair grid (or press next pair) to see the two crops that term compares, as the networks receive them.

Figure 1 · multi-crop and the loss terms
6
Global crops go to both networks, local crops only to the student. The grid has one cell per (teacher crop, student crop) pair; the crossed cells are the pairs Eq. (3) skips. Drag L from 0 to 10: the readout gives the term count, the student's token count per image, and the paper's Table 8 result where one exists.

Table 8 of the paper measures what the extra crops buy on ViT-S/16 trained on two 8-GPU machines. With only the two global crops, 300 epochs take 45.9 hours and reach 72.5% linear accuracy. Adding 10 local crops reaches 74.6% after 100 epochs and 24.2 hours, and 76.1% after 300 epochs (72.6 hours), at the cost of peak memory rising from 9.3 GB to 15.4 GB per GPU. Training longer without multi-crop does not catch up. The gain shrinks at the top: going from 6 to 10 local crops adds 0.2 points at 300 epochs.

A teacher built from the student

Distillation assumes a teacher that is already good. DINO has none, so it builds the teacher from the student. The teacher has the same architecture and its own copy of the weights, initialized equal to the student's. It receives no gradient (the stop-gradient in the paper's diagram); after every optimizer step on the student, its weights move a small fraction of the way toward the student's:

θt←λθt+(1−λ)θs\theta_t \leftarrow \lambda\theta_t + (1 - \lambda)\theta_s

This is an exponential moving average (EMA) of the student's weights over training, the momentum encoder of MoCo. The rate λ\lambda follows BYOL's cosine schedule from 0.996 to 1 over the course of training, recomputed at every iteration kk of the TT iterations in the run (0.004 is 1−0.9961 - 0.996):

λk=1−0.004⋅1+cos⁡(πk/T)2\lambda_k = 1 - 0.004\cdot\frac{1 + \cos(\pi k / T)}{2}

At λ=0.996\lambda = 0.996 the teacher is a weighted average over roughly 1/(1−λ)=2501/(1-\lambda) = 250 recent student iterations. At batch size 1024, one ImageNet epoch is 1,251 iterations, so early in training the teacher averages over about a fifth of an epoch. By the end λ\lambda reaches 1 and the teacher stops moving.

Section 5.2 compares teachers on ViT-S/16 after 300 epochs, by k-NN accuracy. A teacher that is an exact copy of the student collapses to 0.1%, and so does the student from the previous iteration. The student from the previous epoch, held fixed for a whole epoch, gets 66.6%, on par with MoCo-v2 and BYOL. The momentum teacher gets 72.8%.

The paper also plots the teacher and the student during training and finds the teacher ahead the whole time, for ViT and for ResNet-50. Its reading is Polyak-Ruppert averaging: the student's weights wander around a good region because every SGD step is taken on a noisy minibatch gradient, and an average of recent weights sits closer to the middle of that region than any single iterate. The paper describes this weight averaging as a way to simulate an ensemble of the recent models. The student is then trained toward the outputs of this better model, and the teacher, built from the student, improves with it. Figure 2 isolates the averaging half of that loop in two dimensions.

Figure 2 · an averaged copy of noisy SGD
0.95
step 399
A two-weight toy with loss 12(w12+8w22)\tfrac12(w_1^2 + 8w_2^2). The student takes noisy SGD steps; the teacher is its EMA. Drag λ\lambda: at 0 the teacher is the student, at 1 it never leaves the start, and between 0.8 and 0.99 its loss sits well below the student's. Too slow a teacher lags: at 0.999 it is still descending after 400 steps.

In the toy, λ=0.98\lambda = 0.98 brings the teacher's mean loss over the last 100 steps to 0.003 against the student's 0.073. The best λ\lambda depends on the length of the run: a teacher with a 250-iteration memory is too slow for a 400-step toy and right for a training run of hundreds of thousands of iterations. The toy's student ignores the teacher; in DINO the student is pulled toward the teacher's outputs.

Collapse, centering and sharpening

Everything so far admits a trivial solution. If both networks output the same distribution for every image, the student matches the teacher perfectly on every pair of crops and the loss is as low as it can go. The features then say nothing about the image. This is collapse, and every self-supervised method of this kind carries a mechanism against it: a contrastive loss that pushes different images apart (MoCo, SimCLR), a clustering constraint that forces images to spread over clusters (SwAV), a predictor network and batch normalization (BYOL). DINO uses only two operations on the teacher's output.

The paper separates two ways to collapse. In the first, one output dimension dominates for every image: the softmax puts almost all its mass on the same entry regardless of input. In the second, the output is uniform: every entry gets 1/K1/K for every image.

Centering targets the first. The teacher keeps a running mean cc of its own logits over batches,

c←m c+(1−m) 1B∑i=1Bgθt(xi)c \leftarrow m\, c + (1 - m)\, \frac{1}{B} \sum_{i=1}^{B} g_{\theta_t}(x_i)
(4)

with rate m=0.9m = 0.9 and BB the batch size (in the code, all images on all GPUs, both global crops), and subtracts it from the teacher's logits before the softmax. The teacher's distribution is

Pt(x)=softmax ⁣(gθt(x)−cτt)P_t(x) = \mathrm{softmax}\!\Big(\frac{g_{\theta_t}(x) - c}{\tau_t}\Big)

A dimension that is large for every image is large in the batch mean too, so subtracting cc removes the part every image shares and keeps only how each image differs from the average. No single entry can then win for all inputs. The center depends only on a batch mean, a first-order statistic, and it is smoothed over batches, which is why DINO keeps working at small batch sizes (Section 5.5).

Centering pushes toward the second kind of collapse. If the network gives every image the same logits, those logits equal the batch mean, and after centering every entry is zero: the softmax of a zero vector is uniform. Sharpening, the low teacher temperature, counters this. Dividing the centered logits by 0.04 multiplies each image's small deviations from the mean by 25 before the softmax, so an image that is slightly above average on some dimension gets a peaked target on it.

Figure 3 applies the two operations to one batch. Every image carries a +1.0 logit on dimension 4, standing in for a component all images share, and a +0.35 logit on a dimension of its own.

Figure 3 · centering and sharpening on one batch
0.040
Rows: the teacher's distribution for six images over 12 dimensions. Bottom row: the batch average. Turn centering off at τt=0.04\tau_t = 0.04 and every image goes to dimension 4; turn it on and each image goes to its own. Drag τt\tau_t toward 1 and every row flattens toward uniform, centered or not.

A static batch does not show why the balance matters over training, since collapse is a feedback loop. Suppose dimension 4 wins for every image. The student is trained to put its mass on 4 for every crop, the teacher follows the student through the EMA, and dimension 4 wins by a wider margin the next time. The loss goes to zero with the output ignoring the input. The uniform direction has its own loop. The student has to predict the teacher's output on one crop from a different crop, and when a crop is ambiguous (a local crop that could belong to several images) the best prediction is an average over the possible targets, softer than any of them. The teacher averages those softer students, and the targets soften further.

To see which loop is running, the paper splits the loss. The cross-entropy of Eq. (2) is the teacher's entropy plus the Kullback-Leibler divergence from the teacher to the student:

H(Pt,Ps)=h(Pt)+DKL(Pt ∥ Ps)H(P_t, P_s) = h(P_t) + D_{\mathrm{KL}}(P_t \,\|\, P_s)
(5)

The entropy h(Pt)h(P_t) measures how spread out the target is: 0 for a one-hot target, log⁡K\log K for a uniform one. The KL measures how far the student is from the target. Collapse of either kind drives the KL to zero, because a student that outputs a constant can match a teacher that outputs the same constant exactly. The entropy tells the two kinds apart: it goes to 0 when one dimension wins and to log⁡K\log K when the output is uniform. In the paper's Figure 7, removing either operation sends the KL to zero, with the entropy at 0 without centering and at log⁡K\log K without sharpening.

Figure 4 runs that experiment on a toy: six kinds of image, 10 output dimensions, a network that is a table of logits (one shared bias plus one row per image), crops that are confused with a neighbouring image 35% of the time, and the DINO update with a student temperature of 0.1. Press Play in each setting and watch the entropy and the KL.

Figure 4 · the collapse study, as a toy
step 599
Left, the teacher's distribution for each kind of image; right, the entropy and the KL of Eq. (5) over 600 steps. With both operations each image settles on its own dimension and the KL stays near 0.8. Without centering, one dimension takes every image. Without sharpening (teacher at 0.1 like the student), every row turns uniform and the entropy reaches log K. Without momentum, the teacher is a copy of the student and training never settles: the outputs keep snapping onto one or two dimensions and the KL spikes.

In the toy, the working setting ends with an entropy of about 0.6 nats and a KL of about 0.8: the targets are peaked but not one-hot, and the student, working from a confusable crop, never matches them exactly, so the student's gradient never vanishes. The paper's ablations show how narrow the working range is (ViT-S/16, 100 epochs, k-NN): a constant teacher temperature of 0.04 gives 69.6%, 0.06 gives 68.7%, and 0.08 collapses to 0.1% with the training loss sitting at ln⁡K=ln⁡65,536≈11.09\ln K = \ln 65{,}536 \approx 11.09, the cross-entropy of a uniform target. A temperature of 0 (a one-hot argmax target) trains but reaches only 43.9%. The published schedule warms τt\tau_t from 0.04 to 0.07 over the first 30 epochs (69.7%), since a temperature above 0.06 does not collapse if training starts below it. For the center, mm anywhere from 0 to 0.99 works (69.1% to 69.7%); at 0.999 the center moves too slowly to track the logits and the model collapses.

Momentum is the third safeguard, and the others depend on it. Appendix B shows centering alone with a copied teacher collapses (0.1%), while the same network with Sinkhorn-Knopp balancing, SwAV's heavier normalization, reaches 71.8% without momentum. With the momentum teacher, centering is enough (76.1% linear).

One training step, with shapes

Put the pieces together for ViT-S/16 at the paper's batch size of 1024, spread over 16 GPUs as 64 images each. Every image gives 2 global crops and 10 local crops. The student runs on all 12 crops and the teacher on the 2 global ones; each network output is a row of 65,536 logits. Per GPU the step computes:

# One DINO step on one GPU (ViT-S/16, 64 images; 16 GPUs make batch 1024)
# crops: 2 global (3x224x224) + 10 local (3x96x96) per image
g   = [aug_global(x), aug_global(x)]           # 2 tensors, each 64x3x224x224
l   = [aug_local(x) for _ in range(10)]         # 10 tensors, each 64x3x96x96
s   = [head_s(vit_s(v)) / 0.1 for v in g + l]   # 12 x (64 x 65536) logits
with no_grad():
    zt = [head_t(vit_t(v)) for v in g]          # 2 x (64 x 65536), no gradient
    pt = [softmax((z - c) / tau_t) for z in zt]  # center, then sharpen
loss, n = 0, 0
for i, p in enumerate(pt):                      # teacher crop i
    for j, sj in enumerate(s):                  # student crop j
        if j == i: continue                     # skip a crop paired with itself
        loss += (-p * log_softmax(sj)).sum(-1).mean(); n += 1
loss = loss / n                                  # n = 2*12 - 2 = 22 terms
loss.backward(); adamw_step(student)            # gradient reaches the student only
theta_t = lam * theta_t + (1 - lam) * theta_s   # teacher: EMA of the student
c = 0.9 * c + 0.1 * all_gpus_mean(cat(zt))      # center: EMA of raw teacher logits

The gradient flows from the loss into the student's head and backbone only; the teacher's probabilities are treated as constants. The optimizer is AdamW. The learning rate warms up linearly over 10 epochs to 0.0005×batch/2560.0005 \times \text{batch}/256, 0.002 at batch 1024, then decays on a cosine schedule; weight decay rises on a cosine schedule from 0.04 to 0.4. The official code also clips gradients at norm 3 and keeps the last layer of the head frozen during the first epoch. The center update uses the teacher's raw logits, before centering, averaged over all 2,048 global crops in the global batch.

One practical note from the code: the default teacher_temp is 0.04 with no warm-up, and the paper's 0.04 → 0.07 schedule is the README's "boosting" recipe, which sets --teacher_temp 0.07 and --warmup_teacher_temp_epochs 30. The README's 100-epoch ViT-S/16 run on one 8-GPU node takes 1.75 days and reaches 69.3% k-NN and 74.0% linear.

Evaluating features without training on them

A self-supervised model is judged by what its frozen features support. The standard test is linear evaluation: freeze the backbone, train one linear layer on ImageNet's labels on top of the features, report top-1 accuracy. The paper finds this protocol sensitive to its own learning rate and adds a second one with nothing to tune, a weighted k-nearest-neighbour (k-NN) classifier. Compute the ℓ2\ell_2-normalized [CLS] feature of every training image once. For a test image, find the kk training features with the largest cosine similarity sis_i and let them vote, each with weight exp⁡(si/τ)\exp(s_i/\tau), where τ=0.07\tau = 0.07, k=20k = 20, and Nk\mathcal{N}_k is the set of neighbours:

score(c)=∑i∈Nkesi/τ 1[ci=c]\text{score}(c) = \sum_{i \in \mathcal{N}_k} e^{s_i/\tau}\, \mathbf{1}[c_i = c]

The weight grows fast with similarity. A neighbour at cosine 0.9 counts e0.1/0.07≈4.2e^{0.1/0.07} \approx 4.2 times as much as one at 0.8. The protocol needs no training, no augmentation and one pass over the data.

Figure 5 · the weighted k-NN vote
50°
20
Normalized features lie on a sphere; here, on a circle. Drag the white test feature around the ring. Its k most similar training features are linked to it, with line opacity showing each vote's weight. Push k to 160 (every training point) and the unweighted vote splits 25% four ways, while the weighted vote still follows the nearest points.

On ViT-S (800 epochs), DINO gets 77.0% linear and 74.5% k-NN. The same backbone trained with BYOL, MoCo-v2 and SwAV by the authors (BYOL for 300 epochs, since it got worse when trained longer) gets 71.4 / 66.6, 72.7 / 64.4 and 73.5 / 66.3. The k-NN margin (7.9 points over the best of them) is more than twice the linear margin (3.5 points). On ResNet-50, DINO is level with the best prior method (75.3% linear, like SwAV; 67.5% k-NN), and its k-NN accuracy sits 7.8 points below its linear accuracy, the usual gap for convnets. The small gap between k-NN and linear is specific to DINO with a ViT. With 8×8 patches, ViT-S/8 reaches 79.7% linear and 78.3% k-NN, and ViT-B/8 80.1% linear, against a supervised ViT-S at 79.8%.

A good k-NN score means images of the same class already sit near each other in feature space, so the features can index a database with no classifier trained on top. The paper tests this directly on retrieval. On the revisited Oxford and Paris landmark benchmarks, DINO ViT-S/16 features pretrained on ImageNet beat supervised ViT-S/16 features (41.8 vs 33.5 mAP on Oxford, medium split), and pretrained instead on 1.2 million Google Landmarks images, with no labels, reach 51.5, above the best previously published off-the-shelf descriptor (49.8). On copy detection (INRIA Copydays, strong subset, with 10,000 distractors) ViT-B/8 features at 320×320 get 85.5 mAP.

Attention maps that segment objects

In a ViT, each attention head of each block computes, for every token, a softmax distribution over all tokens: how much of each token's value vector to mix in. Take the last block, the [CLS] token as the query, and drop the [CLS]-to-[CLS] entry. What is left is one weight per image patch, a map the size of the patch grid, for each of the heads (6 in ViT-S). For a 480×480 image at patch size 8 the sequence is 60×60+1=360160 \times 60 + 1 = 3601 tokens and each map is 60×60.

In DINO ViTs these maps light up on the foreground objects, and different heads pick out different objects or parts, including small or partly hidden ones. To measure it, the paper turns a map into a mask: sort the patches by attention weight and keep the smallest set whose weights add up to a fixed share of the total (60% in Figure 4 of the paper; the official visualize_attention.py does this with a sorted cumulative sum). Then it compares the mask with the ground-truth object masks of PASCAL VOC 2012 by the Jaccard index, the area of the intersection divided by the area of the union. Figure 6 walks through that procedure.

Figure 6 · from an attention map to a mask
60%
The attention map here is constructed (each head scores patches by how much of one region they cover), not taken from a trained model; the thresholding and the Jaccard score are the paper's procedure. Drag the kept mass from 5% to 100%: a small share keeps only the bird's core, 100% keeps every patch. At 80%, the value Appendix D states, this map's mask already spills into the background. Switch to patch 16 and the mask gets coarser. Heads 2 and 3 attend to the post and the sky, so their Jaccard against the bird is low; the paper reports the best head.

On ViT-S/16, masks from the best head score a Jaccard of 45.9 for DINO, 27.3 for the same network trained with labels, and 22.0 for random weights (ViT-S/8: 44.7, 23.7, 21.8). The paper's examples show the supervised ViT attending poorly to the object in cluttered scenes. Its motivation points at the training signal: an image-level label reduces a picture to one concept out of a fixed list, and any feature that predicts that concept is rewarded, while matching two views of the same image rewards describing whatever the views share.

The property is shared across self-supervised methods. The paper's appendix trains MoCo-v2, BYOL and SwAV on the same ViT-S/16 and gets 46.3, 47.8 and 46.8, all at or above DINO's 45.9; DINO without multi-crop gets 45.1. DINO stands apart from those methods on k-NN accuracy (Table 2 on ViT-S: 74.5% against at most 66.6%).

The patch features carry spatial information beyond the attention. On the DAVIS-2017 video object segmentation benchmark, the paper propagates the first frame's mask through the video by nearest-neighbour matching of patch features between consecutive frames, with no training for the task. DINO ViT-S/8 scores 69.9 (the mean of region and contour accuracy), above a supervised ViT-S/8 at 66.0, and ViT-B/8 scores 71.4.

Smaller patches

Patch size changes the results without changing the parameter count. A 16×16 patch on a 224 image leaves a 14×14 grid; an 8×8 patch leaves 28×28, four times as many tokens, and self-attention compares every token with every other, so its cost grows with the square of the token count. Figure 7, from the ViT explainer, shows the count and the attention cost across patch sizes.

Figure 7 · tokens and attention cost by patch size
16×16 · N+1=197
Sweep the patch size P. The token count is (224/P)² + 1; the attention cost, on a log scale, grows with its square. DINO's /8 models use P = 8.

The parameter count barely moves. The Transformer blocks do not depend on sequence length; the patch embedding shrinks (an 8×8 RGB patch is 192 numbers instead of 768, so about 221,000 fewer weights at width 384); the position embedding gains 588 rows of 384, about 226,000 more. The paper lists both ViT-S models at 21M. Going from /16 to /8, ViT-S goes from 77.0% to 79.7% linear and from 74.5% to 78.3% k-NN, and on DAVIS ViT-B goes from 62.3 to 71.4. Speed drops: inference throughput on a V100 falls from 1,007 images per second for ViT-S/16 to 180 for ViT-S/8, and to 44 with 5×5 patches. ViT-B/8 runs at 63 images per second, which is still 1.4 times faster than the previous best self-supervised model on the linear benchmark, a ResNet-152 three times wider with 794M parameters (79.8%), at a tenth of the parameters.

Which components matter

Table 7 adds and removes one component at a time on ViT-S/16 (300 epochs; k-NN / linear). The default DINO gets 72.8 / 76.1.

The authors' own runs of the reference methods under the same schedule give 66.6 / 71.4 for BYOL, 62.0 / 71.6 for MoCo-v2 and 64.7 / 71.8 for SwAV. Multi-crop is not a generic add-on either: with 6 local crops, MoCo-v2 gains and BYOL loses (66.6 → 59.8 k-NN), and DINO gains most (72.5 → 75.9 linear). Other settings: more attention heads help a little at the same width (6 heads 72.8, 16 heads 73.8 k-NN), longer training helps (100, 300, 800 epochs: 70.9, 72.8, 74.5 k-NN), and batch size matters little (k-NN at 100 epochs without multi-crop: 57.9 at batch 128 on one GPU, 59.9 at 1024).

Fine-tuned on downstream classification datasets, DINO-pretrained ViTs beat supervised pretraining on ImageNet itself (ViT-S/16: 81.5% vs 79.9%; ViT-B/16: 82.8% vs 81.8%) and on most of the six transfer datasets, with ViT-B/16 on iNaturalist 2018 the exception (72.6% vs 73.2%).

Limits and open points

The collapse defenses are narrow. A constant teacher temperature of 0.08 or a center rate of 0.999 each end in total collapse, and the paper's explanation of why centering plus sharpening suffices with a momentum teacher is empirical, from the entropy and KL curves, with no proof. The paper expects small batches to need re-tuned momentum rates, and the code suggests an EMA rate of 0.9995 at batch 256 instead of 0.996.

The best features come from the smallest patches, which are the slowest to run: 180 images per second for ViT-S/8 against 1,007 for ViT-S/16. The segmentation evidence is one metric on one dataset (PASCAL VOC masks from the best head, at a mass threshold stated as 60% in one place and 80% in another), plus pictures, and it does not separate DINO from other self-supervised methods. The attention map is also not trained to be a mask; the paper describes the maps as smooth and not optimized to produce one, and names weakly supervised segmentation as a possible use.

Apart from the landmark-retrieval run, every model is pretrained on ImageNet, a curated dataset with one main object per image. The paper's conclusion names training larger ViTs on uncurated images as the next step. The follow-up, DINOv2 (2023), instead built a curated dataset automatically and scaled the ViT to 1 billion parameters. Other directions for self-supervised ViTs followed within a year or two: MAE reconstructs masked pixels, and I-JEPA predicts the features of masked regions.

Provenance Verified against primary literature
Hinton et al. (2015)Knowledge distillation: a small model is trained on a large, already-trained model's softmax outputs, both computed at the same raised temperature T. DINO keeps the cross-entropy between softmax outputs and changes three things: the teacher is built during training, the two networks have the same architecture, and the teacher uses a lower temperature than the student.
MoCo (2020)The momentum encoder: theta_k <- m theta_k + (1 - m) theta_q with m = 0.999, introduced to keep the keys in a contrastive queue consistent. DINO has no queue and no contrastive loss.
BYOL (2020)The EMA schedule DINO reuses: tau = 1 - (1 - tau_base)(cos(pi k / K) + 1)/2 with tau_base = 0.996, rising to 1 by the end of training. BYOL needs a predictor MLP on the student to avoid collapse; DINO does not.
Mean Teacher (2017)A teacher whose weights are an exponential moving average of the student's, used for semi-supervised consistency targets. The DINO paper reads its momentum teacher this way and as Polyak-Ruppert averaging.
SwAV (2020)Source of multi-crop (two high-resolution views plus several low-resolution ones), of the l2-normalized head with a weight-normalized "prototype" layer, and of the Sinkhorn-Knopp balancing that DINO replaces with centering.
Wu et al. (2018)Weighted k-NN evaluation: neighbours vote with weight exp(s_i / tau), tau = 0.07, on normalized features. Wu et al. used k = 200; DINO reports k = 20.
main_dino.pyLines 363-416, DINOLoss: teacher probabilities are softmax((teacher_output - self.center) / temp), so the center is subtracted; the center is an EMA (center_momentum = 0.9) of the raw teacher outputs of both global crops, averaged over all GPUs; the loss is the mean over the (teacher crop, student crop) pairs.
main_dino.py, READMELines 55-116, defaults: out_dim K = 65536; teacher_temp 0.04 with no warm-up (the README's "boosting" recipe, --teacher_temp 0.07 --warmup_teacher_temp_epochs 30, is the paper's schedule); global crop scale (0.4, 1), local crop scale (0.05, 0.4), 8 local crops; momentum_teacher 0.996 raised to 1 by a cosine schedule over iterations (utils.py cosine_scheduler).
vision_transformer.pyLines 257-291, DINOHead: Linear 384->2048, GELU, Linear 2048->2048, GELU, Linear 2048->256, l2 normalization, weight-normalized Linear 256->65536 without bias, gain fixed at 1 when norm_last_layer is true.
Fig. 4 vs App. DThe Jaccard numbers for masks from [CLS] attention (random 22.0, supervised 27.3, DINO 45.9 on ViT-S/16) appear twice: Fig. 4 says the masks keep 60% of the attention mass, Appendix D says 80%. The appendix table also shows MoCo-v2 (46.3), BYOL (47.8) and SwAV (46.8) trained on the same ViT-S/16 matching or beating DINO on this metric.
Table 6"Self-supervised pretraining with DINO transfers better than supervised pretraining" holds for 12 of the 14 comparisons; ViT-S/16 ties on CIFAR-10 (99.0 both) and ViT-B/16 is lower on iNaturalist 2018 (72.6 vs 73.2).
correctionSign of the centering. Section 3.1 says centering "can be interpreted as adding a bias term c to the teacher: g_t(x) <- g_t(x) + c", and Eq. (4) defines c as the running mean of the teacher outputs. Adding that mean would double every dimension's shared offset instead of removing it. The paper's own Algorithm 1 computes softmax((t - C) / tpt), and the official code computes softmax((teacher_output - self.center) / temp): the center is subtracted. This page writes the teacher as softmax((g_t(x) - c) / tau_t). Separately, Section 5.3 says a KL of zero "indicates a constant output"; a zero KL only says the student reproduces the teacher on every pair of crops, which a constant output achieves trivially, so the page reads collapse from the KL together with the entropy.

Questions you might still have

?

Is it still distillation if the teacher is never trained on its own?
It keeps the machinery of distillation: a student trained by cross-entropy to reproduce a teacher's softmax output, with no gradient into the teacher. What changes is where the teacher comes from. In Hinton-style distillation the teacher is a larger network trained beforehand; in DINO it is an exponential moving average of the student's own weights, updated every iteration. The paper calls this self-distillation with no labels, the source of the name DINO.

?

What are the 65,536 output dimensions? Are they classes?
They are learned slots that the softmax spreads each image over, closer to cluster assignments than to classes. Nothing ties any of them to an ImageNet label, and they are thrown away after training: every evaluation uses the backbone's output, the 384-dimensional [CLS] feature for ViT-S. The paper tried K from 1,024 to 262,144 and found k-NN accuracy between 67.8% and 69.7%, best at 65,536.

?

Is the segmentation in the attention maps something DINO specifically causes?
Mostly no. The paper's appendix trains MoCo-v2, BYOL and SwAV on the same ViT-S/16 and gets Jaccard scores of 46.3, 47.8 and 46.8 against DINO's 45.9, all far above the supervised ViT's 27.3. The intro says so directly: the emergence of segmentation masks seems to be shared across self-supervised methods. What DINO adds is the k-NN accuracy, which needs the momentum teacher and multi-crop.

?

Why does the teacher get only the two large crops?
The loss asks the student to predict, from any crop, what the teacher says about a large crop of the same image. Feeding the teacher only views that cover most of the image makes the target a description of the whole image, so a student looking at a 96-pixel patch of a wing is pushed to output what the teacher outputs for the whole bird. The paper calls this local-to-global correspondence. It also saves compute: the teacher runs on 2 crops instead of 12.

?

What happens if I set the teacher temperature too high?
The model collapses to a uniform output. In the appendix, a constant teacher temperature of 0.08 gives 0.1% k-NN accuracy, and the training loss settles at ln K, about 11.09 for K = 65,536. A constant 0.04 works (69.6%). The published schedule starts at 0.04 and ramps linearly to 0.07 over 30 epochs, which reaches 69.7% because the ramp starts from a value that does not collapse.

?

How much compute does training take, and can I train it on one GPU?
The ViT-S/16 run that reaches 76.1% linear accuracy took 72.6 hours on two 8-GPU machines (300 epochs, 10 local crops). The official README gives a 100-epoch run on one 8-GPU node in 1.75 days reaching 69.3% k-NN. On one GPU the paper trained at batch size 128 (57.9% k-NN after 100 epochs without multi-crop, against 59.9% at batch 1024).

?

How do I get DINO features for my own images?
The official repository publishes the trained backbones through torch.hub, for example torch.hub.load('facebookresearch/dino:main', 'dino_vits16'). The returned model has no projection head: its forward pass returns the [CLS] token after the final LayerNorm, a 384-dimensional vector for ViT-S. L2-normalize it and compare images by dot product to reproduce the k-NN setup. The repository also has a script, visualize_attention.py, that draws the last-layer attention maps.

?

What came after DINO?
The same group published DINOv2 (Oquab et al., 2023), which trains ViTs up to 1 billion parameters on an automatically built, curated image dataset and distills the largest into smaller models. The I-JEPA and MAE explainers on this site cover two other directions for self-supervised ViTs: predicting masked regions in feature space and reconstructing masked pixels.

Footnotes & further reading

  1. The paper: Caron, Touvron, Misra, Jégou, Mairal, Bojanowski & Joulin, Emerging Properties in Self-Supervised Vision Transformers (ICCV 2021; arXiv v2, May 2021). Official code, the source for the defaults, the loss and the head quoted on this page.
  2. Distillation: Hinton, Vinyals & Dean, Distilling the Knowledge in a Neural Network (2015). The distillation explainer covers the temperature and the T2T^2 gradient scaling.
  3. Momentum encoders: He et al., Momentum Contrast for Unsupervised Visual Representation Learning (MoCo, CVPR 2020), and Grill et al., Bootstrap Your Own Latent (BYOL, NeurIPS 2020), which gives the 0.996 → 1 cosine schedule. Mean Teacher: Tarvainen & Valpola, Mean teachers are better role models (NeurIPS 2017).
  4. Multi-crop and the prototype head: Caron et al., Unsupervised Learning of Visual Features by Contrasting Cluster Assignments (SwAV, NeurIPS 2020).
  5. The weighted k-NN protocol: Wu, Xiong, Yu & Lin, Unsupervised Feature Learning via Non-Parametric Instance Discrimination (CVPR 2018), Section 3.4.
  6. The follow-up: Oquab et al., DINOv2: Learning Robust Visual Features without Supervision (2023).