base_detection_head
最后更新
luxonis_train.nodes.heads.base_detection_head预测边界框的头的基类。它保留了 NMS 设置和尺度的步幅。它还选择了导出输出的名称。每个子类在评估模式下自行运行 NMS。类
BaseDetectionHead
n_heads 个输出,每个尺度一个特征图。对于此选择,构造函数将 attach_index 设置为 (-n_heads - 1, -1)。节点 params 中的 attach_index 会替换此值。子类将每个尺度的块存储在 heads 中。在评估模式下,它使用 conf_thres、iou_thres 和 max_det 运行 NMS。在调用 request_detections_pre_nms 后,它还会将 NMS 输入保留在其数据包中。示例
EfficientBBoxHead 是一个检测头。它有两个尺度,读取三个特征图中的最后两个:
Pycon
1>>> from torch import Size
2>>> from luxonis_train.nodes import EfficientBBoxHead
3>>> sizes = [
4... Size([1, 4, 64, 64]),
5... Size([1, 8, 32, 32]),
6... Size([1, 16, 16, 16]),
7... ]
8>>> head = EfficientBBoxHead(
9... n_heads=2,
10... n_classes=3,
11... input_shapes=[{"features": sizes}],
12... original_in_shape=Size([3, 256, 256]),
13... )
14>>> head.attach_index, head.in_channels, head.stride.tolist()
15((-3, -1), [8, 16], [8, 16])方法
init
Python
1def __init__(n_heads: int, conf_thres: float, iou_thres: float, max_det: int, **kwargs):kwargs 必须包含 original_in_shape,以及 input_shapes 或 in_sizes。当头获得的特征图少于 n_heads 时,构造函数会记录警告并将 n_heads 设置为该数字。在 kwargs 中没有 attach_index 的情况下,此检查会计算输入节点的所有输出。参数n_heads(int): 尺度数量。头读取输入节点的最后n_heads个输出。conf_thres(float): NMS 的置信度阈值,范围为[0, 1]。iou_thres(float): NMS 的 IoU 阈值,范围为[0, 1]。max_det(int): NMS 为每张图像保留的最大框数。**kwargs: BaseNode 的关键字参数。其中的attach_index会替换默认的最后n_heads个输出的选择。它必须选择一个范围或"all"。整数索引会导致构造函数失败。此类将in_channels注释为list[int],因此 BaseNode 会引发 IncompatibleError。具有自己类注释的子类会隐藏此注释,例如 PrecisionSegmentBBoxHead。对于此类子类,构造函数会引发TypeError。
fit_stride_to_heads
Python
1def fit_stride_to_heads(self) -> Tensor:n_heads 个大小中读取。它从每个大小的索引 2 中获取 Hi,因此大小必须具有 [B, C, H, W] 的形式。构造函数将结果存储在 stride 中。类示例显示了结果。返回Tensor: 形状为[n_heads]的int32张量。
get_custom_head_config
Python
1def get_custom_head_config(self) -> Params:"subtype"。返回Params: 包含键"iou_threshold"、"conf_threshold"、"max_det"和"strides"的字典。它们分别包含iou_thres、conf_thres、max_det和stride,其中stride是一个列表,每个尺度一个整数。
get_output_names
Python
1def get_output_names(default: list[str]) -> list[str]:default 名称。子类在其 export_output_names 属性中调用此方法。该方法通过 BaseNode.export_output_names 读取 export_output_names 构造函数参数。它不读取调用此方法的子类的属性。当构造函数参数的名称数量为 n_heads 时,它返回这些名称。否则,它会记录警告并返回 default。当参数为 None 时,它也会记录警告。警告: 该方法将名称数量与 n_heads 进行比较,而不是与 default 的长度进行比较。因此,具有多于 n_heads 个输出的子类永远无法从参数中获取所有输出的名称。示例
Pycon
1>>> from torch import Size
2>>> from luxonis_train.nodes import EfficientBBoxHead
3>>> sizes = [Size([1, 8, 32, 32]), Size([1, 16, 16, 16])]
4>>> def build_head(names):
5... return EfficientBBoxHead(
6... n_heads=2,
7... n_classes=3,
8... input_shapes=[{"features": sizes}],
9... original_in_shape=Size([3, 256, 256]),
10... export_output_names=names,
11... )
12>>> build_head(["small", "large"]).get_output_names(["a", "b"])
13['small', 'large']Pycon
1>>> from loguru import logger
2>>> logger.disable("luxonis_train")
3>>> build_head(["boxes"]).get_output_names(["a", "b"])
4['a', 'b']
5>>> logger.enable("luxonis_train")default(list[str]): 当构造函数参数为None或长度错误时返回的名称。子类给出 DepthAI 接受的名称。
list[str]: 构造函数参数的名称,或default。
request_detections_pre_nms
Python
1def request_detections_pre_nms(self):"detections_pre_nms" 键。其值为 NMS 输入:形状为 [B, N, 5 + n_classes] 的张量,其中 N 个锚点中的每个锚点对应一行。每行包含像素坐标中的 xyxy 框、一个常量 1 和类别分数。如果头保留了每个框的更多值,例如关键点或掩码系数,则会在类别分数之后添加列。该张量可能会占用大量内存,因此头仅在请求时才保留它。PrecisionRecallCurve 在其构造函数中调用此方法。示例
Pycon
1>>> import torch
2>>> from torch import Size
3>>> from luxonis_train.nodes import EfficientBBoxHead
4>>> sizes = [Size([1, 8, 32, 32]), Size([1, 16, 16, 16])]
5>>> head = EfficientBBoxHead(
6... n_heads=2,
7... n_classes=3,
8... input_shapes=[{"features": sizes}],
9... original_in_shape=Size([3, 256, 256]),
10... )
11>>> head.keep_detections_pre_nms
12False
13>>> head.request_detections_pre_nms()
14>>> out = head.eval()([torch.zeros(size) for size in sizes])
15>>> out["detections_pre_nms"].shape
16torch.Size([1, 1280, 8])属性
attach_index
in_channels
RuntimeError: 当 in_sizes 找不到输入大小时。ValueError: 当 attach_index 不适合大小时。
in_sizes
in_sizes构造函数参数,如果已设置。- 唯一输入数据包的
"features"条目。 - 该数据包的唯一条目。
- 该数据包中键与
forward参数名称匹配的条目。它们的大小必须相等,并且该属性使用第一个。
"all" 或范围的大小列表。具有多个输入或形状不符合规则的节点必须改为读取 input_shapes。示例
Pycon
1>>> from torch import Size, Tensor
2>>> from luxonis_train.nodes import BaseNode
3>>> class Node(BaseNode, register=False):
4... def forward(self, x: list[Tensor]) -> list[Tensor]:
5... return x
6>>> shapes = [
7... {"features": [Size([2, 8, 64, 64]), Size([2, 16, 32, 32])]}
8... ]
9>>> node = Node(input_shapes=shapes)
10>>> node.attach_index
11'all'
12>>> node.in_sizes
13[torch.Size([2, 8, 64, 64]), torch.Size([2, 16, 32, 32])]
14>>> node.in_channels, node.in_height, node.in_width
15([8, 16], [64, 32], [64, 32])RuntimeError: 当input_shapes缺失或不包含恰好一个数据包时。当没有键与forward参数匹配,或者匹配的大小不同时。当 attach_index 为None且条目为列表时。ValueError: 当 attach_index 不适合大小时。
iou_thres
keep_detections_pre_nms
False。当它为 True 时,子类的评估数据包也包含 "detections_pre_nms" 键。max_det
parser
"YOLO"。子类可以替换它。stride
[n_heads] 的 int32 张量。fit_stride_to_heads 计算它。