## YOLOV8中加入EMA注意力机制和自创可扩展卷积使得小目标检测提点

1、自创可扩展卷积模块

``class RFBconv(nn.Module):def __init__(self, c1):super(RFBconv, self).__init__()# 第一条分支c2 = c1self.branch1 = nn.Sequential(Conv(c1,c2,1,1),nn.Conv2d(c2, c2, kernel_size=3, dilation=1, padding=1))# 第二条分支self.branch2 = nn.Sequential(Conv(c1, c2, 1,1),nn.Conv2d(c2, c2, kernel_size=3, dilation=1, padding=1),nn.Conv2d(c2, c2, kernel_size=3, dilation=3, padding=3))# 第三条分支self.branch3 = nn.Sequential(Conv(c1, c2, 1,1),nn.Conv2d(c2, c2, kernel_size=5, dilation=1, padding=2),nn.Conv2d(c2, c2, kernel_size=3, dilation=5, padding=5))# 1x1卷积用于通道数变换self.conv1x1 = Conv(3 * c2, c2, 1)def forward(self, x):# 分别对三条分支进行前向传播out1 = self.branch1(x)out2 = self.branch2(x)out3 = self.branch3(x)# 将三个分支的结果连接在一起concatenated = torch.cat((out1, out2, out3), dim=1)# 使用1x1卷积进行通道数变换result = self.conv1x1(concatenated)result = result + xreturn result``

2、EMA注意力机制

EMA是CBAM注意力的改进，比CBAM注意力更好

``````class EMA(nn.Module):def __init__(self, channels, factor=8):super(EMA, self).__init__()self.groups = factorassert channels // self.groups > 0self.softmax = nn.Softmax(-1)self.agp = nn.AdaptiveAvgPool2d((1, 1))self.pool_h = nn.AdaptiveAvgPool2d((None, 1))self.pool_w = nn.AdaptiveAvgPool2d((1, None))self.gn = nn.GroupNorm(channels // self.groups, channels // self.groups)self.conv1x1 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=1, stride=1, padding=0)self.conv3x3 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=3, stride=1, padding=1)def forward(self, x):b, c, h, w = x.size()group_x = x.reshape(b * self.groups, -1, h, w)  # b*g,c//g,h,wx_h = self.pool_h(group_x)x_w = self.pool_w(group_x).permute(0, 1, 3, 2)hw = self.conv1x1(torch.cat([x_h, x_w], dim=2))x_h, x_w = torch.split(hw, [h, w], dim=2)x1 = self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid())x2 = self.conv3x3(group_x)x11 = self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1))x12 = x2.reshape(b * self.groups, c // self.groups, -1)  # b*g, c//g, hwx21 = self.softmax(self.agp(x2).reshape(b * self.groups, -1, 1).permute(0, 2, 1))x22 = x1.reshape(b * self.groups, c // self.groups, -1)  # b*g, c//g, hwweights = (torch.matmul(x11, x12) + torch.matmul(x21, x22)).reshape(b * self.groups, 1, h, w)return (group_x * weights.sigmoid()).reshape(b, c, h, w)
``````

3、配置文件

``````# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect# Parameters
nc: 20  # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'# [depth, width, max_channels]# YOLOv8n summary: 225 layers,  3157200 parameters,  3157184 gradients,   8.9 GFLOPs
#  l: [1.00, 1.00, 512]  # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients,  28.8 GFLOPs# s: [0.33, 0.50, 1024]
backbone:# [from, repeats, module, args]- [-1, 1, Conv, [64, 3, 2]]  # 0-P1/2- [-1, 1, Conv, [128, 3, 2]]  # 1-P2/4- [-1, 3, C2f, [128, True]]- [-1, 1, Conv, [256, 3, 2]]  # 3-P3/8- [-1, 6, C2f, [256, True]]- [-1, 1, Conv, [512, 3, 2]]  # 5-P4/16- [-1, 6, C2f, [512, True]]- [-1, 1, Conv, [1024, 3, 2]]  # 7-P5/32- [-1, 3, C2f, [1024, True]]- [-1, 1, SPPF, [1024, 5]]  # 9# YOLOv8.0s head
head:- [-1, 1, nn.Upsample, [None, 2, 'nearest']]- [-1,1,EMA,[]]- [[-1, 6], 1, Concat, [1]]  # cat backbone P4- [-1, 3, C2f, [512]]  # 13- [-1, 1, nn.Upsample, [None, 2, 'nearest']]- [-1,1,EMA,[]]- [[-1, 4], 1, Concat, [1]]  # cat backbone P3- [-1, 3, C2f, [256]]  # 17 (P3/8-small)- [-1, 1, nn.Upsample, [None, 2, 'nearest']]- [-1,1,EMA,[]]- [[-1, 2], 1, Concat, [1]]  # cat backbone P3- [-1, 3, C2f, [128]]  # 18 (P2/4-xsmall)- [-1,1,RFBconv,[]]  #  22- [-1, 1, Conv, [256, 3, 2]]- [[-1, 17], 1, Concat, [1]]  # cat head P4- [-1, 3, C2f, [256]]  # 21 (P3/8-small)- [-1,1,RFBconv,[]]  # 26- [-1, 1, Conv, [512, 3, 2]]- [[-1, 13], 1, Concat, [1]]  # cat head P5- [-1, 3, C2f, [512]]  # 29 (P4/16-medium)- [-1, 1, Conv, [512, 3, 2]]- [[-1, 9], 1, Concat, [1]]  # cat head P5- [-1, 3, C2f, [1024]]  # 32 (P5/32-large)- [[22, 26, 29, 32], 1, Detect, [nc]]  # Detect(P2, P3, P4, P5)
``````

### gif帧数修改怎么操作？一键掌握GIF帧数修改技巧！

gif帧数修改怎么操作&#xff1f;在数字化信息爆炸的时代&#xff0c;GIF动图因其生动有趣的特性而备受广大网友喜爱。然而&#xff0c;很多时候我们可能会遇到GIF动图帧数过多或过少&#xff0c;导致动画效果不尽如人意的情况。那么&#xff0c;如何对GIF动图的帧数进行修改呢…

### 【Python】进阶学习：pandas--describe()函数的使用介绍

&#x1f40d;【Python】进阶学习&#xff1a;pandas——describe()函数的使用介绍 &#x1f308; 个人主页&#xff1a;高斯小哥 &#x1f525; 高质量专栏&#xff1a;Matplotlib之旅&#xff1a;零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程&am…

### Linux Tcpdump抓包入门

Linux Tcpdump抓包入门 一、Tcpdump简介 tcpdump 是一个在Linux系统上用于网络分析和抓包的强大工具。它能够捕获网络数据包并提供详细的分析信息&#xff0c;有助于网络管理员和开发人员诊断网络问题和监控网络流量。 安装部署 # 在Debian/Ubuntu上安装 sudo apt-get install…

### R包Colorfindr识别图片颜色｜用刀剑神域方式打开SCI科研配色

1.前言 最近忙里偷闲&#xff0c;捣鼓一下配色&#xff0c;把童年回忆里的动漫都搬进来&#xff0c;给科研信仰充值吧&#xff5e; 提取颜色之前写过一个Py的&#xff0c;那个很准确不过调参会有点麻烦。这里分享一个比较懒人点的R包吧&#xff0c;虽然会有一定误差&#xff…