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

本页目录

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

softmax_focal_loss

最后更新
Python API: luxonis_train.attached_modules.losses.softmax_focal_lossSoftmax 焦点损失,它降低了简单样本的权重。

类

SoftmaxFocalLoss

多类预测的 softmax 输出上的焦点损失。损失在类别维度上应用 softmax。焦点因子降低了目标类别已具有高概率的元素的损失。
  • 输入::
    • predictions (Tensor): [B, C, ...] logits,其中 C ≥ 2
    • targets (Tensor): 相同形状,one-hot
  • 输出::
    • Tensor: 标量,或当 reduction 为 "none" 时为 [B, ...]
  • 公式:: 在一个元素上,pc 是类别 c 的 softmax 概率,yc 是其目标。对于平滑因子 s > 0,损失首先将每个目标裁剪到 [s ⁄ (C − 1), 1 − s]。然后元素的损失为 pt = ∑cyc pc + s,ℓ = − αt (1 − pt)γlogpt 浮点数 alpha 是每个元素的 αt。列表 alpha 从目标类别的条目(目标的 argmax)给出 αt。然后 reduction 对所有元素取平均值或求和,或保留每个元素的损失。

方法

init
Python
1def __init__(alpha: float | list[float] = 0.25, gamma: float = 2.0, smooth: float = 0.0, reduction: Literal['none', 'mean', 'sum'] = 'mean', **kwargs):
初始化损失并检查平滑因子。参数
  • alpha (float | list[float]): 类别权重 αt。浮点数以相同因子缩放每个元素的损失,因此它不偏向某个类别。列表按类别顺序为每个类别保留一个权重。forward 然后检查列表是否为每个类别包含一个条目。
  • gamma (float): 焦点因子 (1 − pt) 的指数。0 关闭焦点因子。
  • smooth (float): 标签平滑因子 s,在 [0, 1] 中。类别公式显示了它如何改变目标和 pt。
  • reduction (Literal['none', 'mean', 'sum']): 如何减少元素的损失:
    • "none": 返回每个元素的损失。
    • "mean": 返回所有元素的平均值。
    • "sum": 返回所有元素的总和。 forward 将任何其他值视为 "none"。
  • **kwargs: 转发给 BaseLoss 的关键字参数,例如 final_loss_weight 和 node。
引发
  • ValueError: 当 smooth 超出 [0, 1] 时。
forward
Python
1def forward(predictions: Tensor, targets: Tensor) -> Tensor:
计算 logits 和 targets 之间的 softmax 焦点损失。参数
  • predictions (Tensor): 形状为 [B, C, ...] 的 logits,至少包含两个类别。节点的主要输出。
  • targets (Tensor): 与 predictions 形状相同的 one-hot 目标。
返回
  • Tensor: 对于 "mean" 和 "sum" 归约,返回 float32 标量。对于任何其他 reduction,返回每个元素的损失,形状为 [B, ...]。
引发
  • ValueError: 当 predictions 的类别少于两个时,当形状不同时,或者当列表 alpha 没有为每个类别包含一个条目时。

属性

supported_tasks