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.

140 line
4.3KB

  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 xml.etree.ElementTree as ET
  29. import pickle
  30. import os
  31. from os import listdir, getcwd
  32. from os.path import join
  33. sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
  34. classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
  35. def convert(size, box):
  36. dw = 1./(size[0])
  37. dh = 1./(size[1])
  38. x = (box[0] + box[1])/2.0 - 1
  39. y = (box[2] + box[3])/2.0 - 1
  40. w = box[1] - box[0]
  41. h = box[3] - box[2]
  42. x = x*dw
  43. w = w*dw
  44. y = y*dh
  45. h = h*dh
  46. return (x,y,w,h)
  47. def convert_annotation(year, image_id):
  48. in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
  49. out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')
  50. tree=ET.parse(in_file)
  51. root = tree.getroot()
  52. size = root.find('size')
  53. w = int(size.find('width').text)
  54. h = int(size.find('height').text)
  55. for obj in root.iter('object'):
  56. difficult = obj.find('difficult').text
  57. cls = obj.find('name').text
  58. if cls not in classes or int(difficult)==1:
  59. continue
  60. cls_id = classes.index(cls)
  61. xmlbox = obj.find('bndbox')
  62. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
  63. bb = convert((w,h), b)
  64. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  65. wd = getcwd()
  66. for year, image_set in sets:
  67. if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
  68. os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
  69. image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
  70. list_file = open('%s_%s.txt'%(year, image_set), 'w')
  71. for image_id in image_ids:
  72. list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
  73. convert_annotation(year, image_id)
  74. list_file.close()
  75. END
  76. cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt >train.txt
  77. cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt >train.all.txt
  78. python3 - "$@" <<END
  79. import shutil
  80. import os
  81. os.system('mkdir ../VOC/')
  82. os.system('mkdir ../VOC/images')
  83. os.system('mkdir ../VOC/images/train')
  84. os.system('mkdir ../VOC/images/val')
  85. os.system('mkdir ../VOC/labels')
  86. os.system('mkdir ../VOC/labels/train')
  87. os.system('mkdir ../VOC/labels/val')
  88. import os
  89. print(os.path.exists('../tmp/train.txt'))
  90. f = open('../tmp/train.txt', 'r')
  91. lines = f.readlines()
  92. for line in lines:
  93. line = "/".join(line.split('/')[-5:]).strip()
  94. if (os.path.exists("../" + line)):
  95. os.system("cp ../"+ line + " ../VOC/images/train")
  96. line = line.replace('JPEGImages', 'labels')
  97. line = line.replace('jpg', 'txt')
  98. if (os.path.exists("../" + line)):
  99. os.system("cp ../"+ line + " ../VOC/labels/train")
  100. print(os.path.exists('../tmp/2007_test.txt'))
  101. f = open('../tmp/2007_test.txt', 'r')
  102. lines = f.readlines()
  103. for line in lines:
  104. line = "/".join(line.split('/')[-5:]).strip()
  105. if (os.path.exists("../" + line)):
  106. os.system("cp ../"+ line + " ../VOC/images/val")
  107. line = line.replace('JPEGImages', 'labels')
  108. line = line.replace('jpg', 'txt')
  109. if (os.path.exists("../" + line)):
  110. os.system("cp ../"+ line + " ../VOC/labels/val")
  111. END
  112. rm -rf ../tmp # remove temporary directory
  113. echo "VOC download done."