含有忍耐度的TP计算(Precision、Rcall、F1 score)

发布 : 2020-03-30 分类 : 深度学习 浏览 :

对于边缘分割类任务,边缘部分因为人为标记不准确等原因,在评估模型时,需要对这些区域设置一定忍耐度以保证模型评估更完整。这里提供代码参考。(允许两个像素差距)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import cv2
import numpy as np
import time

gt = cv2.imread("/home/harvey/Datasets/DeepCrack/OurResult/11125-1_gt.png", cv2.IMREAD_UNCHANGED)
p_msk = cv2.imread("/home/harvey/Datasets/DeepCrack/OurResult/11125-1_pred.png", cv2.IMREAD_UNCHANGED)
thres = 10
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))


def rolling_cal(ground, pred_msk, thres=10):
H, W = ground.shape
total = H * W
filter_size = 5
msk_t = (pred_msk > thres).astype(np.int)
ground = (ground > 0).astype(np.int)
H, W = ground.shape
gt = np.pad(ground, (2, 2), 'constant', constant_values=(0, 0))
tp, fp, fn = 0.0, 0.0, 0.0
for r in range(0, H):
for c in range(0, W):
# 池化大小的输入区域
gti = gt[r:r + filter_size, c:c + filter_size]
cur_pexl = msk_t[r, c]
gt_patch = (gti * kernel).sum()
if cur_pexl != 0 and gt_patch != 0:
tp += 1
elif cur_pexl != 0 and gti[2][2] == 0:
fp += 1
elif cur_pexl == 0 and gti[2][2] != 0:
fn += 1
return tp, fp, fn, total

start = time.time()
tp, fp, fn, total = rolling_cal(gt, p_msk, 10)
print("time:", time.time() - start)
print(tp, fp, fn, total)
recall = tp / (tp + fn)
precision = tp / (tp + fp)
f1 = 2*tp / (2*tp + fp + fn)
print(precision, recall, f1)
本文作者 : HeoLis
原文链接 : http://ishero.net/%E5%90%AB%E6%9C%89%E5%BF%8D%E8%80%90%E5%BA%A6%E7%9A%84TP%E8%AE%A1%E7%AE%97(Precision%E3%80%81Rcall%E3%80%81F1%20score).html
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

学习、记录、分享、获得

微信扫一扫, 向我投食

微信扫一扫, 向我投食