#!/usr/bin/env python3 """Run end-to-end local DINOv3 image inference.""" from __future__ import annotations import argparse import hashlib import json from pathlib import Path import joblib import numpy as np import torch from PIL import Image from transformers import AutoImageProcessor, AutoModel from extract_features import four_views from train_probe import apply_thresholds, predict def file_sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def extract(paths: list[Path], model_path: Path, batch_size: int, threads: int) -> np.ndarray: torch.set_num_threads(threads) processor = AutoImageProcessor.from_pretrained(model_path, local_files_only=True) model = AutoModel.from_pretrained(model_path, local_files_only=True).eval() register_tokens = int(model.config.num_register_tokens) result = [] for start in range(0, len(paths), batch_size): batch = paths[start:start + batch_size] views = [] for path in batch: with Image.open(path) as image: views.extend(four_views(image)) inputs = processor( images=views, return_tensors="pt", do_resize=False, do_center_crop=False ) with torch.inference_mode(): hidden = model(**inputs).last_hidden_state vectors = torch.cat( (hidden[:, 0], hidden[:, 1 + register_tokens:].mean(dim=1)), dim=1 ).reshape(len(batch), 4, -1) vectors = torch.cat((vectors.mean(dim=1), vectors.amax(dim=1)), dim=1) vectors = torch.nn.functional.normalize(vectors, dim=1) result.append(vectors.cpu().numpy().astype(np.float32)) return np.concatenate(result) def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("images", nargs="+", type=Path) parser.add_argument("--classifier", type=Path, required=True) parser.add_argument("--model", type=Path) parser.add_argument("--batch-size", type=int, default=2) parser.add_argument("--threads", type=int, default=4) args = parser.parse_args() artifact = joblib.load(args.classifier) model_path = args.model or Path(str(artifact["feature_model_path"])) for relative, expected_sha256 in artifact["feature_model_file_sha256"].items(): model_file = model_path / relative if not model_file.is_file(): raise FileNotFoundError(f"missing backbone file: {model_file}") if file_sha256(model_file) != expected_sha256: raise ValueError(f"backbone file does not match classifier provenance: {relative}") features = extract(args.images, model_path, args.batch_size, args.threads) if features.shape[1] != int(artifact["feature_dimension"]): raise ValueError("extracted feature dimension does not match classifier") probabilities = predict(artifact, features) predictions = apply_thresholds(probabilities, np.asarray(artifact["thresholds"])) classes = [str(value) for value in artifact["classes"]] for index, path in enumerate(args.images): print(json.dumps({ "file": str(path), "predicted_characters": [ name for name, present in zip(classes, predictions[index]) if present ], "probabilities": { name: round(float(value), 6) for name, value in zip(classes, probabilities[index]) }, }, ensure_ascii=False)) if __name__ == "__main__": main()