- YOLOv8目前支持BoT-SORT和ByteTrack两种多目标跟踪算法,默认的目标跟踪算法为BoT-SORT
如果要使用ByteTrack跟踪算法,可以添加命令行参数tracker=bytetrack.yaml
一、 VisDrone2019数据集
VisDrone:无人机目标检测和追踪基准数据集。(Detection and Tracking Meet Drones Challenge, IEEE Transactions on Pattern Analysis and Machine Intelligence, 2021)
链接: 数据代码
- 下载yolov8代码
git clone https://github.com/ultralytics/ultralytics.git
pip install ultralytics
二、VisDrone2019数据集处理
需要将VisDrone2019数据集转换为yolo格式数据,labels的生成
在服务器上进入到zip文件所在的文件夹中使用unzip命令解压zip文件。
如:unzip VisDrone2019-DET-train.zip
import os
from pathlib import Pathdef visdrone2yolo(dir):from PIL import Imagefrom tqdm import tqdmdef convert_box(size, box):# Convert VisDrone box to YOLO xywh boxdw = 1. / size[0]dh = 1. / size[1]return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh(dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directorypbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')for f in pbar:img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).sizelines = []with open(f, 'r') as file: # read annotation.txtfor row in [x.split(',') for x in file.read().strip().splitlines()]:if row[4] == '0': # VisDrone 'ignored regions' class 0continuecls = int(row[5]) - 1 # 类别号-1box = convert_box(img_size, tuple(map(int, row[:4])))lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n")with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl:fl.writelines(lines) # write label.txt
dir = Path('datasets') # datasets文件夹下Visdrone2019文件夹目录
# Convert
for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels
正确执行代码后,会在’VisDrone2019-DET-train’, ‘VisDrone2019-DET-val’, 'VisDrone2019-DET-test-dev三个文件夹内新生成labels文件夹,用以存放将VisDrone数据集处理成YoloV8格式后的数据。
三、修改数据配置文件ultralytics/cfg/datasets/VisDrone.yaml
修改path参数
四、yolov8训练
yolo task=detect mode=train model=yolov8s.pt data=cfg/datasets/VisDrone.yaml batch=16 epochs=100 imgsz=640 workers=0 device=0
验证:
yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=cfg/datasets/VisDrone.yaml device=0
五、模型部署
yolo export model=yolov8s.pt format=onnx # export official model
yolo export model=path/to/best.pt format=onnx # export custom trained model
预测:
yolo predict task=detect model=yolov8s.onnx source=0 show=True
跟踪:
yolo mode=track model=yolov8s.onnx source=0 show=True
如果要用TensorRT部署YOLOv8
导出tensorrt
yolo export model=yolov8s.pt format=engine
推理:
yolo task=detect mode=predict model=yolov8s.engineyolo track model=yolov8s.engine source=test_traffic.avi show=True save=True tracker=bytetrack.yaml
需要修改检测类别数量:在 yaml文件里yolo detect train data=coco128.yaml model=yolov8n.yaml pretrained=yolov8n.pt epochs=100 imgsz=640
学习交流访: