Skip to main content

DINO-based perceptual loss for image reconstruction and generation

Project description

DINO Perceptual Loss

A drop-in replacement for LPIPS using DINOv2/v3 features. Achieves comparable perceptual quality to VGG-based LPIPS while using modern self-supervised features—no human perceptual judgments required.

Read the full documentation and benchmarks

Installation

pip install dino-perceptual

Quick Start

import torch
from dino_perceptual import DINOPerceptual

# Initialize loss function (uses DINOv3 by default)
loss_fn = DINOPerceptual(model_size="B").cuda().bfloat16().eval()
loss_fn = torch.compile(loss_fn, fullgraph=True)

# Compute perceptual loss between two images
# Images should be tensors in [-1, 1] range with shape (B, 3, H, W)
loss = loss_fn(predicted, target).mean()

Usage in Autoencoder Training

import torch
import torch.nn as nn
from dino_perceptual import DINOPerceptual

autoencoder = MyAutoencoder().cuda().bfloat16()
perceptual_loss = DINOPerceptual(model_size="B").cuda().bfloat16().eval()
perceptual_loss = torch.compile(perceptual_loss, fullgraph=True)
optimizer = torch.optim.Adam(autoencoder.parameters(), lr=1e-4)

for images in dataloader:
    images = images.cuda().bfloat16()
    reconstructed = autoencoder(images)

    l1_loss = nn.functional.l1_loss(reconstructed, images)
    dino_loss = perceptual_loss(reconstructed, images).mean()

    # DINO weight ~250-1000 works well
    total_loss = l1_loss + 250.0 * dino_loss

    optimizer.zero_grad()
    total_loss.backward()
    optimizer.step()

HuggingFace Authentication

DINOv2/v3 models require accepting the license on HuggingFace:

  1. Accept the license at DINOv2 or DINOv3
  2. Create a token at https://huggingface.co/settings/tokens
  3. Set HF_TOKEN environment variable or run huggingface-cli login

API Reference

DINOPerceptual(
    model_size: str = "B",   # "S", "B", "L", "H", or "G"
    version: str = "v3",     # "v2" or "v3"
    target_size: int = 512,  # Resize images before computing features
    layers: str = "all",     # Which transformer layers to use
)

For feature extraction (e.g., computing FDD):

from dino_perceptual import DINOModel

extractor = DINOModel(model_size="B").cuda().bfloat16().eval()
features, cls_token = extractor(images)  # features: (B, feature_dim)

License

MIT

Citation

@software{dino_perceptual,
  title={DINO Perceptual Loss},
  author={Hansen-Estruch, Philippe and Chen, Jiahui and Ramanujan, Vivek and Zohar, Orr and Ping, Yan and Sinha, Animesh and Georgopoulos, Markos and Schoenfeld, Edgar and Hou, Ji and Juefei-Xu, Felix and Vishwanath, Sriram and Thabet, Ali},
  year={2025},
  url={https://github.com/Na-VAE/dino-perceptual}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dino_perceptual-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dino_perceptual-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file dino_perceptual-0.1.0.tar.gz.

File metadata

  • Download URL: dino_perceptual-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for dino_perceptual-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f05a063c1bb91b8edd5663672207870325c99754d30dee3e31ff5d7b59541677
MD5 c1cef59587ca98c0353c1395dcb10a9e
BLAKE2b-256 a0a9ca1cf83df1045987fe511acc134035df887fc814966e2d61ce65782890cb

See more details on using hashes here.

File details

Details for the file dino_perceptual-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dino_perceptual-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5df5ce57c96fb2457d565fcde9947482c98eb9d56865e513eb1adfe22fd37df
MD5 d3bf4d2d05cf04fc96341af46cdc63d0
BLAKE2b-256 7ec12049ac4b81809f88cda63f4a4e10170290fd5df7d7cc23da8aead7b6b6ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page