File size: 3,627 Bytes
59839e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #!/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()
|