位置: 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(编译安装和普通安装)

  • 个税里的本期收入扣不扣个人社保
  • 税收优惠政策落实情况报告
  • 下列各项中免征增值税的有
  • 更换税控盘后原发票如何导入旧盘开票税局
  • 两处以上取得工资如何交社保
  • 清算期间,公司是否可以经营
  • 往来账审计存在问题及建议
  • 股东往来款怎么处理
  • 哪些费用可以进项抵扣
  • 软件即征即退怎么算
  • 公司支付宝付款
  • 农业公司没发票可以入账吗
  • 先开票后预缴能跨年吗
  • 税控技术服务费计入什么科目
  • 合伙企业个人所得税怎么申报
  • 二手房增值税怎么收取
  • 投资收益是否缴纳印花税
  • 不能抵扣的进项税额转出会计分录
  • 税收分类编码选错了没事吧
  • 关于个税应补退的说明
  • 营改增前取得的不动产出售的计税方法
  • 不是房屋产权人可以卖房吗
  • 住宿业的配套服务有哪些
  • 所有的固定资产都有残值率吗
  • 网络科技公司会计核算及账务处理
  • 子公司借款给母公司要交税吗
  • 印花税申报数据来源
  • 公司清算实收资本是零吗
  • 系统升级为win11
  • 发票抵扣联章子盖的不清怎么办
  • php数组查找
  • 同一张发票可以分两次报销吗
  • 无线路由器温度范围
  • PHP:Memcached::replace()的用法_Memcached类
  • PHP:pg_close()的用法_PostgreSQL函数
  • 房产税城镇土地使用税申报期限
  • 在一株植物上行走的作文
  • 酒店原材料内部分析
  • vue数据表
  • php如何入门
  • js相关知识
  • 福利费计入科目
  • 营改增后还有企业所得税吗?
  • 非营利组织能否开社保账户
  • mongodb基础知识
  • 红字发票开错了是可以作废的吗?
  • 处置固定资产净收益属于利得吗
  • 招标押金有规定吗
  • 公司员工食堂买菜没发票怎么办
  • 研究开发费用加计扣除最新政策
  • 减免税做营业外收入的会计分录
  • 公司配股对股价的影响
  • 押金可不可以抵房租
  • 分支机构与总机构怎么纳税?
  • 红字发票可以跨月入账吗
  • 服务性的行业有哪些
  • 存货成本计算方法有几种?分别是什么?
  • 贸易融资具体包括
  • win8.1怎么关闭更新
  • cmos是一种什么芯片
  • bios的含义
  • linux -al
  • u盘安装linux系统遇到的问题
  • Win10预览版怎么变回正式版
  • u盘运行win10系统
  • 如何dj
  • linux图形界面与命令行
  • ExtJs3.0中Store添加 baseParams 的Bug
  • 检测输入条件的各种组合
  • javascript运行在什么的脚本语言
  • node.js报错998
  • css中文字垂直排列
  • node用mongodb还是mysql好
  • 抛弃无情道剑尊后扶桑知我
  • 请问在javascript程序中
  • Python编程中装饰器的使用示例解析
  • 青年文明号创建目标
  • 汽车排量与购置税的关系
  • 上海房产税2021征收对象
  • 河南省纪检委网站
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设