Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

213 lines
5.9KB

  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. # handle optional download dir
  11. if [ -z "$1" ]; then
  12. # navigate to ~/tmp
  13. echo "navigating to ../tmp/ ..."
  14. mkdir -p ../tmp
  15. cd ../tmp/
  16. else
  17. # check if is valid directory
  18. if [ ! -d $1 ]; then
  19. echo $1 "is not a valid directory"
  20. exit 0
  21. fi
  22. echo "navigating to" $1 "..."
  23. cd $1
  24. fi
  25. echo "Downloading VOC2007 trainval ..."
  26. # Download data
  27. curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
  28. echo "Downloading VOC2007 test data ..."
  29. curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
  30. echo "Done downloading."
  31. # Extract data
  32. echo "Extracting trainval ..."
  33. tar -xf VOCtrainval_06-Nov-2007.tar
  34. echo "Extracting test ..."
  35. tar -xf VOCtest_06-Nov-2007.tar
  36. echo "removing tars ..."
  37. rm VOCtrainval_06-Nov-2007.tar
  38. rm VOCtest_06-Nov-2007.tar
  39. end=$(date +%s)
  40. runtime=$((end - start))
  41. echo "Completed in" $runtime "seconds"
  42. start=$(date +%s)
  43. # handle optional download dir
  44. if [ -z "$1" ]; then
  45. # navigate to ~/tmp
  46. echo "navigating to ../tmp/ ..."
  47. mkdir -p ../tmp
  48. cd ../tmp/
  49. else
  50. # check if is valid directory
  51. if [ ! -d $1 ]; then
  52. echo $1 "is not a valid directory"
  53. exit 0
  54. fi
  55. echo "navigating to" $1 "..."
  56. cd $1
  57. fi
  58. echo "Downloading VOC2012 trainval ..."
  59. # Download data
  60. curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
  61. echo "Done downloading."
  62. # Extract data
  63. echo "Extracting trainval ..."
  64. tar -xf VOCtrainval_11-May-2012.tar
  65. echo "removing tar ..."
  66. rm VOCtrainval_11-May-2012.tar
  67. end=$(date +%s)
  68. runtime=$((end - start))
  69. echo "Completed in" $runtime "seconds"
  70. cd ../tmp
  71. echo "Spliting dataset..."
  72. python3 - "$@" <<END
  73. import xml.etree.ElementTree as ET
  74. import pickle
  75. import os
  76. from os import listdir, getcwd
  77. from os.path import join
  78. sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
  79. classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
  80. def convert(size, box):
  81. dw = 1./(size[0])
  82. dh = 1./(size[1])
  83. x = (box[0] + box[1])/2.0 - 1
  84. y = (box[2] + box[3])/2.0 - 1
  85. w = box[1] - box[0]
  86. h = box[3] - box[2]
  87. x = x*dw
  88. w = w*dw
  89. y = y*dh
  90. h = h*dh
  91. return (x,y,w,h)
  92. def convert_annotation(year, image_id):
  93. in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
  94. out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')
  95. tree=ET.parse(in_file)
  96. root = tree.getroot()
  97. size = root.find('size')
  98. w = int(size.find('width').text)
  99. h = int(size.find('height').text)
  100. for obj in root.iter('object'):
  101. difficult = obj.find('difficult').text
  102. cls = obj.find('name').text
  103. if cls not in classes or int(difficult)==1:
  104. continue
  105. cls_id = classes.index(cls)
  106. xmlbox = obj.find('bndbox')
  107. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
  108. bb = convert((w,h), b)
  109. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  110. wd = getcwd()
  111. for year, image_set in sets:
  112. if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
  113. os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
  114. image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
  115. list_file = open('%s_%s.txt'%(year, image_set), 'w')
  116. for image_id in image_ids:
  117. list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
  118. convert_annotation(year, image_id)
  119. list_file.close()
  120. END
  121. cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt >train.txt
  122. cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt >train.all.txt
  123. python3 - "$@" <<END
  124. import shutil
  125. import os
  126. os.system('mkdir ../VOC/')
  127. os.system('mkdir ../VOC/images')
  128. os.system('mkdir ../VOC/images/train')
  129. os.system('mkdir ../VOC/images/val')
  130. os.system('mkdir ../VOC/labels')
  131. os.system('mkdir ../VOC/labels/train')
  132. os.system('mkdir ../VOC/labels/val')
  133. import os
  134. print(os.path.exists('../tmp/train.txt'))
  135. f = open('../tmp/train.txt', 'r')
  136. lines = f.readlines()
  137. for line in lines:
  138. #print(line.split('/')[-1][:-1])
  139. line = "/".join(line.split('/')[2:])
  140. #print(line)
  141. if (os.path.exists("../" + line[:-1])):
  142. os.system("cp ../"+ line[:-1] + " ../VOC/images/train")
  143. print(os.path.exists('../tmp/train.txt'))
  144. f = open('../tmp/train.txt', 'r')
  145. lines = f.readlines()
  146. for line in lines:
  147. #print(line.split('/')[-1][:-1])
  148. line = "/".join(line.split('/')[2:])
  149. line = line.replace('JPEGImages', 'labels')
  150. line = line.replace('jpg', 'txt')
  151. #print(line)
  152. if (os.path.exists("../" + line[:-1])):
  153. os.system("cp ../"+ line[:-1] + " ../VOC/labels/train")
  154. print(os.path.exists('../tmp/2007_test.txt'))
  155. f = open('../tmp/2007_test.txt', 'r')
  156. lines = f.readlines()
  157. for line in lines:
  158. #print(line.split('/')[-1][:-1])
  159. line = "/".join(line.split('/')[2:])
  160. if (os.path.exists("../" + line[:-1])):
  161. os.system("cp ../"+ line[:-1] + " ../VOC/images/val")
  162. print(os.path.exists('../tmp/2007_test.txt'))
  163. f = open('../tmp/2007_test.txt', 'r')
  164. lines = f.readlines()
  165. for line in lines:
  166. #print(line.split('/')[-1][:-1])
  167. line = "/".join(line.split('/')[2:])
  168. line = line.replace('JPEGImages', 'labels')
  169. line = line.replace('jpg', 'txt')
  170. #print(line)
  171. if (os.path.exists("../" + line[:-1])):
  172. os.system("cp ../"+ line[:-1] + " ../VOC/labels/val")
  173. END
  174. rm -rf ../tmp # remove temporary directory
  175. echo "VOC download done."