in libraries/python/imagenet_test_map.py [0:0]
def mapImageLabels(self):
with open(self.args.label_file, "r") as f:
content = f.read()
dir_label_mapping_str = content.strip().split("\n")
dir_label_mapping = [line.strip().split(",") for line in dir_label_mapping_str]
all_images_map = []
for idx in range(len(dir_label_mapping)):
one_map = dir_label_mapping[idx]
rel_dir = one_map[0]
label = one_map[1]
dir = os.path.join(os.path.abspath(self.args.image_dir), rel_dir)
assert os.path.isdir(dir), "image dir {} doesn't exist".format(dir)
files = os.listdir(dir)
images_map = [
{
"path": os.path.join(dir, filename.strip()),
"index": idx,
"label": label,
}
for filename in files
]
if self.args.shuffle:
random.shuffle(images_map)
if self.args.limit_per_category:
images_map = images_map[: self.args.limit_per_category]
all_images_map.extend(images_map)
if self.args.shuffle:
random.shuffle(all_images_map)
if self.args.limit_files:
all_images_map = all_images_map[: self.args.limit_files]
if self.args.output_image_file:
with open(self.args.output_image_file, "w") as f:
image_files = [item["path"] for item in all_images_map]
content = "\n".join(image_files)
f.write(content)
with open(self.args.output_label_file, "w") as f:
labels = [
str(item["index"]) + "," + item["label"] + "," + item["path"]
for item in all_images_map
]
content = "\n".join(labels)
f.write(content)