Datasets:

Modalities:
Image
Text
Libraries:
Datasets
License:
File size: 5,016 Bytes
94c1e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2022 The HuggingFace Datasets Authors.
#
# 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.
"""NoCaps loading script."""


import json

from collections import defaultdict
import datasets

_CITATION = """\
@inproceedings{agrawal2019nocaps,
  title={nocaps: novel object captioning at scale},
  author={Agrawal, Harsh and Desai, Karan and Wang, Yufei and Chen, Xinlei and Jain, Rishabh and Johnson, Mark and Batra, Dhruv and Parikh, Devi and Lee, Stefan and Anderson, Peter},
  booktitle={Proceedings of the IEEE International Conference on Computer Vision},
  pages={8948--8957},
  year={2019}
}
"""

_DESCRIPTION = """\
Dubbed NoCaps, for novel object captioning at scale, NoCaps consists of 166,100 human-generated captions describing 15,100 images from the Open Images validation and test sets.
The associated training data consists of COCO image-caption pairs, plus Open Images image-level labels and object bounding boxes.
Since Open Images contains many more classes than COCO, nearly 400 object classes seen in test images have no or very few associated training captions (hence, nocaps).
"""

_HOMEPAGE = "https://nocaps.org/"

_LICENSE = "CC BY 2.0"

_URLS = {
    "validation": "https://nocaps.s3.amazonaws.com/nocaps_val_4500_captions.json",
    "test": "https://s3.amazonaws.com/nocaps/nocaps_test_image_info.json",
}


class NoCaps(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "image": datasets.Image(),
                "image_coco_url": datasets.Value("string"),
                "image_date_captured": datasets.Value("string"),
                "image_file_name": datasets.Value("string"),
                "image_height": datasets.Value("int32"),
                "image_width": datasets.Value("int32"),
                "image_id": datasets.Value("int32"),
                "image_license": datasets.Value("int8"),
                "image_open_images_id": datasets.Value("string"),
                "annotations_ids": datasets.Sequence(datasets.Value("int32")),
                "annotations_captions": datasets.Sequence(datasets.Value("string")),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_file = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": data_file["validation"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": data_file["test"],
                },
            ),
        ]

    def _generate_examples(self, data_file):
        with open(data_file, encoding="utf-8") as f:
            data = json.load(f)

        annotations = defaultdict(list)
        if "annotations" in data:
            # Only present for the validation split
            for ann in data["annotations"]:
                image_id = ann["image_id"]
                caption_id = ann["id"]
                caption = ann["caption"]
                annotations[image_id].append((caption_id, caption))

        counter = 0
        for im in data["images"]:
            image_coco_url = im["coco_url"]
            image_date_captured = im["date_captured"]
            image_file_name = im["file_name"]
            image_height = im["height"]
            image_width = im["width"]
            image_id = im["id"]
            image_license = im["license"]
            image_open_images_id = im["open_images_id"]
            yield counter, {
                "image": image_coco_url,
                "image_coco_url": image_coco_url,
                "image_date_captured": image_date_captured,
                "image_file_name": image_file_name,
                "image_height": image_height,
                "image_width": image_width,
                "image_id": image_id,
                "image_license": image_license,
                "image_open_images_id": image_open_images_id,
                "annotations_ids": [ann[0] for ann in annotations[image_id]],
                "annotations_captions": [ann[1] for ann in annotations[image_id]],
            }
            counter += 1