1.准备数据集
1.先给出VisDrone2019数据集的下载地址:
链接:https://pan.baidu.com/s/1e2Q0NgNT-H-Acb2H0Cx8sg
提取码:31dl
2.将数据集VisDrone放在datasets目录下面
2.数据集转换程序
1.在根目录下面新建一个.py文件,取名叫做visdrone2yolov
2.复制以下代码到这个visdrone2yolov.py文件里面
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/VisDrone') # 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
3.代码中可能需要修改的地方
将dir的值换成VisDrone数据集的相对路径
然后运行这个程序。
4.数据集转换完毕
转换之后的数据集结构如下:
3.准备配置(yaml)文件
1.复制VisDrone到同级文件夹,取名叫myVisDrone.yaml
2.配置文件的具体信息如下:
# Ultralytics YOLO 🚀, AGPL-3.0 license
# VisDrone2019-DET dataset https://github.com/VisDrone/VisDrone-Dataset by Tianjin University
# Example usage: yolo train data=VisDrone.yaml
# parent
# ├── ultralytics
# └── datasets
# └── VisDrone ← downloads here (2.3 GB)# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/VisDrone # dataset root dir
train: VisDrone2019-DET-train/images # train images (relative to 'path') 6471 images
val: VisDrone2019-DET-val/images # val images (relative to 'path') 548 images
test: VisDrone2019-DET-test-dev/images # test images (optional) 1610 images# Classes
names:0: pedestrian1: people2: bicycle3: car4: van5: truck6: tricycle7: awning-tricycle8: bus9: motor
4.开始训练
1.使用yolov8s.pt进行训练
1.复制如下代码打开Terminal粘贴之后开始训练
yolo train model=yolov8s.pt data=ultralytics/cfg/datasets/myVisDrone.yaml batch=4 epochs=100 lr0=0.01
2.训练过程中遇到如下报错:OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
可能是因为进程占用的原因,重启电脑之后解决,顺利训练。
开始训练
3.网络未改进之前使用yolov8s.pt训练的效果
尝试了一下,不使用预训练权重开始训练,发现还是会默认使用yolov8n.pt
yolov8s训练最好的效果(所有标签) :map 0.412
2.使用yolov8l.pt进行训练
yolo train model=yolov8l.pt data=ultralytics/cfg/datasets/myVisDrone.yaml batch=4 epochs=100 lr0=0.01
训练效果