赞
踩
官网地址:https://github.com/VisDrone/VisDrone-Dataset 进入官网后下载train,val,test数据集。
下载完成,解压之后为下图
每个文件夹下的目录为下图
annotations下的标签文件格式,如下图
要想使用YOLO来训练数据,必须将标签文件转换为YOLO格式,如下图,每张image对应一个label
在YOLOv7目录下新建一个文件夹存放VisDrone数据集
在YOLOv7目录下新建一个visdrone2yolov7.py文件夹,如下图所示
visdrone2yolov7.py文件内容为 ,将29行的目录改为自己visdrone数据集的位置,运行该代码。
- from utils.general import os, 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
- 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('H:\\wenjian\\yolov7\\yolov7-main\\visdrone') # dataset文件夹下Visdrone2019文件夹路径
-
-
- # Convert
- for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
- visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels
运行结束后,如图所示
VisDrone数据集下就会生成相应的label文件,这里的cache.label是train.py文件运行之后才生成的,这里不用管。
在YOLOv7目录下的data目录下新建visdrone.yaml文件,代码如下。将
- # COCO 2017 dataset http://cocodataset.org
-
- # download command/URL (optional)
- download: bash ./scripts/get_coco.sh
-
- # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
- train: H:\wenjian\yolov7\yolov7-main\mydata\visdrone\VisDrone2019-DET-train\images # 118287 images
- val: H:\wenjian\yolov7\yolov7-main\mydata\visdrone\VisDrone2019-DET-val\images # 5000 images
- test: H:\wenjian\yolov7\yolov7-main\mydata\visdrone\VisDrone2019-DET-test-dev\images # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
-
- # number of classes
- nc: 11
-
- # class names
- names: [ 'pedestrian', 'people', 'bicycle', 'car', 'van','truck', 'tricycle', 'awning-tricycle', 'bus', 'motor', 'others']
将第7,8,9行分别换为自己的路径。然后在train.py文件相应位置替换为自己的路径,即可训练
最后,开始训练。有四个标签转换有问题的图片,训练时会忽略,不用管。结束。
第一次写博客,小白一个,大佬轻喷。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。