CBAM注意力机制是yolov8自带的注意力机制,它是通道注意模块和空间注意模块的结合。
路径:ultralytics/nn/modules/conv.py
复制以下代码添加到tasks.py的指定位置
elif m is CBAM:
c1,c2 = ch[f], args[0]
if c2 != nc:
c2 = make_divisible(min(c2,max_channels) * width, 8)
args = [c1,*args[1:]]
ultralytics/cfg/models/v8/yolov8.yaml
然后在网络结构里面添加CBAM模块
复制下面这段代码
然后将上面复制的代码粘贴进去,再删除不必要的部分,将CFG的值设置为刚刚修改过模型的yaml文件路径
# Ultralytics YOLO , AGPL-3.0 license
import contextlib
from copy import copy
from pathlib import Path
import cv2
import numpy as np
import pytest
import torch
from PIL import Image
from torchvision.transforms import ToTensor
from ultralytics import RTDETR, YOLO
from ultralytics.utils import ASSETS, DEFAULT_CFG, LINUX, MACOS, ONLINE, ROOT, SETTINGS, WINDOWS
CFG = 'ultralytics/cfg/models/v8/yolov8-cbam.yaml'
SOURCE = ASSETS / 'bus.jpg'
def test_model_forward():
model = YOLO(CFG)
model(SOURCE) # also test no source and augment
这就是修改通过的提示。
如果添加失败,最后面有报错原因
UnicodeDecodeError: 'gbk' codec can't decode byte 0x98 in position 1158: illegal multibyte sequence
这应该是windows10特有的问题,因为win10的默认编码方式为utf-8
,而Python读入文本文件的默认编码方式为`gbk`