| """ |
| It performs data-based evaluation |
| """ |
|
|
| import numpy as np |
| import cv2 |
| from sklearn.metrics import confusion_matrix |
| import os |
| import pandas as pd |
|
|
| import warnings |
| warnings.filterwarnings("ignore") |
|
|
| |
| dir_label = r'.\azh_wound_care_center_dataset_patches\test\labels' |
| dir_pred = r'.\predictions\OldDFU' |
|
|
| HARD_LINE = True |
|
|
| names = os.listdir(dir_label) |
|
|
| stp, stn, sfp, sfn = 0, 0, 0, 0 |
|
|
| ep = 1e-6 |
|
|
| save_dir_pred = r'.\predictions' |
|
|
| |
| df = pd.DataFrame(index=[], columns = [ |
| 'Name', 'Accuracy', 'Specificity', 'iou', 'Precision', 'Recall', 'Dice'], dtype='object') |
|
|
| |
| df_data = pd.DataFrame(index=[], columns = [ |
| 'Name', 'type', 'Accuracy', 'Specificity', 'iou', 'Precision', 'Recall', 'Dice', 'stp', 'stn', 'sfp', 'sfn'], dtype='object') |
|
|
| for i, name in enumerate(names): |
|
|
| |
| gt_mask = cv2.imread(os.path.join(dir_label, name), 0) |
|
|
| |
|
|
| pr_mask = cv2.imread(os.path.join(dir_pred, name), 0) |
|
|
| |
| gt_mask = np.squeeze(gt_mask) |
| pred = np.squeeze(pr_mask) |
|
|
| |
| flat_mask = np.squeeze(gt_mask).flatten() |
| flat_pred = np.squeeze(pred).flatten() |
| |
| |
| unq_mask_val = np.unique(flat_mask) |
| |
| 'Case I: If there is no GT pixels in the image' |
| if len(unq_mask_val)==1 and unq_mask_val==0: |
| |
| 'Case I.a: If both GT and prediction are black' |
| if np.array_equal(flat_mask, flat_pred): |
| |
| acc, sp, p, r, dice, iou = 100, 100, 100, 100, 100, 100 |
| print("Img # {:1s}, Image {:1s}: acc: {:3f}, sp: {:3f}, iou: {:3f}, p: {:3f}, r: {:3f}, dice: {:3f}".format(str(i+1), name, acc, sp, iou, p, r, dice)) |
| |
| |
| |
| |
| tn, fp, fn, tp = len(flat_mask), 0, 0, 0 |
| |
| else: |
| 'Case I.b: If GT is black, but prediction not' |
| if HARD_LINE: |
| |
| |
| |
| acc, sp, p, r, dice, iou = 0, 0, 0, 0, 0, 0 |
| |
| |
| tp, fn = 0, 0 |
| fp = np.count_nonzero(flat_pred) |
| tn = len(flat_pred) - fp |
| |
| else: |
| |
| |
| |
| |
| |
| |
| invt_flat_mask = np.logical_not(flat_mask) * 1 |
| invt_flat_pred = np.logical_not(flat_pred) * 1 |
| |
| itn, ifp, ifn, itp = confusion_matrix(invt_flat_mask, invt_flat_pred).ravel() |
| |
| |
| acc = ((itp + itn)/(itp + itn + ifn + ifp))*100 |
| sp = (itn/(itn + ifp + ep))*100 |
| p = (itp/(itp + ifp + ep))*100 |
| r = (itp/(itp + ifn + ep))*100 |
| dice = 0 |
| iou = (itp/(itp + ifp + ifn + ep)) * 100 |
| |
| print("Img # {:1s}, Image {:1s}: acc: {:3f}, sp: {:3f}, iou: {:3f}, p: {:3f}, r: {:3f}, dice: {:3f}".format(str(i+1), name, acc, sp, iou, p, r, dice)) |
| |
| |
| |
| tn, fp, fn, tp = confusion_matrix(flat_mask, flat_pred).ravel() |
| |
| else: |
| 'Case II: If there is some GT pixels in the image' |
| tn, fp, fn, tp = confusion_matrix(flat_mask, flat_pred).ravel() |
| |
| |
| acc = ((tp + tn)/(tp + tn + fn + fp))*100 |
| sp = (tn/(tn + fp + ep))*100 |
| p = (tp/(tp + fp + ep))*100 |
| r = (tp/(tp + fn + ep))*100 |
| dice = (2 * tp / (2 * tp + fp + fn))*100 |
| iou = (tp/(tp + fp + fn + ep)) * 100 |
| print("Img # {:1s}, Image {:1s}: acc: {:3f}, sp: {:3f}, iou: {:3f}, p: {:3f}, r: {:3f}, dice: {:3f}".format(str(i+1), name, acc, sp, iou, p, r, dice)) |
| |
| |
| |
| stp += tp |
| stn += tn |
| sfp += fp |
| sfn += fn |
|
|
| |
| tmp = pd.Series([name, acc, sp, iou, p, r, dice], index=['Name', 'Accuracy', 'Specificity', 'iou', 'Precision', 'Recall', 'Dice']) |
| df = df.append(tmp, ignore_index = True) |
|
|
| df.to_csv(os.path.join(save_dir_pred, 'result_best_model.csv'), index=False) |
|
|
| print("Mean Accuracy: ", df["Accuracy"].mean()) |
| print("Mean Specificity: ", df["Specificity"].mean()) |
| print('Mean IoU: ', df['iou'].mean()) |
| print("Mean precision: ", df["Precision"].mean()) |
| print("Mean recall: ", df["Recall"].mean()) |
| print("Mean dice: ", df["Dice"].mean()) |
|
|
| siou = (stp/(stp + sfp + sfn + ep))*100 |
| sprecision = (stp/(stp + sfp + ep))*100 |
| srecall = (stp/(stp + sfn + ep))*100 |
| sdice = (2 * stp / (2 * stp + sfp + sfn))*100 |
|
|
| print('siou:', siou) |
| print('sprecision:', sprecision) |
| print('srecall:', srecall) |
| print('sdice:', sdice) |
|
|
| |
| with open(os.path.join(save_dir_pred, 'result_data_based_best_model.txt'), 'w') as f: |
| print(f'siou = {siou}', file=f) |
| print(f'sprecision = {sprecision}', file=f) |
| print(f'srecall = {srecall}', file=f) |
| print(f'sdice = {sdice}', file=f) |
|
|