Exemplo n.º 1
0
def evaluation(gt_mask, pred, thresh=0.5):
    '''
    evaluate dice. pred and gt_mask should be (h, w) and pred values are 0-1. if the values are not binary, use thesh to make it binary.
    '''

    if gt_mask.shape != pred.shape:
        print('gt_mask and pred should be the same shape')
        return
    elif len(gt_mask.shape) > 4:
        print('too many rank. only accept 3 or 4')
        return
    elif len(gt_mask.shape) == 3:
        print('squeeze to make rank 2')
        gt_mask = np.squeeze(gt_mask)
        pred = np.squeeze(pred)

    # if the pred is 0-1, make it binary with the thresh
    pred = np.where(pred > thresh, 1, 0)
    #     try:
    # Compute Dice
    gt = np.greater(gt_mask, 0)
    pd = np.greater(pred, 0)
    # if the ground truth and prediction have no mask, return 1
    if (gt.sum() == 0) and (pd.sum() == 0):
        return 1.0
    # if the ground truth has no mask but prediction has masks, return 0
    elif (gt.sum() == 0) and (pd.sum() > 0):
        return 0.0
    # if the ground truth has mask but prediction has no masks, ofcause return 0
    elif (gt.sum() > 0) and (pd.sum() == 0):
        return 0.0

    else:
        dice = 2 * np.logical_and(pd, gt).sum() / (pd.sum() + gt.sum())
        return dice
Exemplo n.º 2
0
def calc_Precision(truth, pred, classes):
    prec_scores = []
    # Iterate over each class
    for i in range(classes):
        try:
            gt = np.equal(truth, i)
            pd = np.equal(pred, i)
            # Calculate precision
            prec = np.logical_and(pd, gt).sum() / pd.sum()
            prec_scores.append(prec)
        except ZeroDivisionError:
            prec_scores.append(0.0)
    # Return computed precision scores
    return prec_scores
Exemplo n.º 3
0
def calc_IoU(truth, pred, classes):
    iou_scores = []
    # Iterate over each class
    for i in range(classes):
        try:
            gt = np.equal(truth, i)
            pd = np.equal(pred, i)
            # Calculate iou
            iou = np.logical_and(pd, gt).sum() / (pd.sum() + gt.sum() - np.logical_and(pd, gt).sum())
            iou_scores.append(iou)
        except ZeroDivisionError:
            iou_scores.append(0.0)
    # Return computed IoU
    return iou_scores
Exemplo n.º 4
0
def calc_DSC(truth, pred, classes):
    dice_scores = []
    # Iterate over each class
    for i in range(classes):
        try:
            gt = np.equal(truth, i)
            pd = np.equal(pred, i)
            # Calculate Dice
            dice = 2*np.logical_and(pd, gt).sum() / (pd.sum() + gt.sum())
            dice_scores.append(dice)
        except ZeroDivisionError:
            dice_scores.append(0.0)
    # Return computed Dice Similarity Coefficients
    return dice_scores
Exemplo n.º 5
0
    def predict(self, X):
        import pandas as pd
        import numpy as np
        import scipy as sp
        from classifier.decisionTree import DecisionTree
        pd = []
        lab = self.labels[:, np.newaxis]
        for clf, wt in zip(self.classifiers, self.eweight):
            pd = sum((clf.predict(X) == lab).T * wt)
        pd /= self.eweights.sum()
        if self.n == 2:
            pd[:, 0] *= -1
            pd = pd.sum(axis=1)
            return self.labels.take(pd > 0, axis=0)

        return self.labels.take(np.argmax(pd, axis=1), axis=0)