#!/usr/bin/env python3 """Run end-to-end local inference with the structured v801 checkpoint.""" from __future__ import annotations import argparse 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 file_sha256, four_views from train_v801 import artifact_probabilities, structured_predictions def extract_descriptors( 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 patches = hidden[:, 1 + register_tokens :] descriptors = torch.stack( ( hidden[:, 0], patches.mean(dim=1), patches.amax(dim=1), patches.std(dim=1, unbiased=False), ), dim=1, ).reshape(len(batch), 4, 4, -1) result.append(descriptors.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) if artifact.get("version") != "v801": raise ValueError("the supplied classifier is not a v801 checkpoint") model_path = args.model or Path(str(artifact["feature_model_path"])) for relative, expected in artifact["feature_model_file_sha256"].items(): path = model_path / relative if not path.is_file(): raise FileNotFoundError(path) if file_sha256(path) != expected: raise ValueError(f"backbone provenance mismatch: {relative}") descriptors = extract_descriptors( args.images, model_path, args.batch_size, args.threads ) probabilities = artifact_probabilities(artifact, descriptors) predictions = structured_predictions(artifact, probabilities) classes = np.asarray(artifact["classes"]).astype(str) for index, path in enumerate(args.images): print(json.dumps({ "file": str(path), "model_version": "v801", "predicted_characters": classes[predictions[index].astype(bool)].tolist(), "probabilities": { name: round(float(value), 6) for name, value in zip(classes, probabilities[index]) }, }, ensure_ascii=False)) if __name__ == "__main__": main()