YOLOv7默默更新了Anchor-Free | 无痛再涨1.4个mAP

点击下方卡片,关注「集智书童」公众号

点击加入👉「集智书童-YOLO算法」交流群

「首先恭喜YOLOv7登录CVPR2023的顶会列车!!!」

YOLOv7-u6分支的实现是基于Yolov5和Yolov6进行的。并在此基础上开发了Anchor-Free方法。所有安装、数据准备和使用与Yolov5相同,大家可以酌情尝试,如果电费不要钱,那就不要犹豫了!!!

先看原始的YOLOv7的精度

picture.image

当时原始版本就是无敌的存在,YOLOv7的base版本就有51.2的精度了!!!

再看Anchor-Free版本YOLOv7的精度

picture.image

再看原作复现的Anchor-Free版本,相对于原始版本的51.2的精度,分别提升了1.1个点和1.4个点(使用了albumentation数据增强),可以看出还是很给力的结构。

架构改进部分

其实,关于复现的YOLOv7-u6(Anchor-Free),Backbone和Neck部分是没有发生变化的,下面看一下Head部分的变化。

1、YOLOv7的Anchor-Base Head

通过下图的YAML知道,YOLOv7的head使用了重参结构,并且也加入了隐藏知识Trick的加入。

picture.image

2、YOLOv7的Anchor-Free Head

去除了RepConv卷积,使用了最为基本的Conv模块,同时检测头换为了YOLOv6的Head形式,同时加入了IDetect的隐藏知识Implicit层思想。

picture.image

3、IV6Detect的实现如下


                      
                        
class IV6Detect(nn.Module):  
    dynamic = False  # force grid reconstruction  
    export = False  # export mode  
    shape = None  
    anchors = torch.empty(0)  # init  
    strides = torch.empty(0)  # init  
  
    def \_\_init\_\_(self, nc=80, ch=(), inplace=True):  # detection layer  
        super().__init__()  
        self.nc = nc  # number of classes  
        self.nl = len(ch)  # number of detection layers  
        self.reg_max = 16  
        self.no = nc + self.reg_max * 4  # number of outputs per anchor  
        self.inplace = inplace  # use inplace ops (e.g. slice assignment)  
        self.stride = torch.zeros(self.nl)  # strides computed during build  
  
        c2, c3 = max(ch[0] // 4, 16), max(ch[0], self.no - 4)  # channels  
        self.cv2 = nn.ModuleList(  
            nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3), nn.Conv2d(c2, 4 * self.reg_max, 1)) for x in ch)  
        self.cv3 = nn.ModuleList(  
            nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch)  
        # DFL层  
        self.dfl = DFL(self.reg_max)  
        # Implicit层  
        self.ia2 = nn.ModuleList(ImplicitA(x) for x in ch)  
        self.ia3 = nn.ModuleList(ImplicitA(x) for x in ch)  
        self.im2 = nn.ModuleList(ImplicitM(4 * self.reg_max) for _ in ch)  
        self.im3 = nn.ModuleList(ImplicitM(self.nc) for _ in ch)  
  
    def forward(self, x):  
        shape = x[0].shape  # BCHW  
        for i in range(self.nl):  
            x[i] = torch.cat((self.im2[i](self.cv2[i](self.ia2[i](x[i]))), self.im3[i](self.cv3[i](self.ia3[i](x[i])))), 1)  
        box, cls = torch.cat([xi.view(shape[0], self.no, -1) for xi in x], 2).split((self.reg_max * 4, self.nc), 1)  
        if self.training:  
            return x, box, cls  
        elif self.dynamic or self.shape != shape:  
            self.anchors, self.strides = (x.transpose(0, 1) for x in make_anchors(x, self.stride, 0.5))  
            self.shape = shape  
  
        dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides  
        y = torch.cat((dbox, cls.sigmoid()), 1)  
        return y if self.export else (y, (x, box, cls))  
  
    def bias\_init(self):  
        m = self  # self.model[-1]  # Detect() module  
        for a, b, s in zip(m.cv2, m.cv3, m.stride):  # from  
            a[-1].bias.data[:] = 1.0  # box  
            b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2)   

                    

关于损失函数与样本匹配的穿搭

一句话吧,其实就是YOLOv8本来的样子,也可能YOLOv8是原来YOLOv7-u6本来的样子。使用了TaskAligned Assigner,BCE Loss、CIOU Loss以及DFL Loss。可以说是标准搭配了!!!


                      
                        
class ComputeLoss:  
    def \_\_init\_\_(self, model, use\_dfl=True):  
        device = next(model.parameters()).device  # get model device  
        h = model.hyp  # hyperparameters  
  
        # Define criteria  
        # 分类损失  
        BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls\_pw"]], device=device), reduction='none')  
  
        # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3  
        self.cp, self.cn = smooth_BCE(eps=h.get("label\_smoothing", 0.0))  # positive, negative BCE targets  
  
        # Focal loss  
        g = h["fl\_gamma"]  # focal loss gamma  
        if g > 0:  
            BCEcls = FocalLoss(BCEcls, g)  
  
        m = de_parallel(model).model[-1]  # Detect() module  
        self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02])  # P3-P7  
        self.BCEcls = BCEcls  
        self.hyp = h  
        self.stride = m.stride  # model strides  
        self.nc = m.nc  # number of classes  
        self.nl = m.nl  # number of layers  
        self.device = device  
        # 正负样本匹配  
        self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)),  
                                            num_classes=self.nc,  
                                            alpha=float(os.getenv('YOLOA', 0.5)),  
                                            beta=float(os.getenv('YOLOB', 6.0)))  
        # 回归损失函数  
        self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device)  
        self.proj = torch.arange(m.reg_max).float().to(device)  # / 120.0  
        self.use_dfl = use_dfl  

                    

参考

[1].https://github.com/WongKinYiu/yolov7/tree/u6.

推荐阅读

[picture.image

CVPR2023最新Backbone |FasterNet远超ShuffleNet、MobileNet、MobileViT等模型](https://mp.weixin.qq.com/s?__biz=MzU5OTA2Mjk5Mw==&mid=2247504366&idx=1&sn=d5496129aed9dae947a29b449221dab1&chksm=feb83150c9cfb846c4d162fa2dc7115856cd0029d032ae31fd1389c8b16d3e9b6e5dab6e1bd1&scene=21#wechat_redirect)

[picture.image

全新剪枝框架 | YOLOv5模型缩减4倍,推理速度提升2倍](https://mp.weixin.qq.com/s?__biz=MzU5OTA2Mjk5Mw==&mid=2247504323&idx=1&sn=a651ed5651dd3503dcf52f9be04a3d62&chksm=feb8317dc9cfb86ba029e7782458896f2ee9cd30516718961fda58ae9052734838b5aac4e9f9&scene=21#wechat_redirect)

[picture.image

即插即用! | 国防科大联合慕尼黑工业大学提出新型解耦头 TSCODE: 助力目标检测器轻松涨点!](https://mp.weixin.qq.com/s?__biz=MzU5OTA2Mjk5Mw==&mid=2247504262&idx=1&sn=486df84e5e7ab925b71dd5ebcb40c982&chksm=feb83138c9cfb82ed67a919acf75dacc4ba127eea3bdb9bded60cfe5691e6e3f29d3bf73f13a&scene=21#wechat_redirect)

picture.image

扫码加入👉「集智书童-YOLO算法」交流群

(备注: 方向+学校/公司+昵称 )

picture.image

picture.image

picture.image

picture.image

picture.image

picture.image

想要了解更多:

前沿AI视觉感知全栈知识👉「分类、检测、分割、关键点、车道线检测、3D视觉(分割、检测)、多模态、目标跟踪、NerF」

行业技术方案 👉「AI安防、AI医疗、AI自动驾驶」

AI模型部署落地实战 👉「CUDA、TensorRT、NCNN、OpenVINO、MNN、ONNXRuntime以及地平线框架」

欢迎扫描上方二维码,加入「 集智书童-知识星球 」,日常分享论文、学习笔记、问题解决方案、部署方案以及全栈式答疑,期待交流!

免责声明

凡本公众号注明“来源:XXX(非集智书童)”的作品,均转载自其它媒体,版权归原作者所有,如有侵权请联系我们删除,谢谢。

点击下方“ 阅读原文 ”,

了解更多AI学习路上的 「武功秘籍」

0
0
0
0
评论
未登录
暂无评论