47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
"""KNP input files for the one-time fresh-test evaluation (600 images, test 0700–0999 per class).
|
||
|
||
D: learned features (K 512 model from make_knp_features.py, same gain), top-256 per image; training part =
|
||
the first 18000 rows of kmeans_top256_validation.bin (9000 images + mirror copies).
|
||
E: v2 sparse HOG (cats-dogs/features.py, descriptor(sparse=True)); training part = the first 4000 rows of
|
||
features-v2/sparse_hog_validation.bin (v2 training set).
|
||
|
||
usage: ~/anvenv/bin/python prepare_fresh_knp.py
|
||
"""
|
||
import shutil
|
||
import sys
|
||
|
||
import numpy as np
|
||
|
||
sys.path.insert(0, "/mnt/c/knp/objects")
|
||
sys.path.insert(0, "/mnt/c/knp/cats-dogs")
|
||
import kmeans_features as kf # noqa: E402
|
||
from features import descriptor # noqa: E402
|
||
|
||
root = "/home/int/run/cats-dogs"
|
||
fresh = np.load(f"{root}/data-v3/fresh_test.npz")
|
||
labels = "\n".join(map(str, fresh["labels"])) + "\n"
|
||
|
||
|
||
def write(path, train_bin, train_target, n_train, width, test):
|
||
x = np.fromfile(train_bin, np.uint8).reshape(-1, width)[:n_train]
|
||
y = open(train_target).read().split()[:n_train]
|
||
np.concatenate([x, test]).tofile(path + ".bin")
|
||
open(path + ".target", "w").write("\n".join(y) + "\n" + labels)
|
||
print(path, n_train, "+", len(test))
|
||
|
||
|
||
# D
|
||
kf.CONTRAST_DIVIDE = False
|
||
m = dict(np.load(f"{root}/features-v3/kmeans_model.npz"))
|
||
f = kf.encode(fresh["rgb"], m, alpha=0.25)
|
||
q = np.clip(np.rint(f / m["gain"] * 255), 0, 255).astype(np.uint8)
|
||
keep = np.argpartition(q, -256, axis=1)[:, -256:]
|
||
s = np.zeros_like(q)
|
||
np.put_along_axis(s, keep, np.take_along_axis(q, keep, 1), 1)
|
||
write(f"{root}/features-v3/kmeans_top256_fresh", f"{root}/features-v3/kmeans_top256_validation.bin",
|
||
f"{root}/features-v3/kmeans_top256_validation.target", 18000, 2048, s)
|
||
|
||
# E
|
||
h = descriptor(fresh["rgb"].reshape(-1, 3, 1024), sparse=True)
|
||
write(f"{root}/features-v2/sparse_hog_fresh", f"{root}/features-v2/sparse_hog_validation.bin",
|
||
f"{root}/features-v2/sparse_hog_validation.target", 4000, 2048, h)
|