jordyvl commited on
Commit
dd969e9
1 Parent(s): 0aca80a

binary mode consumes a lot of RAM, added non binary mode

Browse files
Files changed (2) hide show
  1. DUDE_loader.py +38 -33
  2. test_loader.py +1 -1
DUDE_loader.py CHANGED
@@ -108,21 +108,38 @@ def open_pdf_binary(pdf_file):
108
  return f.read()
109
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  class DUDE(datasets.GeneratorBasedBuilder):
112
  """DUDE dataset."""
113
 
 
 
114
  BUILDER_CONFIGS = [
115
- datasets.BuilderConfig(
116
- name="DUDE",
117
- version=datasets.Version("0.0.1"),
118
- description=_DESCRIPTION,
119
- )
120
  ]
121
 
122
- DEFAULT_CONFIG_NAME = "DUDE"
123
 
124
- def _info(self):
125
 
 
126
  features = datasets.Features(
127
  {
128
  "docId": datasets.Value("string"),
@@ -141,8 +158,8 @@ class DUDE(datasets.GeneratorBasedBuilder):
141
  "answers_variants": datasets.Sequence(datasets.Value("string")),
142
  "answer_type": datasets.Value("string"),
143
  "data_split": datasets.Value("string"),
144
- "document": datasets.Value("binary"),
145
- "OCR": datasets.Value("binary"),
146
  }
147
  )
148
 
@@ -190,44 +207,32 @@ class DUDE(datasets.GeneratorBasedBuilder):
190
  def _generate_examples(self, binary_extraction_path, annotations, split):
191
  def retrieve_doc(docid):
192
  extracted_path = os.path.join(binary_extraction_path, "PDF", split, docid + ".pdf")
193
- with open(extracted_path, "rb") as f:
194
- return f.read()
195
 
196
  def retrieve_OCR(docid, ocr_engine="Amazon", format="original"):
197
  extracted_path = os.path.join(
198
  binary_extraction_path, "OCR", ocr_engine, docid + f"_{format}.json"
199
  )
200
-
201
- with open(extracted_path, "rb") as f:
202
- return f.read()
203
 
204
  question = self.info.features["question"]
205
  answers = self.info.features["answers"]
206
 
207
- extensions = {"pdf", "PDF"}
208
-
209
  annotations = [x for x in annotations if x["data_split"] == split]
210
 
211
  for i, a in enumerate(annotations):
212
  a["data_split"] = split
213
  if a["docId"] in SKIP_DOC_IDS:
214
  continue
215
- a["document"] = retrieve_doc(a["docId"])
216
- a["OCR"] = retrieve_OCR(a["docId"])
217
-
218
  a["answers_page_bounding_boxes"] = parse_bbox(a["answers_page_bounding_boxes"])
219
- yield i, a
220
-
221
- """
222
- # FIXES for faulty generation
223
- # a.pop("answers_page_bounding_boxes") # fix later
224
- if a["answers_page_bounding_boxes"] in [[], [[]]]:
225
- a["answers_page_bounding_boxes"] = None
226
  else:
227
- if isinstance(a["answers_page_bounding_boxes"][0], list):
228
- a["answers_page_bounding_boxes"] = a["answers_page_bounding_boxes"][0]
229
- # if i == 2303:
230
- try:
231
- except Exception as e:
232
- print(f"Something wrong in {split}-{i} {e}")
233
- """
 
108
  return f.read()
109
 
110
 
111
+ class DUDEConfig(datasets.BuilderConfig):
112
+ """BuilderConfig for DUDE."""
113
+
114
+ def __init__(
115
+ self,
116
+ binary_mode: bool,
117
+ **kwargs,
118
+ ):
119
+ """BuilderConfig for DUDE.
120
+ Args:
121
+ binary_mode: `boolean`, load binary PDFs/OCR or pass along paths on local file system
122
+ **kwargs: keyword arguments forwarded to super.
123
+ """
124
+ BINARY_MODE = False
125
+ super(DUDEConfig, self).__init__(description=_DESCRIPTION, **kwargs)
126
+ self.binary_mode = binary_mode or BINARY_MODE
127
+
128
+
129
  class DUDE(datasets.GeneratorBasedBuilder):
130
  """DUDE dataset."""
131
 
132
+ VERSION = datasets.Version("0.0.1")
133
+
134
  BUILDER_CONFIGS = [
135
+ DUDEConfig(name='DUDE', version=VERSION, binary_mode=False),
136
+ DUDEConfig(name='DUDE-binary', version=VERSION, binary_mode=True)
 
 
 
137
  ]
138
 
139
+ DEFAULT_CONFIG_NAME = "DUDE" #for some reason not working
140
 
 
141
 
142
+ def _info(self):
143
  features = datasets.Features(
144
  {
145
  "docId": datasets.Value("string"),
 
158
  "answers_variants": datasets.Sequence(datasets.Value("string")),
159
  "answer_type": datasets.Value("string"),
160
  "data_split": datasets.Value("string"),
161
+ "document": datasets.Value("binary") if self.config.binary_mode else datasets.Value("string"),
162
+ "OCR": datasets.Value("binary") if self.config.binary_mode else datasets.Value("string"),
163
  }
164
  )
165
 
 
207
  def _generate_examples(self, binary_extraction_path, annotations, split):
208
  def retrieve_doc(docid):
209
  extracted_path = os.path.join(binary_extraction_path, "PDF", split, docid + ".pdf")
210
+ return extracted_path
211
+
212
 
213
  def retrieve_OCR(docid, ocr_engine="Amazon", format="original"):
214
  extracted_path = os.path.join(
215
  binary_extraction_path, "OCR", ocr_engine, docid + f"_{format}.json"
216
  )
217
+ return extracted_path
 
 
218
 
219
  question = self.info.features["question"]
220
  answers = self.info.features["answers"]
221
 
 
 
222
  annotations = [x for x in annotations if x["data_split"] == split]
223
 
224
  for i, a in enumerate(annotations):
225
  a["data_split"] = split
226
  if a["docId"] in SKIP_DOC_IDS:
227
  continue
 
 
 
228
  a["answers_page_bounding_boxes"] = parse_bbox(a["answers_page_bounding_boxes"])
229
+ docpath = retrieve_doc(a["docId"])
230
+ ocrpath = retrieve_OCR(a["docId"])
231
+ if self.config.binary_mode:
232
+ with open(docpath, "rb") as f, open(ocrpath, "rb") as g:
233
+ a["document"] = f.read()
234
+ a["OCR"] = g.read()
 
235
  else:
236
+ a["document"] = docpath
237
+ a["OCR"] = ocrpath
238
+ yield i, a
 
 
 
 
test_loader.py CHANGED
@@ -22,7 +22,7 @@ from codetiming import Timer
22
  for binding in ["dict_annotations (new)"]: #"dict_PDF",
23
  with Timer(name=f"{binding}", text=binding + " Elapsed time: {:.4f} seconds"):
24
  if binding == "dict_annotations (new)":
25
- ds = load_dataset("../DUDE_loader/DUDE_loader.py", data_dir="/home/jordy/Downloads/DUDE_train-val-test_binaries", writer_batch_size=10) #ignore_verifications=True,
26
  else:
27
  ds = load_dataset("jordyvl/DUDE_loader", revision='db20bbf751b14e14e8143170bc201948ef5ac83c')
28
 
 
22
  for binding in ["dict_annotations (new)"]: #"dict_PDF",
23
  with Timer(name=f"{binding}", text=binding + " Elapsed time: {:.4f} seconds"):
24
  if binding == "dict_annotations (new)":
25
+ ds = load_dataset("../DUDE_loader/DUDE_loader.py", 'DUDE', data_dir="/home/jordy/Downloads/DUDE_train-val-test_binaries") #ignore_verifications=True, , writer_batch_size=10
26
  else:
27
  ds = load_dataset("jordyvl/DUDE_loader", revision='db20bbf751b14e14e8143170bc201948ef5ac83c')
28