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.

117 line
4.1KB

  1. #!/bin/bash
  2. # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/
  3. # Download command: bash data/scripts/get_voc.sh
  4. # Train command: python train.py --data voc.yaml
  5. # Default dataset location is next to YOLOv5:
  6. # /parent_folder
  7. # /VOC
  8. # /yolov5
  9. start=$(date +%s)
  10. mkdir -p ../tmp
  11. cd ../tmp/
  12. # Download/unzip images and labels
  13. d='.' # unzip directory
  14. url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
  15. f1=VOCtrainval_06-Nov-2007.zip # 446MB, 5012 images
  16. f2=VOCtest_06-Nov-2007.zip # 438MB, 4953 images
  17. f3=VOCtrainval_11-May-2012.zip # 1.95GB, 17126 images
  18. for f in $f3 $f2 $f1; do
  19. echo 'Downloading' $url$f '...'
  20. curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background
  21. done
  22. wait # finish background tasks
  23. end=$(date +%s)
  24. runtime=$((end - start))
  25. echo "Completed in" $runtime "seconds"
  26. echo "Splitting dataset..."
  27. python3 - "$@" <<END
  28. import os
  29. import xml.etree.ElementTree as ET
  30. from os import getcwd
  31. sets = [('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
  32. classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog",
  33. "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
  34. def convert_box(size, box):
  35. dw = 1. / (size[0])
  36. dh = 1. / (size[1])
  37. 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]
  38. return x * dw, y * dh, w * dw, h * dh
  39. def convert_annotation(year, image_id):
  40. in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml' % (year, image_id))
  41. out_file = open('VOCdevkit/VOC%s/labels/%s.txt' % (year, image_id), 'w')
  42. tree = ET.parse(in_file)
  43. root = tree.getroot()
  44. size = root.find('size')
  45. w = int(size.find('width').text)
  46. h = int(size.find('height').text)
  47. for obj in root.iter('object'):
  48. difficult = obj.find('difficult').text
  49. cls = obj.find('name').text
  50. if cls not in classes or int(difficult) == 1:
  51. continue
  52. cls_id = classes.index(cls)
  53. xmlbox = obj.find('bndbox')
  54. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
  55. float(xmlbox.find('ymax').text))
  56. bb = convert_box((w, h), b)
  57. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  58. cwd = getcwd()
  59. for year, image_set in sets:
  60. if not os.path.exists('VOCdevkit/VOC%s/labels/' % year):
  61. os.makedirs('VOCdevkit/VOC%s/labels/' % year)
  62. image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt' % (year, image_set)).read().strip().split()
  63. list_file = open('%s_%s.txt' % (year, image_set), 'w')
  64. for image_id in image_ids:
  65. list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n' % (cwd, year, image_id))
  66. convert_annotation(year, image_id)
  67. list_file.close()
  68. END
  69. cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt >train.txt
  70. cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt >train.all.txt
  71. mkdir ../VOC ../VOC/images ../VOC/images/train ../VOC/images/val
  72. mkdir ../VOC/labels ../VOC/labels/train ../VOC/labels/val
  73. python3 - "$@" <<END
  74. import os
  75. print(os.path.exists('../tmp/train.txt'))
  76. with open('../tmp/train.txt', 'r') as f:
  77. for line in f.readlines():
  78. line = "/".join(line.split('/')[-5:]).strip()
  79. if os.path.exists("../" + line):
  80. os.system("cp ../" + line + " ../VOC/images/train")
  81. line = line.replace('JPEGImages', 'labels').replace('jpg', 'txt')
  82. if os.path.exists("../" + line):
  83. os.system("cp ../" + line + " ../VOC/labels/train")
  84. print(os.path.exists('../tmp/2007_test.txt'))
  85. with open('../tmp/2007_test.txt', 'r') as f:
  86. for line in f.readlines():
  87. line = "/".join(line.split('/')[-5:]).strip()
  88. if os.path.exists("../" + line):
  89. os.system("cp ../" + line + " ../VOC/images/val")
  90. line = line.replace('JPEGImages', 'labels').replace('jpg', 'txt')
  91. if os.path.exists("../" + line):
  92. os.system("cp ../" + line + " ../VOC/labels/val")
  93. END
  94. rm -rf ../tmp # remove temporary directory
  95. echo "VOC download done."