You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.3KB

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