YOLOv8的改进,我接触的主要分为网络改进和代码改进,网络改进就是以注意力、主干为主,代码改进就是类似于Iou,类别权重等修改。
以下是yolov8的原始模型。
# 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: 80 # 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
# 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, 12], 1, Concat, [1]] # cat head P4
- [-1, 3, C2f, [512]] # 18 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 9], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f, [1024]] # 21 (P5/32-large)
- [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)
在改进过程中,要注意网络中是C2f还是C3卷积层。不能拿带有C3的卷积层和YOLOv8原始模型比较,因为yolov8原始模型是C2f。
而且yolov5添加注意力机制的通道数和yolov8的添加注意力机制的通道数好像不太一样,注意修改。
注意力机制改进一般都是在自己需要的地方进行插入,并将对应的模块载入即可。
以NAM注意力机制为例,
在head最后一层加入一行,同时在整个yaml文件中修改21->22,因为我们多添加了一层。
同时在nn/models/conv.py文件夹中载入NAMAttention类,在__init__.py中声明。在task.py文件中调用,并导入即可。
主要就是改进backbone里面的一些模块,具体修改看个人而定。
这是最常见的错误,可能由于ultralytics在虚拟环境和本地的包冲突,导致在ultralytics下面的包进行修改无效,还是找不到修改后的模块,导致key error
然后,又发现一个比较头疼的问题,上一次改完还能用,换个新模块就会key error,重新执行一下方法2中的两句代码即可
方法1:将nn/models这个文件夹复制到/path/.conda/envs/yolov8/lib/python3.8/site-packages/ultralytics/nn下
方法2:卸载ultralytics这个包。执行以下命令:
pip unstall ultralytics
python setup.py install
setup.py文件在8.1版本的yolov8中没有,需要自己创建(这个代码是我在yolov8的网页中找到的)
import re
from pathlib import Path
import pkg_resources as pkg
from setuptools import find_packages, setup
# Settings
FILE = Path(__file__).resolve()
ROOT = FILE.parent # root directory
README = (ROOT / "README.md").read_text(encoding="utf-8")
REQUIREMENTS = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements((ROOT / 'requirements.txt').read_text())]
def get_version():
file = ROOT / 'ultralytics/__init__.py'
return re.search(r'^__version__ = [\'"]([^\'"]*)[\'"]', file.read_text(), re.M)[1]
setup(
name="ultralytics", # name of pypi package
version=get_version(), # version of pypi package
python_requires=">=3.7.0",
license='GPL-3.0',
description='Ultralytics YOLOv8 and HUB',
long_description=README,
long_description_content_type="text/markdown",
# url="https://github.com/ultralytics/ultralytics",
url="https://github.com/ultralytics/ultralytics",
project_urls={
'Bug Reports': 'https://github.com/ultralytics/ultralytics/issues',
'Funding': 'https://ultralytics.com',
'Source': 'https://github.com/ultralytics/ultralytics',},
author="Ultralytics",
author_email='hello@ultralytics.com',
packages=find_packages(), # required
include_package_data=True,
install_requires=REQUIREMENTS,
extras_require={
'dev': ['check-manifest'],
'test': ['pytest', 'pytest-cov', 'coverage'],},
classifiers=[
"Intended Audience :: Developers", "Intended Audience :: Science/Research",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10",
"Topic :: Software Development", "Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Scientific/Engineering :: Image Recognition", "Operating System :: POSIX :: Linux",
"Operating System :: MacOS", "Operating System :: Microsoft :: Windows"],
keywords="machine-learning, deep-learning, vision, ML, DL, AI, YOLO, YOLOv3, YOLOv5, YOLOv8, HUB, Ultralytics")
可能会需要安装其他的包,因为ultralytics将其他的依赖集成了,卸载ultralytics可能需要下载其他外部包。
requirements.txt(忘了从谁那里找的了,反正能用,博主看到以后私聊我一下,我在这里声明一下你的名字)
# Ultralytics requirements
# Example: pip install -r requirements.txt
# Base ----------------------------------------
matplotlib>=3.3.0
numpy>=1.22.2 # pinned by Snyk to avoid a vulnerability
opencv-python>=4.6.0
pillow>=7.1.2
pyyaml>=5.3.1
requests>=2.23.0
scipy>=1.4.1
torch>=1.8.0
torchvision>=0.9.0
tqdm>=4.64.0
# Logging -------------------------------------
# tensorboard>=2.13.0
# dvclive>=2.12.0
# clearml
# comet
# Plotting ------------------------------------
pandas>=1.1.4
seaborn>=0.11.0
# Export --------------------------------------
# coremltools>=7.0 # CoreML export
# onnx>=1.12.0 # ONNX export
# onnxsim>=0.4.1 # ONNX simplifier
# nvidia-pyindex # TensorRT export
# nvidia-tensorrt # TensorRT export
# scikit-learn==0.19.2 # CoreML quantization
# tensorflow>=2.4.1,<=2.13.1 # TF exports (-cpu, -aarch64, -macos)
# tflite-support
# tensorflowjs>=3.9.0 # TF.js export
# openvino-dev>=2023.0 # OpenVINO export
# Extras --------------------------------------
psutil # system utilization
py-cpuinfo # display CPU info
thop>=0.1.1 # FLOPs computation
# ipython # interactive notebook
# albumentations>=1.0.3 # training augmentations
# pycocotools>=2.0.6 # COCO mAP
# roboflow
安装这个文件即可。
方法3::在创建虚拟环境时就直接不安装ultralytics包,转而安装需要的其他包,安装上述的requirements.txt文件。
在train.py中,最上面加入以下两行代码:
import sys
sys.path.append('/你的绝对路径/ultralytics') #这个ultralytics是第一层ultralytics
反正看哪个能用用哪个。实在不行结合着用,我的就是掺着用的,已经解决问题了。
这个问题和上面那个问题一样。如果你不修改yolov8,直接pip install ultralytics 就可以了。修改,那就按照第一个问题解决就行。
这个尺度不匹配最暴力的方法就是修改尺度。
比如你的报错信息中,提示新添加的一层网络是256 to 1024,直接将1024改成256就行。(当然这是对一般的注意力机制来说是管用的,对其他的,还是老老实实计算输入输出吧)
本文记录本人学习中的问题,大家可以一起交流,有问题可以指出,我看到了会修改的。
转载本文记得声明一下。