ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

目标检测 YOLO 系列:快速迭代 YOLO v5

2021-08-01 12:30:11  阅读:615  来源: 互联网

标签:loss YOLOv5 torch 迭代 YOLO v5 device model targets


目标检测 YOLO 系列:快速迭代 YOLO v5

作者:Glenn Jocher
发表时间:2020
Paper 原文:没有发表论文,通过 github(yolov5) 发布。

1. 概览

YOLOv5 刚发布之初还颇有争议,有人觉得它能不能叫 YOLOv5,但是它凭借优秀的性能和完善的工程配套(移植到其他平台)能力,现在(2021年)YOLOv5 依然是检测领域最活跃的模型。YOLOv5 不仅生而不凡,更关键的是它还非常勤快,从发布至今,已经发布了 5 个大版本。所以在使用 YOLOv5 的时候需要注意它的小版本。下面是 各个小版本网络结构上的区别。

  • YOLOv5 5.0

    • P5 结构和 4.0 一致

    • P6 4 个输出层(stride 分别为 8,16,32,64)

  • YOLOv5 4.0

    • 将 YOLOv5 3.0 nn.LeakyReLU(0.1) 和 nn.Hardswish() 替换为 nn.SiLU()
  • YOLOv5 3.1

    • 主要是 bug fix
  • YOLOv5 3.0

    • 采样 nn.Hardswish() 激活函数
  • YOLOv5 2.0

    • 结构不变,主要是 bugfix,但是于 1.0 有兼容性问题
  • YOLOv5 1.0

    • 横空出世

除开小版本之外,为了方便进行不同场景下模型的选择,又分为 s, m, l, x 四种不同大小的网络结构。

2. 网络结构

网络结构建议参考 深入浅出Yolo系列之Yolov5核心基础知识完整讲解,里面有高清大图,非常详细。整的来说,相比 v4,主要是加入了 Focus 层,然后激活函数有调整,整体网络架构还是比较类似的。

3. 损失函数

以 5.0 的代码为例,loss 中可以通过设置 fl_gamma 来激活 focal loss。另外 IOU loss 也支持 GIOU, DIOU, CIOU。

def compute_loss(p, targets, model):  # predictions, targets, model
    device = targets.device
    lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device)
    tcls, tbox, indices, anchors = build_targets(p, targets, model)  # targets
    h = model.hyp  # hyperparameters

    # Define criteria
    BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device))  # weight=model.class_weights)
    BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device))

    # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
    cp, cn = smooth_BCE(eps=0.0)

    # Focal loss
    g = h['fl_gamma']  # focal loss gamma
    if g > 0:
        BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)

    # Losses
    nt = 0  # number of targets
    no = len(p)  # number of outputs
    balance = [4.0, 1.0, 0.3, 0.1, 0.03]  # P3-P7
    for i, pi in enumerate(p):  # layer index, layer predictions
        b, a, gj, gi = indices[i]  # image, anchor, gridy, gridx
        tobj = torch.zeros_like(pi[..., 0], device=device)  # target obj

        n = b.shape[0]  # number of targets
        if n:
            nt += n  # cumulative targets
            ps = pi[b, a, gj, gi]  # prediction subset corresponding to targets

            # Regression
            pxy = ps[:, :2].sigmoid() * 2. - 0.5
            pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
            pbox = torch.cat((pxy, pwh), 1)  # predicted box
            iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)  # iou(prediction, target)
            lbox += (1.0 - iou).mean()  # iou loss

            # Objectness
            tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * iou.detach().clamp(0).type(tobj.dtype)  # iou ratio

            # Classification
            if model.nc > 1:  # cls loss (only if multiple classes)
                t = torch.full_like(ps[:, 5:], cn, device=device)  # targets
                t[range(n), tcls[i]] = cp
                lcls += BCEcls(ps[:, 5:], t)  # BCE

            # Append targets to text file
            # with open('targets.txt', 'a') as file:
            #     [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]

        lobj += BCEobj(pi[..., 4], tobj) * balance[i]  # obj loss

    s = 3 / no  # output count scaling
    lbox *= h['box'] * s
    lobj *= h['obj']
    lcls *= h['cls'] * s
    bs = tobj.shape[0]  # batch size

    loss = lbox + lobj + lcls
    return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach()

4. 后处理

YOLO v5 的后处理代码看起来有点费劲,我整理了一下后处理的流程图,可以作为参考。

在这里插入图片描述

5. 性能

YOLO v5 在精度上和 v4 相比可能并没有优势,但是速度(训练和推理,特别是在训练阶段优势明显)上却有很多的优势,另外就是工程化能力也是优势。

参考

标签:loss,YOLOv5,torch,迭代,YOLO,v5,device,model,targets
来源: https://blog.csdn.net/kuweicai/article/details/119296670

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有