File size: 1,887 Bytes
82dfc77 | 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 | """Next Match inference (NumPy only): rank catalog items for a reference image plus a text edit.
Usage:
python inference.py --selftest
from inference import load, search
ids = search(spec, ref_image_uint8_hwc, "bunun kırmızı olanı")
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import numpy as np # noqa: E402
import match_core as mc # noqa: E402
from nlrt import load_model # noqa: E402
def load(path=os.path.join(HERE, "model.json")):
return load_model(path)
def to_chw(img_uint8_hwc):
return np.asarray(img_uint8_hwc, dtype=np.float64).transpose(2, 0, 1) / 255.0
def search(spec, ref_image, text, mode="combined", top=10, keys=None):
"""ref_image: (48, 48, 3) uint8. Returns catalog indices (into the released catalog) best first."""
q = mc.encode_query(spec, to_chw(ref_image), text, mode)
name = {"combined": "gallery_keys", "image": "gallery_keys_img_only", "text": "gallery_keys_txt_only"}[mode]
K = spec["tensors"][name] if keys is None else keys
s = K @ q
return sorted(range(len(s)), key=lambda i: (-s[i], i))[:top]
def _selftest():
spec = load()
with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f:
cases = json.load(f)["cases"]
bad = []
for c in cases:
img = np.asarray(c["pixels"], dtype=np.uint8).reshape(3, 48, 48).transpose(1, 2, 0)
got = {m: search(spec, img, c["text"], m, 5) for m in ("combined", "image", "text")}
if got != c["expected"]:
bad.append({"text": c["text"], "got": got, "expected": c["expected"]})
print(json.dumps({"passed": not bad, "cases": len(cases), "mismatches": bad[:2]}, ensure_ascii=False))
return not bad
if __name__ == "__main__":
if "--selftest" in sys.argv:
sys.exit(0 if _selftest() else 1)
print(__doc__)
|