|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from random import shuffle |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +HOMEDIR = os.path.expanduser("~") |
| 9 | +CURDIR = os.path.dirname(os.path.realpath(__file__)) |
| 10 | + |
| 11 | +# If true, re-create all list files. |
| 12 | +redo = False |
| 13 | +# The root directory which holds all information of the dataset. |
| 14 | +data_dir = "{}/data/ILSVRC".format(HOMEDIR) |
| 15 | +# The directory name which holds the image sets. |
| 16 | +imgset_dir = "ImageSets/DET" |
| 17 | +# The direcotry which contains the images. |
| 18 | +img_dir = "Data/DET" |
| 19 | +img_ext = "JPEG" |
| 20 | +# The directory which contains the annotations. |
| 21 | +anno_dir = "Annotations/DET" |
| 22 | +anno_ext = "xml" |
| 23 | + |
| 24 | +train_list_file = "{}/trainval1.txt".format(CURDIR) |
| 25 | +val_list_file = "{}/val2.txt".format(CURDIR) |
| 26 | +val_name_size_file = "{}/val2_name_size.txt".format(CURDIR) |
| 27 | +test_list_file = "{}/test.txt".format(CURDIR) |
| 28 | +test_name_size_file = "{}/test_name_size.txt".format(CURDIR) |
| 29 | + |
| 30 | +# Create training set. |
| 31 | +# We follow Ross Girschick's split in R-CNN. |
| 32 | +if redo or not os.path.exists(train_list_file): |
| 33 | + datasets = ["train", "val1"] |
| 34 | + img_files = [] |
| 35 | + anno_files = [] |
| 36 | + for dataset in datasets: |
| 37 | + imgset_file = "{}/{}/{}.txt".format(data_dir, imgset_dir, dataset) |
| 38 | + with open(imgset_file, "r") as f: |
| 39 | + for line in f.readlines(): |
| 40 | + name = line.strip("\n").split(" ")[0] |
| 41 | + subset = name.split("/")[0].split("_")[1] |
| 42 | + anno_file = "{}/{}/{}.{}".format(anno_dir, subset, name, anno_ext) |
| 43 | + # Ignore image if it does not have annotation. These are the negative images in ILSVRC. |
| 44 | + if not os.path.exists("{}/{}".format(data_dir, anno_file)): |
| 45 | + continue |
| 46 | + img_file = "{}/{}/{}.{}".format(img_dir, subset, name, img_ext) |
| 47 | + assert os.path.exists("{}/{}".format(data_dir, img_file)) |
| 48 | + img_files.append(img_file) |
| 49 | + anno_files.append(anno_file) |
| 50 | + # Shuffle the images. |
| 51 | + idx = [i for i in xrange(len(img_files))] |
| 52 | + shuffle(idx) |
| 53 | + with open(train_list_file, "w") as f: |
| 54 | + for i in idx: |
| 55 | + f.write("{} {}\n".format(img_files[i], anno_files[i])) |
| 56 | + |
| 57 | +if redo or not os.path.exists(val_list_file): |
| 58 | + datasets = ["val2"] |
| 59 | + subset = "val" |
| 60 | + img_files = [] |
| 61 | + anno_files = [] |
| 62 | + for dataset in datasets: |
| 63 | + imgset_file = "{}/{}/{}.txt".format(data_dir, imgset_dir, dataset) |
| 64 | + with open(imgset_file, "r") as f: |
| 65 | + for line in f.readlines(): |
| 66 | + name = line.strip("\n").split(" ")[0] |
| 67 | + img_file = "{}/{}/{}.{}".format(img_dir, subset, name, img_ext) |
| 68 | + assert os.path.exists("{}/{}".format(data_dir, img_file)) |
| 69 | + anno_file = "{}/{}/{}.{}".format(anno_dir, subset, name, anno_ext) |
| 70 | + assert os.path.exists("{}/{}".format(data_dir, anno_file)) |
| 71 | + img_files.append(img_file) |
| 72 | + anno_files.append(anno_file) |
| 73 | + with open(val_list_file, "w") as f: |
| 74 | + for i in xrange(len(img_files)): |
| 75 | + f.write("{} {}\n".format(img_files[i], anno_files[i])) |
| 76 | + |
| 77 | +if redo or not os.path.exists(val_name_size_file): |
| 78 | + dataset = 'val2' |
| 79 | + imgset_file = "{}/{}/{}.txt".format(data_dir, imgset_dir, dataset) |
| 80 | + cmd = "{}/../../build/tools/get_image_size --name_id_file={} {} {} {}".format( |
| 81 | + CURDIR, imgset_file, data_dir, val_list_file, val_name_size_file) |
| 82 | + print cmd |
| 83 | + process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) |
| 84 | + output = process.communicate()[0] |
| 85 | + |
| 86 | +if redo or not os.path.exists(test_list_file): |
| 87 | + datasets = ["test"] |
| 88 | + subset = "test" |
| 89 | + img_files = [] |
| 90 | + for dataset in datasets: |
| 91 | + imgset_file = "{}/{}/{}.txt".format(data_dir, imgset_dir, dataset) |
| 92 | + with open(imgset_file, "r") as f: |
| 93 | + for line in f.readlines(): |
| 94 | + name = line.strip("\n").split(" ")[0] |
| 95 | + img_file = "{}/{}/{}.{}".format(img_dir, subset, name, img_ext) |
| 96 | + assert os.path.exists("{}/{}".format(data_dir, img_file)) |
| 97 | + img_files.append(img_file) |
| 98 | + with open(test_list_file, "w") as f: |
| 99 | + for i in xrange(len(img_files)): |
| 100 | + f.write("{} 0\n".format(img_files[i])) |
| 101 | + |
| 102 | +if redo or not os.path.exists(test_name_size_file): |
| 103 | + dataset = 'test' |
| 104 | + imgset_file = "{}/{}/{}.txt".format(data_dir, imgset_dir, dataset) |
| 105 | + cmd = "{}/../../build/tools/get_image_size --name_id_file={} {} {} {}".format( |
| 106 | + CURDIR, imgset_file, data_dir, test_list_file, test_name_size_file) |
| 107 | + print cmd |
| 108 | + process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) |
| 109 | + output = process.communicate()[0] |
0 commit comments