File size: 9,578 Bytes
7d5136d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

import datasets
from datasets.data_files import DataFilesDict
from datasets.download.download_manager import ArchiveIterable, DownloadManager
from datasets.features import Features
from datasets.info import DatasetInfo

# Typing
_TYPING_BOX = Tuple[float, float, float, float]

_CITATION = """\
@article{DBLP:journals/corr/LinMBHPRDZ14,
  author    = {Tsung{-}Yi Lin and
               Michael Maire and
               Serge J. Belongie and
               Lubomir D. Bourdev and
               Ross B. Girshick and
               James Hays and
               Pietro Perona and
               Deva Ramanan and
               Piotr Doll{\'{a}}r and
               C. Lawrence Zitnick},
  title     = {Microsoft {COCO:} Common Objects in Context},
  journal   = {CoRR},
  volume    = {abs/1405.0312},
  year      = {2014},
  url       = {http://arxiv.org/abs/1405.0312},
  archivePrefix = {arXiv},
  eprint    = {1405.0312},
  timestamp = {Mon, 13 Aug 2018 16:48:13 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/LinMBHPRDZ14},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""

_DESCRIPTION = """\
This dataset contains all COCO 2017 images and annotations split in training (118287 images) \
    and validation (5000 images).
"""

_HOMEPAGE = "https://cocodataset.org"

_URLS = {
    "annotations": "http://images.cocodataset.org/annotations/annotations_trainval2017.zip",
    "train": "http://images.cocodataset.org/zips/train2017.zip",
    "val": "http://images.cocodataset.org/zips/val2017.zip",
}

_SPLITS = ["train", "val"]

_PATHS = {
    "annotations": {
        "train": Path("annotations/instances_train2017.json"),
        "val": Path("annotations/instances_val2017.json"),
    },
    "images": {
        "train": Path("train2017"),
        "val": Path("val2017"),
    },
}

_CLASSES = [
    "None",
    "person",
    "bicycle",
    "car",
    "motorcycle",
    "airplane",
    "bus",
    "train",
    "truck",
    "boat",
    "traffic light",
    "fire hydrant",
    "street sign",
    "stop sign",
    "parking meter",
    "bench",
    "bird",
    "cat",
    "dog",
    "horse",
    "sheep",
    "cow",
    "elephant",
    "bear",
    "zebra",
    "giraffe",
    "hat",
    "backpack",
    "umbrella",
    "shoe",
    "eye glasses",
    "handbag",
    "tie",
    "suitcase",
    "frisbee",
    "skis",
    "snowboard",
    "sports ball",
    "kite",
    "baseball bat",
    "baseball glove",
    "skateboard",
    "surfboard",
    "tennis racket",
    "bottle",
    "plate",
    "wine glass",
    "cup",
    "fork",
    "knife",
    "spoon",
    "bowl",
    "banana",
    "apple",
    "sandwich",
    "orange",
    "broccoli",
    "carrot",
    "hot dog",
    "pizza",
    "donut",
    "cake",
    "chair",
    "couch",
    "potted plant",
    "bed",
    "mirror",
    "dining table",
    "window",
    "desk",
    "toilet",
    "door",
    "tv",
    "laptop",
    "mouse",
    "remote",
    "keyboard",
    "cell phone",
    "microwave",
    "oven",
    "toaster",
    "sink",
    "refrigerator",
    "blender",
    "book",
    "clock",
    "vase",
    "scissors",
    "teddy bear",
    "hair drier",
    "toothbrush",
    "hair brush",
]

def round_box_values(box, decimals=2):
    return [round(val, decimals) for val in box]

class COCOHelper:
    """Helper class to load COCO annotations"""

    def __init__(self, annotation_path: Path, images_dir: Path) -> None:
        with open(annotation_path, "r") as file:
            data = json.load(file)
        self.data = data
        
        dict_id2annot: Dict[int, Any] = {}
        for annot in self.annotations:
            dict_id2annot.setdefault(annot["image_id"], []).append(annot)

        # Sort by id
        dict_id2annot = {
            k: list(sorted(v, key=lambda a: a["id"])) for k, v in dict_id2annot.items()
        }

        self.dict_path2annot: Dict[str, Any] = {}
        self.dict_path2id: Dict[str, Any] = {}
        for img in self.images:
            path_img = images_dir / str(img["file_name"])
            path_img_str = str(path_img)
            idx = int(img["id"])
            annot = dict_id2annot.get(idx, [])
            self.dict_path2annot[path_img_str] = annot
            self.dict_path2id[path_img_str] = img["id"]


    def __len__(self) -> int:
        return len(self.data["images"])

    @property
    def info(self) -> Dict[str, Union[str, int]]:
        return self.data["info"]

    @property
    def licenses(self) -> List[Dict[str, Union[str, int]]]:
        return self.data["licenses"]

    @property
    def images(self) -> List[Dict[str, Union[str, int]]]:
        return self.data["images"]

    @property
    def annotations(self) -> List[Any]:
        return self.data["annotations"]

    @property
    def categories(self) -> List[Dict[str, Union[str, int]]]:
        return self.data["categories"]

    def get_annotations(self, image_path: str) -> List[Any]:
        return self.dict_path2annot.get(image_path, [])

    def get_image_id(self, image_path: str) -> int:
        return self.dict_path2id.get(image_path, -1)


class COCO2017(datasets.GeneratorBasedBuilder):
    """COCO 2017 dataset."""

    VERSION = datasets.Version("1.0.1")
    
    def _info(self) -> datasets.DatasetInfo:
        """
        Returns the dataset metadata and features.

        Returns:
            DatasetInfo: Metadata and features of the dataset.
        """
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "image_id": datasets.Value("int64"),
                    "objects": datasets.Sequence(
                        {
                            "id": datasets.Value("int64"),
                            "area": datasets.Value("float64"),
                            "bbox": datasets.Sequence(
                                datasets.Value("float32"), length=4
                            ),
                            "label": datasets.ClassLabel(names=_CLASSES),
                            "iscrowd": datasets.Value("bool"),
                        }
                    ),
                }
            ),
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(
        self, dl_manager: DownloadManager
    ) -> List[datasets.SplitGenerator]:
        """
        Provides the split information and downloads the data.

        Args:
            dl_manager (DownloadManager): The DownloadManager to use for downloading and
                extracting data.

        Returns:
            List[SplitGenerator]: List of SplitGenerator objects representing the data splits.
        """
        archive_annots = dl_manager.download_and_extract(_URLS["annotations"])

        splits = []
        for split in _SPLITS:
            archive_split = dl_manager.download(_URLS[split])
            annotation_path = Path(archive_annots) / _PATHS["annotations"][split]
            images = dl_manager.iter_archive(archive_split)
            splits.append(
                datasets.SplitGenerator(
                    name=datasets.Split(split),
                    gen_kwargs={
                        "annotation_path": annotation_path,
                        "images_dir": _PATHS["images"][split],
                        "images": images,
                    },
                )
            )
        return splits
            
    def _generate_examples(
        self, annotation_path: Path, images_dir: Path, images: ArchiveIterable
    ) -> Iterator:
        """
        Generates examples for the dataset.

        Args:
            annotation_path (Path): The path to the annotation file.
            images_dir (Path): The path to the directory containing the images.
            images: (ArchiveIterable): An iterable containing the images.

        Yields:
            Dict[str, Union[str, Image]]: A dictionary containing the generated examples.
        """
        coco_annotation = COCOHelper(annotation_path, images_dir)
        
        for image_path, f in images:
            annotations = coco_annotation.get_annotations(image_path)
            ret = {
                "image": {"path": image_path, "bytes": f.read()},
                "image_id": coco_annotation.get_image_id(image_path),
                "objects": [
                    {
                        "id": annot["id"],
                        "area": annot["area"],
                        "bbox": round_box_values(annot["bbox"], 2), # [x, y, w, h]
                        "label": annot["category_id"],
                        "iscrowd": bool(annot["iscrowd"]),
                    }
                    for annot in annotations
                ],
            }

            yield image_path, ret