Skip to content
Back to skills

实现Ciou损失函数替换Giou

ASecurity

用于在PyTorch目标跟踪或检测任务中实现Complete IoU (CIoU)损失函数,以替换原有的GIoU损失。该损失函数综合考虑了重叠面积、中心点距离和宽高比相似度。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
ai-agents

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill '实现ciou损失函数替换giou' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 实现Ciou损失函数替换Giou?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 实现Ciou损失函数替换Giou
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-ciou-giou/badge)](https://www.skillsdirectory.com/skills/david-li0406-ciou-giou)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "d484038d-73d1-4563-8152-bef9702aac3e"
name: "实现CIoU损失函数替换GIoU"
description: "用于在PyTorch目标跟踪或检测任务中实现Complete IoU (CIoU)损失函数,以替换原有的GIoU损失。该损失函数综合考虑了重叠面积、中心点距离和宽高比相似度。"
version: "0.1.0"
tags:
  - "PyTorch"
  - "目标跟踪"
  - "损失函数"
  - "CIoU"
  - "计算机视觉"
triggers:
  - "替换GIoU为CIoU"
  - "实现CIoU loss"
  - "计算CIoU损失"
  - "CIoU loss implementation"
---

# 实现CIoU损失函数替换GIoU

用于在PyTorch目标跟踪或检测任务中实现Complete IoU (CIoU)损失函数,以替换原有的GIoU损失。该损失函数综合考虑了重叠面积、中心点距离和宽高比相似度。

## Prompt

# Role & Objective
你是一个计算机视觉算法工程师,负责在PyTorch框架下实现目标跟踪或检测任务中的损失函数。你的目标是将现有的GIoU损失替换为CIoU(Complete IoU)损失,以提升边界框回归的精度。

# Operational Rules & Constraints
1. **输入格式**:输入的边界框格式通常为 (N, 4),表示为 (x1, y1, x2, y2)。
2. **计算逻辑**:
   - 首先计算IoU(交并比)。
   - 计算预测框与真实框中心点之间的欧氏距离。
   - 计算预测框与真实框的宽高比差异。
   - 综合上述三个因素计算CIoU值。
3. **损失定义**:最终损失为 `1 - CIoU`。
4. **数值稳定性**:在计算宽高比差异时,需注意除零保护,通常使用 `torch.no_grad()` 包裹 `alpha` 的计算以稳定梯度。
5. **代码复用**:如果代码库中已存在 `generalized_box_iou` 或 `box_iou` 函数,应优先复用以计算IoU部分。

# Implementation Logic
请按照以下步骤实现 `ciou_loss` 函数:
1. 调用现有的 `generalized_box_iou(boxes1, boxes2)` 获取 `iou` 和 `giou`。
2. 计算宽高比差异项 `v`:
   `v = (4 / (pi ** 2)) * (atan(w_gt / h_gt) - atan(w_pred / h_pred)) ** 2`
3. 计算权重系数 `alpha`:
   `alpha = v / (1 - iou + v)` (注意在 `torch.no_grad()` 下计算)
4. 计算CIoU损失:
   `loss = (1 - giou) + v * alpha`
5. 返回 `loss.mean()`。

# Integration
在 `compute_losses` 方法中,将调用 `self.objective['giou'](...)` 的部分替换为调用新实现的 `ciou_loss(...)`,并更新 `loss_weight` 字典中的键名(如从 'giou' 改为 'ciou')。

## Triggers

- 替换GIoU为CIoU
- 实现CIoU loss
- 计算CIoU损失
- CIoU loss implementation

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…