评价指标的计算代码,需要注意传入路径需要加上后缀,下面示例为计算255*255的影像
1 import numpy as np 2 import glob 3 import tqdm 4 from PIL import Image 5 import cv2 as cv 6 import os 7 from sklearn.metrics import confusion_matrix,cohen_kappa_score 8 # from skimage import io 9 # from skimage import measure 10 from scipy import ndimage 11 from sklearn.metrics import f1_score 12 from tqdm import tqdm 13 14 def mean_iou(input, target, classes = 2): 15 """ compute the value of mean iou 16 :param input: 2d array, int, prediction 17 :param target: 2d array, int, ground truth 18 :param classes: int, the number of class 19 :return: 20 miou: float, the value of miou 21 """ 22 miou = 0 23 for i in range(classes): 24 intersection = np.logical_and(target == i, input == i) 25 # print(intersection.any()) 26 union = np.logical_or(target == i, input == i) 27 print(union) 28 temp = np.sum(intersection) / np.sum(union) 29 miou += temp 30 return miou/classes 31 32 def compute_f1(prediction, target): 33 """ 34 :param prediction: 2d array, int, 35 estimated targets as returned by a classifier 36 :param target: 2d array, int, 37 ground truth 38 :return: 39 f1: float 40 """ 41 prediction.tolist(), target.tolist() 42 img, target = np.array(prediction).flatten(), np.array(target).flatten() 43 f1 = f1_score(y_true=target, y_pred=img, average='micro') 44 return f1 45 46 47 def compute_kappa(prediction, target): 48 """ 49 :param prediction: 2d array, int, 50 estimated targets as returned by a classifier 51 :param target: 2d array, int, 52 ground truth 53 :return: 54 kappa: float 55 """ 56 prediction.tolist(), target.tolist() 57 img, target = np.array(prediction).flatten(), np.array(target).flatten() 58 kappa = cohen_kappa_score(target, img) 59 return kappa 60 61 def calculateTP(GT, PRE): 62 TP = 0 63 for y in range(0, 256): 64 for x in range(0, 256): 65 if GT[y, x] == 255 and PRE[y, x] == 255: 66 TP += 1 67 else: 68 continue 69 return TP 70 def calculateTN(GT, PRE): 71 TN = 0 72 for y in range(0,256): 73 for x in range(0,256): 74 if GT[y,x] == 0 and PRE[y,x] == 0: 75 TN += 1 76 else: 77 continue 78 return TN 79 80 def calculateFP(GT, PRE): 81 FP = 0 82 for y in range(0,256): 83 for x in range(0,256): 84 if GT[y,x] == 0 and PRE[y,x] == 255: 85 FP+=1 86 else: 87 continue 88 return FP 89 90 def calculateFN(GT,PRE): 91 FN = 0 92 for y in range(0,256): 93 for x in range(0,256): 94 if GT[y,x] == 255 and PRE[y,x] == 0: 95 FN += 1 96 else: 97 continue 98 return FN 100 101 all_targets = glob.glob(r'OUT0_255\*.png') 102 #print(len(all_targets)) 103 all_inputs = glob.glob(r'result0_255\*.png') # 读取所有图像文件 106 sum_mean_IoU = 0 107 sum_F1 = 0 108 sum_kappa = 0 109 TP_sum = 0 110 TN_sum = 0 111 FP_sum = 0 112 FN_sum = 0 113 114 for i in tqdm(range(len(all_inputs))): 115 target = all_targets[i] 116 input = all_inputs[i] 117 # print(target) 118 target = Image.open(target) 119 #target = target.convert('L') 120 target = np.array(target) # 将pillow对象转换为np数据 121 input = Image.open(input) 122 input= np.array(input) 123 # input = cv.resize(input, (500, 500), interpolation=cv.INTER_CUBIC) 124 #input[input == 255 ] = 255 # 对np_img做处理,像素大于0置为1,像素二值化 125 #input = cv.cvtColor(input, cv.COLOR_BGR2GRAY) 126 # print(target.shape) 127 128 # meanIoU = mean_iou(input, target) 129 #F1 = compute_f1(input, target) 130 #kappa = compute_kappa(input, target) 131 132 133 TP = calculateTP(target, input) 134 TN = calculateTN(target, input) 135 FP = calculateFP(target, input) 136 FN = calculateFN(target, input) 137 TP_sum += TP 138 TN_sum += TN 139 FP_sum += FP 140 FN_sum += FN 141 142 143 144 presion = TP_sum / (TP_sum + FP_sum) 145 recall1 = TP_sum / (TP_sum + FN_sum) 146 #recall2 = TN_sum / (TP_sum + FN_sum) 147 Accuracy = (TP_sum + TN_sum) / (TP_sum + TN_sum + FP_sum + FN_sum) 148 F1 = (2 * recall1 * presion) / (recall1 + presion) 149 # 预测结果前景iou 150 IOU1 = TP_sum / (TP_sum + FP_sum + FN_sum) 151 # 预测结果背景iou 152 IOU2 = TN_sum / (TN_sum + FP_sum + FN_sum) 153 # MIoU = (IoU正例p + IoU反例n) / 2 = [ TP / (TP + FP + FN) + TN / (TN + FN + FP) ] / 2 154 IOU = (IOU1 + IOU2) / 2 155 156 print("presion: ", presion) 157 print("recall: ", recall1) 158 print("Accuracy: ", Accuracy) 159 print("F1: ", F1) 160 print("mIou: ", IOU)
标签:分割,target,sum,语义,TP,TN,input,评价,FN From: https://www.cnblogs.com/yokon/p/17417344.html