|
a |
|
b/data/VOC.yaml |
|
|
1 |
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license |
|
|
2 |
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford |
|
|
3 |
# Example usage: python train.py --data VOC.yaml |
|
|
4 |
# parent |
|
|
5 |
# ├── yolov5 |
|
|
6 |
# └── datasets |
|
|
7 |
# └── VOC ← downloads here (2.8 GB) |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
# 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, ..] |
|
|
11 |
path: ../datasets/VOC |
|
|
12 |
train: # train images (relative to 'path') 16551 images |
|
|
13 |
- images/train2012 |
|
|
14 |
- images/train2007 |
|
|
15 |
- images/val2012 |
|
|
16 |
- images/val2007 |
|
|
17 |
val: # val images (relative to 'path') 4952 images |
|
|
18 |
- images/test2007 |
|
|
19 |
test: # test images (optional) |
|
|
20 |
- images/test2007 |
|
|
21 |
|
|
|
22 |
# Classes |
|
|
23 |
names: |
|
|
24 |
0: aeroplane |
|
|
25 |
1: bicycle |
|
|
26 |
2: bird |
|
|
27 |
3: boat |
|
|
28 |
4: bottle |
|
|
29 |
5: bus |
|
|
30 |
6: car |
|
|
31 |
7: cat |
|
|
32 |
8: chair |
|
|
33 |
9: cow |
|
|
34 |
10: diningtable |
|
|
35 |
11: dog |
|
|
36 |
12: horse |
|
|
37 |
13: motorbike |
|
|
38 |
14: person |
|
|
39 |
15: pottedplant |
|
|
40 |
16: sheep |
|
|
41 |
17: sofa |
|
|
42 |
18: train |
|
|
43 |
19: tvmonitor |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
# Download script/URL (optional) --------------------------------------------------------------------------------------- |
|
|
47 |
download: | |
|
|
48 |
import xml.etree.ElementTree as ET |
|
|
49 |
|
|
|
50 |
from tqdm import tqdm |
|
|
51 |
from utils.general import download, Path |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
def convert_label(path, lb_path, year, image_id): |
|
|
55 |
def convert_box(size, box): |
|
|
56 |
dw, dh = 1. / size[0], 1. / size[1] |
|
|
57 |
x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2] |
|
|
58 |
return x * dw, y * dh, w * dw, h * dh |
|
|
59 |
|
|
|
60 |
in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml') |
|
|
61 |
out_file = open(lb_path, 'w') |
|
|
62 |
tree = ET.parse(in_file) |
|
|
63 |
root = tree.getroot() |
|
|
64 |
size = root.find('size') |
|
|
65 |
w = int(size.find('width').text) |
|
|
66 |
h = int(size.find('height').text) |
|
|
67 |
|
|
|
68 |
names = list(yaml['names'].values()) # names list |
|
|
69 |
for obj in root.iter('object'): |
|
|
70 |
cls = obj.find('name').text |
|
|
71 |
if cls in names and int(obj.find('difficult').text) != 1: |
|
|
72 |
xmlbox = obj.find('bndbox') |
|
|
73 |
bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')]) |
|
|
74 |
cls_id = names.index(cls) # class id |
|
|
75 |
out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n') |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
# Download |
|
|
79 |
dir = Path(yaml['path']) # dataset root dir |
|
|
80 |
url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/' |
|
|
81 |
urls = [f'{url}VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images |
|
|
82 |
f'{url}VOCtest_06-Nov-2007.zip', # 438MB, 4953 images |
|
|
83 |
f'{url}VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images |
|
|
84 |
download(urls, dir=dir / 'images', delete=False, curl=True, threads=3) |
|
|
85 |
|
|
|
86 |
# Convert |
|
|
87 |
path = dir / 'images/VOCdevkit' |
|
|
88 |
for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'): |
|
|
89 |
imgs_path = dir / 'images' / f'{image_set}{year}' |
|
|
90 |
lbs_path = dir / 'labels' / f'{image_set}{year}' |
|
|
91 |
imgs_path.mkdir(exist_ok=True, parents=True) |
|
|
92 |
lbs_path.mkdir(exist_ok=True, parents=True) |
|
|
93 |
|
|
|
94 |
with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f: |
|
|
95 |
image_ids = f.read().strip().split() |
|
|
96 |
for id in tqdm(image_ids, desc=f'{image_set}{year}'): |
|
|
97 |
f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path |
|
|
98 |
lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path |
|
|
99 |
f.rename(imgs_path / f.name) # move image |
|
|
100 |
convert_label(path, lb_path, year, id) # convert labels to YOLO format |