Yolov8目标检测

Yolov8目标检测

目录

  • Yolov8目标检测
  • 一、准备数据集
  • 二、源码下载配置
    • 2.1 下载库
    • 2.2 修改配置
    • 2.3 训练
    • 2.4 验证
    • 2.5 测试
    • 2.6 模型导出
    • 2.7 本地测试


一、准备数据集

Yolov8只支持yolo格式的数据,所以,需要将数据集格式调整为

datasets
     |
      images
            |
            train
            	|
            	000000.jpg
            	000001.jpg
            test
            	|
            	100000.jpg
            	100001.jpg
       labels
            |
             train
            	|
            	000000.txt
            	000001.txt
            test
            	|
            	100000.txt
            	100001.txt

二、源码下载配置

2.1 下载库

pip install ultralytics

2.2 修改配置

新建一个模型配置文件yolov8.yaml,里面添加数据路径和类别:

train: "/home/dxfcv/workspace/sunsirui/label/dataset/train/images"
val: "/home/dxfcv/workspace/sunsirui/label/dataset/test/images"
nc: 3
names: ["cement","metal","plastics"]

2.3 训练

具体参数可参看链接

yolo task=detect mode=train model=yolov8s.pt data=yolov8.yaml batch=8 epochs=100 imgsz=640 workers=16 device=0

训练完会生成一个文件夹存放所有结果

2.4 验证

yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=yolov8.yaml device=0

2.5 测试

yolo task=detect mode=predict model=runs/detect/train/weights/best.pt source=/home/dxfcv/workspace/sunsirui/label/dataset/images device=0

在生成的predict文件夹下就能看到效果了(像标注的效果)

2.6 模型导出

yolo task=detect mode=export model=runs/detect/train/weights/best.pt format=onnx

2.7 本地测试

使用不同方式(比如opencv)调图片去检测,结果会在对应文件夹下出现

from ultralytics import YOLO
from PIL import Image
import cv2
 
model = YOLO("/home/dxfcv/workspace/sunsirui/label/dataset/runs/detect/train/weights/best.onnx")
# accepts all formats - image/dir/Path/URL/video/PIL/ndarray. 0 for webcam
# results = model.predict(source="0")
# results = model.predict(source="folder", show=True) # Display preds. Accepts all YOLO predict arguments
 
# # from PIL
# im1 = Image.open("bus.jpg")
# results = model.predict(source=im1, save=True)  # save plotted images
 
# from ndarray
im2 = cv2.imread("/home/dxfcv/workspace/sunsirui/label/dataset/images/camera2_200025.jpg")
results = model.predict(source=im2, save=True, save_txt=True)  # save predictions as labels
 
# from list of PIL/ndarray
# results = model.predict(source=[im1, im2]

你可能感兴趣的:(pytorch,目标跟踪,人工智能,计算机视觉)