此页面由 AI 自动翻译。查看英文原版
最后更新

本页目录

  • 类
  • TransformerClassificationHead
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包

transformer_classification_head

最后更新
Python API: luxonis_train.nodes.heads.transformer_classification_headTransformer 主干网络 CLS token 上的分类头。

类

TransformerClassificationHead

Transformer 主干网络 CLS token 的分类头。DinoV3 在其 return_sequence 参数为 True 时提供 CLS token。
  • 输入::
    • inputs (Tensor): [B, C] CLS token
  • 输出::
    • classification (Tensor): [B, nclasses] logits
  • 变体:: 无。通过 params 配置节点。

方法

init
Python
1def __init__(dropout_rate: float = 0.2, **kwargs):
构建 dropout 和线性层。线性层将 in_channels 值映射到 n_classes logits。参数
  • dropout_rate (float): 在训练模式下,dropout 层将 CLS token 值设置为零的概率,范围为 [0, 1]。该层将其他值按 1 ⁄ (1 − p) 缩放,其中 p 是 dropout_rate。默认为 0.2。
  • **kwargs: BaseNode 的关键字参数。它们必须通过 input_shapes 或 in_sizes 包含输入大小,并通过 n_classes 或 dataset_metadata 包含类别计数。
forward
Python
1def forward(x: Tensor) -> Tensor:
从 CLS token 计算类别 logits。该方法应用 dropout,然后应用线性层。dropout 仅在训练模式下起作用。参数
  • x (Tensor): 形状为 [B, C] 的 CLS token,其中 C 是嵌入大小。
返回
  • Tensor: 形状为 [B, n_classes] 的 logits。BaseNode.run 将它们放在 "classification" 键下。
get_custom_head_config
Python
1def get_custom_head_config(self) -> Params:
返回 NN Archive 的头部特定元数据。子类重写该方法以向其解析器提供更多值。get_head_config 将结果合并到 "metadata" 字典中。基本实现返回一个空字典。返回
  • Params: 附加元数据键及其值。

属性

attach_index
dropout
fc
in_channels
CLS token 的嵌入大小。它是 BaseNode.in_sizes 的最后一个维度,即输入形状 [B, C] 中的 C。它取代了 BaseNode.in_channels,后者从末尾读取第三个维度。引发
  • TypeError: 当 BaseNode.in_sizes 是大小列表时。这发生在 attach_index 为 "all" 或范围时。构造函数读取该属性,因此它也会引发错误。
parser