Esempio n. 1
0
def update_eval_metrics(preds, labels, eval_metrics):

    if len(labels.shape) == 2:
        preds = np.expand_dims(preds, axis=0)
        labels = np.expand_dims(labels, axis=0)

    N = labels.shape[0]

    for i in range(N):
        pred = preds[i, :, :]
        label = labels[i, :, :]
        eval_metrics['dice score'].append(dc(pred, label))
        eval_metrics['precision'].append(precision(pred, label))
        eval_metrics['recall'].append(recall(pred, label))
        eval_metrics['sensitivity'].append(sensitivity(pred, label))
        eval_metrics['specificity'].append(specificity(pred, label))

        if np.sum(pred) > 0 and np.sum(label) > 0:
            eval_metrics['hausdorff'].append(hd(pred, label))
            eval_metrics['hausdorff 95%'].append(hd95(pred, label))
            eval_metrics['asd'].append(asd(pred, label))
            eval_metrics['assd'].append(assd(pred, label))
            eval_metrics['jaccard'].append(jc(pred, label))
        else:
            eval_metrics['hausdorff'].append('nan')
            eval_metrics['hausdorff 95%'].append('nan')
            eval_metrics['asd'].append('nan')
            eval_metrics['assd'].append('nan')
            eval_metrics['jaccard'].append('nan')

    return eval_metrics
def metrics(img_gt, img_pred, voxel_size):
    global VOXEL_SPACING
    """
    Function to compute the metrics between two segmentation maps given as input.

    Parameters
    ----------
    img_gt: np.array
    Array of the ground truth segmentation map.

    img_pred: np.array
    Array of the predicted segmentation map.

    voxel_size: list, tuple or np.array
    The size of a voxel of the images used to compute the volumes.

    Return
    ------
    A list of metrics in this order, [Dice LV, Volume LV, Err LV(ml),
    Dice RV, Volume RV, Err RV(ml), Dice MYO, Volume MYO, Err MYO(ml)]
    """
    print (img_gt.shape)
    print (img_pred.shape)
    if img_gt.ndim != img_pred.ndim:
        raise ValueError("The arrays 'img_gt' and 'img_pred' should have the "
                         "same dimension, {} against {}".format(img_gt.ndim,
                                                                img_pred.ndim))

    res = []
    # Loop on each classes of the input images
    for c in [255]:
        # Copy the gt image to not alterate the input
        gt_c_i = np.copy(img_gt)
        gt_c_i[gt_c_i != c] = 0

        # Copy the pred image to not alterate the input
        pred_c_i = np.copy(img_pred)
        pred_c_i[pred_c_i != c] = 0

        # Clip the value to compute the volumes
        gt_c_i = np.clip(gt_c_i, 0, 1)
        pred_c_i = np.clip(pred_c_i, 0, 1)

        # Compute the Dice
        dice = dc(gt_c_i, pred_c_i)
        hd_value=hd(gt_c_i,pred_c_i,voxelspacing=VOXEL_SPACING,connectivity=1)
        assd_value = assd(gt_c_i, pred_c_i, voxelspacing=VOXEL_SPACING,connectivity=1)
        jd=jc(gt_c_i,pred_c_i)

        # Compute volume
        # volpred = pred_c_i.sum() * np.prod(voxel_size) / 1000.
        # volgt = gt_c_i.sum() * np.prod(voxel_size) / 1000.
       # res+=[dice,jd]
        res += [dice,jd,hd_value,assd_value]#,volpred, volpred-volgt]

    return res
Esempio n. 3
0
def compute_scores(preds, labels):
    preds_data = preds.data.cpu().numpy()
    labels_data = labels.data.cpu().numpy()

    dice_score = dc(preds_data, labels_data)
    jaccard_coef = jc(preds_data, labels_data)
    hausdorff_dist = hd(preds_data, labels_data)
    asd_score = asd(preds_data, labels_data)
    assd_score = assd(preds_data, labels_data)
    precision_value = precision(preds_data, labels_data)
    recall_value = recall(preds_data, labels_data)
    sensitivity_value = sensitivity(preds_data, labels_data)
    specificity_value = specificity(preds_data, labels_data)
    return {
        'dice score': dice_score,
        'jaccard': jaccard_coef,
        'hausdorff': hausdorff_dist,
        'asd': asd_score,
        'assd': assd_score,
        'precision': precision_value,
        'recall': recall_value,
        'sensitivity': sensitivity_value,
        'specificity': specificity_value
    }
Esempio n. 4
0
    print(img.max(), lab.max())

    # non_zero_count = np.count_nonzero(lab)
    # ratio = non_zero_count / (lab.shape[0] * lab.shape[1])
    # print(ratio)

    input = Variable(
        torch.from_numpy(img).type('torch.FloatTensor').cuda(gpu_id))
    res = fcn(input).detach().cpu().numpy()
    # res =res/res.max()
    # res = (res-1)/res.max()

    ret, res = cv2.threshold(res[0, 0, :, :], 0.8, 1, cv2.THRESH_BINARY)
    # res = res[0,0,:,:]
    dice = dc(lab, res)
    jaccard = jc(lab, res)
    sens = sensitivity(lab, res)
    spec = specificity(lab, res)
    pres = precision(lab, res)
    rec = recall(lab, res)

    print('dice:', dice)
    print('jaccad:', jaccard)
    print('sensitivity:', sens)
    print('precision:', pres)

    dc_list.append(dice)
    jc_list.append(jaccard)
    sens_list.append(sens)
    spec_list.append(spec)
    pres_list.append(pres)
Esempio n. 5
0
    
    metricsDir = './{}/metrics/'.format(dataset, indexName)
    if not os.path.exists(metricsDir):
        os.makedirs(metricsDir)

    outputMetrics = metricsDir + '{}.csv'.format(indexName)
    filenames = glob.glob("./{}/cortadas/labels/*.tif".format(dataset))
    
    with open(outputMetrics, 'w') as csvfile:
        
        fieldNames = ['Image', 'Dice', 'Jaccard', 'Precision', 'Recall', 'Sensitivity', 'Specificity']    
        writer = csv.writer(csvfile, delimiter=";")
        writer.writerow(fieldNames)

        for maskFileName in filenames:

            fileName = maskFileName.replace('./{}/cortadas/labels/'.format(dataset), '')
            mask = cv2.imread(maskFileName, cv2.IMREAD_COLOR)
            processed = cv2.imread(processedDir + fileName, cv2.IMREAD_COLOR)

            dc = str(mbin.dc(mask, processed)).replace(".", ",")
            jc = str(mbin.jc(mask, processed)).replace(".", ",")

            precision = str(mbin.precision(processed, mask)).replace(".", ",")
            recall = str(mbin.recall(processed, mask)).replace(".", ",")
            
            sensitivity = str(mbin.sensitivity(processed, mask)).replace(".", ",")
            specificity = str(mbin.specificity(processed, mask)).replace(".", ",")

            writer.writerow([fileName, dc, jc, precision, recall, sensitivity, specificity])