Commit
·
99bb7ec
1
Parent(s):
c95b941
Update funsd.py
Browse files
funsd.py
CHANGED
|
@@ -7,10 +7,20 @@ from PIL import Image
|
|
| 7 |
import datasets
|
| 8 |
from transformers import AutoTokenizer
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
_URL = "https://github.com/doc-analysis/XFUND/releases/download/v1.0/"
|
| 12 |
-
|
| 13 |
-
_LANG = ["zh", "de", "es", "fr", "en", "it", "ja", "pt"]
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
|
|
@@ -47,29 +57,29 @@ def load_image(image_path):
|
|
| 47 |
image = image.transpose(2, 0, 1) # move channels to first dimension
|
| 48 |
return image, (w, h)
|
| 49 |
|
| 50 |
-
class
|
| 51 |
-
"""BuilderConfig for
|
| 52 |
|
| 53 |
-
def __init__(self,
|
| 54 |
-
"""
|
| 55 |
Args:
|
| 56 |
-
|
| 57 |
-
**kwargs: keyword arguments forwarded to super.
|
| 58 |
"""
|
| 59 |
-
super(
|
| 60 |
-
self.lang = lang
|
| 61 |
-
self.additional_langs = additional_langs
|
| 62 |
|
| 63 |
|
| 64 |
-
class
|
| 65 |
-
"""
|
| 66 |
|
| 67 |
-
BUILDER_CONFIGS = [
|
|
|
|
|
|
|
| 68 |
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
|
| 70 |
|
| 71 |
def _info(self):
|
| 72 |
return datasets.DatasetInfo(
|
|
|
|
| 73 |
features=datasets.Features(
|
| 74 |
{
|
| 75 |
"id": datasets.Value("string"),
|
|
@@ -103,33 +113,14 @@ class XFUND(datasets.GeneratorBasedBuilder):
|
|
| 103 |
|
| 104 |
def _split_generators(self, dl_manager):
|
| 105 |
"""Returns SplitGenerators."""
|
| 106 |
-
|
| 107 |
-
"train": [f"{_URL}{self.config.lang}.train.json", f"{_URL}{self.config.lang}.train.zip"],
|
| 108 |
-
"val": [f"{_URL}{self.config.lang}.val.json", f"{_URL}{self.config.lang}.val.zip"],
|
| 109 |
-
# "test": [f"{_URL}{self.config.lang}.test.json", f"{_URL}{self.config.lang}.test.zip"],
|
| 110 |
-
}
|
| 111 |
-
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
| 112 |
-
train_files_for_many_langs = [downloaded_files["train"]]
|
| 113 |
-
val_files_for_many_langs = [downloaded_files["val"]]
|
| 114 |
-
# test_files_for_many_langs = [downloaded_files["test"]]
|
| 115 |
-
if self.config.additional_langs:
|
| 116 |
-
additional_langs = self.config.additional_langs.split("+")
|
| 117 |
-
if "all" in additional_langs:
|
| 118 |
-
additional_langs = [lang for lang in _LANG if lang != self.config.lang]
|
| 119 |
-
for lang in additional_langs:
|
| 120 |
-
urls_to_download = {"train": [f"{_URL}{lang}.train.json", f"{_URL}{lang}.train.zip"]}
|
| 121 |
-
additional_downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
| 122 |
-
train_files_for_many_langs.append(additional_downloaded_files["train"])
|
| 123 |
-
|
| 124 |
-
logger.info(f"Training on {self.config.lang} with additional langs({self.config.additional_langs})")
|
| 125 |
-
logger.info(f"Evaluating on {self.config.lang}")
|
| 126 |
-
logger.info(f"Testing on {self.config.lang}")
|
| 127 |
return [
|
| 128 |
-
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_files_for_many_langs}),
|
| 129 |
datasets.SplitGenerator(
|
| 130 |
-
name=datasets.Split.
|
|
|
|
|
|
|
|
|
|
| 131 |
),
|
| 132 |
-
# datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepaths": test_files_for_many_langs}),
|
| 133 |
]
|
| 134 |
|
| 135 |
def _generate_examples(self, filepaths):
|
|
|
|
| 7 |
import datasets
|
| 8 |
from transformers import AutoTokenizer
|
| 9 |
|
| 10 |
+
_CITATION = """\
|
| 11 |
+
@article{Jaume2019FUNSDAD,
|
| 12 |
+
title={FUNSD: A Dataset for Form Understanding in Noisy Scanned Documents},
|
| 13 |
+
author={Guillaume Jaume and H. K. Ekenel and J. Thiran},
|
| 14 |
+
journal={2019 International Conference on Document Analysis and Recognition Workshops (ICDARW)},
|
| 15 |
+
year={2019},
|
| 16 |
+
volume={2},
|
| 17 |
+
pages={1-6}
|
| 18 |
+
}
|
| 19 |
+
"""
|
| 20 |
+
_DESCRIPTION = """\
|
| 21 |
+
https://guillaumejaume.github.io/FUNSD/
|
| 22 |
+
"""
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
|
|
|
|
| 57 |
image = image.transpose(2, 0, 1) # move channels to first dimension
|
| 58 |
return image, (w, h)
|
| 59 |
|
| 60 |
+
class FunsdConfig(datasets.BuilderConfig):
|
| 61 |
+
"""BuilderConfig for FUNSD"""
|
| 62 |
|
| 63 |
+
def __init__(self, **kwargs):
|
| 64 |
+
"""BuilderConfig for FUNSD.
|
| 65 |
Args:
|
| 66 |
+
**kwargs: keyword arguments forwarded to super.
|
|
|
|
| 67 |
"""
|
| 68 |
+
super(FunsdConfig, self).__init__(**kwargs)
|
|
|
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
+
class Funsd(datasets.GeneratorBasedBuilder):
|
| 72 |
+
"""FUNSD dataset."""
|
| 73 |
|
| 74 |
+
BUILDER_CONFIGS = [
|
| 75 |
+
FunsdConfig(name="funsd", version=datasets.Version("1.0.0"), description="FUNSD dataset"),
|
| 76 |
+
]
|
| 77 |
|
| 78 |
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
|
| 79 |
|
| 80 |
def _info(self):
|
| 81 |
return datasets.DatasetInfo(
|
| 82 |
+
description=_DESCRIPTION,
|
| 83 |
features=datasets.Features(
|
| 84 |
{
|
| 85 |
"id": datasets.Value("string"),
|
|
|
|
| 113 |
|
| 114 |
def _split_generators(self, dl_manager):
|
| 115 |
"""Returns SplitGenerators."""
|
| 116 |
+
downloaded_file = dl_manager.download_and_extract("https://guillaumejaume.github.io/FUNSD/dataset.zip")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return [
|
|
|
|
| 118 |
datasets.SplitGenerator(
|
| 119 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepath": f"{downloaded_file}/dataset/training_data/"}
|
| 120 |
+
),
|
| 121 |
+
datasets.SplitGenerator(
|
| 122 |
+
name=datasets.Split.TEST, gen_kwargs={"filepath": f"{downloaded_file}/dataset/testing_data/"}
|
| 123 |
),
|
|
|
|
| 124 |
]
|
| 125 |
|
| 126 |
def _generate_examples(self, filepaths):
|