目标检测计算TP,FP,FN代码
时间: 2023-09-11 09:05:56 浏览: 160
以下是Python代码示例,用于计算目标检测中的True positive(TP)、False positive(FP)和False negative(FN):
```python
def calculate_TP_FP_FN(pred_boxes, true_boxes, iou_thresh):
# pred_boxes: 预测框的坐标,格式为[x_min, y_min, x_max, y_max]
# true_boxes: 真实框的坐标,格式为[x_min, y_min, x_max, y_max]
# iou_thresh: IOU阈值
TP = 0
FP = 0
FN = 0
# 对于每个预测框,找到与其IOU大于阈值的真实框
for i in range(len(pred_boxes)):
iou_max = 0
match = -1
for j in range(len(true_boxes)):
iou = calculate_iou(pred_boxes[i], true_boxes[j])
if iou > iou_thresh and iou > iou_max:
iou_max = iou
match = j
# 如果找到了匹配的真实框,则计算TP
if match != -1:
TP += 1
true_boxes.pop(match)
# 否则,计算FP
else:
FP += 1
# 计算未匹配的真实框数,即FN
FN = len(true_boxes)
return TP, FP, FN
def calculate_iou(box1, box2):
# box1和box2的格式为[x_min, y_min, x_max, y_max]
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
intersection = max(0, x2 - x1) * max(0, y2 - y1)
area_box1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area_box2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
union = area_box1 + area_box2 - intersection
iou = intersection / union
return iou
```
其中,`calculate_iou`函数用于计算两个框之间的IOU,`calculate_TP_FP_FN`函数用于计算TP、FP和FN。其中,输入的预测框和真实框均为坐标格式,即左上角和右下角的坐标值。
阅读全文