ST-DTKD / scripts /predict.py
yellowwt's picture
Release binary forecasting code and seed42 checkpoints
21419ae verified
Raw History Blame Contribute Delete
1.26 kB
"""Frozen inference on preprocessed tensor inputs, not raw magnetograms."""
import argparse,json
from common import *
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--artifact',required=True);p.add_argument('--input',required=True,help='torch tensor dictionary: HMI and/or AIA');p.add_argument('--output',default='predictions.npz');p.add_argument('--device',default='cpu');p.add_argument('--checkpoint-root');p.add_argument('--run-tag');args=p.parse_args()
import torch,numpy as np
a=artifact(args.artifact);model=load_model(a,args.device,args.checkpoint_root,args.run_tag);batch=torch.load(args.input,map_location='cpu',weights_only=True)
with torch.inference_mode():
if a['architecture']=='mdt':logits=model(batch['AIA'].to(args.device),batch['HMI'].to(args.device))[0]
elif a['temporal']:logits=model(batch[a['modality']].to(args.device),return_features=False)
else:logits=model(batch[a['modality']].to(args.device))
assert logits.ndim==2 and logits.shape[1]==2 and torch.isfinite(logits).all();prob=logits.softmax(-1).cpu().numpy()
out=output_path(args.output);np.savez(out,logits=logits.cpu().numpy(),probabilities=prob,predictions=(prob[:,1]>=.5).astype(int));print(json.dumps({'artifact':a['id'],'output':str(out),'n':len(prob)}))