dinov2 README

The project's README from github.com/facebookresearch/dinov2. As one markdown file: README.md.

:new: [2025-12-18] Added support for loading XRay-DINO backbone following Advancing human-centric AI for robust X-ray analysis through holistic self-supervised learning, more details are here

:new: [2025-12-16] Added Channel-Adaptive DINO code following Scaling Channel-Adaptive Self-Supervised Learning, more details are here

:new: [2025-12-16] Added Cell-DINO code following [Cell-DINO: Self-Supervised Image-based Embeddings for Cell Fluorescent Microscopy](to appear in Plos One Computational Biology), more details are here

[2025-08-14] Please check out the more recent DINOv3 effort continuing this line of work.

[2025-06-11] Added dino.txt inference code, following DINOv2 Meets Text: A Unified Framework for Image- and Pixel-Level Vision-Language Alignment.

[2023-10-26] Added DINOv2 backbones with registers, following Vision Transformers Need Registers.

DINOv2: Learning Robust Visual Features without Supervision

Meta AI Research, FAIR

Maxime Oquab, Timothée Darcet, Théo Moutakanni, Huy V. Vo, Marc Szafraniec, Vasil Khalidov, Patrick Labatut, Armand Joulin, Piotr Bojanowski

[Paper #1] Paper #2] [Blog] [Demo] [BibTeX]

PyTorch implementation and pretrained models for DINOv2. For details, see the papers: DINOv2: Learning Robust Visual Features without Supervision and Vision Transformers Need Registers.

DINOv2 models produce high-performance visual features that can be directly employed with classifiers as simple as linear layers on a variety of computer vision tasks; these visual features are robust and perform well across domains without any requirement for fine-tuning. The models were pretrained on a dataset of 142 M images without using any labels or annotations.

https://github.com/facebookresearch/dinov2/assets/60359573/f168823e-7922-415a-b429-578badf5c356

Visualization of the three first principal components of the patch features of all frames, mapped to RGB values.

Pretrained models

model # of
params
with
registers
ImageNet
k-NN
ImageNet
linear
download
ViT-S/14 distilled 21 M :x: 79.0% 81.1% backbone only
ViT-S/14 distilled 21 M :white_check_mark: 79.1% 80.9% backbone only
ViT-B/14 distilled 86 M :x: 82.1% 84.5% backbone only
ViT-B/14 distilled 86 M :white_check_mark: 82.0% 84.6% backbone only
ViT-L/14 distilled 300 M :x: 83.5% 86.3% backbone only
ViT-L/14 distilled 300 M :white_check_mark: 83.8% 86.7% backbone only
ViT-g/14 1,100 M :x: 83.5% 86.5% backbone only
ViT-g/14 1,100 M :white_check_mark: 83.7% 87.1% backbone only

Pretrained backbones (via PyTorch Hub)

Please follow the instructions here to install PyTorch (the only required dependency for loading the model). Installing PyTorch with CUDA support is strongly recommended.

A corresponding model card is included in the repository.

import torch

# DINOv2
dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
dinov2_vitb14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
dinov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')
dinov2_vitg14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14')

# DINOv2 with registers
dinov2_vits14_reg = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14_reg')
dinov2_vitb14_reg = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14_reg')
dinov2_vitl14_reg = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14_reg')
dinov2_vitg14_reg = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14_reg')

Pretrained backbone: XRay-DINO

Request for downloading the model is here:

https://ai.meta.com/resources/models-and-libraries/raydino-downloads/

After filling the form, you will get an email with a temporary link. You can either download it using wget and indicate the checkpoint path in your local filesystem, or you can directly use the URL from the email in the following code:

import torch

REPO_DIR = <PATH/TO/A/LOCAL/DIRECTORY/WHERE/THE/DINOV2/REPO/WAS/CLONED>

xray_dino_vitl16 = torch.hub.load(REPO_DIR, 'xray_dino_vitl16', source='local', weights=<CHECKPOINT/URL/OR/PATH>)

License Model weights are released under the FAIR Noncommercial Research License. See LICENSE_XRAY_DINO_MODEL for additional details.

Pretrained heads - Image classification

backbone with
registers
download
ImageNet
ViT-S/14 distilled :x: linear head (1 layer, 4 layers)
ViT-S/14 distilled :white_check_mark: linear head (1 layer, 4 layers)
ViT-B/14 distilled :x: linear head (1 layer, 4 layers)
ViT-B/14 distilled :white_check_mark: linear head (1 layer, 4 layers)
ViT-L/14 distilled :x: linear head (1 layer, 4 layers)
ViT-L/14 distilled :white_check_mark: linear head (1 layer, 4 layers)
ViT-g/14 :x: linear head (1 layer, 4 layers)
ViT-g/14 :white_check_mark: linear head (1 layer, 4 layers)

The (full) classifier models can be loaded via PyTorch Hub:

import torch

# DINOv2
dinov2_vits14_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14_lc')
dinov2_vitb14_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14_lc')
dinov2_vitl14_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14_lc')
dinov2_vitg14_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14_lc')

# DINOv2 with registers
dinov2_vits14_reg_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14_reg_lc')
dinov2_vitb14_reg_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14_reg_lc')
dinov2_vitl14_reg_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14_reg_lc')
dinov2_vitg14_reg_lc = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14_reg_lc')

Pretrained heads - Depth estimation

backbone download head
NYUd KITTI
ViT-S/14 distilled linear (1 layer, 4 layers), DPT linear (1 layer, 4 layers), DPT
ViT-B/14 distilled linear (1 layer, 4 layers), DPT linear (1 layer, 4 layers), DPT
ViT-L/14 distilled linear (1 layer, 4 layers), DPT linear (1 layer, 4 layers), DPT
ViT-g/14 linear (1 layer, 4 layers), DPT linear (1 layer, 4 layers), DPT

Pretrained heads - Semantic segmentation

backbone download model download head
ADE20K ADE20K VOC2012
ViT-S/14 distilled linear, multi-scale linear, multi-scale
ViT-B/14 distilled linear, multi-scale linear, multi-scale
ViT-L/14 distilled linear, multi-scale linear, multi-scale
ViT-g/14 Mask2Former linear, multi-scale linear, multi-scale

Pretrained heads - Zero-shot tasks with dino.txt

backbone with
registers
download
ViT-L/14 distilled :white_check_mark: vision head, text model, vocabulary, vocabulary license

The (full) dino.txt model can be loaded via PyTorch Hub:

import torch

# DINOv2
dinov2_vitl14_reg4_dinotxt_tet1280d20h24l = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14_reg4_dinotxt_tet1280d20h24l')

Installation

The training and evaluation code requires PyTorch 2.0 and xFormers 0.0.18 as well as a number of other 3rd party packages. Note that the code has only been tested with the specified versions and also expects a Linux environment. To setup all the required dependencies for training and evaluation, please follow the instructions below:

conda (Recommended) - Clone the repository and then create and activate a dinov2 conda environment using the provided environment definition:

conda env create -f conda.yaml
conda activate dinov2

pip - Clone the repository and then use the provided requirements.txt to install the dependencies:

pip install -r requirements.txt

For dense tasks (depth estimation and semantic segmentation), there are additional dependencies (specific versions of mmcv and mmsegmentation) which are captured in the extras dependency specifications:

conda (Recommended):

conda env create -f conda-extras.yaml
conda activate dinov2-extras

pip:

pip install -r requirements.txt -r requirements-extras.txt

Data preparation

ImageNet-1k

The root directory of the dataset should hold the following contents:

The provided dataset implementation expects a few additional metadata files to be present under the extra directory:

These metadata files can be generated (once) with the following lines of Python code:

from dinov2.data.datasets import ImageNet

for split in ImageNet.Split:
    dataset = ImageNet(split=split, root="<ROOT>", extra="<EXTRA>")
    dataset.dump_extra()

Note that the root and extra directories do not have to be distinct directories.

ImageNet-22k

Please adapt the dataset class to match your local setup.


:warning: To execute the commands provided in the next sections for training and evaluation, the dinov2 package should be included in the Python module search path, i.e. simply prefix the command to run with PYTHONPATH=..

Training

Fast setup: training DINOv2 ViT-L/16 on ImageNet-1k

Run DINOv2 training on 4 A100-80GB nodes (32 GPUs) in a SLURM cluster environment with submitit:

python dinov2/run/train/train.py \
    --nodes 4 \
    --config-file dinov2/configs/train/vitl16_short.yaml \
    --output-dir <PATH/TO/OUTPUT/DIR> \
    train.dataset_path=ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

Training time is approximately 1 day and the resulting checkpoint should reach 81.6% on k-NN eval and 82.9% on linear eval.

The training code saves the weights of the teacher in the eval folder every 12500 iterations for evaluation.

Long setup: training DINOv2 ViT-L/14 on ImageNet-22k

Run DINOv2 training on 12 A100-80GB nodes (96 GPUs) in a SLURM cluster environment with submitit:

python dinov2/run/train/train.py \
    --nodes 12 \
    --config-file dinov2/configs/train/vitl14.yaml \
    --output-dir <PATH/TO/OUTPUT/DIR> \
    train.dataset_path=ImageNet22k:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

Training time is approximately 3.3 days and the resulting checkpoint should reach 82.0% on k-NN eval and 84.5% on linear eval.

The training code saves the weights of the teacher in the eval folder every 12500 iterations for evaluation.

Evaluation

The training code regularly saves the teacher weights. In order to evaluate the model, run the following evaluation on a single node:

k-NN classification on ImageNet-1k

python dinov2/run/eval/knn.py \
    --config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
    --pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
    --output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/knn \
    --train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
    --val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

Logistic regression classification on ImageNet-1k

python dinov2/run/eval/log_regression.py \
    --config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
    --pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
    --output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/logreg \
    --train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
    --val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

Linear classification with data augmentation on ImageNet-1k

python dinov2/run/eval/linear.py \
    --config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
    --pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
    --output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/linear \
    --train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
    --val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

We release the weights from evaluating the different models:

model with
registers
ImageNet
top-1
linear evaluation
ViT-S/14 distilled :x: 81.1% linear head weights
ViT-S/14 distilled :white_check_mark: 80.8% linear head weights
ViT-B/14 distilled :x: 84.5% linear head weights
ViT-B/14 distilled :white_check_mark: 84.4% linear head weights
ViT-L/14 distilled :x: 86.3% linear head weights
ViT-L/14 distilled :white_check_mark: 86.5% linear head weights
ViT-g/14 :x: 86.5% linear head weights
ViT-g/14 :white_check_mark: 87.0% linear head weights

The performance of the provided pretrained model weights can be evaluated as follows on ImageNet-1k:

python dinov2/run/eval/linear.py \
    --config-file dinov2/configs/eval/vitg14_pretrain.yaml \
    --pretrained-weights https://dl.fbaipublicfiles.com/dinov2/dinov2_vitg14/dinov2_vitg14_pretrain.pth \
    --train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
    --val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>

Notebooks

A few notebooks are provided to help the community leverage the models and code:

License

DINOv2 code and model weights are released under the Apache License 2.0. See LICENSE for additional details.

Contributing

See contributing and the code of conduct.

Citing DINOv2

If you find this repository useful, please consider giving a star :star: and citation :t-rex::

@misc{oquab2023dinov2,
  title={DINOv2: Learning Robust Visual Features without Supervision},
  author={Oquab, Maxime and Darcet, Timothée and Moutakanni, Theo and Vo, Huy V. and Szafraniec, Marc and Khalidov, Vasil and Fernandez, Pierre and Haziza, Daniel and Massa, Francisco and El-Nouby, Alaaeldin and Howes, Russell and Huang, Po-Yao and Xu, Hu and Sharma, Vasu and Li, Shang-Wen and Galuba, Wojciech and Rabbat, Mike and Assran, Mido and Ballas, Nicolas and Synnaeve, Gabriel and Misra, Ishan and Jegou, Herve and Mairal, Julien and Labatut, Patrick and Joulin, Armand and Bojanowski, Piotr},
  journal={arXiv:2304.07193},
  year={2023}
}
@misc{darcet2023vitneedreg,
  title={Vision Transformers Need Registers},
  author={Darcet, Timothée and Oquab, Maxime and Mairal, Julien and Bojanowski, Piotr},
  journal={arXiv:2309.16588},
  year={2023}
}
@misc{jose2024dinov2meetstextunified,
  title={DINOv2 Meets Text: A Unified Framework for Image- and Pixel-Level Vision-Language Alignment},
  author={Cijo Jose and Théo Moutakanni and Dahyun Kang and Federico Baldassarre and Timothée Darcet and Hu Xu and Daniel Li and Marc Szafraniec and Michaël Ramamonjisoa and Maxime Oquab and Oriane Siméoni and Huy V. Vo and Patrick Labatut and Piotr Bojanowski},
  journal={arXiv:2412.16334},
  year={2024}
}

DINOv2 for Biology

The contents of the source code contained in the cell_dino folders, including the code and model weights, are intended for research use only. It is not for use in medical procedures, including any diagnostics, treatment, or curative applications. Do not use this model for any clinical purpose or as a substitute for professional medical judgement.

Scaling Channel-Adaptive Self-Supervised Learning (Channel-Adaptive DINO)

[Paper )] [BibTeX]

Alice V. De Lorenci, Seungeun Yi, Théo Moutakanni, Piotr Bojanowski, Camille Couprie, Juan C. Caicedo, Wolfgang M. Pernice,

with special thanks to Elouan Gardes for his contributions to the codebase.

README

Cell-DINO: Self-Supervised Image-based Embeddings for Cell Fluorescent Microscopy (Cell-DINO)

Théo Moutakanni, Camille Couprie, Seungeun Yi, Elouan Gardes, Piotr Bojanowski, Hugo Touvron, Michael Doron, Zitong S. Chen, Nikita Moshkov, Mathilde Caron, Armand Joulin, Wolfgang M. Pernice, Juan C. Caicedo

to appear soon.

README

Pretrained models

ℹ️ Please follow the link provided below to get access to all the model weights: once accepted, an e-mail will be sent with the complete list of URLs pointing to all the available model weights. These URLs can then be used to either:

⚠️ Please use wget instead of a web browser to download the weights.

Download link: https://ai.meta.com/resources/models-and-libraries/cell-dino-downloads/

import torch

REPO_DIR = <PATH/TO/A/LOCAL/DIRECTORY/WHERE/THE/DINOV2/REPO/WAS/CLONED>

# You can either download the URL link first, then load:
cell_dino_vits8 = torch.hub.load(REPO_DIR, 'cell_dino_cp_vits8', source='local', pretrained_path=<CHECKPOINT/PATH>)
# Or directly download the URL while using `torch.hub.load`:
cell_dino_vits8 = torch.hub.load(REPO_DIR, 'cell_dino_cp_vits8', source='local', pretrained_url=<CHECKPOINT/URL>)

# Similarily for other models:
cell_dino_vitl16_hpa_sc = torch.hub.load(REPO_DIR, 'cell_dino_hpa_vitl16', source='local', pretrained_path=<CHECKPOINT/PATH>)
cell_dino_vitl16_hpa_fov = torch.hub.load(REPO_DIR, 'cell_dino_hpa_vitl16', source='local', pretrained_path=<CHECKPOINT/PATH>)
channel_adaptive_dino_vitl16 = torch.hub.load(REPO_DIR, 'channel_adaptive_dino_vitl16', source='local', pretrained_path=<CHECKPOINT/PATH>)
cell_dino_vitl14 = torch.hub.load(REPO_DIR, 'cell_dino_hpa_vitl14', source='local', pretrained_path=<CHECKPOINT/PATH>)

Licenses

Code is released under the CC BY NC License. See LICENSE_CELL_DINO_CODE for additional details. Model weights are released under the FAIR Noncommercial Research License. See LICENSE_CELL_DINO_CODE_WEIGHTS for additional details.

dinov2 (facebookresearch/dinov2)

Searching Database…

13.4K ·GitHub
这是什么?DINOv2 是 Meta AI 旗下 FAIR 团队推出的自监督视觉特征学习方法,以 PyTorch 代码和预训练模型的形式发布。它的特征可以直接接入简单到只有线性层的分类器,适用于多种视觉任务,无需微调。
这是一个可以通过 pip 安装的库,还是一组预训练模型?它是研究代码加预训练权重,不是 pip 包:你需要克隆仓库并配置它的 conda 环境,而模型本身则通过 PyTorch Hub 用 torch.hub.load 加载。
怎么让预训练模型跑起来?安装 PyTorch(加载权重的唯一依赖,强烈建议装 CUDA 版),然后调用 torch.hub.load 获取一个骨干网络,或已发布的分类、深度估计、语义分割头。完整的训练和评估则需要用仓库的 conda 环境。
如果我想自己训练或评估,需要装哪些东西?需要 PyTorch 2.0 和 xFormers 0.0.18 以及其他第三方包,通过提供的 conda 环境或 requirements.txt 安装。深度估计、语义分割这类密集任务还需要特定版本的 mmcv 和 mmsegmentation。
代码能在 Windows 或 macOS 上跑吗,还是只支持 Linux?训练和评估代码假定的是 Linux 环境,而且只在锁定的依赖版本下测试过。用 torch.hub 加载权重只需要 PyTorch,但仓库提供的脚本都是面向 Linux 的。
这里面的东西都能商用吗?核心的 DINOv2 代码和权重采用 Apache 2.0 协议,但较新的扩展仅限研究用途:XRay-DINO 和 Cell-DINO 的权重受 FAIR Noncommercial Research License 约束,Cell-DINO 的代码则是 CC BY-NC。
训练其中一个模型需要多强的 GPU?得用数据中心级别的算力:在 ImageNet-1k 上训练 ViT-L/16 需要约一天,用 4 个 A100-80GB 节点(32 块 GPU);在 SLURM 集群上训练 ImageNet-22k 则需要 12 个节点(96 块 GPU),耗时约 3.3 天。
不自己训练的话,我能用 DINOv2 的特征做什么?能做的很多:可以在 ImageNet 上用 k-NN 或线性探针评估这些特征,也可以加载已发布的深度估计和语义分割头。dino.txt 模型还支持文本与图像、像素级的零样本对齐,而且这些特征的设计目标就是不做微调也能跨领域迁移。
这些模型有多大,准确率怎么样?共有四种 ViT 规格:从 ViT-S/14(2100 万参数)到 ViT-g/14(11 亿参数),每种还有“带 registers”的版本,这一变体的效果略有提升。ImageNet 线性探针 top-1 准确率从 ViT-S 的约 81% 到带 registers 的 ViT-g 的 87.1%。
这里的“无监督”是什么意思——它是怎么训练出来的?这些骨干网络在 1.42 亿张图像上预训练,完全没有使用任何标签或标注:网络通过自监督的方式学习视觉特征。正因如此,只需在顶部加一个线性层,它们就能迁移到新任务上。
DINOv2 还是 Meta 目前主打的模型系列吗?这个仓库在 2025 年下半年之前还在持续扩展,陆续加入了带 registers 的骨干网络、dino.txt、XRay-DINO、Cell-DINO 和 Channel-Adaptive DINO,不过作者建议读者关注 DINOv3——延续这一方向的更新的工作。
Size
2.9 MB
Stars
13.4K
Updated
CommitJun 03
Age
3y
Commits · 90 Days