計算深度學習評價名額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)
3.Precision
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 = os.listdir(label_path)
list1 = []
for file in files:
result_name = os.path.join(label_path,file)
list1.append(result_name)
files = os.listdir(predict_path)
list2 = []
print("lable_len:",len(files))
for file in files:
result_name = os.path.join(predict_path,file)
list2.append(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 = cv2.imread(list1[i],0)
img_predict = cv2.imread(list2[i],0)
x = np.array(img_lable)
for j in range(x.shape[0]):
index = np.arange(0, x.shape[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))