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.

206 lines
5.6KB

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