PyTorch code for Vision Transformers training with the Self-Supervised learning method DINO
PyTorch code for Vision Transformers training with the Self-Supervised learning method DINO
:new: Please check out our more recent DINOv2 effort in the same line of work.
PyTorch implementation and pretrained models for DINO. For details, see Emerging Properties in Self-Supervised Vision Transformers.
[blogpost] [arXiv] [Yannic Kilcher's video]
You can choose to download only the weights of the pretrained backbone used for downstream tasks, or the full checkpoint which contains backbone and projection head weights for both student and teacher networks. We also provide the backbone in onnx format, as well as detailed arguments and training/evaluation logs. Note that DeiT-S and ViT-S names refer exactly to the same architecture.
| arch | params | k-nn | linear | download | |||||
|---|---|---|---|---|---|---|---|---|---|
| ViT-S/16 | 21M | 74.5% | 77.0% | backbone only | full ckpt | onnx | args | logs | eval logs |
| ViT-S/8 | 21M | 78.3% | 79.7% | backbone only | full ckpt | onnx | args | logs | eval logs |
| ViT-B/16 | 85M | 76.1% | 78.2% | backbone only | full ckpt | onnx | args | logs | eval logs |
| ViT-B/8 | 85M | 77.4% | 80.1% | backbone only | full ckpt | onnx | args | logs | eval logs |
| ResNet-50 | 23M | 67.5% | 75.3% | backbone only | full ckpt | onnx | args | logs | eval logs |
We also release XCiT models ([arXiv] [code]) trained with DINO:
| arch | params | k-nn | linear | download | ||||
|---|---|---|---|---|---|---|---|---|
| xcit_small_12_p16 | 26M | 76.0% | 77.8% | backbone only | full ckpt | args | logs | eval |
| xcit_small_12_p8 | 26M | 77.1% | 79.2% | backbone only | full ckpt | args | logs | eval |
| xcit_medium_24_p16 | 84M | 76.4% | 78.8% | backbone only | full ckpt | args | logs | eval |
| xcit_medium_24_p8 | 84M | 77.9% | 80.3% | backbone only | full ckpt | args | logs | eval |
…Please install PyTorch and download the ImageNet dataset. This codebase has been developed with python version 3.6, PyTorch version 1.7.1, CUDA 11.0 and torchvision 0.8.2. The exact arguments to reproduce the models presented in our paper can be found in the args column of the pretrained models section. For a glimpse at the full documentation of DINO training please run:
python main_dino.py --helpRun DINO with ViT-small network on a single node with 8 GPUs for 100 epochs with the following command. Training time is 1.75 day and the resulting checkpoint should reach 69.3% on k-NN eval and 74.0% on linear eval. We provide training and linear evaluation logs (with batch size 256 at evaluation time) for this run to help reproducibility.
python -m torch.distributed.launch --nproc_per_node=8 main_dino.py --arch vit_small --data_path /path/to/imagenet/train --output_dir /path/to/saving_dirWe use Slurm and submitit (pip install submitit). To train on 2 nodes with 8 GPUs each (total 16 GPUs):
python run_with_submitit.py --nodes 2 --ngpus 8 --arch vit_small --data_path /path/to/imagenet/train --output_dir /path/to/saving_dirpython run_with_submitit.py --nodes 2 --ngpus 8 --use_volta32 --arch vit_base --data_path /path/to/imagenet/train --output_dir /path/to/saving_dirYou can improve the performance of the vanilla run by:
--epochs 300,--teacher_temp 0.07 --warmup_teacher_temp_epochs 30.--arch vit_small): --norm_last_layer false,python run_with_submitit.py --arch vit_small --epochs 300 --teacher_temp 0.07 --warmup_teacher_temp_epochs 30 --norm_last_layer false --data_path /path/to/imagenet/train --output_dir /path/to/saving_dirThe resulting pretrained model should reach 73.3% on k-NN eval and 76.0% on linear eval. Training time is 2.6 days with 16 GPUs. We provide training and linear evaluation logs (with batch size 256 at evaluation time) for this run to help reproducibility.
This code also works for training DINO on convolutional networks, like ResNet-50 for example. We highly recommend to adapt some optimization arguments in this case. For example following is a command
Only reporting the accuracy of the first gpu by eval_linear.py
Loss is not decresing
loss increase sharply after the first epoch
ResNet backbone for DINO trainig
After fine-tuning, the model performs worse than the pre-trained model.
about the DINO training loss
How to get the feature map by using pretrained model.
train dino with human image for human Image retrieval got bad performance
Best features for image similarity
Help!Why the results of 'attention_visual.py' are different from those in the paper?