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 Path
def visdrone2yolo(dir):
from PIL import Image
from tqdm import tqdm
def convert_box(size, box):
# Convert VisDrone box to YOLO xywh box
dw = 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 directory
pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')
for f in pbar:
img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size
lines = []
with open(f, 'r') as file: # read annotation.txt
for row in [x.split(',') for x in file.read().strip().splitlines()]:
if row[4] == '0': # VisDrone 'ignored regions' class 0
continue
cls = int(row[5]) - 1 # 类别号-1
box = 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: pedestrian
1: people
2: bicycle
3: car
4: van
5: truck
6: tricycle
7: awning-tricycle
8: bus
9: motor
4.开始训练
1.使用yolov8s.pt进行训练
1.复制如下代码打开Terminal粘贴之后开始训练
pretrained表示预训练权重
data表示数据集的配置文件
model表示网络结构的配置文件
batch表示每一批处理图片的张数
epochs表示训练轮数
如果后面进行网络改进的话,修改model也就是网络结构的配置文件即可
yolo train pretrained=yolov8s.pt data=ultralytics/cfg/datasets/myVisDrone.yaml model=ultralytics/cfg/models/v8/yolov8s.yaml batch=4 epochs=300
2.训练过程中遇到如下报错:OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
可能是因为进程占用的原因,重启电脑之后解决,顺利训练。
开始训练
3.网络未改进之前使用yolov8s.pt训练的效果
尝试了一下,不使用预训练权重开始训练,发现还是会默认使用yolov8n.pt
yolov8s训练最好的效果(所有标签) :map 0.412
4.使用训练之后的权重文件在测试集上面进行测试
yolo task=detect mode=val model=runs/detect/train21/weights/best.pt data=ultralytics/cfg/datasets/TestVisDrone.yaml
其中TestVisDrone.yaml的内容如下,就是将测试集放在val这里