DUDE_loader / DUDE_loader.py
jordyvl's picture
linked trainvalv3 as annotations
0ae7826
raw
history blame
No virus
8.38 kB
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""DUDE dataset loader"""
import os
import copy
import json
from typing import List, Literal
import pdf2image
from tqdm import tqdm
import datasets
_CITATION = """
@inproceedings{dude2023icdar,
title={ICDAR 2023 Challenge on Document UnderstanDing of Everything (DUDE)},
author={Van Landeghem, Jordy et . al.},
booktitle={Proceedings of the ICDAR},
year={2023}
}
"""
_DESCRIPTION = """\
DUDE requires models to reason and understand about document layouts in multi-page images/PDFs to answer questions about them.
Specifically, models need to incorporate a new modality of layout present in the images/PDFs and reason
over it to answer DUDE questions.
""" # DUDE Contains X questions and Y and ...
_HOMEPAGE = "https://rrc.cvc.uab.es/?ch=23"
_LICENSE = "CC BY 4.0"
_SPLITS = ["train", "val"]
_URLS = {
# "binaries": "https://huggingface.co/datasets/jordyvl/DUDE_loader/resolve/main/data/DUDE_binaries.tar.gz", #
# "annotations": "https://huggingface.co/datasets/jordyvl/DUDE_loader/resolve/main/data/DUDE_dataset-sample_gt.json" #"
"binaries": "https://huggingface.co/datasets/jordyvl/DUDE_loader/resolve/main/data/DUDE_train-val-test_binaries.tar.gz", # DUDE_train-val-test_binaries.tar.gz
"annotations": "https://zenodo.org/record/7652563/files/DUDE_gt_release-candidate_trainval.json?download=1",
}
SKIP_DOC_IDS = ["ef03364aa27a0987c9870472e312aceb", "5c5a5880e6a73b4be2315d506ab0b15b"]
def parse_bbox(bbox):
if bbox in [[], [[]]]:
return None
answers_page_bounding_boxes = []
if isinstance(bbox[0], list):
bbox = bbox[0]
keys = ["left", "top", "width", "height", "page"]
for page_bb in bbox:
if len(page_bb) == 0:
continue
page_bb = {key: page_bb[key] for key in keys}
answers_page_bounding_boxes.append(page_bb)
return answers_page_bounding_boxes
def batched_conversion(pdf_file):
info = pdf2image.pdfinfo_from_path(pdf_file, userpw=None, poppler_path=None)
maxPages = info["Pages"]
logger.info(f"{pdf_file} has {str(maxPages)} pages")
images = []
for page in range(1, maxPages + 1, 10):
images.extend(
pdf2image.convert_from_path(
pdf_file, dpi=200, first_page=page, last_page=min(page + 10 - 1, maxPages)
)
)
return images
def open_pdf_binary(pdf_file):
with open(pdf_file, "rb") as f:
return f.read()
class DUDEConfig(datasets.BuilderConfig):
"""BuilderConfig for DUDE."""
def __init__(
self,
binary_mode: bool = False,
ocr_engine: Literal["Azure", "Amazon", "Tesseract"] = "Amazon",
format: Literal["original", "due"] = "original",
**kwargs,
):
"""BuilderConfig for DUDE.
Args:
binary_mode: `boolean`, load binary PDFs/OCR or pass along paths on local file system
**kwargs: keyword arguments forwarded to super.
"""
super(DUDEConfig, self).__init__(description=_DESCRIPTION, **kwargs)
self.binary_mode = binary_mode
self.ocr_engine = ocr_engine
self.format = format
def builder_configs(version):
configurations = []
for binary_mode in [True, False]:
for ocr_engine in ["Azure", "Amazon", "Tesseract"]:
for format in ["original", "due"]:
binary_name = "bin_" if binary_mode else ""
configurations.append(
DUDEConfig(
name=f"{binary_name}{ocr_engine}_{format}",
version=version,
binary_mode=binary_mode,
ocr_engine=ocr_engine,
format=format,
)
)
return configurations
class DUDE(datasets.GeneratorBasedBuilder):
"""DUDE dataset."""
VERSION = datasets.Version("0.0.1")
BUILDER_CONFIGS = builder_configs(VERSION)
DEFAULT_CONFIG_NAME = (
"Amazon_original" # for some reason not working, need to pass a config anyway
)
def _info(self):
features = datasets.Features(
{
"docId": datasets.Value("string"),
"questionId": datasets.Value("string"),
"question": datasets.Value("string"),
"answers": datasets.Sequence(datasets.Value("string")),
"answers_page_bounding_boxes": datasets.Sequence(
{
"left": datasets.Value("int32"),
"top": datasets.Value("int32"),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"page": datasets.Value("int32"),
}
),
"answers_variants": datasets.Sequence(datasets.Value("string")),
"answer_type": datasets.Value("string"),
"data_split": datasets.Value("string"),
"document": datasets.Value("binary")
if self.config.binary_mode
else datasets.Value("string"),
"OCR": datasets.Value("binary")
if self.config.binary_mode
else datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> List[datasets.SplitGenerator]:
annotations = json.load(open(_URLS[f"annotations"], "r"))
# binaries_archive = dl_manager.iter_archive(binaries_path)
if self.config.data_dir: # when unpacked to a custom directory
binary_extraction_path = self.config.data_dir
else:
binaries_path = dl_manager.download(_URLS["binaries"])
binary_extraction_path = dl_manager.extract(binaries_path)
splits = []
for split in _SPLITS:
splits.append(
datasets.SplitGenerator(
name=split,
gen_kwargs={
"binary_extraction_path": binary_extraction_path,
"annotations": annotations,
"split": split,
},
)
)
return splits
def _generate_examples(self, binary_extraction_path, annotations, split):
def retrieve_doc(docid):
extracted_path = os.path.join(binary_extraction_path, "PDF", split, docid + ".pdf")
return extracted_path
def retrieve_OCR(docid, ocr_engine="Amazon", format="original"):
extracted_path = os.path.join(
binary_extraction_path, "OCR", ocr_engine, docid + f"_{format}.json"
)
return extracted_path
annotations = [x for x in annotations if x["data_split"] == split]
for i, a in enumerate(annotations):
if a["docId"] in SKIP_DOC_IDS:
continue
a = dict(a)
a["data_split"] = split
a["answers_page_bounding_boxes"] = parse_bbox(a["answers_page_bounding_boxes"])
docpath = retrieve_doc(a["docId"])
ocrpath = retrieve_OCR(a["docId"])
if self.config.binary_mode:
with open(docpath, "rb") as f, open(ocrpath, "rb") as g:
a["document"] = f.read()
a["OCR"] = g.read()
else:
a["document"] = docpath
a["OCR"] = ocrpath
yield i, a