File size: 8,013 Bytes
ef175eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53f558
ef175eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Inspired from
https://huggingface.co/datasets/ydshieh/coco_dataset_script/blob/main/coco_dataset_script.py
"""

import json
import os
import datasets
import collections


class COCOBuilderConfig(datasets.BuilderConfig):
    def __init__(self, name, splits, **kwargs):
        super().__init__(name, **kwargs)
        self.splits = splits


# Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@article{doclaynet2022,
  title = {DocLayNet: A Large Human-Annotated Dataset for Document-Layout Analysis},  
  doi = {10.1145/3534678.353904},
  url = {https://arxiv.org/abs/2206.01062},
  author = {Pfitzmann, Birgit and Auer, Christoph and Dolfi, Michele and Nassar, Ahmed S and Staar, Peter W J},
  year = {2022}
}
"""

# Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
Dataset for the ICDAR 2023 Competition on Robust Layout Segmentation in Corporate Documents.
"""

# Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://ds4sd.github.io/icdar23-doclaynet/"

# Add the licence for the dataset here if you can find it
_LICENSE = "apache-2.0"

# Add link to the official dataset URLs here
# The HuggingFace dataset library don't host the datasets but only point to the original files
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)

_URLs = {
    "dev": "https://ds4sd-icdar23-doclaynet-competition.s3.eu-de.cloud-object-storage.appdomain.cloud/dev-dataset-public.zip",
    "test": "https://ds4sd-icdar23-doclaynet-competition.s3.eu-de.cloud-object-storage.appdomain.cloud/competition-dataset-public.zip"
}

# Name of the dataset usually match the script name with CamelCase instead of snake_case
class COCODataset(datasets.GeneratorBasedBuilder):
    """An example dataset script to work with the local (downloaded) COCO dataset"""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIG_CLASS = COCOBuilderConfig
    BUILDER_CONFIGS = [
        COCOBuilderConfig(name="2023.01", splits=["dev", "test"]),
    ]
    DEFAULT_CONFIG_NAME = "2023.01"

    def _info(self):
        features = datasets.Features(
            {
                "image_id": datasets.Value("int64"),
                "image": datasets.Image(),
                "width": datasets.Value("int32"),
                "height": datasets.Value("int32"),
                # Custom fields
                # "doc_category": datasets.Value(
                #     "string"
                # ),  # high-level document category
                # "collection": datasets.Value("string"),  # sub-collection name
                # "doc_name": datasets.Value("string"),  # original document filename
                # "page_no": datasets.Value("int64"),  # page number in original document
            }
        )
        object_dict = {
            "category_id": datasets.ClassLabel(
                names=[
                    "Caption",
                    "Footnote",
                    "Formula",
                    "List-item",
                    "Page-footer",
                    "Page-header",
                    "Picture",
                    "Section-header",
                    "Table",
                    "Text",
                    "Title",
                ]
            ),
            "image_id": datasets.Value("string"),
            "id": datasets.Value("int64"),
            "area": datasets.Value("int64"),
            "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
            "segmentation": [[datasets.Value("float32")]],
            "iscrowd": datasets.Value("bool"),
            "precedence": datasets.Value("int32"),
        }
        # features["objects"] = [object_dict]

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        archive_path = dl_manager.download_and_extract(_URLs)
        splits = []
        for split in self.config.splits:
            if split in ["val", "valid", "validation", "dev"]:
                dataset = datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "json_path": os.path.join(
                            archive_path["dev"], "coco.json"
                        ),
                        "image_dir": os.path.join(archive_path["dev"], "PNG"),
                        "split": "dev",
                    },
                )
            elif split == "test":
                dataset = datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "json_path": os.path.join(
                            archive_path["test"], "coco.json"
                        ),
                        "image_dir": os.path.join(archive_path["test"], "PNG"),
                        "split": "test",
                    },
                )
            else:
                continue

            splits.append(dataset)
        return splits

    def _generate_examples(
        # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
        self,
        json_path,
        image_dir,
        split,
    ):
        """Yields examples as (key, example) tuples."""
        # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is here for legacy reason (tfds) and is not important in itself.
        def _image_info_to_example(image_info, image_dir):
            image = image_info["file_name"]
            return {
                "image_id": image_info["id"],
                "image": os.path.join(image_dir, image),
                "width": image_info["width"],
                "height": image_info["height"],
                # "doc_category": image_info["doc_category"],
                # "collection": image_info["collection"],
                # "doc_name": image_info["doc_name"],
                # "page_no": image_info["page_no"],
            }

        with open(json_path, encoding="utf8") as f:
            annotation_data = json.load(f)
            images = annotation_data["images"]
            # annotations = annotation_data["annotations"]
            # image_id_to_annotations = collections.defaultdict(list)
            # for annotation in annotations:
            #     image_id_to_annotations[annotation["image_id"]].append(annotation)

        for idx, image_info in enumerate(images):
            example = _image_info_to_example(image_info, image_dir)
            # annotations = image_id_to_annotations[image_info["id"]]
            # objects = []
            # for annotation in annotations:
            #     category_id = annotation["category_id"]  # Zero based counting
            #     if category_id != -1:
            #         category_id = category_id - 1
            #     annotation["category_id"] = category_id
            #     objects.append(annotation)
            # example["objects"] = objects
            yield idx, example