def eval_net(net, dataset, slicetotal, batch_size=12, gpu=True):
    'Evaluation without the densecrf with the dice coefficient'

    net.eval()
    start = time.time()
    dice_ = torch.zeros(14).cuda()
    jac_ = torch.zeros(14).cuda()
    NE = torch.zeros(14).cuda()
    JNE = torch.zeros(14).cuda()

    accuracy_ = torch.zeros(14).cuda()
    precision_ = torch.zeros(14).cuda()
    recall_ = torch.zeros(14).cuda()
    specificity_ = torch.zeros(14).cuda()

    print(1)
    with torch.no_grad():
        for i, b in enumerate(batch(dataset, batch_size)):

            imgs = np.array([k[0] for k in b]).astype(np.float32)
            true_masks = np.array([k[1] for k in b])

            imgs = torch.from_numpy(imgs)
            imgs = imgs.unsqueeze(1)
            true_masks = torch.from_numpy(true_masks)

            pre_masks_eval = torch.zeros(true_masks.shape[0],14,256,256)
            true_masks_eval = torch.zeros(true_masks.shape[0],14,256,256)
            batchshape = true_masks.shape[0]

            batch_dice = torch.zeros(14).cuda()
            if gpu:
                imgs = imgs.cuda()
                true_masks = true_masks.cuda()
                net.cuda()

            output_img = net(imgs)
            input = output_img.cuda()
            pre_masks = input.max(1)[1].float() #索引代表像素所属类别的数>字
            for ak in range(14):
                if ak == 0:
                    continue
                pre_masks_eval[:,ak] = (pre_masks==ak)
                true_masks_eval[:,ak] = (true_masks==ak)
                premasks = pre_masks_eval[:,ak].view(true_masks.shape[0],-1)
                truemasks = true_masks_eval[:,ak].view(true_masks.shape[0],-1)

                intersection = premasks * truemasks
                TP = intersection.sum(1)
                FP = premasks.sum(1) - TP
                FN = truemasks.sum(1) - TP
                TN = ((premasks == 0) * (truemasks == 0)).sum(1)

                for bk in range(true_masks.shape[0]):
                    if TP[bk] == 0 and FP[bk] == 0 and FN[bk] == 0:
                        NE[ak] += 1
                        JNE[ak] += 1
                    else:
                        batch_dice[ak] = batch_dice[ak] + 2*TP[bk] / (2*TP[bk] + FP[bk] + FN[bk])
                        jac_[ak] = jac_[ak] + TP[bk] / (TP[bk] + FP[bk] + FN[bk])
                        
                        # Compute accuracy
                        accuracy_[ak] = accuracy_[ak] + (TP[bk] + TN[bk]) / (TP[bk] + TN[bk] + FP[bk] + FN[bk])
                        
                        # Compute precision
                        precision_[ak] = precision_[ak] + TP[bk] / (TP[bk] + FP[bk])
                        
                        # Compute recall
                        recall_[ak] = recall_[ak] + TP[bk] / (TP[bk] + FN[bk])
                        
                        # Compute specificity
                        specificity_[ak] = specificity_[ak] + TN[bk] / (TN[bk] + FP[bk])

            dice_ = dice_ + batch_dice

        for knum in range(14):
            dice_[knum] = dice_[knum] / (slicetotal - NE[knum])
            jac_[knum] = jac_[knum] / (slicetotal - JNE[knum])
            accuracy_[knum] = accuracy_[knum] / batchshape
            precision_[knum] = precision_[knum] / batchshape
            recall_[knum] = recall_[knum] / batchshape
            specificity_[knum] = specificity_[knum] / batchshape
    end = time.time()
    print('time used:',end - start)

    return dice_,jac_,accuracy_,precision_,recall_,specificity_ 
Python代码:计算图像分割模型的Accuracy, Precision, Recall和Specificity

原文地址: https://www.cveoy.top/t/topic/fRSX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录