peal.global_utils.DINOEvaluator

class peal.global_utils.DINOEvaluator[source]

Bases: object

Distribution metrics computed in a DINOv2 feature space.

Instead of the usual Inception features, the CLS embedding of a DINOv2 model is used, which is cheap to run locally and works on non-natural images as well. After fit() has stored mean and covariance of a reference (real) distribution, the evaluator provides a FID-style score for a set of generated images, a per-sample Mahalanobis distance that says how far one counterfactual left the data manifold, and a LPIPS-style cosine distance between two batches.

Parameters:
  • model_name (str, optional) – Hugging Face id of the backbone; the bare model without a classification head is used.

  • device (str, optional) – Falls back to "cpu" when no GPU is available.

Variables:
  • sigma_inv_real (mu_real, sigma_real,) – Statistics of the reference distribution, filled by fit().

  • fitted (bool) – Whether those statistics are available.

Methods

compute_fid

Calculates FID score between the fitted real distribution and the provided fake inputs.

compute_lpips

compute_mahalanobis

Calculates Mahalanobis distance for specific samples against the real distribution.

fit

Step 1: Precompute statistics for the Real (Training) distribution.

__init__(model_name='facebook/dinov2-small', device='cuda')[source]

Load the DINOv2 backbone and its processor in eval mode.

fit(real_dataloader, save_path=None)[source]

Step 1: Precompute statistics for the Real (Training) distribution. Run this once at the beginning. The fitted state is written to save_path only when one is given; the old default, a relative dino_evaluation_state.pt, landed in the working directory (the read-only checkout in the web demo container).

compute_fid(fake_inputs)[source]

Calculates FID score between the fitted real distribution and the provided fake inputs.

compute_mahalanobis(single_image_or_batch)[source]

Calculates Mahalanobis distance for specific samples against the real distribution. Returns a tensor of distances (one per image).

compute_lpips(x1, x2)

Calculates LPIPS perceptual distance using DINOv2 feature representations between two image batches x1 and x2.