our_knp/altai-objects/score_features.py
2026-10-15 08:11:15 +03:00

30 lines
1.3 KiB
Python

"""Quick quality score of KNP-learned feature detectors (tuning aid only; final numbers come from KNP).
Takes float32 weights [K][216] written by `altai_objects features`, encodes vehicles train (first N) and validation
with the numpy retina prototype (centred weights, threshold 0, quadrant pooling) and reports ridge accuracy.
usage: ~/anvenv/bin/python score_features.py <weights.bin> <K> [--train 6000]
"""
import argparse
import sys
import numpy as np
sys.path.insert(0, "/mnt/c/knp/altai-objects")
import retina_proto as rp # noqa: E402
ap = argparse.ArgumentParser()
ap.add_argument("weights")
ap.add_argument("k", type=int)
ap.add_argument("--train", type=int, default=6000)
a = ap.parse_args()
rp.DOG = rp.gauss(0.5) - rp.gauss(1.5)
w = np.fromfile(a.weights, np.float32).reshape(a.k, -1)
d = "/home/int/run/cifar/vehicles"
tr, va = np.load(f"{d}/train.npz"), np.load(f"{d}/val.npz")
xt, yt = tr["rgb"][:a.train], tr["labels"][:a.train].astype(int)
xv, yv = va["rgb"], va["labels"].astype(int)
ft = rp.encode(rp.retina(xt), w, 0.0, 2, centre=True)
fv = rp.encode(rp.retina(xv), w, 0.0, 2, centre=True)
dead = int((ft.reshape(len(ft), 4, -1).sum((0, 1)) == 0).sum())
print(f"{a.weights}: ridge validation " + " | ".join(
f"lam {lam}: {rp.ridge_acc(ft, yt, fv, yv, lam, 4):.1f}%" for lam in (100, 1000)) + f"; silent detectors {dead}/{a.k}")