yolov8之训练、验证、预测、导出

y o l o v 8 之训练、验证、预测、导出 yolov8之训练、验证、预测、导出 yolov8之训练、验证、预测、导出

链接:https://pan.baidu.com/s/1_9hI8ZshNCJSMJVXNTVxbQ?pwd=1234
提取码:1234


一 YoloV8数据集制作

1.labelme的使用

2.数据集转为yolo格式


二 使用yolov8进行训练、验证、预测、导出

  • 1.图像分类
  • 2.图像检测
  • 3.实例分割

1.图像分类

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-cls.pt"
# 数据集yaml文件路径
data_path = r"D:\YoloV8Manual\dataset\DogCat-cls"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Cls"
predict_ImgPath = r"dataset/DogCat-cls/test/cat"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
model.train(data=data_path, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(data=data_path)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)

训练指标说明

yolov8之训练、验证、预测、导出_第1张图片

在这里插入图片描述

验证指标说明

from ultralytics import YOLO
import os
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Cls\weights\best.pt"
# 数据集yaml文件路径
data_path = r"D:\YoloV8Manual\dataset\DogCat-cls"
# 训练轮数
epochs = 1
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Cls"
predict_ImgPath = r"dataset/DogCat-cls/test/cat"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
#model.train(data=data_path, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(workers=0)  # 在验证集上评估模型性能
# 4 模型预测
#results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
#model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第2张图片

模型预测结果

from ultralytics import YOLO
import os
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Cls\weights\best.pt"
# 数据集yaml文件路径
data_path = r"D:\YoloV8Manual\dataset\DogCat-cls"
# 训练轮数
epochs = 1
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Cls"
predict_ImgPath = r"dataset/DogCat-cls/val/cat"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
#model.train(data=data_path, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(workers=0)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
#model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第3张图片
yolov8之训练、验证、预测、导出_第4张图片

模型导出

yolov8之训练、验证、预测、导出_第5张图片

1.导出TensorRT

TensorRT环境配置

from ultralytics import YOLO
import os
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Cls\weights\best.pt"
# 数据集yaml文件路径
data_path = r"D:\YoloV8Manual\dataset\DogCat-cls"
# 训练轮数
epochs = 1
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Cls"
predict_ImgPath = r"dataset/DogCat-cls/val/cat"
save_predictImg_flag = True
exportType= "engine"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_path, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType)


yolov8之训练、验证、预测、导出_第6张图片
yolov8之训练、验证、预测、导出_第7张图片

2.导出OpenVino

OpenVino环境配置

from ultralytics import YOLO
import os
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Cls\weights\best.pt"
# 数据集yaml文件路径
data_path = r"D:\YoloV8Manual\dataset\DogCat-cls"
# 训练轮数
epochs = 1
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Cls"
predict_ImgPath = r"dataset/DogCat-cls/val/cat"
save_predictImg_flag = True
exportType= "openvino"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_path, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType)

yolov8之训练、验证、预测、导出_第8张图片

2.图像检测

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\det_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Det"
predict_ImgPath = r"dataset/DogCat-det/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(data=data_yaml)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)

训练指标说明

yolov8之训练、验证、预测、导出_第9张图片

在这里插入图片描述

验证指标说明

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Det\weights\best.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\det_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Det"
predict_ImgPath = r"dataset/DogCat-det/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
# model.export(format=exportType, imgsz=imgsz)

yolov8之训练、验证、预测、导出_第10张图片

模型预测结果

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Det\weights\best.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\det_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Det"
predict_ImgPath = r"dataset/DogCat-det/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
# model.export(format=exportType, imgsz=imgsz)

yolov8之训练、验证、预测、导出_第11张图片
yolov8之训练、验证、预测、导出_第12张图片

导出结果

1 导出TensorRT

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Det\weights\best.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\det_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Det"
predict_ImgPath = r"dataset/DogCat-det/images/val"
save_predictImg_flag = True
exportType= "engine"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第13张图片

yolov8之训练、验证、预测、导出_第14张图片

2 导出OpenVino

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"D:\YoloV8Manual\AIModel\CatAndDog_Det\weights\best.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\det_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Det"
predict_ImgPath = r"dataset/DogCat-det/images/val"
save_predictImg_flag = True
exportType= "openvino"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)

yolov8之训练、验证、预测、导出_第15张图片
yolov8之训练、验证、预测、导出_第16张图片

3.实例分割

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-seg.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\seg_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Seg"
predict_ImgPath = r"dataset/DogCat-seg/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(data=data_yaml)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)

训练指标说明

yolov8之训练、验证、预测、导出_第17张图片

验证指标说明

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-seg.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\seg_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Seg"
predict_ImgPath = r"dataset/DogCat-seg/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
metrics = model.val(data=data_yaml)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
# model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第18张图片

模型预测结果

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-seg.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\seg_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Seg"
predict_ImgPath = r"dataset/DogCat-seg/images/val"
save_predictImg_flag = True
exportType= "onnx"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
# model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第19张图片
yolov8之训练、验证、预测、导出_第20张图片

导出结果

1.导出TensorRT引擎

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-seg.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\seg_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Seg"
predict_ImgPath = r"dataset/DogCat-seg/images/val"
save_predictImg_flag = True
exportType= "engine"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)


yolov8之训练、验证、预测、导出_第21张图片
yolov8之训练、验证、预测、导出_第22张图片

2.导出openvino

from ultralytics import YOLO
# 0 参数配置
# 模型路径
model_path = r"./yolov8s-seg.pt"
# 数据集yaml文件路径
data_yaml = r"D:\YoloV8Manual\data_yaml\seg_data.yaml"
# 训练轮数
epochs = 500
imgsz = 224
batch = 4
project = r"./AIModel"
name = "CatAndDog_Seg"
predict_ImgPath = r"dataset/DogCat-seg/images/val"
save_predictImg_flag = True
exportType= "openvino"
exist_ok = True
# 1 加载模型
model = YOLO(model_path)
# 2 训练模型
# model.train(data=data_yaml, epochs=epochs, imgsz=imgsz,batch=batch,workers=0,project=project,name=name,exist_ok=True)
# 3 验证模型
# metrics = model.val(data=data_yaml,workers=0)  # 在验证集上评估模型性能
# 4 模型预测
# results = model.predict(source=predict_ImgPath,  imgsz=imgsz ,save=save_predictImg_flag,batch=batch)  # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format=exportType, imgsz=imgsz)

yolov8之训练、验证、预测、导出_第23张图片
yolov8之训练、验证、预测、导出_第24张图片


补充:

如何解决安装Anaconda后,使用pip install 包名 安装的包未出现在Anaconda目录的sit-site-packages,而出现在C盘python的sitepackag里问题

你可能感兴趣的:(YOLO)