Leyo commited on
Commit
d7f90c3
·
1 Parent(s): 9998724

allow for multiple datasets per split

Browse files
Files changed (1) hide show
  1. NLVR2.py +33 -28
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
- "test1": "test1",
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={"files_path": files_path[self.config.name]["train"], "images_path": train_img_path, "split_name": "train"},
95
  ),
96
  datasets.SplitGenerator(
97
  name=datasets.Split.VALIDATION,
98
- gen_kwargs={"files_path": files_path[self.config.name]["validation"], "images_path": validation_img_path, "split_name": "dev"},
99
  ),
100
  datasets.SplitGenerator(
101
  name=datasets.Split.TEST,
102
- gen_kwargs={"files_path": files_path[self.config.name]["test1"], "images_path": test1_img_path, "split_name": "test1"},
103
  ),
104
  ]
105
 
106
- def _generate_examples(self, files_path, images_path, split_name):
107
  idx = 0
108
- for line in open(files_path).readlines():
109
- ex = json.loads(line)
110
- common_img_identifier = ex["identifier"].split("-")
111
- left_img_identifier = f"{split_name}-{common_img_identifier[1]}-{common_img_identifier[2]}-img0.png"
112
- right_img_identifier = f"{split_name}-{common_img_identifier[1]}-{common_img_identifier[2]}-img1.png"
113
- if split_name == "train":
114
- directory = str(ex["directory"])
115
- left_image_path = str(os.path.join(images_path, directory, left_img_identifier))
116
- right_image_path = str(os.path.join(images_path, directory, right_img_identifier))
117
- else:
118
- left_image_path = str(os.path.join(images_path, left_img_identifier))
119
- right_image_path = str(os.path.join(images_path, right_img_identifier))
120
- assert (os.path.exists(left_image_path))
121
- assert (os.path.exists(right_image_path))
122
- record = {
123
- "identifier": ex["identifier"],
124
- "sentence": ex["sentence"],
125
- "left_image": left_image_path,
126
- "right_image": right_image_path,
127
- "label": ex["label"],
128
- }
129
- idx += 1
130
- yield idx, record
 
 
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