Download inference.py from thelamapi/Next-Shift: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/thelamapi/Next-Shift/resolve/main/inference.py
- Command line
-
hf download hf://thelamapi/Next-Shift/inference.py
-
curl -L -o inference.py https://huggingface.co/thelamapi/Next-Shift/resolve/main/inference.py
1.81 kB
| """Next Shift inference (NumPy only): adapt to a few labeled target images and predict. | |
| Usage: | |
| python inference.py --selftest | |
| from inference import load, predict, adapt | |
| """ | |
| 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 shift_core as sc # noqa: E402 | |
| from nlrt import load_model # noqa: E402 | |
| def load(path=os.path.join(HERE, "model.json")): | |
| spec = load_model(path) | |
| t = spec["tensors"] | |
| spec["top"] = {"w1": t["w1"], "b1": t["b1"], "w2": t["w2"], "b2": t["b2"]} | |
| return spec | |
| def adapt(spec, images, labels): | |
| """images: (N, 16, 16) in [0, 1] from the target environment; returns adapted top-layer weights.""" | |
| hy = spec["meta"]["hyper"] | |
| F = sc.conv_features(spec["nets"]["conv_target"], images) | |
| return sc.finetune_top(spec["top"], F, np.asarray(labels), hy["ft_steps"], hy["ft_lr"]) | |
| def predict(spec, images, top=None): | |
| """Class probabilities (N, 10) for target-environment images (16x16, values in [0, 1]).""" | |
| F = sc.conv_features(spec["nets"]["conv_target"], images) | |
| return sc.softmax(sc.mlp_logits(top or spec["top"], F)[0]) | |
| def _selftest(): | |
| spec = load() | |
| with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f: | |
| t = json.load(f) | |
| imgs = np.asarray(t["images"], dtype=float) / 255.0 | |
| top = adapt(spec, imgs[:len(t["labels"])], t["labels"]) | |
| P = predict(spec, imgs, top) | |
| diff = float(np.abs(P - np.asarray(t["expected_proba"])).max()) | |
| ok = diff < 1e-6 | |
| print(json.dumps({"passed": ok, "max_abs_diff": diff, "n_images": len(imgs), "n_labels": len(t["labels"])})) | |
| return ok | |
| if __name__ == "__main__": | |
| if "--selftest" in sys.argv: | |
| sys.exit(0 if _selftest() else 1) | |
| print(__doc__) | |