3
0

VOC.yaml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # YOLOv5 🚀 by Ultralytics, GPL-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. 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 = [f'{url}VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
  51. f'{url}VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
  52. f'{url}VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
  53. download(urls, dir=dir / 'images', delete=False, curl=True, threads=3)
  54. # Convert
  55. path = dir / '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. with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
  62. image_ids = f.read().strip().split()
  63. for id in tqdm(image_ids, desc=f'{image_set}{year}'):
  64. f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
  65. lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
  66. f.rename(imgs_path / f.name) # move image
  67. convert_label(path, lb_path, year, id) # convert labels to YOLO format