Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

VOC.yaml 3.3 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...