File size: 6,487 Bytes
0afc661 |
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 |
# coding=utf-8
# Copyright 2022 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.
"""COCO"""
import json
import os
from pathlib import Path
import datasets
_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},
eprinttype = {arXiv},
eprint = {1405.0312},
timestamp = {Mon, 13 Aug 2018 16:48:13 +0200},
biburl = {https://dblp.org/rec/journals/corr/LinMBHPRDZ14.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""
_DESCRIPTION = """
MS COCO is a large-scale object detection, segmentation, and captioning dataset.
COCO has several features: Object segmentation, Recognition in context, Superpixel stuff segmentation, 330K images (>200K labeled), 1.5 million object instances, 80 object categories, 91 stuff categories, 5 captions per image, 250,000 people with keypoints.
"""
_HOMEPAGE = "https://cocodataset.org/#home"
_LICENSE = "CC BY 4.0"
_IMAGES_URLS = {
"train": "http://images.cocodataset.org/zips/train2014.zip",
"validation": "http://images.cocodataset.org/zips/val2014.zip",
}
_KARPATHY_FILES_URL = "https://cs.stanford.edu/people/karpathy/deepimagesent/caption_datasets.zip"
_SPLIT_MAP = {"train": "train2014", "validation": "val2014"}
_FEATURES = datasets.Features(
{
"image": datasets.Image(),
"filepath": datasets.Value("string"),
"sentids": [datasets.Value("int32")],
"filename": datasets.Value("string"),
"imgid": datasets.Value("int32"),
"split": datasets.Value("string"),
"sentences": {
"tokens": [datasets.Value("string")],
"raw": datasets.Value("string"),
"imgid": datasets.Value("int32"),
"sentid": datasets.Value("int32"),
},
"cocoid": datasets.Value("int32"),
}
)
class COCO(datasets.GeneratorBasedBuilder):
"""COCO"""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="2014", version=VERSION, description="2014 version of COCO with Karpathy annotations and splits"),
]
DEFAULT_CONFIG_NAME = "2014"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=_FEATURES,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
annotation_file = os.path.join(dl_manager.download_and_extract(_KARPATHY_FILES_URL), "dataset_coco.json")
image_folders = {k: Path(v) for k, v in dl_manager.download_and_extract(_IMAGES_URLS).items()}
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"annotation_file": annotation_file,
"image_folders": image_folders,
"split_key": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"annotation_file": annotation_file,
"image_folders": image_folders,
"split_key": "validation",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"annotation_file": annotation_file,
"image_folders": image_folders,
"split_key": "test",
},
),
]
def _generate_examples(self, annotation_file, image_folders, split_key):
counter = 0
with open(annotation_file, "r", encoding="utf-8") as fi:
annotations = json.load(fi)
for image_metadata in annotations["images"]:
if split_key == "train":
if image_metadata["split"] != "train" and image_metadata["split"] != "restval":
continue
elif split_key == "validation":
if image_metadata["split"] != "val":
continue
elif split_key == "test":
if image_metadata["split"] != "test":
continue
if "val2014" in image_metadata["filename"]:
image_path = image_folders["validation"] / _SPLIT_MAP["validation"]
else:
image_path = image_folders["train"] / _SPLIT_MAP["train"]
image_path = image_path / image_metadata["filename"]
for caption in image_metadata["sentences"]:
yield counter, {
"image": str(image_path.absolute()),
"filepath": image_metadata["filename"],
"sentids": image_metadata["sentids"],
"filename": image_metadata["filename"],
"imgid": image_metadata["imgid"],
"split": image_metadata["split"],
"sentences": {
"tokens": caption["tokens"],
"raw": caption["raw"],
"imgid": caption["imgid"],
"sentid": caption["sentid"],
},
"cocoid": image_metadata["cocoid"],
}
counter += 1
|