| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """DocBank document understanding dataset.""" |
|
|
| import os |
| import datasets |
|
|
| |
| _CITATION = """\ |
| @misc{li2020docbank, |
| title={DocBank: A Benchmark Dataset for Document Layout Analysis}, |
| author={Minghao Li and Yiheng Xu and Lei Cui and Shaohan Huang and Furu Wei and Zhoujun Li and Ming Zhou}, |
| year={2020}, |
| eprint={2006.01038}, |
| archivePrefix={arXiv}, |
| primaryClass={cs.CL} |
| } |
| """ |
|
|
| |
| _DESCRIPTION = """\ |
| DocBank is a new large-scale dataset that is constructed using a weak supervision approach. |
| It enables models to integrate both the textual and layout information for downstream tasks. |
| The current DocBank dataset totally includes 500K document pages, where 400K for training, 50K for validation and 50K for testing. |
| """ |
|
|
| _HOMEPAGE = "https://doc-analysis.github.io/docbank-page/index.html" |
|
|
| _LICENSE = "Apache-2.0 license" |
|
|
|
|
| class DocBank(datasets.GeneratorBasedBuilder): |
| """DocBank is a dataset for Visual Document Understanding. |
| It enable models to integrate both textual and layout informtion for downstream tasks.""" |
|
|
| VERSION = datasets.Version("1.1.0") |
|
|
| @property |
| def manual_download_instructions(self): |
| return """\ |
| Please download the DocBank dataset from https://doc-analysis.github.io/docbank-page/index.html. Uncompress the dataset and use that location in |
| --data_dir argument. """ |
| |
| def _info(self): |
|
|
| features = datasets.Features( |
| { |
| "image": datasets.Image(), |
| "token": datasets.Value("string"), |
| "bounding_box": datasets.Sequence(datasets.Sequence(datasets.Value("uint16"))), |
| "color": datasets.Sequence(datasets.Sequence(datasets.Value("uint8"))), |
| "font": datasets.Value("string"), |
| "label": datasets.Value("string"), |
| } |
| ) |
| |
| return datasets.DatasetInfo( |
| |
| description=_DESCRIPTION, |
| |
| features=features, |
| |
| |
| |
| |
| homepage=_HOMEPAGE, |
| |
| license=_LICENSE, |
| |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| self.data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir)) |
| cwd = os.path.dirname(os.path.abspath(__file__)) |
| |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| |
| gen_kwargs={ |
| "filepath": os.path.join(cwd,"train.jsonl"), |
| "split": "train", |
| }, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, |
| |
| gen_kwargs={ |
| "filepath": os.path.join(cwd,"dev.jsonl"), |
| "split": "dev", |
| }, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, |
| |
| gen_kwargs={ |
| "filepath": os.path.join(cwd,"test.jsonl"), |
| "split": "test" |
| }, |
| ), |
| ] |
|
|
| |
| def _generate_examples(self, filepath, split): |
| |
| with open(filepath,'rt') as fp: |
| for file in fp: |
| index,basename = eval(file) |
| |
| txt_file = self.data_dir+'/DocBank_500K_txt/'+basename+'.txt' |
| img_file = self.data_dir+'/DocBank_500K_ori_img/'+basename+'_ori.jpg' |
| |
| with open(txt_file, 'r', encoding='utf8') as fp: |
| |
| words = [] |
| bboxes = [] |
| rgbs = [] |
| fontnames = [] |
| structures = [] |
| |
| for row in fp: |
| tts = row.split('\t') |
| |
| assert len(tts) == 10, f'Incomplete line in file {txt_file}' |
|
|
| word = tts[0] |
| bbox = list(map(int, tts[1:5])) |
| rgb = list(map(int, tts[5:8])) |
| fontname = tts[8] |
| structure = tts[9].strip() |
| |
| words.append(word) |
| bboxes.append(bbox) |
| rgbs.append(rgb) |
| fontnames.append(fontname) |
| structures.append(structure) |
| |
| |
| |
| yield index, { |
| "image": img_file, |
| "token": words, |
| "bounding_box": bboxes, |
| "color": rgbs, |
| "font": fontnames, |
| "label": structures, |
| } |