File size: 1,376 Bytes
76eb7b3 | 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 | """Apply a trained residual readout to v12 leave-one-manufacturer-out slab features."""
import argparse
from pathlib import Path
import numpy as np
def predict(head, features, view_stride):
strides = head["view_strides"].tolist()
if view_stride not in strides:
raise ValueError(f"view stride must be one of {strides}")
x = np.asarray(features, dtype=np.float32)
if x.ndim != 2 or x.shape[1] != 512 or not len(x) or not np.isfinite(x).all():
raise ValueError("expected finite slab features with shape (slabs, 512)")
i = strides.index(view_stride)
return x + head["bias"][i] + (x - head["input_mean"][i]) @ head["matrix"][i]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("head", type=Path)
parser.add_argument(
"features", type=Path, help="Numpy .npy matrix from the matching base encoder"
)
parser.add_argument("output", type=Path, help="output .npy file")
parser.add_argument("--view-stride", type=int, required=True, choices=[1, 2, 4, 8])
args = parser.parse_args()
with np.load(args.head, allow_pickle=False) as head:
result = predict(
head, np.load(args.features, allow_pickle=False), args.view_stride
)
with args.output.open("wb") as stream:
np.save(stream, result)
if __name__ == "__main__":
main()
|