transformer_classification_head
最后更新
luxonis_train.nodes.heads.transformer_classification_headTransformer 主干网络 CLS token 上的分类头。类
TransformerClassificationHead
return_sequence 参数为 True 时提供 CLS token。输入::inputs(Tensor): [B, C] CLS token
输出::classification(Tensor): [B, nclasses] logits
参考
- 来源:本项目。
- 许可证:Apache-2.0(本项目)
注意事项
该头部对 CLS token 应用 dropout 和一个线性层。它没有池化,因为 CLS token 没有空间维度。dropout 仅在训练模式下起作用。该模式不改变输出键或形状。这包括导出模式。
变体:: 无。通过params配置节点。
示例
配置中
model.nodes 部分的节点条目:Yaml
1- name: TransformerClassificationHead
2 inputs: [DinoV3]兼容::- 附加索引:
-1,输入节点的最后一个输出 - 所需标签:
classification - 损失:
- 指标:
- 可视化工具:ClassificationVisualizer
- 导出解析器:
ClassificationParser
- 附加索引:
方法
init
Python
1def __init__(dropout_rate: float = 0.2, **kwargs):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:示例
Pycon
1>>> import torch
2>>> from torch import Size
3>>> from luxonis_train.nodes import TransformerClassificationHead
4>>> head = TransformerClassificationHead(
5... n_classes=5,
6... input_shapes=[{"features": [Size([2, 384])]}],
7... )
8>>> head.in_channels
9384
10>>> head(torch.zeros(2, 384)).shape
11torch.Size([2, 5])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:"metadata" 字典中。基本实现返回一个空字典。返回Params: 附加元数据键及其值。
属性
attach_index
dropout
fc
in_channels
[B, C] 中的 C。它取代了 BaseNode.in_channels,后者从末尾读取第三个维度。引发TypeError: 当 BaseNode.in_sizes 是大小列表时。这发生在attach_index为"all"或范围时。构造函数读取该属性,因此它也会引发错误。