YOLOV8添加注意力机制_scale = tuple(scales


## 2.修改tasks.py


导入注意力机制模块和修改parse\_model函数即可



from ultralytics.nn.modules.attention import EMA,SimAM#导入注意力机制



def parse_model(d, ch, verbose=True): # model_dict, input_channels(3)
“”“Parse a YOLO model.yaml dictionary into a PyTorch model.”“”
import ast

# Args
max_channels = float('inf')
nc, act, scales = (d.get(x) for x in ('nc', 'activation', 'scales'))
depth, width, kpt_shape = (d.get(x, 1.0) for x in ('depth_multiple', 'width_multiple', 'kpt_shape'))
if scales:
    scale = d.get('scale')
    if not scale:
        scale = tuple(scales.keys())[0]
        LOGGER.warning(f"WARNING ⚠️ no model scale passed. Assuming scale='{scale}'.")
    depth, width, max_channels = scales[scale]

if act:
    Conv.default_act = eval(act)  # redefine default activation, i.e. Conv.default_act = nn.SiLU()
    if verbose:
        LOGGER.info(f"{colorstr('activation:')} {act}")  # print

if verbose:
    LOGGER.info(f"\n{'':>3}{'from':>20}{'n':>3}{'params':>10}  {'module':<45}{'arguments':<30}")
ch = [ch]
layers, save, c2 = [], [], ch[-1]  # layers, savelist, ch out
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']):  # from, number, module, args
    m = getattr(torch.nn, m[3:]) if 'nn.' in m else globals()[m]  # get module
    for j, a in enumerate(args):
        if isinstance(a, str):
            with contextlib.suppress(ValueError):
                args[j] = locals()[a] if a in locals() else ast.literal_eval(a)

    n = n_ = max(round(n * depth), 1) if n > 1 else n  # depth gain
    if m in (Classify, Conv, ConvTranspose, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, Focus,
             BottleneckCSP, C1, C2, C2f, C3, C3TR, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x, RepC3):
        c1, c2 = ch[f], args[0]
        if c2 != nc:  # if c2 not equal to number of classes (i.e. for Classify() output)
            c2 = make_divisible(min(c2, max_channels) * width, 8)

        args = [c1, c2, *args[1:]]
        if m in (BottleneckCSP, C1, C2, C2f, C3, C3TR, C3Ghost, C3x, RepC3):
            args.insert(2, n)  # number of repeats
            n = 1
    elif m is AIFI:
        args = [ch[f], *args]
    elif m in (HGStem, HGBlock):
        c1, cm, c2 = ch[f], args[0], args[1]
        args = [c1, cm, c2, *args[2:]]
        if m is HGBlock:
            args.insert(4, n)  # number of repeats
            n = 1
    elif m is ResNetLayer:
        c2 = args[1] if args[3] else args[1] * 4
    elif m is nn.BatchNorm2d:
        args = [ch[f]]
    elif m is Concat:
        c2 = sum(ch[x] for x in f)
    elif m in (Detect, Segment, Pose):
        args.append([ch[x] for x in f])
        if m is Segment:
            args[2] = make_divisible(min(args[2], max_channels) * width, 8)
    elif m is RTDETRDecoder:  # special case, channels arg must be passed in index 1
        args.insert(1, [ch[x] for x in f])
    elif m in {EMA}:
        c2=ch[f]
        args=[c2,*args]
    elif m in {SimAM}:
        c2=ch[f]
    else:
        c2 = ch[f]

    m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args)  # module
    t = str(m)[8:-2].replace('__main__.', '')  # module type
    m.np = sum(x.numel() for x in m_.parameters())  # number params
    m_.i, m_.f, m_.type = i, f, t  # attach index, 'from' index, type
    if verbose:
        LOGGER.info(f'{i:>3}{str(f):>20}{n_:>3}{m.np:10.0f}  {t:<45}{str(args):<30}')  # print
    save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1)  # append to savelist
    layers.append(m_)
    if i == 0:
        ch = []
    ch.append(c2)
return nn.Sequential(*layers), sorted(save)

## 3.修改yaml文件(新建yolov8-attention.yaml)


添加EMA后层数加1,所以后面层数要加1



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: 1 # number of classes
scales: # model compound scaling constants, i.e. ‘model=yolov8n.yaml’ will call yolov8.yaml with scale ‘n’

[depth, width, max_channels]

n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs

YOLOv8.0n backbone

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
  • [-1,1,‘EMA’,[]] #10

YOLOv8.0n head

head:

  • [-1, 1, nn.Upsample, [None, 2, ‘nearest’]]

  • [[-1, 6], 1, Concat, [1]] # cat backbone P4

  • [-1, 3, C2f, [512]] # 12

  • [-1, 1, nn.Upsample, [None, 2, ‘nearest’]]

  • [[-1, 4], 1, Concat, [1]] # cat backbone P3

  • [-1, 3, C2f, [256]] # 15 (P3/8-small)

  • [-1, 1, Conv, [256, 3, 2]]

  • [[-1, 13], 1, Concat, [1]] # cat head P4

  • [-1, 3, C2f, [512]] # 18 (P4/16-medium)

  • [-1, 1, Conv, [512, 3, 2]]

  • [[-1, 10], 1, Concat, [1]] # cat head P5

  • [-1, 3, C2f, [1024]] # 21 (P5/32-large)

  • [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)


##  4.测试


使用yolov8-attention.yaml训练时会显示,第十层为EMA


**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/b5758388d0525d0ff76453dacad1f46d.png)

 

![img](https://img-blog.csdnimg.cn/img_convert/92a60536791686d338c4b66abf13e794.png)

![img](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)

![img](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**



**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

<img src="https://img-community.csdnimg.cn/images/fd6ebf0d450a4dbea7428752dc7ffd34.jpg" alt="img" style="zoom:50%;" />
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值