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

VarifocalLoss在Yolov8中的应用

调用VFL Loss

  • 在ultralytics/utils/loss.py可以发现v8实现了VarifocalLoss,但是好像和原论文有点不一样,这里有待考证
  • 原文地址:论文
  • 在cls损失处
 # Cls loss
        loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum  # VFL way
        # loss[1] = self.bce(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum  # BCE

这里可以看到调用varifocal_loss的地方是注释的,同时里面的target_labels是找不到的

实现损失的计算
  1. _, target_bboxes, target_scores, fg_mask, _ = self.assigner替换为target_labels,…具体如下
target_labels, target_bboxes, target_scores, fg_mask, _ = self.assigner(
            pred_scores.detach().sigmoid(),
            (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype),
            anchor_points * stride_tensor,
            gt_labels,
            gt_bboxes,
            mask_gt,
        )

  1. 本人找到了两种处理target_labels的方法,建议第二种,官方认证github issues
    第一种:target_labels = torch.where(target_scores > 0 , 1, 0)
    第二种:
target_labels = target_labels.unsqueeze(-1).expand(-1, -1, self.nc)  # self.nc: class num
one_hot = torch.zeros(target_labels.size(), device=self.device)
target_labels = one_hot.scatter_(-1, target_labels, 1)
  1. 完整代码
class v8DetectionLoss:
    """Criterion class for computing training losses."""

    def __init__(self, model):  # model must be de-paralleled
        """Initializes v8DetectionLoss with the model, defining model-related properties and BCE loss function."""
        device = next(model.parameters()).device  # get model device
        h = model.args  # hyperparameters
        # import ipdb;ipdb.set_trace()
        m = model.model[-1]  # Detect() module
        self.bce = nn.BCEWithLogitsLoss(reduction="none")
        self.hyp = h
        self.stride = m.stride  # model strides
        self.nc = m.nc  # number of classes
        self.no = m.nc + m.reg_max * 4
        self.reg_max = m.reg_max
        self.device = device
        self.varifocal_loss=VarifocalLoss().to(device)
        self.use_dfl = m.reg_max > 1

        self.assigner = TaskAlignedAssigner(topk=10, num_classes=self.nc, alpha=0.5, beta=6.0)
        self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=self.use_dfl).to(device)
        self.proj = torch.arange(m.reg_max, dtype=torch.float, device=device)

    def preprocess(self, targets, batch_size, scale_tensor):
        """Preprocesses the target counts and matches with the input batch size to output a tensor."""
        if targets.shape[0] == 0:
            out = torch.zeros(batch_size, 0, 5, device=self.device)
        else:
            i = targets[:, 0]  # image index
            _, counts = i.unique(return_counts=True)
            counts = counts.to(dtype=torch.int32)
            out = torch.zeros(batch_size, counts.max(), 5, device=self.device)
            for j in range(batch_size):
                matches = i == j
                n = matches.sum()
                if n:
                    out[j, :n] = targets[matches, 1:]
            out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor))
        return out

    def bbox_decode(self, anchor_points, pred_dist):
        """Decode predicted object bounding box coordinates from anchor points and distribution."""
        if self.use_dfl:
            b, a, c = pred_dist.shape  # batch, anchors, channels
            pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype))
            # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype))
            # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2)
        return dist2bbox(pred_dist, anchor_points, xywh=False)

    def __call__(self, preds, batch):
        """Calculate the sum of the loss for box, cls and dfl multiplied by batch size."""
        loss = torch.zeros(3, device=self.device)  # box, cls, dfl
        feats = preds[1] if isinstance(preds, tuple) else preds
        pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(
            (self.reg_max * 4, self.nc), 1
        )

        pred_scores = pred_scores.permute(0, 2, 1).contiguous()
        pred_distri = pred_distri.permute(0, 2, 1).contiguous()

        dtype = pred_scores.dtype
        batch_size = pred_scores.shape[0]
        imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0]  # image size (h,w)
        anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5)

        # Targets
        targets = torch.cat((batch["batch_idx"].view(-1, 1), batch["cls"].view(-1, 1), batch["bboxes"]), 1)
        targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]])
        gt_labels, gt_bboxes = targets.split((1, 4), 2)  # cls, xyxy
        mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0)

        # Pboxes
        pred_bboxes = self.bbox_decode(anchor_points, pred_distri)  # xyxy, (b, h*w, 4)

        target_labels, target_bboxes, target_scores, fg_mask, _ = self.assigner(
            pred_scores.detach().sigmoid(),
            (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype),
            anchor_points * stride_tensor,
            gt_labels,
            gt_bboxes,
            mask_gt,
        )

        target_scores_sum = max(target_scores.sum(), 1)
        # target_labels = torch.where(target_scores > 0 , 1, 0)
        target_labels = target_labels.unsqueeze(-1).expand(-1, -1, self.nc)  # self.nc: class num
        one_hot = torch.zeros(target_labels.size(), device=self.device)
        target_labels = one_hot.scatter_(-1, target_labels, 1)
        # Cls loss
        loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum  # VFL way
        # loss[1] = self.bce(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum  # BCE

        # Bbox loss
        if fg_mask.sum():
            target_bboxes /= stride_tensor
            loss[0], loss[2] = self.bbox_loss(
                pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask
            )

        loss[0] *= self.hyp.box  # box gain
        loss[1] *= self.hyp.cls  # cls gain
        loss[2] *= self.hyp.dfl  # dfl gain

        return loss.sum() * batch_size, loss.detach()  # loss(box, cls, dfl)
参考

参考1
参考2
参考3


http://www.kler.cn/a/445378.html

相关文章:

  • 本地缓存和Redis缓存 存储更新时间的更新套路
  • 【Flutter_Web】Flutter编译Web第二篇(webview篇):flutter_inappwebview如何改造方法,变成web之后数据如何交互
  • OpenCV 学习记录:首篇
  • redis数据类型:list
  • 在 Unity 6 中使用APV为您的世界创建全局照明的新方法(一)
  • day38-SSH安全登录
  • SpringBoot 启动类 SpringApplication 一 构造器方法
  • ESP32物联网开发
  • ChatGPT等大语言模型与水文水资源、水环境领域的深度融合
  • Spring(一)---IOC(控制权反转)
  • Vue.js前端框架教程10:Vue生命周期钩子onBeforeMount 和onMounted
  • ctf文件包含
  • [创业之路-200]:什么是business(业务)?B2B, B2C, B2G业务, 什么是业务设计?
  • 任务一登录安全加固
  • 前端TypeScript学习day03-TS高级类型
  • 任务2 配置防火墙firewalld
  • MySQL——EXPLAIN
  • 深入理解 C++ 中 std::vector 和 std::set 容器的使用
  • 机器学习中做时间序列模型考虑把时间作为特征分析
  • Java 后端给前端返回的long精度缺失,导致数据不一致
  • 京东大数据治理探索与实践 | 京东零售技术实践
  • 构建全方位大健康零售帮助中心:提升服务与体验
  • ES6学习Set 、Map(六)
  • 新能源汽车产销数据分析
  • MYSQL_联合查询(全)
  • MacOS安装MySQL