VOC.yaml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # 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 utils.general import download, Path
  46. def convert_label(path, lb_path, year, image_id):
  47. def convert_box(size, box):
  48. dw, dh = 1. / size[0], 1. / size[1]
  49. 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]
  50. return x * dw, y * dh, w * dw, h * dh
  51. in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')
  52. out_file = open(lb_path, 'w')
  53. tree = ET.parse(in_file)
  54. root = tree.getroot()
  55. size = root.find('size')
  56. w = int(size.find('width').text)
  57. h = int(size.find('height').text)
  58. names = list(yaml['names'].values()) # names list
  59. for obj in root.iter('object'):
  60. cls = obj.find('name').text
  61. if cls in names and int(obj.find('difficult').text) != 1:
  62. xmlbox = obj.find('bndbox')
  63. bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
  64. cls_id = names.index(cls) # class id
  65. out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
  66. # Download
  67. dir = Path(yaml['path']) # dataset root dir
  68. url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
  69. urls = [f'{url}VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
  70. f'{url}VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
  71. f'{url}VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
  72. download(urls, dir=dir / 'images', delete=False, curl=True, threads=3)
  73. # Convert
  74. path = dir / 'images/VOCdevkit'
  75. for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'):
  76. imgs_path = dir / 'images' / f'{image_set}{year}'
  77. lbs_path = dir / 'labels' / f'{image_set}{year}'
  78. imgs_path.mkdir(exist_ok=True, parents=True)
  79. lbs_path.mkdir(exist_ok=True, parents=True)
  80. with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
  81. image_ids = f.read().strip().split()
  82. for id in tqdm(image_ids, desc=f'{image_set}{year}'):
  83. f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
  84. lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
  85. f.rename(imgs_path / f.name) # move image
  86. convert_label(path, lb_path, year, id) # convert labels to YOLO format