jordyvl commited on
Commit
2df1756
1 Parent(s): 815fbd9

First commit

Browse files
Files changed (3) hide show
  1. README.md +8 -1
  2. rvl_cdip_multi.py +125 -0
  3. test_loader.py +3 -0
README.md CHANGED
@@ -1,3 +1,10 @@
1
  ---
2
- license: cc-by-nc-sa-4.0
3
  ---
 
 
 
 
 
 
 
 
1
  ---
2
+ license: cc-by-nc-4.0
3
  ---
4
+
5
+ ## Required installation
6
+
7
+ ```bash
8
+ pip3 install pypdf2 pdf2image
9
+ sudo apt-get install poppler-utils
10
+ ```
rvl_cdip_multi.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List
3
+ import datasets
4
+ import pdf2image
5
+
6
+ logger = datasets.logging.get_logger(__name__)
7
+
8
+
9
+ _CITATION = """\
10
+ @inproceedings{jordyvlRVLmulti,
11
+ title = {},
12
+ author = {Jordy Van Landeghem},
13
+ booktitle = {},
14
+ year = {2023}
15
+ }
16
+ """
17
+
18
+
19
+ _DESCRIPTION = """\
20
+ blabla
21
+ """
22
+
23
+
24
+ _HOMEPAGE = "TBD"
25
+
26
+
27
+ _LICENSE = "https://www.industrydocuments.ucsf.edu/help/copyright/"
28
+
29
+
30
+ _BINARY_URL = (
31
+ "https://huggingface.co/datasets/jordyvl/unit-test_PDFfolder/resolve/main/data/data.tar.gz"
32
+ )
33
+
34
+ _BACKOFF_folder = "/mnt/lerna/data/RVL_CDIP_pdf"
35
+ # "/mnt/lerna/data/RVL_CDIP_multi.tar.gz"
36
+
37
+ _CLASSES = [
38
+ "letter",
39
+ "form",
40
+ "email",
41
+ "handwritten",
42
+ "advertisement",
43
+ "scientific report",
44
+ "scientific publication",
45
+ "specification",
46
+ "file folder",
47
+ "news article",
48
+ "budget",
49
+ "invoice",
50
+ "presentation",
51
+ "questionnaire",
52
+ "resume",
53
+ "memo",
54
+ ]
55
+
56
+
57
+ # folder
58
+ # train
59
+ # categoryA
60
+ # file1
61
+ # test
62
+ # ...
63
+
64
+
65
+ class RvlCdipMulti(datasets.GeneratorBasedBuilder):
66
+ def _info(self):
67
+ folder = None
68
+ if isinstance(self.config.data_files, str):
69
+ folder = self.config.data_files # needs to be extracted cuz zip/tar
70
+ else:
71
+ if isinstance(self.config.data_dir, str):
72
+ folder = self.config.data_dir # contains the folder structure at someone local disk
73
+ else:
74
+ folder = _BACKOFF_folder # my local path, others should set data_dir or data_files
75
+ self.config.data_dir = folder
76
+
77
+ return datasets.DatasetInfo(
78
+ description=_DESCRIPTION,
79
+ features=datasets.Features(
80
+ {
81
+ "file": datasets.Sequence(datasets.Image()),
82
+ "labels": datasets.features.ClassLabel(names=_CLASSES),
83
+ }
84
+ ),
85
+ task_templates=None,
86
+ )
87
+
88
+ def _split_generators(
89
+ self, dl_manager: datasets.DownloadManager
90
+ ) -> List[datasets.SplitGenerator]:
91
+
92
+ if os.path.isdir(self.config.data_dir):
93
+ data_files = {
94
+ labelset: os.path.join(self.config.data_dir, labelset)
95
+ for labelset in os.listdir(self.config.data_dir)
96
+ }
97
+
98
+ elif self.self.config.data_dir.endswith(".tar.gz"):
99
+ archive_path = dl_manager.download(self.config.data_dir)
100
+ data_files = dl_manager.iter_archive(archive_path)
101
+ raise NotImplementedError()
102
+ elif self.self.config.data_dir.endswith(".zip"):
103
+ archive_path = dl_manager.download_and_extract(self.config.data_dir)
104
+ data_files = dl_manager.iter_archive(archive_path)
105
+ raise NotImplementedError()
106
+
107
+ splits = []
108
+ for split_name, folder in data_files.items():
109
+ splits.append(
110
+ datasets.SplitGenerator(name=split_name, gen_kwargs={"archive_path": folder})
111
+ )
112
+ return splits
113
+
114
+ def _generate_examples(self, archive_path):
115
+ labels = self.info.features["labels"]
116
+
117
+ extensions = {"pdf", "PDF"}
118
+
119
+ for i, path in enumerate(Path(archive_path).glob("**/*")):
120
+ if path.suffix in extensions:
121
+ images = pdf2image.convert_from_path(path)
122
+ yield file_path, {
123
+ "file": images,
124
+ "labels": labels.encode_example(path.parent.name.lower()),
125
+ }
test_loader.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from datasets import load_dataset
2
+ dataset = load_dataset("jordyvl/rvl_cdip_multi")
3
+ import pdb; pdb.set_trace() # breakpoint 768996ef //