def evalidation(epoch, dataloader, model, criterion, device, writer, tb_test_imgs):
    print('\neval epoch {}'.format(epoch))
    model.eval()
    recall = Recall((lambda x: (x[0], x[1])))
    precision = Precision((lambda x: (x[0], x[1])))
    mean_recall = []
    mean_precision = []
    mean_loss = []
    with torch.no_grad():
        for (idx, (pre_img, post_img, targets)) in enumerate(dataloader):
            (pre_img, post_img, targets) = (pre_img.to(device), post_img.to(device), targets.to(device))
            outputs = model(pre_img, post_img)
            loss = criterion(outputs, targets)
            preds = outputs.argmax(1)
            precision.update((preds, targets))
            recall.update((preds, targets))
            mean_loss.append(loss.item())
            mean_recall.append(recall.compute().item())
            mean_precision.append(precision.compute().item())
            writer.add_scalar('test/loss', loss.item(), ((len(dataloader) * epoch) + idx))
            if (idx < tb_test_imgs):
                writer.add_image('test/pre', pre_img[0], idx)
                writer.add_image('test/post', post_img[0], idx)
                writer.add_image('test/label', label[0], idx)
                writer.add_image('test/pred', preds, idx)
    (mean_precision, mean_recall) = (np.array(mean_precision).mean(), np.array(mean_recall).mean())
    f1 = (((mean_precision * mean_recall) * 2) / ((mean_precision + mean_recall) + 1e-20))
    print('precision: {:07.5}, recall: {:07.5}, f1: {:07.5}\n'.format(mean_precision, mean_recall, f1))
    writer.add_scalar('test/epoch-loss', np.array(mean_loss).mean(), epoch)
    writer.add_scalar('test/f1', f1, epoch)
    writer.add_scalar('test/precision', mean_precision, epoch)
    writer.add_scalar('test/recall', mean_recall, epoch)
def evalidation(epoch, dataloader, model, criterion, device, writer, tb_test_imgs):
    print('\neval epoch {}'.format(epoch))
    model.eval()
    recall = Recall((lambda x: (x[0], x[1])))
    precision = Precision((lambda x: (x[0], x[1])))
    mean_recall = []
    mean_precision = []
    mean_loss = []
    with torch.no_grad():
        for (idx, (pre_img, post_img, targets)) in enumerate(dataloader):
            (pre_img, post_img, targets) = (pre_img.to(device), post_img.to(device), targets.to(device))
            outputs = model(pre_img, post_img)
            loss = criterion(outputs, targets)
            preds = outputs.argmax(1)
            precision.update((preds, targets))
            recall.update((preds, targets))
            mean_loss.append(loss.item())
            mean_recall.append(recall.compute().item())
            mean_precision.append(precision.compute().item())
            writer.add_scalar('test/loss', loss.item(), ((len(dataloader) * epoch) + idx))
            if (idx < tb_test_imgs):
                writer.add_image('test/pre', pre_img[0], idx)
                writer.add_image('test/post', post_img[0], idx)
                writer.add_image('test/label', label[0], idx)
                writer.add_image('test/pred', preds, idx)
    (mean_precision, mean_recall) = (np.array(mean_precision).mean(), np.array(mean_recall).mean())
    f1 = (((mean_precision * mean_recall) * 2) / ((mean_precision + mean_recall) + 1e-20))
    print('precision: {:07.5}, recall: {:07.5}, f1: {:07.5}\n'.format(mean_precision, mean_recall, f1))
    writer.add_scalar('test/epoch-loss', np.array(mean_loss).mean(), epoch)
    writer.add_scalar('test/f1', f1, epoch)
    writer.add_scalar('test/precision', mean_precision, epoch)
    writer.add_scalar('test/recall', mean_recall, epoch)