File size: 1,812 Bytes
8fd3398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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__)