File size: 3,392 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
#!/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()