目录
1.源码下载
2.环境配置
3. 数据集准备
4.训练配置
5.训练时遇到的错误
1.源码下载
GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > CoreML > TFLite
2.环境配置
运行环境需要的包和YOLOv5/v7一样,这里不演示,默认环境依赖都完成。
3. 数据集准备
数据集和训练yolov5/v7的没有任何区别 ,在yolov8.yaml中修改你数据集类别数,没有数据集可到下面链接中博文里有数据集下载链接
YOLOv5下载编译运行-口罩检测_Evan_qin_yi_quan的博客-CSDN博客
自己制作数据集可看有手就行的自定义制作coco、voc、yolo格式数据集_Evan_qin_yi_quan的博客-CSDN博客
4.训练配置
代码下载解压后,在ultralytics-main/目录下创建user_main.py和 user_predict.py,用于训练和预测
如果不需要载入与训练权重,则把第2、3个"model = ......"注释掉即可
#user_main.py 代码如下
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.yaml') # build a new model from YAML
model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
model = YOLO('yolov8n.yaml').load('yolov8n.pt') # build from YAML and transfer weights
# Train the model,传入数据集
model.train(data='D:/deep_learn/yolov8_20230701/Tomato_diseases/data.yaml', epochs=100, batch=32)
#user_predict.py 代码如下
from ultralytics import YOLO
# Load a pretrained YOLOv8n model
model = YOLO('runs/detect/train14/weights/best.pt') #已经训练好的模型
# Define path to the image file
source = 'D:/deep_learn/yolov8_20230701/ultralytics-main/examples/predict-data' #待预测的数据保存路径
# Run inference on the source
results = model(source, mode='predict', save=True) # list of Results objects
训练则直接执行user_main.py,预测直接执行 user_predict.py即可
下图是执行user_predict.py的结果
5.训练时遇到的错误
1. File "D:\SoftInstall\Anaconda\lib\site-packages\torch\serialization.py", line 242, in __init__
super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory
原因:无法载入预训练权重导致,原代码中的yolov8n.pt损坏,重新到github上下载预训练权重替换原来的