Apply sigmoid before calc dice in eval_net()
Former-commit-id: 0da18fda34f29c81968425715e19c5dc76c9ec46
This commit is contained in:
parent
d081192e90
commit
d292e8c6cd
26
eval.py
26
eval.py
|
@ -5,28 +5,28 @@ from tqdm import tqdm
|
||||||
from dice_loss import dice_coeff
|
from dice_loss import dice_coeff
|
||||||
|
|
||||||
|
|
||||||
def eval_net(net, loader, device, n_val):
|
def eval_net(net, loader, device):
|
||||||
"""Evaluation without the densecrf with the dice coefficient"""
|
"""Evaluation without the densecrf with the dice coefficient"""
|
||||||
net.eval()
|
net.eval()
|
||||||
|
mask_type = torch.float32 if net.n_classes == 1 else torch.long
|
||||||
|
n_val = len(loader) # the number of batch
|
||||||
tot = 0
|
tot = 0
|
||||||
|
|
||||||
with tqdm(total=n_val, desc='Validation round', unit='img', leave=False) as pbar:
|
with tqdm(total=n_val, desc='Validation round', unit='batch', leave=False) as pbar:
|
||||||
for batch in loader:
|
for batch in loader:
|
||||||
imgs = batch['image']
|
imgs, true_masks = batch['image'], batch['mask']
|
||||||
true_masks = batch['mask']
|
|
||||||
|
|
||||||
imgs = imgs.to(device=device, dtype=torch.float32)
|
imgs = imgs.to(device=device, dtype=torch.float32)
|
||||||
mask_type = torch.float32 if net.n_classes == 1 else torch.long
|
|
||||||
true_masks = true_masks.to(device=device, dtype=mask_type)
|
true_masks = true_masks.to(device=device, dtype=mask_type)
|
||||||
|
|
||||||
mask_pred = net(imgs)
|
with torch.no_grad():
|
||||||
|
mask_pred = net(imgs)
|
||||||
|
|
||||||
for true_mask, pred in zip(true_masks, mask_pred):
|
if net.n_classes > 1:
|
||||||
|
tot += F.cross_entropy(mask_pred, true_masks).item()
|
||||||
|
else:
|
||||||
|
pred = torch.sigmoid(mask_pred)
|
||||||
pred = (pred > 0.5).float()
|
pred = (pred > 0.5).float()
|
||||||
if net.n_classes > 1:
|
tot += dice_coeff(pred, true_masks).item()
|
||||||
tot += F.cross_entropy(pred.unsqueeze(dim=0), true_mask.unsqueeze(dim=0)).item()
|
pbar.update()
|
||||||
else:
|
|
||||||
tot += dice_coeff(pred, true_mask.squeeze(dim=1)).item()
|
|
||||||
pbar.update(imgs.shape[0])
|
|
||||||
|
|
||||||
return tot / n_val
|
return tot / n_val
|
||||||
|
|
2
train.py
2
train.py
|
@ -88,7 +88,7 @@ def train_net(net,
|
||||||
pbar.update(imgs.shape[0])
|
pbar.update(imgs.shape[0])
|
||||||
global_step += 1
|
global_step += 1
|
||||||
if global_step % (len(dataset) // (10 * batch_size)) == 0:
|
if global_step % (len(dataset) // (10 * batch_size)) == 0:
|
||||||
val_score = eval_net(net, val_loader, device, n_val)
|
val_score = eval_net(net, val_loader, device)
|
||||||
if net.n_classes > 1:
|
if net.n_classes > 1:
|
||||||
logging.info('Validation cross entropy: {}'.format(val_score))
|
logging.info('Validation cross entropy: {}'.format(val_score))
|
||||||
writer.add_scalar('Loss/test', val_score, global_step)
|
writer.add_scalar('Loss/test', val_score, global_step)
|
||||||
|
|
Loading…
Reference in a new issue