如何使用YOLOv8训练水面垃圾检测数据集,并进行模型测试和推理。水面漂浮物检测数据集河道垃圾检测数据集 3000张 水面垃圾 带标注 voc yolo 11类

目标检测实战:仪表盘指针识别

保姆级 YOLOv8/9/10 教程,附完整数据集与源码

水面垃圾检测数据集 3000张 水面垃圾 带标注 voc yolo分类名: (图片张数, 标注个数)在这里插入图片描述

can:
(694,746)
leaf: (628, 1190)
branch: (1053, 2111)
grass:
(309, 481)
milk_ box: (459, 542) .
plastic_ bag: (664, 823)
bottle: (922, 1486)
plastic_ box: (287, 309)
glass_ bottle:
(124,128)
bad_ fruit: (110, 248)
ball: (37, 37)
总数: (3090, 8101)
总类(nc): 11类
在这里插入图片描述
好的,让我们继续详细地介绍如何使用YOLOv8训练水面垃圾检测数据集,并附上可视化代码,以便在训练和推理过程中查看检测结果。以下是完整的步骤:

1. 数据集准备

确保你的数据集已经按照YOLO格式标注,并且数据集结构如下:
在这里插入图片描述

water_trash_dataset/
│
├── images/
│   ├── train/
│   ├── val/
│   └── test/
│
├── labels/
│   ├── train/
│   ├── val/
│   └── test/
│
└── data.yaml

在这里插入图片描述

2. 数据集标注

确保每个标注文件是YOLO格式的,例如:

# Example YOLO format label file
0 0.5 0.5 0.1 0.1  # Class ID, x_center, y_center, width, height
1 0.6 0.6 0.1 0.1  # Class ID, x_center, y_center, width, height

在这里插入图片描述

3. 数据配置文件 (data.yaml)

创建一个data.yaml文件,用于配置数据集:

train: ./water_trash_dataset/images/train
val: ./water_trash_dataset/images/val
test: ./water_trash_dataset/images/test

nc: 11  # 类别数量(11类)
names: ['can', 'leaf', 'branch', 'grass', 'milk_box', 'plastic_bag', 'bottle', 'plastic_box', 'glass_bottle', 'bad_fruit', 'ball']  # 类别名称

4. 安装依赖

确保你已经安装了ultralytics库。如果没有安装,可以使用以下命令安装:

pip install ultralytics

5. 训练模型

使用以下命令训练YOLOv8模型:

python train.py --data ./water_trash_dataset/data.yaml --img 640 --batch 16 --epochs 100 --name yolov8_water_trash --weights yolov8n.pt

6. 可视化代码

6.1 训练过程中的可视化

在训练过程中,YOLOv8会自动保存中间结果,并在训练完成后生成一个results.mp4文件,用于展示训练过程中的检测结果。

6.2 推理过程中的可视化

以下是一个简单的Python脚本,用于使用训练好的模型进行推理,并可视化检测结果:

import cv2
from ultralytics import YOLO

def detect_and_visualize(image_path, model):
    # 读取图像
    image = cv2.imread(image_path)
    
    # 进行检测
    results = model(image)
    
    # 获取检测结果
    for result in results:
        boxes = result.boxes
        for box in boxes:
            x1, y1, x2, y2 = box.xyxy[0].tolist()
            label = box.cls[0].item()
            confidence = box.conf[0].item()
            
            # 绘制边界框
            cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
            cv2.putText(image, f"{model.names[int(label)]}: {confidence:.2f}", (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    
    # 显示结果
    cv2.imshow('Detection Results', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 加载模型
model = YOLO('runs/train/yolov8_water_trash/weights/best.pt')

# 示例图像路径
image_path = 'path/to/image.jpg'

# 运行检测和可视化
detect_and_visualize(image_path, model)

7. 模型测试

训练完成后,你可以使用以下命令测试模型:

python val.py --data ./water_trash_dataset/data.yaml --weights runs/train/yolov8_water_trash/weights/best.pt

8. 模型推理

使用训练好的模型进行推理:

python detect.py --weights runs/train/yolov8_water_trash/weights/best.pt --source path/to/image_or_video

9. 示例代码

以下是一个完整的Python脚本,用于使用训练好的模型进行推理,并可视化检测结果:

import cv2
from ultralytics import YOLO

def detect_and_visualize(image_path, model):
    # 读取图像
    image = cv2.imread(image_path)
    
    # 进行检测
    results = model(image)
    
    # 获取检测结果
    for result in results:
        boxes = result.boxes
        for box in boxes:
            x1, y1, x2, y2 = box.xyxy[0].tolist()
            label = box.cls[0].item()
            confidence = box.conf[0].item()
            
            # 绘制边界框
            cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
            cv2.putText(image, f"{model.names[int(label)]}: {confidence:.2f}", (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    
    # 显示结果
    cv2.imshow('Detection Results', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 加载模型
model = YOLO('runs/train/yolov8_water_trash/weights/best.pt')

# 示例图像路径
image_path = 'path/to/image.jpg'

# 运行检测和可视化
detect_and_visualize(image_path, model)

总结

通过上述步骤,你可以使用YOLOv8训练水面垃圾检测数据集,并进行模型测试和推理。同时,提供了可视化代码,以便在训练和推理过程中查看检测结果。确保数据集结构正确,标注文件格式正确,并且路径配置正确

目标检测实战:仪表盘指针识别

保姆级 YOLOv8/9/10 教程,附完整数据集与源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值