本文主要是介绍使用yolov8训练数据集及使用中遇到的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.下载yolov8文件夹
下载链接
就是这个文件夹,别怕
2.yolov8模型
下载链接,我下了yolov8s.py,放在该路径E:\nfshare\yolov8\ultralytics\weights
ps:model文件类型可以是yaml,也可以是pt
3.修改yolov8.yaml文件
E:\nfshare\yolov8\ultralytics\cfg\models\v8\yolov8.yaml,就改类别数
nc: 9 # number of classes
4.新建data文件
E:\nfshare\yolov8\ultralytics\cfg\datasets\hr.yaml,这些和yolov5一样
5.修改default.yaml文件
E:\nfshare\yolov8\ultralytics\cfg\default.yaml ,就动了以下几个参数
model: weights/yolov8s.pt # (str, optional) path to model file, i.e. yolov8n.pt, yolov8n.yaml
data: cfg/datasets/hr.yaml # (str, optional) path to data file, i.e. coco128.yaml
epochs: 1 # (int) number of epochs to train for
amp: False
batch: 8
运行代码 yolo cfg=cfg/default.yaml
6.训练yolov8模型(train)
1.新建.py文件训练模型
在路径下新建python脚本文件\yolov8\ultralytics\demo.py,就像运行yolov5的模型一样,运行该脚本文件。
下面这些参数要怎么设置我还没懂。
#import sys
#sys.path.append("/home/yyt/nfshare/yolov8/")
from ultralytics import YOLO# Create a new YOLO model from scratch
#model = YOLO('/home/yyt/nfshare/yolov8/ultralytics/cfg/models/v8/yolov8.yaml')# Load a pretrained YOLO model (recommended for training)
model = YOLO('/home/yyt/nfshare/yolov8/ultralytics/weights/yolov8s.pt')# Train the model using the 'coco128.yaml' dataset for 3 epochs
results = model.train(data='/home/yyt/nfshare/yolov8/ultralytics/cfg/datasets/hr.yaml',amp=False,epochs=2,batch=8,val=True)# Evaluate the model's performance on the validation set
#results = model.val(data='/home/yyt/nfshare/yolov8/ultralytics/cfg/datasets/hr.yaml',amp=False,epochs=2,batch=8)success = model.export(format='onnx')
2.运行default.yaml文件训练模型
直接输入yolo cfg=/文件的路径/default.yaml
3.命令运行直接输:
yolo task=detect mode=train model=/yolov8/ultralytics/cfg/runs/detect/train6//weights/last.pt(模型位置,模型可以是.yaml形式或者.pt) data=/yolov8/ultralytics/cfg/datasets/hr.yaml(数据集.yaml文件位置) epochs=150 save=True resume=True (后面的都是参数,具体写什么看default.yaml里面你需要改什么)
7.测试训练后的模型(test)
1.运行default.yaml
修改default.yaml,运行代码和train一样
修改mode: val,model:/runs/detect/train6/weights/last.pt ,split: test
2.代码运行
这个方法能显示test每张图片检测的结果
yolo predict model= '/home/yyt/nfshare/yolov8/ultralytics/cfg/runs/detect/train6/weights/last.pt'(模型路径) source= '/home/yyt/nfshare/zijianshujuji/image/test'(图片文件路径)
问题
1.box_loss cls_loss dfl_loss全显示为nan,map全为0
显卡问题,batch值太大
解决:在default.yaml中,改小batch值 ,amp改为False
2.val的box_loss、cls_loss、dfl_loss为0,train不为0
排查问题:
1.validator.py文件
为了解决问题1 result全显示为0,我删去了validator中的如下代码,恢复看看能不能跑通val
self.args.half = self.device.type != 'cpu' # force FP16 val during training
失败
2.amp
amp改为ture,好的这个不能改,改了又都是nan
不是我瞎改的问题
尝试:
1.在trainer.py里面搜索half关键字,把所有有.half()变为.float()
#'model': deepcopy(de_parallel(self.model)).half(),'model': deepcopy(de_parallel(self.model)).float(),#'ema': deepcopy(self.ema.ema).half(),'ema': deepcopy(self.ema.ema).float(),
无效果
2.继续修改val.py,修改batch['img'].half()改为batch['img'].float()
#batch['img'] = (batch['img'].half() if self.args.half else batch['img'].float()) / 255batch['img'] = (batch['img'].float()) / 255
3.继续修改validator.py
不行
4.改小batch_size
batch_size=4
不行
解决:更新环境和其他安装包,pip install -r requirement.txt
3.运行default.yaml,报错 ModuleNotFoundError: No module named 'ultralytics'
(yolov5) root@xxdell:/home/yyt/nfshare/yolov8/ultralytics# yolo cfg=/home/yyt/nfshare/yolov8/ultralytics/cfg/default.yaml
Traceback (most recent call last):File "/home/nephilim/environment/anaconda3/envs/yolov5/bin/yolo", line 5, in <module>from ultralytics.cfg import entrypoint
ModuleNotFoundError: No module named 'ultralytics'
解决方法:在/home/xx/environment/anaconda3/envs/yolov5/bin/yolo文件中添加路径
sys.path.append("/home/yyt/nfshare/yolov8/")
再次运行,解决
4.训练结果runs文件保存路径改变
原本的训练结果保存在/home/yyt/nfshare/yolov8/runs 里面,即我的共享文件夹和yolov8存在的项目文件了,现在被更改了也不知道是怎么回事,前两次训练结果就没有出现。现在的文件夹路径在虚拟机的环境路径里/home/xx/environment/anaconda3/envs/yolov5/bin/runs/detect
感觉使用的不是yolov8,而是yolov5
(base) yyt@dell:/home/xx/environment/anaconda3/envs/yolov5/bin/runs/detect$ stat trainFile: trainSize: 4096 Blocks: 8 IO Block: 4096 directory
Device: 824h/2084d Inode: 16883842 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-08-21 15:02:27.587039861 +0800
Modify: 2023-08-21 14:59:15.937472731 +0800
Change: 2023-08-21 14:59:15.937472731 +0800
分析:
这几次运行的都是配置default.yaml,运行python文件,正常显示。
5.断网导致训练中断,继续训练
参考http://t.csdn.cn/UZRqy
命令行直接输入代码如下,模型改为之前跑的last.pt,epochs是总的训练次数
yolo task=detect mode=train model=/home/yyt/nfshare/yolov8/ultralytics/cfg/runs/detect/train6//weights/last.pt data=/home/yyt/nfshare/yolov8/ultralytics/cfg/datasets/hr.yaml epochs=150 save=True resume=True
6.ValueError: cannot convert float NaN to integer
训练完模型后,继续跑val测试,出现以下错误。
Validating runs/detect/train/weights/best.pt...
Ultralytics YOLOv8.0.173 🚀 Python-3.9.17 torch-2.0.1+cu117 CUDA:0 (NVIDIA GeForce GTX 1660 SUPER, 5928MiB)
Model summary (fused): 168 layers, 11129067 parameters, 0 gradientsClass Images Instances Box(P R mAP50 mAP50-95): 1%|▏ | 1/78 [00:00<00:26, 2.87it/s]Exception in thread Thread-25:
Traceback (most recent call last):File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/threading.py", line 980, in _bootstrap_innerself.run()File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/threading.py", line 917, in runself._target(*self._args, **self._kwargs)File "/home/yyt/.local/lib/python3.9/site-packages/ultralytics/utils/plotting.py", line 446, in plot_imagesannotator.box_label(box, label, color=color)File "/home/yyt/.local/lib/python3.9/site-packages/ultralytics/utils/plotting.py", line 116, in box_labelself.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/site-packages/PIL/ImageDraw.py", line 556, in textdraw_text(ink)File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/site-packages/PIL/ImageDraw.py", line 496, in draw_textcoord.append(int(xy[i]))
ValueError: cannot convert float NaN to integerClass Images Instances Box(P R mAP50 mAP50-95): 3%|▎ | 2/78 [00:01<00:44, 1.70it/s]Exception in thread Thread-27:
Traceback (most recent call last):File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/threading.py", line 980, in _bootstrap_innerself.run()File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/threading.py", line 917, in runself._target(*self._args, **self._kwargs)File "/home/yyt/.local/lib/python3.9/site-packages/ultralytics/utils/plotting.py", line 446, in plot_imagesannotator.box_label(box, label, color=color)File "/home/yyt/.local/lib/python3.9/site-packages/ultralytics/utils/plotting.py", line 116, in box_labelself.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/site-packages/PIL/ImageDraw.py", line 556, in textdraw_text(ink)File "/home/nephilim/environment/anaconda3/envs/yolov5/lib/python3.9/site-packages/PIL/ImageDraw.py", line 496, in draw_textcoord.append(int(xy[i]))
暂时没解决
7.关于requirements.txt安装文件的路径
这个默认安装路径一直让我很烦,安装前需要提前转到conda环境的路径下再运行命令
cd /home/111/yyt/anaconda3/envs/yolov8/lib/python3.10/site-packages(安装包路径)
pip install -r 路径/requirements.txt
参考这两篇文章http://t.csdn.cn/9edbL,http://t.csdn.cn/oJXMF,提前确定好自己的安装位置,免得找不到安装包。
8.command 'yolo' not found ,did you mean:command 'rolo' from deb rolo
类似找不到命令的情况在yolov8里很常见
参考http://t.csdn.cn/R6aQY
运行yolov8目录下的setup.py文件
python setup.py install
9.seaborn/_oldcore.py:1119:FuturwWarning:use_inf_as_na option is be removed in a future version.Cohvert inf values to NaN before operating instead:
未来预警,意思是seaborn/_oldcore.py:1119:FuturwWarning:use_inf_as_na 选项将在未来版本中删除。在操作之前将 inf 值转换为 NaN:
只要不是报错不管他。
10.AssertionError: /home/wsjdy/yyt/nfshare/yolov8/ultralytics/runs/detect/train5/weights/last.pt training to 150 epochs is finished, nothing to resume.
训练150轮结束后,想要继续训练,增加训练轮数
修改resume=True epochs=300 models=/上次训练的结果/last.pt
结果显示报错如上。
尝试:yolov8断点恢复训练及减少训练次数和增加训练次数-CSDN博客
修改ultralytics/engine/trainer.py文件
修改self.epochs=想要训练的总次数,结果失败
尝试2:Yolov8断点续训/继续训练_q1224352995的博客-CSDN博客 失败
屈服了
直接把models改为last.pt,resume=False,epochs=150,在原有的基础上加训
这篇关于使用yolov8训练数据集及使用中遇到的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!