BraggNN / inference.py
dennistrujillo's picture
added inference.py
c59d416
from model import model_init, BraggNN
from torch.utils.data import DataLoader
from dataset import PatchWiseDataset
import torch, argparse, os
from util import str2bool, str2tuple, s2ituple
from plot import plot_loss, plot_error
import numpy as np
parser = argparse.ArgumentParser(description='Bragg peak finding for HEDM.')
parser.add_argument('-p_file', type=str, default="debug", help='frame/patch metadata h5 file')
parser.add_argument('-f_file', type=str, default="debug", help='frame/patch h5 file')
parser.add_argument('-m_file', type=str, help='model state_dict file')
parser.add_argument('-psz', type=int, default=15, help='working patch size')
parser.add_argument('-fcsz', type=s2ituple, default='16_8_4_2', help='size of dense layers')
parser.add_argument('-expName',type=str, default="debug", help='Experiment name')
args, unparsed = parser.parse_known_args()
def main(args):
device = torch.device("cpu")#"cuda" if torch.cuda.is_available() else "cpu")
#model = BraggNN(imgsz=args.psz, fcsz=args.fcsz)
if device.type == 'cpu':
model=torch.load(args.m_file,map_location=torch.device('cpu'))
else:
model=torch.load(args.m_file)
model.eval()
ds_valid = PatchWiseDataset(psz=args.psz, rnd_shift=0, use='validation', pfile=args.p_file, ffile=args.f_file)
dl_valid = DataLoader(dataset=ds_valid, batch_size=len(ds_valid), shuffle=True, \
num_workers=4, drop_last=False, pin_memory=True)
mse=0.0
with torch.no_grad():
for i, (inputs, labels) in enumerate(dl_valid):
inputs = inputs.to(device)
y_pred = model(inputs)
y_pred = y_pred.cpu().numpy()
labels = labels.cpu().numpy()
plot_error(y_pred,labels,args.expName)
mse += np.power(y_pred[:,0] - labels[:,0],2) + np.power(y_pred[:,1] - labels[:,1],2)
if __name__ == "__main__":
main(args)