Leyo commited on
Commit
d77f7bd
·
1 Parent(s): 9962099

add image paths

Browse files
Files changed (1) hide show
  1. NLVR2.py +32 -7
NLVR2.py CHANGED
@@ -44,6 +44,13 @@ _SPLITS = {
44
  "test1": "test1.json",
45
  "test2": "test2.json",
46
  }
 
 
 
 
 
 
 
47
 
48
 
49
  class NLVR2Dataset(datasets.GeneratorBasedBuilder):
@@ -57,7 +64,7 @@ class NLVR2Dataset(datasets.GeneratorBasedBuilder):
57
  return datasets.DatasetInfo(
58
  description=_DESCRIPTION,
59
  features=datasets.Features(
60
- {
61
  "identifier": datasets.Value("string"),
62
  "sentence": datasets.Value("string"),
63
  "left_image": datasets.Image(),
@@ -80,31 +87,49 @@ class NLVR2Dataset(datasets.GeneratorBasedBuilder):
80
  },
81
  }
82
  files_path = dl_manager.download_and_extract(urls)
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  return [
84
  datasets.SplitGenerator(
85
  name=datasets.Split.TRAIN,
86
- gen_kwargs={"files_paths": [files_path[self.config.name]["train"]]},
87
  ),
88
  datasets.SplitGenerator(
89
  name=datasets.Split.VALIDATION,
90
- gen_kwargs={"files_paths": [files_path[self.config.name]["validation"]]},
91
  ),
92
  datasets.SplitGenerator(
93
  name=datasets.Split.TEST,
94
- gen_kwargs={"files_paths": [files_path[self.config.name]["test1"], files_path[self.config.name]["test2"]]},
95
  ),
96
  ]
97
 
98
- def _generate_examples(self, files_paths):
99
  idx = 0
100
  for path in files_paths:
101
  for line in open(path).readlines():
102
  ex = json.loads(line)
 
 
 
 
 
103
  record = {
104
  "identifier": ex["identifier"],
105
  "sentence": ex["sentence"],
106
- "left_image": str(ex["left_url"]),
107
- "right_image": str(ex["right_url"]),
108
  "label": ex["label"],
109
  }
110
  idx += 1
 
44
  "test1": "test1.json",
45
  "test2": "test2.json",
46
  }
47
+ _JZ_IMG_FOLDER_PATH = f"{os.environ['cnw_ALL_CCFRSCRATCH']}/local_datasets/nlvr2"
48
+ _IMG_SPLITS = {
49
+ "train": "train.zip",
50
+ "validation": "dev.zip",
51
+ "test1": "test1.zip",
52
+ "test2": "test2..zip",
53
+ }
54
 
55
 
56
  class NLVR2Dataset(datasets.GeneratorBasedBuilder):
 
64
  return datasets.DatasetInfo(
65
  description=_DESCRIPTION,
66
  features=datasets.Features(
67
+ {
68
  "identifier": datasets.Value("string"),
69
  "sentence": datasets.Value("string"),
70
  "left_image": datasets.Image(),
 
87
  },
88
  }
89
  files_path = dl_manager.download_and_extract(urls)
90
+
91
+ images_files = {
92
+ "Default": {
93
+ "train": os.path.join(_JZ_IMG_FOLDER_PATH, _IMG_SPLITS["train"]),
94
+ "validation": os.path.join(_JZ_IMG_FOLDER_PATH, _IMG_SPLITS["validation"]),
95
+ "test1": os.path.join(_JZ_IMG_FOLDER_PATH, _IMG_SPLITS["test1"]),
96
+ "test2": os.path.join(_JZ_IMG_FOLDER_PATH, _IMG_SPLITS["test2"]),
97
+ },
98
+ }
99
+ images_path = os.path.join(
100
+ dl_manager.extract(images_files),
101
+ "nlvr2-images"
102
+ )
103
  return [
104
  datasets.SplitGenerator(
105
  name=datasets.Split.TRAIN,
106
+ gen_kwargs={"files_paths": [files_path[self.config.name]["train"]], "images_path": images_path, "split_name": "train"},
107
  ),
108
  datasets.SplitGenerator(
109
  name=datasets.Split.VALIDATION,
110
+ gen_kwargs={"files_paths": [files_path[self.config.name]["validation"]], "images_path": images_path, "split_name": "dev"},
111
  ),
112
  datasets.SplitGenerator(
113
  name=datasets.Split.TEST,
114
+ gen_kwargs={"files_paths": [files_path[self.config.name]["test1"], files_path[self.config.name]["test2"]], "images_path": images_path, "split_name": "test1"},
115
  ),
116
  ]
117
 
118
+ def _generate_examples(self, files_paths, images_path, split_name):
119
  idx = 0
120
  for path in files_paths:
121
  for line in open(path).readlines():
122
  ex = json.loads(line)
123
+ common_img_identifier = ex["identifier"].split("-")
124
+ left_img_identifier = f"{split_name-common_img_identifier[1]}-{common_img_identifier[2]}-img0.png"
125
+ right_img_identifier = f"{split_name-common_img_identifier[1]}-{common_img_identifier[2]}-img1.png"
126
+ left_image_path = str(os.path.join(images_path, left_img_identifier))
127
+ right_image_path = str(os.path.join(images_path, right_img_identifier))
128
  record = {
129
  "identifier": ex["identifier"],
130
  "sentence": ex["sentence"],
131
+ "left_image": left_image_path,
132
+ "right_image": right_image_path,
133
  "label": ex["label"],
134
  }
135
  idx += 1