projet-long/eval.py
milesial 4c0f0a7a7b Global cleanup, better logging and CLI
Former-commit-id: ff1ac0936c118d129bc8a8014958948d3b3883be
2019-10-26 23:17:48 +02:00

27 lines
727 B
Python

import torch
from tqdm import tqdm
from dice_loss import dice_coeff
def eval_net(net, dataset, device, n_val):
"""Evaluation without the densecrf with the dice coefficient"""
net.eval()
tot = 0
for i, b in tqdm(enumerate(dataset), total=n_val, desc='Validation round', unit='img'):
img = b[0]
true_mask = b[1]
img = torch.from_numpy(img).unsqueeze(0)
true_mask = torch.from_numpy(true_mask).unsqueeze(0)
img = img.to(device=device)
true_mask = true_mask.to(device=device)
mask_pred = net(img).squeeze(dim=0)
mask_pred = (mask_pred > 0.5).float()
tot += dice_coeff(mask_pred, true_mask.squeeze(dim=1)).item()
return tot / n_val