jordyvl commited on
Commit
5e15b58
1 Parent(s): 264d183

First version

Browse files
Files changed (3) hide show
  1. README.md +49 -1
  2. data.tar.gz +3 -0
  3. rvl_cdip_n_mp.py +158 -0
README.md CHANGED
@@ -1,3 +1,51 @@
1
  ---
2
- license: cc-by-nc-sa-4.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ license: cc-by-nc-4.0
3
+ dataset_info:
4
+ features:
5
+ - name: id
6
+ dtype: string
7
+ - name: file
8
+ dtype: binary
9
+ - name: labels
10
+ dtype:
11
+ class_label:
12
+ names:
13
+ '0': letter
14
+ '1': form
15
+ '2': email
16
+ '3': handwritten
17
+ '4': advertisement
18
+ '5': scientific report
19
+ '6': scientific publication
20
+ '7': specification
21
+ '8': file folder
22
+ '9': news article
23
+ '10': budget
24
+ '11': invoice
25
+ '12': presentation
26
+ '13': questionnaire
27
+ '14': resume
28
+ '15': memo
29
+ splits:
30
+ - name: test
31
+ num_bytes: 1349159996
32
+ num_examples: 991
33
+ download_size: 0
34
+ dataset_size: 1349159996
35
  ---
36
+
37
+
38
+ # Dataset Card for RVL-CDIP-N_MultiPage
39
+
40
+ ## Extension
41
+
42
+ The data loader provides support for loading RVL_CDIP-N in its extended multipage format.
43
+ Big kudos to the original authors (first in CITATION) for collecting the RVL-CDIP-N dataset.
44
+ We stand on the shoulders of giants :)
45
+
46
+ ## Required installation
47
+
48
+ ```bash
49
+ pip3 install pypdf2 pdf2image
50
+ sudo apt-get install poppler-utils
51
+ ```
data.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:40cea6613e5b52c6dc617fa645b7f32b1c9566c314497ac6676e72df9ec440c6
3
+ size 1254580948
rvl_cdip_n_mp.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """RVL-CDIP (Ryerson Vision Lab Complex Document Information Processing) dataset"""
16
+
17
+
18
+ import os
19
+ import datasets
20
+ from pathlib import Path
21
+ from tqdm import tqdm
22
+ import pdf2image
23
+
24
+ datasets.logging.set_verbosity_info()
25
+ logger = datasets.logging.get_logger(__name__)
26
+
27
+ _MODE = "binary"
28
+
29
+ _CITATION = """\
30
+ @inproceedings{larson2022evaluating,
31
+ title={Evaluating Out-of-Distribution Performance on Document Image Classifiers},
32
+ author={Larson, Stefan and Lim, Gordon and Ai, Yutong and Kuang, David and Leach, Kevin},
33
+ booktitle={Thirty-sixth Conference on Neural Information Processing Systems Datasets and Benchmarks Track},
34
+ year={2022}
35
+ }
36
+
37
+ @inproceedings{bdpc,
38
+ title = {Beyond Document Page Classification},
39
+ author = {Anonymous},
40
+ booktitle = {Under Review},
41
+ year = {2023}
42
+ }
43
+ """
44
+
45
+ _DESCRIPTION = """\
46
+ The RVL-CDIP-N (Ryerson Vision Lab Complex Document Information Processing) dataset consists of newly gathered documents in 16 classes
47
+ There are 998 documents for testing purposes. There were 3 documents from the original dataset that could not be retrieved based on the metadata.
48
+ """
49
+
50
+
51
+ _HOMEPAGE = "https://www.cs.cmu.edu/~aharley/rvl-cdip/"
52
+ _LICENSE = "https://www.industrydocuments.ucsf.edu/help/copyright/"
53
+
54
+
55
+ SOURCE = "jordyvl/rvl_cdip_mp"
56
+ _URL = f"https://huggingface.co/datasets/{SOURCE}/resolve/main/data.gz"
57
+ _BACKOFF_folder = "/mnt/lerna/data/RVL-CDIP-NO/RVL-CDIP-N_pdf/data"
58
+
59
+ _CLASSES = [
60
+ "letter",
61
+ "form",
62
+ "email",
63
+ "handwritten",
64
+ "advertisement",
65
+ "scientific report",
66
+ "scientific publication",
67
+ "specification",
68
+ "file folder",
69
+ "news article",
70
+ "budget",
71
+ "invoice",
72
+ "presentation",
73
+ "questionnaire",
74
+ "resume",
75
+ "memo",
76
+ ]
77
+
78
+
79
+ def batched_conversion(pdf_file):
80
+ info = pdf2image.pdfinfo_from_path(pdf_file, userpw=None, poppler_path=None)
81
+ maxPages = info["Pages"]
82
+
83
+ logger.info(f"{pdf_file} has {str(maxPages)} pages")
84
+
85
+ images = []
86
+
87
+ for page in range(1, maxPages + 1, 10):
88
+ images.extend(
89
+ pdf2image.convert_from_path(pdf_file, dpi=200, first_page=page, last_page=min(page + 10 - 1, maxPages))
90
+ )
91
+ return images
92
+
93
+
94
+ def open_pdf_binary(pdf_file):
95
+ with open(pdf_file, "rb") as f:
96
+ return f.read()
97
+
98
+
99
+ class RvlCdipNMp(datasets.GeneratorBasedBuilder):
100
+ VERSION = datasets.Version("1.0.0")
101
+ DEFAULT_CONFIG_NAME = "default"
102
+
103
+ def _info(self):
104
+ if isinstance(self.config.data_dir, str):
105
+ folder = self.config.data_dir # contains the folder structure at someone local disk
106
+ else:
107
+ if not os.path.exists(_BACKOFF_folder):
108
+ raise ValueError("No data folder found. Please set data_dir or data_files.")
109
+ folder = _BACKOFF_folder # my local path, others should set data_dir or data_files
110
+ self.config.data_dir = folder
111
+
112
+ return datasets.DatasetInfo(
113
+ description=_DESCRIPTION,
114
+ features=datasets.Features(
115
+ {
116
+ "id": datasets.Value("string"),
117
+ "file": datasets.Value("binary"),
118
+ "labels": datasets.features.ClassLabel(names=_CLASSES),
119
+ }
120
+ ),
121
+ homepage=_HOMEPAGE,
122
+ citation=_CITATION,
123
+ license=_LICENSE,
124
+ task_templates=None,
125
+ )
126
+
127
+ def _split_generators(self, dl_manager):
128
+ if self.config.data_dir.endswith(".tar.gz"):
129
+ archive_path = dl_manager.download(self.config.data_dir)
130
+ data_files = dl_manager.iter_archive(archive_path)
131
+ else:
132
+ data_files = self.config.data_dir
133
+
134
+ return [datasets.SplitGenerator(name="test", gen_kwargs={"archive_path": data_files})]
135
+
136
+ def _generate_examples(self, archive_path):
137
+ labels = self.info.features["labels"]
138
+
139
+ extensions = {".pdf", ".PDF"}
140
+
141
+ for i, path in tqdm(enumerate(Path(archive_path).glob("**/*")), desc=f"{archive_path}"):
142
+ if path.suffix in extensions:
143
+ try:
144
+ if _MODE == "binary":
145
+ images = open_pdf_binary(path)
146
+ # batched_conversion(path)
147
+ else:
148
+ images = path
149
+
150
+ a = dict(
151
+ id=path.name,
152
+ file=images,
153
+ labels=labels.encode_example(path.parent.name.lower()),
154
+ )
155
+ yield path.name, a
156
+
157
+ except Exception as e:
158
+ logger.warning(f"{e} failed to parse {i}")