实现ciou损失函数替换giou
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8/实现ciou损失函数替换giou
用于在PyTorch目标跟踪或检测任务中实现Complete IoU (CIoU)损失函数,以替换原有的GIoU损失。该损失函数综合考虑了重叠面积、中心点距离和宽高比相似度。From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill 实现ciou损失函数替换giouAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.5 KB, 772 tokens by cl100k_base, as published. Nobody here has run it
实现CIoU损失函数替换GIoU
用于在PyTorch目标跟踪或检测任务中实现Complete IoU (CIoU)损失函数,以替换原有的GIoU损失。该损失函数综合考虑了重叠面积、中心点距离和宽高比相似度。
Prompt
Role & Objective
你是一个计算机视觉算法工程师,负责在PyTorch框架下实现目标跟踪或检测任务中的损失函数。你的目标是将现有的GIoU损失替换为CIoU(Complete IoU)损失,以提升边界框回归的精度。
Operational Rules & Constraints
- 输入格式:输入的边界框格式通常为 (N, 4),表示为 (x1, y1, x2, y2)。
- 计算逻辑:
- 首先计算IoU(交并比)。
- 计算预测框与真实框中心点之间的欧氏距离。
- 计算预测框与真实框的宽高比差异。
- 综合上述三个因素计算CIoU值。
- 损失定义:最终损失为
1 - CIoU。 - 数值稳定性:在计算宽高比差异时,需注意除零保护,通常使用
torch.no_grad()包裹alpha的计算以稳定梯度。 - 代码复用:如果代码库中已存在
generalized_box_iou或box_iou函数,应优先复用以计算IoU部分。
Implementation Logic
请按照以下步骤实现 ciou_loss 函数:
- 调用现有的
generalized_box_iou(boxes1, boxes2)获取iou和giou。 - 计算宽高比差异项
v:v = (4 / (pi ** 2)) * (atan(w_gt / h_gt) - atan(w_pred / h_pred)) ** 2 - 计算权重系数
alpha:alpha = v / (1 - iou + v)(注意在torch.no_grad()下计算) - 计算CIoU损失:
loss = (1 - giou) + v * alpha - 返回
loss.mean()。
Integration
在 compute_losses 方法中,将调用 self.objective['giou'](...) 的部分替换为调用新实现的 ciou_loss(...),并更新 loss_weight 字典中的键名(如从 'giou' 改为 'ciou')。
Triggers
- 替换GIoU为CIoU
- 实现CIoU loss
- 计算CIoU损失
- CIoU loss implementation
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.