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 get_score(truth, prediction, masking_functions): score = list() dice_score = [dc(func(truth), func(prediction)) for func in masking_functions] # hd_score = [hd(func(truth), func(prediction)) # for func in masking_functions] sensitivity_score = [sensitivity(func(truth), func(prediction)) for func in masking_functions] specificity_score = [specificity(func(truth), func(prediction)) for func in masking_functions] score.extend(dice_score) # score.extend(sensitivity_score) # score.extend(specificity_score) return score
def binary_measures_numpy(result, target, binary_threshold=0.5): result_binary = (result > binary_threshold).astype(numpy.uint8) target_binary = (target > binary_threshold).astype(numpy.uint8) result = BinaryMeasuresDto(mpm.dc(result_binary, target_binary), numpy.Inf, numpy.Inf, mpm.precision(result_binary, target_binary), mpm.sensitivity(result_binary, target_binary), mpm.specificity(result_binary, target_binary)) if result_binary.any() and target_binary.any(): result.hd = mpm.hd(result_binary, target_binary) result.assd = mpm.assd(result_binary, target_binary) return result
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 }
# 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) recall_list.append(rec)
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])