我正在尝试为我的物体检测模型计算精度,召回率,f1分数。我想知道我的方法是否正确吗?
def f1_eval(bbox_pred, bbox_gt, cls_pred, cls_gt, metric_iou=0.5):
"""
:param bbox_pred: [M x4]
:param bbox_gt: [N x 4]
:param cls_pred: [M]
:param cls_gt: [N]
:return:
"""
overlap = box_overlap(bbox_pred, bbox_gt)
max_overlap = np.argmax(overlap, 1)
num_of_preds = bbox_pred.shape[0]
num_of_gts = bbox_gt.shape[0]
TP = 0
FP = 0
FN = 0
for i in range(num_of_preds):
max_overlap_value = overlap[i, max_overlap[i]]
if max_overlap_value >= metric_iou:
if cls_pred[i] == cls_gt[max_overlap[i]]:
TP +=1
else:
FN +=1
else:
FP +=1
recall = TP / (TP + FN)
precision = TP / (TP + FP)
f1 = 2 * recall * precision / (recall + precision)
return f1, precision, recall