计算深度学习评价指标Precision、Recall、F1
对深度学习的结果进行评价是深度学习的重要一环,一般常用的评价方法有:准确率(Accuracy),精确率(Precision),召回率(Recall),像素精度(PA),平均精度(AP),交并比(IoU)等方法。
1.什么是TP、TN、FP、FN
TP(True positives):正样本被正确识别为正样本。
TN(True negatives):负样本被正确识别为负样本。
FP(False positives):假的正样本,即负样本被错误识别为正样本。
FN(False negatives):假的负样本,即正样本被错误识别为负样本。
2. Recall
Recall是测试集中所有正样本样例中,被正确识别为正样本的比例。
Recall=TP/(TP+FN)
Precision就是在识别出来的正样本中,True positives所占的比率。
Precision=TP/(TP+FP)
4.F1
F1 score为精确率与召回率的调和均值,是用来衡量二分类模型精确度的一种指标。
F1=2*(Precision*Recall)/(Precision+Recall)
具体实现如下:
```python
import cv2
import numpy as np
import os
from time import time
start = time()
predict_path = r"E:\allsample_chenjun\predict\thin"
label_path = r"E:\allsample_chenjun\testline"
files = (label_path)
list1 = []
for file in files:
result_name = (label_path,file)
(result_name)
files = (predict_path)
list2 = []
print("lable_len:",len(files))
for file in files:
result_name = (predict_path,file)
(result_name)
print("predict_len:",len(files))
Recall = 0
Precision = 0
F1 = 0
for i in range(len(files)):
TP = 0
FP = 0
FN = 0
TN = 0
img_lable = (list1[i],0)
img_predict = (list2[i],0)
x = (img_lable)
for j in range([0]):
index = (0, [1])
a = index[img_lable[j]==img_predict[j]]
for k in range(len(a)):
if img_lable[j][a[k]] == 255:
TP += 1
else :
TN += 1
b = index[img_lable[j] != img_predict[j]]
for k in range(len(b)):
if img_lable[j][b[k]] == 255:
FN += 1
else :
FP += 1
Precision += TP/(TP+FP)
Recall += TP/(TP+FN)
F1 += (2 * (TP/(TP+FP)) * (TP/(TP+FN))/((TP/(TP+FP)) + (TP/(TP+FN))))
print("第",i+1,"次计算完成")
end = time()
print('running time is :%f'%(end-start))
print("Precision:" ,Precision/len(files),"Recall:",Recall/len(files),"F1:",F1/len(files))