Download inference.py from thelamapi/Next-Signal: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/thelamapi/Next-Signal/resolve/main/inference.py
- Command line
-
hf download hf://thelamapi/Next-Signal/inference.py
-
curl -L -o inference.py https://huggingface.co/thelamapi/Next-Signal/resolve/main/inference.py
1.52 kB
| """Next Signal inference (NumPy only): embed inertial windows and classify them with the released probe. | |
| Usage: | |
| python inference.py --selftest | |
| from inference import load, embed, classify | |
| """ | |
| 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 signal_core as sg # noqa: E402 | |
| from nlrt import load_model # noqa: E402 | |
| def load(path=os.path.join(HERE, "model.json")): | |
| return load_model(path) | |
| def embed(spec, windows): | |
| """windows: (N, 9, 128) raw UCI-HAR style inertial windows -> (N, 64) self-supervised embeddings.""" | |
| m = spec["meta"] | |
| return sg.encode(spec["nets"]["encoder"], sg.normalise(windows, m["mean"], m["std"])) | |
| def classify(spec, windows, budget=50): | |
| """Activity probabilities (N, 6) from the frozen encoder + linear probe trained with `budget` labels per class.""" | |
| return sg.probe_proba(spec["meta"]["probes"]["ssl"][str(budget)], embed(spec, windows)) | |
| def _selftest(): | |
| spec = load() | |
| with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f: | |
| t = json.load(f) | |
| P = classify(spec, np.asarray(t["windows"]), t["budget"]) | |
| 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_windows": len(t["windows"])})) | |
| return ok | |
| if __name__ == "__main__": | |
| if "--selftest" in sys.argv: | |
| sys.exit(0 if _selftest() else 1) | |
| print(__doc__) | |