2018-04-09 03:15:24 +00:00
|
|
|
import torch
|
2019-10-30 18:54:57 +00:00
|
|
|
import torch.nn.functional as F
|
2019-10-24 19:37:21 +00:00
|
|
|
from tqdm import tqdm
|
2017-11-30 06:44:34 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
from dice_loss import dice_coeff
|
2017-08-19 08:59:51 +00:00
|
|
|
|
|
|
|
|
2019-11-23 13:22:42 +00:00
|
|
|
def eval_net(net, loader, device, n_val):
|
2018-06-08 17:27:32 +00:00
|
|
|
"""Evaluation without the densecrf with the dice coefficient"""
|
2018-09-26 06:57:10 +00:00
|
|
|
net.eval()
|
2017-08-19 08:59:51 +00:00
|
|
|
tot = 0
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2019-11-23 13:22:42 +00:00
|
|
|
for i, b in tqdm(enumerate(loader), desc='Validation round', unit='img'):
|
|
|
|
imgs = b['image']
|
|
|
|
true_masks = b['mask']
|
2017-08-19 08:59:51 +00:00
|
|
|
|
2019-11-23 13:22:42 +00:00
|
|
|
imgs = imgs.to(device=device, dtype=torch.float32)
|
|
|
|
true_masks = true_masks.to(device=device, dtype=torch.float32)
|
2017-08-19 08:59:51 +00:00
|
|
|
|
2019-11-23 13:22:42 +00:00
|
|
|
mask_pred = net(imgs)
|
2017-08-19 08:59:51 +00:00
|
|
|
|
2019-11-23 13:22:42 +00:00
|
|
|
for true_mask in true_masks:
|
|
|
|
mask_pred = (mask_pred > 0.5).float()
|
|
|
|
if net.n_classes > 1:
|
|
|
|
tot += F.cross_entropy(mask_pred.unsqueeze(dim=0), true_mask.unsqueeze(dim=0)).item()
|
|
|
|
else:
|
|
|
|
tot += dice_coeff(mask_pred, true_mask.squeeze(dim=1)).item()
|
2019-10-30 18:54:57 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
return tot / n_val
|