FP、FN、TP、FN解释

本文介绍了FP(假阳性)、FN(假阴性)、TP(真阳性)和TN(真阴性)的概念,这些是评估分类算法性能的关键指标。通过一个具体的例子,展示了它们在矩阵中的表示,以及如何计算精确率(Precision)、召回率(Recall)和准确率(Accuracy)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FP、FN、TP、TN与精确率(Precision),召回率(Recall),准确率(Accuracy)

true :1,0,1,0,0,1,0,1

pred:0,1,1,1,1,0,0,1

TP表示真的是对的  =2(11)

TN表示真的是错的 =1 (00)

FP表示误以为是对的 =3 (01)

FN表示以为是错的漏掉了(其实是对的)=2(10)

(括号用矩阵表示)

准确率(Accuracy)

ACC=(TP+TN)/(Tp+TN+FP+FN) 

所有的预判与结果一致的/所有期望和预判的并集

### 目标检测中计算TPFPFN的代码实现 对于目标检测任务而言,理解如何计算真阳性(True Positive, TP)、假阳性(False Positive, FP)以及假阴性(False Negative, FN)至关重要。这不仅有助于评估模型性能,还能帮助定位具体问题所在。 #### 计算逻辑说明 为了判断预测框是否为TP/FP/FN,通常会设定一个交并比阈值(Intersection over Union, IoU)。当预测框与真实框之间的IoU超过该阈值,则认为是一个有效的匹配;反之则不是。基于此原则: - **TP**:预测框成功匹配到对应的真实框; - **FP**:未能找到匹配的真实框或被错误分类的目标; - **FN**:实际存在但未被正确检测出来的对象[^1]。 #### Python代码示例 下面给出一段Python代码用于计算上述三个量,在这里假设已经获得了所有测试图片上的ground truth bounding boxes列表`gt_boxes`和对应的detection results `pred_boxes`: ```python def calculate_tp_fp_fn(gt_boxes, pred_boxes, iou_threshold=0.5): """ Calculate the number of true positives (TP), false positives (FP), and false negatives (FN) based on given ground-truth and prediction data. Parameters: gt_boxes : list[List[float]] List containing lists where each sublist represents a single GT box with format [xmin,ymin,xmax,ymax]. pred_boxes : list[tuple] List containing tuples where each tuple contains two elements, first element is confidence score(float), second one is predicted bbox(list). iou_threshold : float Threshold value used to determine whether an object has been correctly detected. Returns: dict: Dictionary holding counts for 'tp', 'fp' and 'fn'. """ from itertools import chain # Sort predictions by their scores in descending order sorted_pred_boxes = sorted(pred_boxes, key=lambda x:x[0], reverse=True) matched_gt_indices = set() tp_count = fp_count = fn_count = 0 for conf_score, pred_bbox in sorted_pred_boxes: best_iou = -float('inf') match_index = None for idx, gt_bbox in enumerate(gt_boxes): current_iou = compute_iou(pred_bbox, gt_bbox) if current_iou >= iou_threshold and current_iou > best_iou \ and idx not in matched_gt_indices: best_iou = current_iou match_index = idx if best_iou != -float('inf'): tp_count += 1 matched_gt_indices.add(match_index) else: fp_count += 1 total_gts = len(set(chain(*[[i]*len(b) for i,b in enumerate(gt_boxes)]))) unmatched_gts = total_gts - len(matched_gt_indices) fn_count = unmatched_gts return {"tp": tp_count, "fp": fp_count, "fn": fn_count} def compute_iou(boxA, boxB): """Compute intersection-over-union between two bounding boxes.""" # Determine coordinates of intersecting rectangle xA = max(boxA[0], boxB[0]) yA = max(boxA[1], boxB[1]) xB = min(boxA[2], boxB[2]) yB = min(boxA[3], boxB[3]) interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1) area_A = (boxA[2]-boxA[0]+1)*(boxA[3]-boxA[1]+1) area_B = (boxB[2]-boxB[0]+1)*(boxB[3]-boxB[1]+1) union_area = float(area_A + area_B - interArea) return interArea / union_area if union_area!=0 else 0 if __name__ == "__main__": # Example usage gt_bboxes = [[100, 78, 394, 282]] # Format:[xmin, ymin, xmax, ymax] det_results = [(0.9, [98, 76, 396, 284]), (.4, [200, 200, 400, 400])] result = calculate_tp_fp_fn(gt_bboxes, det_results) print(f"Results:\nTP={result['tp']}\nFP={result['fp']}\nFN={result['fn']}") ``` 这段程序定义了一个函数`calculate_tp_fp_fn()`来接收真实的边界框(`gt_boxes`)和预测的结果(`pred_boxes`)作为输入参数,并返回字典形式的结果,其中包含了TPFPFN 的数量。此外还提供了一个辅助方法`compute_iou()`用来计算两个矩形区域间的IOU值[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值