-
Notifications
You must be signed in to change notification settings - Fork 320
Add DINOv2 to Docs with Example #1849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yutong-xiang-97
merged 14 commits into
master
from
yutong-trn-583-lightlyssl-add-dinov2-to-docs-with-example
Jun 30, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba624d2
init
yutong-xiang-97 3cc2182
update lightning example
yutong-xiang-97 ebdbee6
finish dist example
yutong-xiang-97 6eb5b36
auto-gen notebooks
yutong-xiang-97 e75a7e0
pytorch example
yutong-xiang-97 5a54e42
fix params update in lightning
yutong-xiang-97 a629d6d
add docs
yutong-xiang-97 c70dc7d
fix paths
yutong-xiang-97 4121c93
improve docs wip
yutong-xiang-97 39577ab
fix script cont
yutong-xiang-97 14a8cef
update epochs
yutong-xiang-97 ab1be7b
fix comment style issues
yutong-xiang-97 88a2ecf
fix ci
yutong-xiang-97 7c38043
update
yutong-xiang-97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| .. _dinov2: | ||
|
|
||
| DINOv2 | ||
| ====== | ||
|
|
||
| DINOv2 (DIstillation with NO labels v2) [0]_ is an advanced self-supervised learning framework developed by Meta AI for robust visual representation learning without labeled data. Extending the original DINO [1]_ approach, DINOv2 trains a student network to match outputs from a momentum-averaged teacher network. By leveraging self-distillation objectives at both image and patch levels, enhances both global and local feature learning. Combined with other various innovations in both the training recipe and efficient training implementation, DINOv2 exhibits state-of-the-art performance across various computer vision tasks, including classification, segmentation, and depth estimation, without the necessity for task-specific fine-tuning. | ||
|
|
||
| Key Components | ||
| -------------- | ||
|
|
||
| - **Multi-level Objectives**: DINOv2 employs DINO loss for the image-level objective and iBOT [2]_ loss for patch-level objective. This multi-level approach enhances both global and local feature representations, significantly improving performance on dense prediction tasks like segmentation and depth estimation. | ||
| - **KoLeo Regularizer**: DINOv2 introduces the KoLeo regularizer [3]_, which promotes uniform spreading of features within a batch, significantly enhancing the quality of nearest-neighbor retrieval tasks without negatively affecting performance on dense downstream tasks. | ||
|
|
||
| Good to Know | ||
| ------------ | ||
|
|
||
| - **SOTA out-of-the-box**: DINOv2 currently represents the state-of-the-art (SOTA) among self-supervised learning (SSL) methods in computer vision, outperforming existing frameworks in various benchmarks. | ||
| - **Relation to other SSL methods**: DINOv2 can be seen as a combination of DINO and iBOT losses with the centering of SwAV [4]_. | ||
|
|
||
| Reference: | ||
|
|
||
| .. [0] `DINOv2: Learning Robust Visual Features without Supervision, 2023 <https://arxiv.org/abs/2304.07193>`_ | ||
| .. [1] `Emerging Properties in Self-Supervised Vision Transformers, 2021 <https://arxiv.org/abs/2104.14294>`_ | ||
| .. [2] `iBOT: Image BERT Pre-Training with Online Tokenizer, 2021 <https://arxiv.org/abs/2111.07832>`_ | ||
| .. [3] `Spreading vectors for similarity search, 2018 <https://arxiv.org/abs/1806.03198>`_ | ||
| .. [4] `Unsupervised Learning of Visual Features by Contrasting Cluster Assignments, 2020 <https://arxiv.org/abs/2006.09882>`_ | ||
|
|
||
|
|
||
| .. tabs:: | ||
| .. tab:: PyTorch | ||
|
|
||
| .. image:: https://img.shields.io/badge/Open%20in%20Colab-blue?logo=googlecolab&label=%20&labelColor=5c5c5c | ||
| :target: https://colab.research.google.com/github/lightly-ai/lightly/blob/master/examples/notebooks/pytorch/dinov2.ipynb | ||
|
|
||
| This example can be run from the command line with:: | ||
|
|
||
| python lightly/examples/pytorch/dinov2.py | ||
|
|
||
| .. literalinclude:: ../../../examples/pytorch/dinov2.py | ||
|
|
||
| .. tab:: Lightning | ||
|
|
||
| .. image:: https://img.shields.io/badge/Open%20in%20Colab-blue?logo=googlecolab&label=%20&labelColor=5c5c5c | ||
| :target: https://colab.research.google.com/github/lightly-ai/lightly/blob/master/examples/notebooks/pytorch_lightning/dinov2.ipynb | ||
|
|
||
| This example can be run from the command line with:: | ||
|
|
||
| python lightly/examples/pytorch_lightning/dinov2.py | ||
|
|
||
| .. literalinclude:: ../../../examples/pytorch_lightning/dinov2.py | ||
|
|
||
| .. tab:: Lightning Distributed | ||
|
|
||
| .. image:: https://img.shields.io/badge/Open%20in%20Colab-blue?logo=googlecolab&label=%20&labelColor=5c5c5c | ||
| :target: https://colab.research.google.com/github/lightly-ai/lightly/blob/master/examples/notebooks/pytorch_lightning_distributed/dinov2.ipynb | ||
|
|
||
| This example runs on multiple gpus using Distributed Data Parallel (DDP) | ||
| training with Pytorch Lightning. At least one GPU must be available on | ||
| the system. The example can be run from the command line with:: | ||
|
|
||
| python lightly/examples/pytorch_lightning_distributed/dinov2.py | ||
|
|
||
| The model differs in the following ways from the non-distributed | ||
| implementation: | ||
|
|
||
| - Distributed Data Parallel is enabled | ||
| - Synchronized Batch Norm is used in place of standard Batch Norm | ||
| - Distributed Sampling is used in the dataloader | ||
|
|
||
| Note that Synchronized Batch Norm is optional and the model can also be | ||
| trained without it. Without Synchronized Batch Norm the batch norm for | ||
| each GPU is only calculated based on the features on that specific GPU. | ||
| Distributed Sampling makes sure that each distributed process sees only | ||
| a subset of the data. | ||
yutong-xiang-97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| .. literalinclude:: ../../../examples/pytorch_lightning_distributed/dinov2.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.