VOC.yaml 3.4 KB

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