allow for multiple datasets per split
Browse files
NLVR2.py
CHANGED
|
@@ -42,7 +42,7 @@ _URL_IMG = f"https://lil.nlp.cornell.edu/resources/NLVR2/"
|
|
| 42 |
_SPLITS = {
|
| 43 |
"train": "train",
|
| 44 |
"validation": "dev",
|
| 45 |
-
"
|
| 46 |
}
|
| 47 |
|
| 48 |
|
|
@@ -76,6 +76,7 @@ class NLVR2Dataset(datasets.GeneratorBasedBuilder):
|
|
| 76 |
"train": os.path.join(_URL_JSON, f'{_SPLITS["train"]}.json'),
|
| 77 |
"validation": os.path.join(_URL_JSON, f'{_SPLITS["validation"]}.json'),
|
| 78 |
"test1": os.path.join(_URL_JSON, f'{_SPLITS["test1"]}.json'),
|
|
|
|
| 79 |
},
|
| 80 |
}
|
| 81 |
files_path = dl_manager.download_and_extract(urls)
|
|
@@ -84,47 +85,51 @@ class NLVR2Dataset(datasets.GeneratorBasedBuilder):
|
|
| 84 |
"train": os.path.join(_URL_IMG, f'{_SPLITS["train"]}_img.zip'),
|
| 85 |
"validation": os.path.join(_URL_IMG, f'{_SPLITS["validation"]}_img.zip'),
|
| 86 |
"test1": os.path.join(_URL_IMG, f'{_SPLITS["test1"]}_img.zip'),
|
|
|
|
| 87 |
}
|
| 88 |
train_img_path = os.path.join(dl_manager.extract(images_files["train"]), "images", "train")
|
| 89 |
validation_img_path = os.path.join(dl_manager.download_and_extract(images_files["validation"]), "dev")
|
| 90 |
test1_img_path = os.path.join(dl_manager.download_and_extract(images_files["test1"]), "test1")
|
|
|
|
|
|
|
| 91 |
return [
|
| 92 |
datasets.SplitGenerator(
|
| 93 |
name=datasets.Split.TRAIN,
|
| 94 |
-
gen_kwargs={"
|
| 95 |
),
|
| 96 |
datasets.SplitGenerator(
|
| 97 |
name=datasets.Split.VALIDATION,
|
| 98 |
-
gen_kwargs={"
|
| 99 |
),
|
| 100 |
datasets.SplitGenerator(
|
| 101 |
name=datasets.Split.TEST,
|
| 102 |
-
gen_kwargs={"
|
| 103 |
),
|
| 104 |
]
|
| 105 |
|
| 106 |
-
def _generate_examples(self,
|
| 107 |
idx = 0
|
| 108 |
-
for
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
| 42 |
_SPLITS = {
|
| 43 |
"train": "train",
|
| 44 |
"validation": "dev",
|
| 45 |
+
"test": "test",
|
| 46 |
}
|
| 47 |
|
| 48 |
|
|
|
|
| 76 |
"train": os.path.join(_URL_JSON, f'{_SPLITS["train"]}.json'),
|
| 77 |
"validation": os.path.join(_URL_JSON, f'{_SPLITS["validation"]}.json'),
|
| 78 |
"test1": os.path.join(_URL_JSON, f'{_SPLITS["test1"]}.json'),
|
| 79 |
+
"test2": os.path.join(_URL_JSON, f'{_SPLITS["test2"]}.json'),
|
| 80 |
},
|
| 81 |
}
|
| 82 |
files_path = dl_manager.download_and_extract(urls)
|
|
|
|
| 85 |
"train": os.path.join(_URL_IMG, f'{_SPLITS["train"]}_img.zip'),
|
| 86 |
"validation": os.path.join(_URL_IMG, f'{_SPLITS["validation"]}_img.zip'),
|
| 87 |
"test1": os.path.join(_URL_IMG, f'{_SPLITS["test1"]}_img.zip'),
|
| 88 |
+
"test2": os.path.join(_URL_IMG, f'{_SPLITS["test2"]}.zip'),
|
| 89 |
}
|
| 90 |
train_img_path = os.path.join(dl_manager.extract(images_files["train"]), "images", "train")
|
| 91 |
validation_img_path = os.path.join(dl_manager.download_and_extract(images_files["validation"]), "dev")
|
| 92 |
test1_img_path = os.path.join(dl_manager.download_and_extract(images_files["test1"]), "test1")
|
| 93 |
+
test2_img_path = os.path.join(dl_manager.download_and_extract(images_files["test2"]), "test2")
|
| 94 |
+
|
| 95 |
return [
|
| 96 |
datasets.SplitGenerator(
|
| 97 |
name=datasets.Split.TRAIN,
|
| 98 |
+
gen_kwargs={"files_paths": [files_path[self.config.name]["train"]], "images_paths": [train_img_path], "split_name": "train"},
|
| 99 |
),
|
| 100 |
datasets.SplitGenerator(
|
| 101 |
name=datasets.Split.VALIDATION,
|
| 102 |
+
gen_kwargs={"files_paths": [files_path[self.config.name]["validation"]], "images_paths": [validation_img_path], "split_name": "dev"},
|
| 103 |
),
|
| 104 |
datasets.SplitGenerator(
|
| 105 |
name=datasets.Split.TEST,
|
| 106 |
+
gen_kwargs={"files_paths": [files_path[self.config.name]["test1"], files_path[self.config.name]["test1"]], "images_paths": [test1_img_path, test2_img_path], "split_name": "test"},
|
| 107 |
),
|
| 108 |
]
|
| 109 |
|
| 110 |
+
def _generate_examples(self, files_paths, images_paths, split_name):
|
| 111 |
idx = 0
|
| 112 |
+
for i, files_path in enumerate(files_paths):
|
| 113 |
+
for line in open(files_path).readlines():
|
| 114 |
+
ex = json.loads(line)
|
| 115 |
+
common_img_identifier = ex["identifier"].split("-")
|
| 116 |
+
left_img_identifier = f"{common_img_identifier[0]}-{common_img_identifier[1]}-{common_img_identifier[2]}-img0.png"
|
| 117 |
+
right_img_identifier = f"{common_img_identifier[0]}-{common_img_identifier[1]}-{common_img_identifier[2]}-img1.png"
|
| 118 |
+
if split_name == "train":
|
| 119 |
+
directory = str(ex["directory"])
|
| 120 |
+
left_image_path = str(os.path.join(images_paths[i], directory, left_img_identifier))
|
| 121 |
+
right_image_path = str(os.path.join(images_paths[i], directory, right_img_identifier))
|
| 122 |
+
else:
|
| 123 |
+
left_image_path = str(os.path.join(images_paths[i], left_img_identifier))
|
| 124 |
+
right_image_path = str(os.path.join(images_paths[i], right_img_identifier))
|
| 125 |
+
assert (os.path.exists(left_image_path))
|
| 126 |
+
assert (os.path.exists(right_image_path))
|
| 127 |
+
record = {
|
| 128 |
+
"identifier": ex["identifier"],
|
| 129 |
+
"sentence": ex["sentence"],
|
| 130 |
+
"left_image": left_image_path,
|
| 131 |
+
"right_image": right_image_path,
|
| 132 |
+
"label": ex["label"],
|
| 133 |
+
}
|
| 134 |
+
idx += 1
|
| 135 |
+
yield idx, record
|