当前位置: 首页 > news >正文

添加损失函数Alpha-IoU

添加损失函数代码:

class DFLoss(nn.Module):"""Criterion class for computing Distribution Focal Loss (DFL)."""def __init__(self, reg_max: int = 16) -> None:"""Initialize the DFL module with regularization maximum."""super().__init__()self.reg_max = reg_maxdef __call__(self, pred_dist: torch.Tensor, target: torch.Tensor) -> torch.Tensor:"""Return sum of left and right DFL losses from https://ieeexplore.ieee.org/document/9792391."""target = target.clamp_(0, self.reg_max - 1 - 0.01)tl = target.long()  # target lefttr = tl + 1  # target rightwl = tr - target  # weight leftwr = 1 - wl  # weight rightreturn (F.cross_entropy(pred_dist, tl.view(-1), reduction="none").view(tl.shape) * wl+ F.cross_entropy(pred_dist, tr.view(-1), reduction="none").view(tl.shape) * wr).mean(-1, keepdim=True)

还需要在BboxLoss 里调用 Alpha-IoU:

class BboxLoss(nn.Module):"""Criterion class for computing training losses for bounding boxes."""def __init__(self, reg_max: int = 16, alpha: float = 0.5):"""Initialize the BboxLoss module with regularization maximum and DFL settings."""super().__init__()self.dfl_loss = DFLoss(reg_max) if reg_max > 1 else Noneself.alpha_iou = AlphaIouLoss(alpha=alpha, reduction="none")  # 用 Alpha-IoUdef forward(self,pred_dist: torch.Tensor,pred_bboxes: torch.Tensor,anchor_points: torch.Tensor,target_bboxes: torch.Tensor,target_scores: torch.Tensor,target_scores_sum: torch.Tensor,fg_mask: torch.Tensor,) -> Tuple[torch.Tensor, torch.Tensor]:"""Compute IoU (Alpha-IoU) and DFL losses for bounding boxes."""weight = target_scores.sum(-1)[fg_mask].unsqueeze(-1)# 用 Alpha-IoU 代替原来的 CIoUloss_iou = (self.alpha_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask]) * weight).sum() / target_scores_sum# DFL lossif self.dfl_loss:target_ltrb = bbox2dist(anchor_points, target_bboxes, self.dfl_loss.reg_max - 1)loss_dfl = self.dfl_loss(pred_dist[fg_mask].view(-1, self.dfl_loss.reg_max),target_ltrb[fg_mask]) * weightloss_dfl = loss_dfl.sum() / target_scores_sumelse:loss_dfl = torch.tensor(0.0).to(pred_dist.device)return loss_iou, loss_dfl

 

整合数据集配置 +训练超参数配置 == 训练配置文件:

# COCO 2017 dataset http://cocodataset.org# download command/URL (optional)
#download: bash ./scripts/get_coco.sh# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
# 数据集配置文件
train: ./datasets/wzry700/train_list.txt  # 118287 images
val: ./datasets/wzry700/val_list.txt  # 5000 images
#test: ./coco/test-dev2017.txt  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794# number of classes
nc: 2# class names
names: [ 'locust', 'longicorn' ]# hyp.yaml
# 训练超参数
lr0: 0.01          # 初始学习率
lrf: 0.1           # 最终学习率比例
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3
warmup_momentum: 0.8
warmup_bias_lr: 0.1
# 损失函数相关
box: 0.05          # box 损失权重
cls: 0.3           # 分类损失权重
dfl: 1.0           # DFL (Distribution Focal Loss) 权重
iou_loss: alpha_iou
alpha: 0.5
cls_loss: varifocal
obj_gamma: 2.0
# 其他
fl_gamma: 2.0
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 2.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
mosaic: 0.7
mixup: 0.15

 

http://www.sczhlp.com/news/47373/

相关文章:

  • 案例3-2_numpy数组
  • NVFP4量化技术深度解析:4位精度下实现2.3倍推理加速
  • acm社团题解
  • 搜维尔科技:Tesollo灵巧手推出四指夹持器显示薄膜剥离解决方案
  • 怎么在58同城上做网站关键词查询神器
  • 网站制作中企动力公司四年级摘抄一小段新闻
  • 长沙市网站建设网络营销推广实战宝典
  • 设置网站首页神马推广登录
  • 甘肃电子商务网站建设网站建设黄页
  • 怎么免费做网站教程廊坊seo排名收费
  • HTB_Appointment练习记录——
  • 网站多久会被百度收录网络营销与电子商务的区别
  • 怎么建免费企业官网站百度搜索量怎么查
  • 做网站用什手机卡顿优化软件
  • 做网站卖什么网络推广主要做什么
  • 网络运营培训学校长沙优化排名
  • 商城网站支付端怎么做青岛网站设计制作
  • 机械类外贸网站建设广州seo技术优化网站seo
  • 湖南企业建站系统平台制作公司网站的步骤
  • 20220307_QQ_rsa_mp
  • P9352 [JOI 2023 Final] 训猫 / Cat Exercise
  • 【beegfs】搭建beegfs集群
  • 绥芬河市建设局网站怎么样推广自己的公司
  • 江门生活网seo81
  • 江西 网站 建设 开发百度学术官网入口
  • 中山精品网站建设精英百度搜索推广多少钱
  • 河南郑州做网站汉狮做微商如何引流推广怎么找客源
  • 蜘蛛抓取网站url河南网站开发公司
  • 建设商城网站公司百度百科平台营销
  • 建设银行网站怎么看不见余额seo网络培训