precision_recall_curve
最后更新
luxonis_train.attached_modules.metrics.precision_recall_curve在置信度阈值网格上的精确度、召回率和 F1 分数。该指标在头的 NMS 前候选上运行自己的 NMS。因此,头的置信度阈值不会限制曲线。类
PrecisionRecallCurve
输入::detections_pre_nms(Tensor): [B, N, 5 + nclasses + E],头的 NMS 输入,框以xyxy像素表示。E 是关键点或分割头的额外值数量,否则为0。target_boundingbox(Tensor): [Nt, 6],[batch, class, x, y, w, h],归一化。
输出::confidence,precision,recall,f1(Tensor): [T] 在阈值网格上max_f1,confidence_at_max_f1(Tensor): 标量
公式:: 对于每个批次,该指标使用nms_conf_threshold、nms_iou_threshold和max_detections运行 NMS。它从最高分数到最低分数遍历图像中保留的检测。当其类别的自由目标与它具有至少matching_iou_threshold的 IoU 时,检测为真阳性。然后,检测选择具有最高 IoU 的自由目标。对于阈值 c,分数至少为 c 的检测计数: P(c) = (TP(c))/(TP(c) + FP(c)),R(c) = (TP(c))/(Nt),F1(c) = (2 P(c) R(c))/(P(c) + R(c)) Nt 是所有批次的目标数量。当没有检测达到 c 时,P 为1,因此曲线在高阈值处不会降至0。当没有目标时,R 为0,当 P + R = 0 时,F1 为0。
参考
- 来源:本项目。
- 许可证:Apache-2.0 (本项目)
注意
- 该节点必须是 BaseDetectionHead。构造函数调用 BaseDetectionHead.request_detections_pre_nms,因此该节点在评估模式下将其候选添加到其数据包中。
- get_loggable_values 选择
max_f1作为指标的日志值。 get_artifacts 绘制曲线。
示例
附加到
model.nodes 中的 EfficientBBoxHead:Yaml
1- name: EfficientBBoxHead
2 inputs: [RepPANNeck]
3 metrics:
4 - name: PrecisionRecallCurve方法
init
Python
1def __init__(*, confidence_thresholds: list[float] | None = None, num_thresholds: int | None = None, min_confidence: float | None = None, max_confidence: float | None = None, matching_iou_threshold: float = 0.5, nms_conf_threshold: float = 0.001, nms_iou_threshold: float | None = None, max_detections: int | None = None, **kwargs):confidence_thresholds(list[float] | None): 显式网格,至少包含两个严格递增的[0, 1]值。None构建一个包含num_thresholds个从min_confidence到max_confidence均匀分布的值的网格。num_thresholds(int | None): 构建网格的值数量,至少为2。None选择101。min_confidence(float | None): 构建网格的第一个值,在[0, 1]中。None选择0.0。max_confidence(float | None): 构建网格的最后一个值,在[0, 1]中且高于min_confidence。None选择1.0。matching_iou_threshold(float): 检测与其类别的目标匹配的最小 IoU,在[0, 1]中。nms_conf_threshold(float): NMS 的分数下限,在[0, 1]中。NMS 会丢弃低于下限的候选,因此它们永远不会被计数,即使在第一个阈值处也不会。该指标使用此值和网格的第一个值中较大的一个。较低的下限会保留更多候选,NMS 也会花费更多时间。下限为0会保留所有分数高于0的候选。nms_iou_threshold(float | None): NMS 的 IoU 阈值,在[0, 1]中。None选择节点的iou_thres。max_detections(int | None): NMS 为每个图像保留的最大检测数量,高于0。None选择节点的max_det。**kwargs: 转发给 BaseMetric 的关键字参数。它们必须包含node。
ValueError: 当confidence_thresholds与num_thresholds、min_confidence或max_confidence中的一个一起出现时。当值超出其范围时,以及当显式网格不是严格递增时。
build_curve_figure
Python
1def build_curve_figure(values: dict[str, Tensor]) -> Figure:- 左:精确度与召回率。
- 中:精确度与置信度。
- 右:召回率与置信度。
[0, 1]。参数values(dict[str, Tensor]): compute 的结果。该方法读取"confidence"、"precision"和"recall"。
Figure: 一个matplotlib图形,尺寸为12x4英寸,布局受限。
compute
Python
1def compute(self) -> dict[str, Tensor]:示例
一个分数为
0.8 的检测覆盖了唯一的目标。在阈值 0.9 处,没有检测计数。此时精确度为 1,召回率为 0:Pycon
1>>> import torch
2>>> from torch import Size
3>>> from luxonis_train.nodes import EfficientBBoxHead
4>>> head = EfficientBBoxHead(
5... n_heads=1,
6... n_classes=1,
7... input_shapes=[{"features": [Size([1, 8, 8, 8])]}],
8... original_in_shape=Size([3, 64, 64]),
9... )
10>>> metric = PrecisionRecallCurve(
11... node=head, confidence_thresholds=[0.0, 0.5, 0.9]
12... )
13>>> candidates = torch.tensor([[[0.0, 0.0, 32.0, 32.0, 1.0, 0.8]]])
14>>> target = torch.tensor([[0.0, 0.0, 0.0, 0.0, 0.5, 0.5]])
15>>> metric.update(candidates, target)
16>>> values = metric.compute()
17>>> values["precision"].tolist(), values["recall"].tolist()
18([1.0, 1.0, 1.0], [1.0, 1.0, 0.0])dict[str, Tensor]: 曲线及其最佳点。"confidence"包含阈值网格,形状为[T]。"precision"、"recall"和"f1"包含阈值处的值,每个形状为[T]。"max_f1"包含最大的 F1,一个标量。"confidence_at_max_f1"包含达到max_f1的第一个阈值,一个标量。
get_artifact_names
Python
1def get_artifact_names(self) -> tuple[str, ...]:tuple[str, ...]:("curves",),get_artifacts 使用的键。
get_artifacts
Python
1def get_artifacts(values: dict[str, Tensor]) -> dict[str, Tensor]:values(dict[str, Tensor]): compute 的结果。
dict[str, Tensor]: 键"curves",包含形状为[3, 400, 1200]的uint8RGB 图像。
get_loggable_values
Python
1def get_loggable_values(values: dict[str, Tensor]) -> tuple[Tensor, dict[str, Tensor]]:max_f1 作为指标的日志值。训练器只记录返回的值,因此曲线不会出现在标量日志中。参数values(dict[str, Tensor]): compute 的结果。
tuple[Tensor, dict[str, Tensor]]:values["max_f1"],以及一个包含键"confidence_at_max_f1"的字典。
update
Python
1def update(detections_pre_nms: Tensor, target_boundingbox: Tensor):true_positives 和 false_positives。它将目标数量添加到 target_count。首先,它将推理模式下 reset 创建的每个状态替换为普通张量。参数detections_pre_nms(Tensor): 头的候选,形状为[B, N, 5 + n_classes + E]。每行包含像素中的xyxy框、一个常量1、类别分数和E个指标忽略的额外值。target_boundingbox(Tensor): 目标框,形状为[N_t, 6],表示为[batch, class, x, y, w, h],值为归一化。该方法将它们移动到候选的设备。