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.1.tar.gz (6.6 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.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dino_perceptual-0.1.1.tar.gz
  • Upload date:
  • Size: 6.6 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.1.tar.gz
Algorithm Hash digest
SHA256 917938758ac9d5bd149f158e6d3c13dd25973779b82437f829529d09cd9354e3
MD5 f1acfe1955a38489cf534b2c6e16254c
BLAKE2b-256 1732a798fcab655d83fb5de53b7314bfedb2faf95c321ee01c2d83905e5e0149

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dino_perceptual-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9f67a6c8a08c5a97541d43287182820f5c2baa9d1089a2d85b0f122c4bd31fd
MD5 a68e08dc2a2967ff19ae08062391a692
BLAKE2b-256 dfa154570f40f6206be50b4514cc1580c4c39bb8fb09d3deac1d76bd1fffa56b

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