位置: IT常识 - 正文

yolox改进--添加Coordinate Attention模块(CVPR2021)(yolo改进方法)

编辑:rootadmin
yolox改进--添加Coordinate Attention模块(CVPR2021) yolox改进--添加Coordinate Attention模块Coordinate Attention代码建立包含CAM代码的attention.py在yolo_pafpn.py中添加CAM总结

推荐整理分享yolox改进--添加Coordinate Attention模块(CVPR2021)(yolo改进方法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:yolov2改进,yolo增加检测层,yolov5如何改进,yolo增加检测层,改进yolov3,改进yolov3,yolov2改进,改进yolov3,内容如对您有帮助,希望把文章链接给更多的朋友!

因为项目需要,尝试魔改一下yolox-s,看看能不能在个人数据集上刷高点mAP。因为Coordinate Attention模块(以下简称CAM)的作者提供了代码,并且之前不少博主公开了CAM用在yolov5或者yolox等模型的代码,所以一开始我直接当了搬运工,但在搬运过程,我发现官方的代码不能直接用在yolox上,且之前公开CAM用在yolox的代码根本跑不通。在debug之后,发现问题是出现在官方的代码上,于是心血来潮写下这篇文章,废话不多说,来看修改后的代码吧!

Coordinate Attentionyolox改进--添加Coordinate Attention模块(CVPR2021)(yolo改进方法)

论文来源: http://arxiv.org/abs/2103.02907 官方代码:https://github.com/Andrew-Qibin/CoordAttention

注意力机制广泛用于深度神经网络中来提高模型的性能。然而,因为其昂贵的计算代价,很难应用在一些轻量级网络,但不乏有一些注意力模块脱颖而出,具有代表性的有SE、CBAM等。SE模块通过2D全局池化来计算通道注意力,在非常低的计算成本下达到了提升网络性能的目的,遗憾的是,SE模块忽视了捕获位置信息的注意力;CBAM模块通过使用大尺寸卷积来获得位置信息的注意力,但只偏向于捕获局部的位置信息。 CAM模块来源于2021CVPR,该模块通过将位置信息嵌入到通道注意力中,因为其较少的计算代价,使轻量级网可以较大的区域中获得注意力。为了缓解位置信息丢失的问题,论文作者将2D全局池化替换成分别在特征的w和h并行提取特征的两个1D池化,可以有效捕获空间坐标信息;而后这两个并行的特征图通过两个卷积来生成两个独立方向的注意力图;通过将两个注意力图乘入到原始特征图中,以达到增强特征图的表征能力。

代码建立包含CAM代码的attention.py

在./yolox/models/文件夹下建立attention.py,CAM代码如下。相较于官方的代码,为了适配yolox,这里将nn.AdaptiveAvgPool2d直接用于forward。

class CAM(nn.Module): def __init__(self, channels, reduction=32): super(CAM, self).__init__() self.conv_1x1 = nn.Conv2d(in_channels=channels, out_channels=channels // reduction, kernel_size=1, stride=1, bias=False) self.mish = Mish() # 可用自行选择激活函数 self.bn = nn.BatchNorm2d(channels // reduction) self.F_h = nn.Conv2d(in_channels=channels // reduction, out_channels=channels, kernel_size=1, stride=1, bias=False) self.F_w = nn.Conv2d(in_channels=channels // reduction, out_channels=channels, kernel_size=1, stride=1, bias=False) self.sigmoid_h = nn.Sigmoid() self.sigmoid_w = nn.Sigmoid() def forward(self, x): h, w = x.shape[2], x.shape[3] avg_pool_x = nn.AdaptiveAvgPool2d((h, 1)) avg_pool_y = nn.AdaptiveAvgPool2d((1, w)) x_h = avg_pool_x(x).permute(0, 1, 3, 2) x_w = avg_pool_y(x) x_cat_conv_relu = self.mish(self.conv_1x1(torch.cat((x_h, x_w), 3))) x_cat_conv_split_h, x_cat_conv_split_w = x_cat_conv_relu.split([h, w], 3) s_h = self.sigmoid_h(self.F_h(x_cat_conv_split_h.permute(0, 1, 3, 2))) s_w = self.sigmoid_w(self.F_w(x_cat_conv_split_w)) out = x * s_h.expand_as(x) * s_w.expand_as(x) return out在yolo_pafpn.py中添加CAM

CAM作为即插即用的注意力模块,添加位置可以完全替换例如CBAM等经典的注意力机制模块,具体可参考其他有关yolox在head中插入注意力机制的教程,这里给的代码以添加在pafpn为例,添加在哪效果好要取决于添加位置在特定数据集的表现。

#!/usr/bin/env python# -*- encoding: utf-8 -*-# Copyright (c) Megvii Inc. All rights reserved.import torchimport torch.nn as nnfrom .darknet import CSPDarknetfrom .network_blocks import BaseConv, CSPLayer, DWConvfrom .attention import CAMclass YOLOPAFPN(nn.Module): """ YOLOv3 model. Darknet 53 is the default backbone of this model. """ def __init__( self, depth=1.0, width=1.0, in_features=("dark3", "dark4", "dark5"), in_channels=[256, 512, 1024], depthwise=False, act="silu", ): super().__init__() self.backbone = CSPDarknet(depth, width, depthwise=depthwise, act=act) self.in_features = in_features self.in_channels = in_channels Conv = DWConv if depthwise else BaseConv self.upsample = nn.Upsample(scale_factor=2, mode="nearest") # self.upsample = nn.Upsample(scale_factor=2, mode="bilinear") self.lateral_conv0 = BaseConv( int(in_channels[2] * width), int(in_channels[1] * width), 1, 1, act=act ) self.C3_p4 = CSPLayer( int(2 * in_channels[1] * width), int(in_channels[1] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # cat self.reduce_conv1 = BaseConv( int(in_channels[1] * width), int(in_channels[0] * width), 1, 1, act=act ) self.C3_p3 = CSPLayer( int(2 * in_channels[0] * width), int(in_channels[0] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # bottom-up conv self.bu_conv2 = Conv( int(in_channels[0] * width), int(in_channels[0] * width), 3, 2, act=act ) self.C3_n3 = CSPLayer( int(2 * in_channels[0] * width), int(in_channels[1] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # bottom-up conv self.bu_conv1 = Conv( int(in_channels[1] * width), int(in_channels[1] * width), 3, 2, act=act ) self.C3_n4 = CSPLayer( int(2 * in_channels[1] * width), int(in_channels[2] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) self.CAM0 = CAM(int(in_channels[2] * width)) self.CAM1 = CAM(int(in_channels[1] * width)) self.CAM2 = CAM(int(in_channels[0] * width)) # self.CAM3 = CAM(int(in_channels[0] * width)) # self.CAM4 = CAM(int(in_channels[1] * width)) # self.CAM5 = CAM(int(in_channels[2] * width)) def forward(self, input): """ Args: inputs: input images. Returns: Tuple[Tensor]: FPN feature. """ # backbone out_features = self.backbone(input) features = [out_features[f] for f in self.in_features] [x2, x1, x0] = features #############add CAM############## x0 = self.CAM0(x0) x1 = self.CAM1(x1) x2 = self.CAM2(x2) ################################## fpn_out0 = self.lateral_conv0(x0) # 1024->512/32 f_out0 = self.upsample(fpn_out0) # 512/16 f_out0 = torch.cat([f_out0, x1], 1) # 512->1024/16 f_out0 = self.C3_p4(f_out0) # 1024->512/16 fpn_out1 = self.reduce_conv1(f_out0) # 512->256/16 f_out1 = self.upsample(fpn_out1) # 256/8 f_out1 = torch.cat([f_out1, x2], 1) # 256->512/8 pan_out2 = self.C3_p3(f_out1) # 512->256/8 # pan_out2 = self.CAM3(pan_out2) p_out1 = self.bu_conv2(pan_out2) # 256->256/16 p_out1 = torch.cat([p_out1, fpn_out1], 1) # 256->512/16 pan_out1 = self.C3_n3(p_out1) # 512->512/16 # p_out1 = self.CAM4(p_out1) p_out0 = self.bu_conv1(pan_out1) # 512->512/32 p_out0 = torch.cat([p_out0, fpn_out0], 1) # 512->1024/32 pan_out0 = self.C3_n4(p_out0) # 1024->1024/32 # pan_out0 = self.CAM5(pan_out0) outputs = (pan_out2, pan_out1, pan_out0) return outputs总结

CAM,同SE、CBAM等模块一样,作为即插即用的注意力机制,在yolov5、yolox等轻量级网络中有着重要的作用。本文介绍的CAM+yolox在我的数据集上,mAP比不添加的时候提高了0.02个点,相比使用CBAM提高了0.01个点,效果还是很可观的。

本文链接地址:https://www.jiuchutong.com/zhishi/297518.html 转载请保留说明!

上一篇:前端使用lottie-web,使用AE导出的JSON动画贴心教程(前端使用vue)

下一篇:下载、编译、安装、使用 vue-devtools(编译安装和普通安装)

  • 企业可否自行决算利润
  • 税控服务费全额抵扣增值税申报表中哪一栏
  • 未分配利润与净利润不相等
  • 异常凭证进项税额转出
  • 其他收益和营业收益
  • 企业年报股东及出资信息要怎么填写
  • 上年结转未抵扣
  • 个人所得税怎么扣
  • 免税农产品包括烟叶吗
  • 代收水电费的账务处理
  • 原材料进口关税怎么算
  • 土地所有权可以出资吗
  • 总分机构移送固定资产是否缴增值税
  • 业务招待费超支原因怎么写
  • 盘亏的设备做营业外支出的会计分录怎么处理?
  • 原材料什么时候结转成本
  • 无形资产摊销的年限规定
  • 消费税价外费用的计算
  • 销售礼盒增值税税率
  • 增值税已交税金借方有余额
  • 金蝶用户管理怎么设置
  • 所得税的税率有哪几种
  • 盘盈入库和其他入库
  • 申报附加专项扣除
  • 施工企业资产负债率
  • 框架采购合同如何计贴印花税?
  • vmware虚拟机无法打开网页
  • 企业免征增值税证明怎么打印
  • 企业所得税调增调减怎么理解
  • 腾讯电脑管家病毒库更新
  • win11系统开机密码怎么修改
  • 高新企业研发费用比例
  • 工程施工广告牌
  • 营改增后不动产发票
  • windows7如何制表
  • 供热企业有哪些
  • 社会保险费征缴暂行条例是谁制定
  • 债权转让抵押权一并转让,需要办理抵押变更手续
  • php写文件函数
  • css选择器分哪几类
  • vue前进后退
  • 文件不知道在哪怎么找
  • timestamp 0
  • 公允价值变动损益怎么算出来的
  • 营改增之后增值税怎么算
  • 帝国cms自动推送插件
  • 出口货物离岸价差异原因说明表在电子税务局的位置
  • sql server 实例
  • 小规模纳税人结转增值税的账务处理
  • 个人所得税的计算公式方法Excel
  • 本年利润是什么性质的科目
  • 融资租入固定资产的改建支出
  • 应收账款无法收回会计分录
  • 中小微企业优惠政策
  • 购买商品加包装怎么入账
  • 房产税开征利好那些概念股 新闻
  • 任何单位和个人都应当()为报警
  • 信用代码证过期了6年怎么办理
  • 工会经费按实际发放交还是以计提的
  • 销售边角废料取名怎么取
  • 防火墙监视模式
  • 苹果mac怎么下载淘宝
  • linux 转换文件编码为utf8编码
  • 怎么修改win10登录名
  • keyword是啥
  • linux查看开机运行时间
  • win8.1卸载软件在哪里
  • window10耳机有电流
  • linux反转
  • js设置图片边框
  • unity角色扮演游戏
  • androidstudio webview
  • js dom操作方法
  • android java编程
  • 上海地铁和公交乘车码
  • 动员讲话简短精辟
  • 海珠税务局许丰
  • 中国进口奶粉关税税率表2019
  • 三种人不交个人所得税?
  • 长沙房产税如何征收
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设