File size: 1,258 Bytes
21419ae
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
"""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)}))