pierreguillou commited on
Commit
ec15867
1 Parent(s): d882a4c

Update DocLayNet-base.py

Browse files
Files changed (1) hide show
  1. DocLayNet-base.py +27 -29
DocLayNet-base.py CHANGED
@@ -42,7 +42,7 @@ _CITATION = """\
42
 
43
  # You can copy an official description
44
  _DESCRIPTION = """\
45
- Accurate document layout analysis is a key requirement for high-quality PDF document conversion. With the recent availability of public, large ground-truth datasets such as PubLayNet and DocBank, deep-learning models have proven to be very effective at layout detection and segmentation. While these datasets are of adequate size to train such models, they severely lack in layout variability since they are sourced from scientific article repositories such as PubMed and arXiv only. Consequently, the accuracy of the layout segmentation drops significantly when these models are applied on more challenging and diverse layouts. In this paper, we present \textit{DocLayNet}, a new, publicly available, document-layout annotation dataset in COCO format. It contains 80863 manually annotated pages from diverse data sources to represent a wide variability in layouts. For each PDF page, the layout annotations provide labelled bounding-boxes with a choice of 11 distinct classes. DocLayNet also provides a subset of double- and triple-annotated pages to determine the inter-annotator agreement. In multiple experiments, we provide baseline accuracy scores (in mAP) for a set of popular object detection models. We also demonstrate that these models fall approximately 10\% behind the inter-annotator agreement. Furthermore, we provide evidence that DocLayNet is of sufficient size. Lastly, we compare models trained on PubLayNet, DocBank and DocLayNet, showing that layout predictions of the DocLayNet-trained models are more robust and thus the preferred choice for general-purpose document-layout analysis.
46
  """
47
 
48
  _HOMEPAGE = "https://developer.ibm.com/exchanges/data/all/doclaynet/"
@@ -65,17 +65,17 @@ def load_image(image_path):
65
  logger = datasets.logging.get_logger(__name__)
66
 
67
 
68
- class DocLayNetConfig(datasets.BuilderConfig):
69
  """BuilderConfig for DocLayNet base"""
70
 
71
- def __init__(self, **kwargs):
72
  """BuilderConfig for DocLayNet base.
73
  Args:
74
  **kwargs: keyword arguments forwarded to super.
75
  """
76
- super(DocLayNetConfig, self).__init__(**kwargs)
77
-
78
 
 
79
  class DocLayNet(datasets.GeneratorBasedBuilder):
80
  """
81
  DocLayNet base is a about 10% of the dataset DocLayNet (more information at https://huggingface.co/datasets/pierreguillou/DocLayNet-base)
@@ -98,12 +98,15 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
98
  # You will be able to load one or the other configurations in the following list with
99
  # data = datasets.load_dataset('my_dataset', 'first_domain')
100
  # data = datasets.load_dataset('my_dataset', 'second_domain')
 
 
 
101
  BUILDER_CONFIGS = [
102
- DocLayNetConfig(name="DocLayNet", version=datasets.Version("1.0.0"), description="DocLayNeT base dataset"),
103
  ]
104
 
105
- #DEFAULT_CONFIG_NAME = "DocLayNet" # It's not mandatory to have a default configuration. Just use one if it make sense.
106
-
107
  def _info(self):
108
 
109
  features = datasets.Features(
@@ -155,54 +158,49 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
155
  # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
156
  # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
157
  # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
158
-
159
  downloaded_file = dl_manager.download_and_extract("https://huggingface.co/datasets/pierreguillou/DocLayNet-base/resolve/main/data/dataset_base.zip")
160
- splits = []
161
- for split in self.config.splits:
162
- if split == "train":
163
- dataset = datasets.SplitGenerator(
164
  name=datasets.Split.TRAIN,
165
  # These kwargs will be passed to _generate_examples
166
  gen_kwargs={
167
  "filepath": os.path.join(downloaded_file, "base_dataset/train/"),
168
- "split": "train",
169
  },
170
- )
171
- elif split in ["val", "valid", "validation", "dev"]:
172
- dataset = datasets.SplitGenerator(
173
  name=datasets.Split.VALIDATION,
174
  # These kwargs will be passed to _generate_examples
175
  gen_kwargs={
176
  "filepath": os.path.join(downloaded_file, "base_dataset/val/"),
177
- "split": "val",
178
  },
179
- )
180
- elif split in ["test"]:
181
- dataset = datasets.SplitGenerator(
182
  name=datasets.Split.TEST,
183
  # These kwargs will be passed to _generate_examples
184
  gen_kwargs={
185
  "filepath": os.path.join(downloaded_file, "base_dataset/test/"),
186
- "split": "test",
187
  },
188
- )
189
- else:
190
- continue
191
 
192
- splits.append(dataset)
193
- return splits
194
 
195
- def _generate_examples(self, filepath, split):
196
  logger.info("⏳ Generating examples from = %s", filepath)
197
  ann_dir = os.path.join(filepath, "annotations")
198
  img_dir = os.path.join(filepath, "images")
199
  pdf_dir = os.path.join(filepath, "pdfs")
 
200
  for guid, file in enumerate(sorted(os.listdir(ann_dir))):
201
  texts = []
202
  bboxes_block = []
203
  bboxes_line = []
204
  categories = []
205
-
206
  # get json
207
  file_path = os.path.join(ann_dir, file)
208
  with open(file_path, "r", encoding="utf8") as f:
 
42
 
43
  # You can copy an official description
44
  _DESCRIPTION = """\
45
+ Accurate document layout analysis is a key requirement for high-quality PDF document conversion. With the recent availability of public, large ground-truth datasets such as PubLayNet and DocBank, deep-learning models have proven to be very effective at layout detection and segmentation. While these datasets are of adequate size to train such models, they severely lack in layout variability since they are sourced from scientific article repositories such as PubMed and arXiv only. Consequently, the accuracy of the layout segmentation drops significantly when these models are applied on more challenging and diverse layouts. In this paper, we present \textit{DocLayNet}, a new, publicly available, document-layout annotation dataset in COCO format. It contains 80863 manually annotated pages from diverse data sources to represent a wide variability in layouts. For each PDF page, the layout annotations provide labelled bounding-boxes with a choice of 11 distinct classes. DocLayNet also provides a subset of double- and triple-annotated pages to determine the inter-annotator agreement. In multiple experiments, we provide smallline accuracy scores (in mAP) for a set of popular object detection models. We also demonstrate that these models fall approximately 10\% behind the inter-annotator agreement. Furthermore, we provide evidence that DocLayNet is of sufficient size. Lastly, we compare models trained on PubLayNet, DocBank and DocLayNet, showing that layout predictions of the DocLayNet-trained models are more robust and thus the preferred choice for general-purpose document-layout analysis.
46
  """
47
 
48
  _HOMEPAGE = "https://developer.ibm.com/exchanges/data/all/doclaynet/"
 
65
  logger = datasets.logging.get_logger(__name__)
66
 
67
 
68
+ class DocLayNetBuilderConfig(datasets.BuilderConfig):
69
  """BuilderConfig for DocLayNet base"""
70
 
71
+ def __init__(self, name, **kwargs):
72
  """BuilderConfig for DocLayNet base.
73
  Args:
74
  **kwargs: keyword arguments forwarded to super.
75
  """
76
+ super().__init__(name, **kwargs)
 
77
 
78
+
79
  class DocLayNet(datasets.GeneratorBasedBuilder):
80
  """
81
  DocLayNet base is a about 10% of the dataset DocLayNet (more information at https://huggingface.co/datasets/pierreguillou/DocLayNet-base)
 
98
  # You will be able to load one or the other configurations in the following list with
99
  # data = datasets.load_dataset('my_dataset', 'first_domain')
100
  # data = datasets.load_dataset('my_dataset', 'second_domain')
101
+
102
+ DEFAULT_CONFIG_NAME = "DocLayNet_2022.08_processed_on_2023.01" # It's not mandatory to have a default configuration. Just use one if it make sense.
103
+
104
  BUILDER_CONFIGS = [
105
+ DocLayNetBuilderConfig(name=DEFAULT_CONFIG_NAME, version=VERSION, description="DocLayNet base dataset"),
106
  ]
107
 
108
+ BUILDER_CONFIG_CLASS = DocLayNetBuilderConfig
109
+
110
  def _info(self):
111
 
112
  features = datasets.Features(
 
158
  # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
159
  # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
160
  # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
161
+
162
  downloaded_file = dl_manager.download_and_extract("https://huggingface.co/datasets/pierreguillou/DocLayNet-base/resolve/main/data/dataset_base.zip")
163
+
164
+ return [
165
+ datasets.SplitGenerator(
 
166
  name=datasets.Split.TRAIN,
167
  # These kwargs will be passed to _generate_examples
168
  gen_kwargs={
169
  "filepath": os.path.join(downloaded_file, "base_dataset/train/"),
170
+ # "split_key": "train",
171
  },
172
+ ),
173
+ datasets.SplitGenerator(
 
174
  name=datasets.Split.VALIDATION,
175
  # These kwargs will be passed to _generate_examples
176
  gen_kwargs={
177
  "filepath": os.path.join(downloaded_file, "base_dataset/val/"),
178
+ # "split_key": "validation",
179
  },
180
+ ),
181
+ datasets.SplitGenerator(
 
182
  name=datasets.Split.TEST,
183
  # These kwargs will be passed to _generate_examples
184
  gen_kwargs={
185
  "filepath": os.path.join(downloaded_file, "base_dataset/test/"),
186
+ # "split_key": "test"
187
  },
188
+ ),
189
+ ]
 
190
 
 
 
191
 
192
+ def _generate_examples(self, filepath):
193
  logger.info("⏳ Generating examples from = %s", filepath)
194
  ann_dir = os.path.join(filepath, "annotations")
195
  img_dir = os.path.join(filepath, "images")
196
  pdf_dir = os.path.join(filepath, "pdfs")
197
+
198
  for guid, file in enumerate(sorted(os.listdir(ann_dir))):
199
  texts = []
200
  bboxes_block = []
201
  bboxes_line = []
202
  categories = []
203
+
204
  # get json
205
  file_path = os.path.join(ann_dir, file)
206
  with open(file_path, "r", encoding="utf8") as f: