Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluation result #25

Closed
asdjia opened this issue Jun 12, 2022 · 2 comments
Closed

Evaluation result #25

asdjia opened this issue Jun 12, 2022 · 2 comments

Comments

@asdjia
Copy link

asdjia commented Jun 12, 2022

Hi,

I appreciate your code. I was wondering how to get the evaluation results for each testing dataset.

I appreciate your help.

Thanks

@CauchyComplete
Copy link
Collaborator

Hi,
I'm uploading test_val_v2 function.

def testval_v2(testloader, model):
    avg_acc = AverageMeter()
    avg_p_acc = AverageMeter()
    avg_mIoU = AverageMeter()
    avg_p_mIoU = AverageMeter()
    avg_F1 = AverageMeter()
    avg_p_F1 = AverageMeter()
    avg_AP = AverageMeter()
    avg_p_AP = AverageMeter()

    with torch.no_grad():
        for index, (image, label, qtable) in enumerate(tqdm(testloader)):
            size = label.size()
            image = image.cuda()
            label = label.long().cuda()
            model.eval()
            _, pred = model(image, label, qtable)

            pred = torch.squeeze(pred, 0)
            pred = F.softmax(pred, dim=0)[1]
            pred = pred.unsqueeze(0).unsqueeze(0)

            if pred.size()[-2] != size[-2] or pred.size()[-1] != size[-1]:
                pred = F.upsample(pred, (size[-2], size[-1]), mode='bilinear', align_corners=False)

            pred_r = pred.cpu().numpy().squeeze(axis=0).squeeze(axis=0).ravel()
            label_r = label.cpu().numpy().squeeze(axis=0).ravel()
            pred_r = pred_r[label_r != -1]
            label_r = label_r[label_r != -1]
            bin_pred = (pred_r >= 0.5).astype(np.float)
            correct = (bin_pred == label_r).astype(np.float)
            incorrect = (bin_pred != label_r).astype(np.float)
            TP = np.count_nonzero(correct[label_r==1])
            TN = np.count_nonzero(correct[label_r==0])
            FP = np.count_nonzero(incorrect[label_r==0])
            FN = np.count_nonzero(incorrect[label_r==1])

            mean_IoU = 0.5 * (TP / np.maximum(1.0, TP + FP + FN)) + 0.5 * (TN / np.maximum(1.0, FP + TN + FN))
            avg_mIoU.update(mean_IoU)
            p_mIoU = 0.5 * (FN / np.maximum(1.0, FN + TP + TN)) + 0.5 * (FP / np.maximum(1.0, FP + TP + TN))
            avg_p_mIoU.update(np.maximum(mean_IoU, p_mIoU))
            acc = (TP+TN)/(TP+TN+FP+FN)
            avg_acc.update(acc)
            p_acc = np.maximum(acc, (FP+FN)/(TP+TN+FP+FN))
            avg_p_acc.update(p_acc)
            F1 = (2*TP) / np.maximum(1.0, 2*TP+FN+FP)
            avg_F1.update(F1)
            p_f1 = (2*FN) / np.maximum(1.0, 2*FN+TP+TN)
            avg_p_F1.update(np.maximum(F1, p_f1))

            AP = average_precision_score(label_r, pred_r)
            avg_AP.update(AP)
            p_AP = average_precision_score(label_r, 1-pred_r)
            avg_p_AP.update(np.maximum(AP, p_AP))


            del pred
            torch.cuda.empty_cache()
            gc.collect()

    results = {'avg_p_acc': avg_p_acc.average(),
               'avg_p_mIoU': avg_p_mIoU.average(),
               'avg_p_F1': avg_p_F1.average(),
               'avg_p_AP': avg_p_AP.average(),
               }

    return results

@asdjia
Copy link
Author

asdjia commented Jul 6, 2022

Thanks for your reply. I am trying to test using trained model. But it was a struggle for me to process the test dataset. I wonder if you can provide some examples about that.

I appreciate your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants