MEDA / predict.py
Dharini24's picture
Add MEDA heads for Merlin and CT-NEXUS, model card and loader
7ddbfcc verified
Raw History Blame Contribute Delete
1.44 kB
"""Score CT embeddings with the MEDA head of one finding.
python predict.py --encoder merlin --finding ascites --embeddings embeddings.npy
``embeddings.npy`` holds one row per CT study: 2048 columns for Merlin or 1120 for CT-NEXUS.
Prints the score and the positive/negative call for every study.
"""
import argparse
import joblib
import numpy as np
from huggingface_hub import hf_hub_download
REPO = "Dharini24/MEDA"
def load_head(encoder, finding, repo=REPO):
path = hf_hub_download(repo, f"models/{encoder}/{finding}.joblib")
return joblib.load(path)
def predict(head, embeddings):
scores = head["model"].predict_proba(np.asarray(embeddings, dtype=np.float32))[:, 1]
return scores, (scores >= head["threshold"]).astype(int)
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--encoder", required=True, choices=["merlin", "ctnexus"])
ap.add_argument("--finding", required=True)
ap.add_argument("--embeddings", required=True)
args = ap.parse_args()
head = load_head(args.encoder, args.finding)
scores, calls = predict(head, np.load(args.embeddings))
print(f"{args.finding} ({args.encoder}): {head['name']}, threshold {head['threshold']:.4f}")
for i, (s, c) in enumerate(zip(scores, calls)):
print(f"study {i}: score {s:.4f} -> {'positive' if c else 'negative'}")