|
import json |
|
import os |
|
import datasets |
|
import torch |
|
|
|
|
|
class COCOBuilderConfig(datasets.BuilderConfig): |
|
|
|
def __init__(self, name, splits, **kwargs): |
|
super().__init__(name, **kwargs) |
|
self.splits = splits |
|
|
|
|
|
|
|
|
|
_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 = """\ |
|
COCO is a large-scale object detection, segmentation, and captioning dataset. |
|
""" |
|
|
|
|
|
_HOMEPAGE = "http://cocodataset.org/#home" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
|
|
|
|
|
|
|
|
_URLs = {} |
|
|
|
|
|
|
|
class COCODataset(datasets.GeneratorBasedBuilder): |
|
"""An example dataset script to work with the local (downloaded) COCO dataset""" |
|
|
|
VERSION = datasets.Version("0.0.0") |
|
|
|
BUILDER_CONFIG_CLASS = COCOBuilderConfig |
|
BUILDER_CONFIGS = [ |
|
COCOBuilderConfig(name='2017', splits=['train', 'valid', 'test']), |
|
] |
|
DEFAULT_CONFIG_NAME = "2017" |
|
|
|
def _info(self): |
|
|
|
|
|
feature_dict = { |
|
"image_id": datasets.Value("int64"), |
|
"caption_id": datasets.Value("int64"), |
|
"caption": datasets.Value("string"), |
|
"height": datasets.Value("int64"), |
|
"width": datasets.Value("int64"), |
|
"file_name": datasets.Value("string"), |
|
"coco_url": datasets.Value("string"), |
|
"image_path": datasets.Value("string"), |
|
"category_ids": datasets.Sequence(datasets.Value("int64")), |
|
"category_one_hot": datasets.Sequence(datasets.Value("int64")), |
|
} |
|
|
|
features = datasets.Features(feature_dict) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
supervised_keys=None, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
data_dir = self.config.data_dir |
|
if not data_dir: |
|
raise ValueError( |
|
"This script is supposed to work with local (downloaded) COCO dataset. The argument `data_dir` in `load_dataset()` is required." |
|
) |
|
|
|
_DL_URLS = { |
|
"train": os.path.join(data_dir, "train2017.zip"), |
|
"val": os.path.join(data_dir, "val2017.zip"), |
|
"test": os.path.join(data_dir, "test2017.zip"), |
|
"annotations_trainval": os.path.join(data_dir, "annotations_trainval2017.zip"), |
|
"image_info_test": os.path.join(data_dir, "image_info_test2017.zip"), |
|
} |
|
|
|
splits = [] |
|
for split in self.config.splits: |
|
if split == 'train': |
|
dataset = datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"captions_json_path": os.path.join(data_dir, "annotations", "captions_train2017.json"), |
|
"instances_json_path": os.path.join(data_dir, "annotations", "instances_train2017.json"), |
|
"image_dir": os.path.join(data_dir, "train2017"), |
|
"split": "train", |
|
} |
|
) |
|
elif split in ['val', 'valid', 'validation', 'dev']: |
|
dataset = datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"captions_json_path": os.path.join(data_dir, "annotations", "captions_val2017.json"), |
|
"instances_json_path": os.path.join(data_dir, "annotations", "instances_val2017.json"), |
|
"image_dir": os.path.join(data_dir, "val2017"), |
|
"split": "valid", |
|
}, |
|
) |
|
elif split == 'test': |
|
dataset = datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"captions_json_path": os.path.join(data_dir, "annotations", "image_info_test2017.json"), |
|
"instances_json_path": os.path.join(data_dir, "annotations", "image_info_test2017.json"), |
|
"image_dir": os.path.join(data_dir, "test2017"), |
|
"split": "test", |
|
}, |
|
) |
|
else: |
|
continue |
|
|
|
splits.append(dataset) |
|
|
|
return splits |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _generate_examples( |
|
|
|
self, captions_json_path, instances_json_path, image_dir, split |
|
): |
|
""" Yields examples as (key, example, categories) tuples. """ |
|
|
|
|
|
|
|
_features = ["image_id", "caption_id", "caption", "height", "width", "file_name", "coco_url", "image_path", "id", "category_ids", "category_one_hot"] |
|
features = list(_features) |
|
|
|
if split in "valid": |
|
split = "val" |
|
|
|
with open(captions_json_path, 'r', encoding='UTF-8') as fp: |
|
captions_data = json.load(fp) |
|
|
|
with open(instances_json_path, 'r', encoding='UTF-8') as fp: |
|
instances_data = json.load(fp) |
|
|
|
|
|
images = captions_data["images"] |
|
instances_annotations = instances_data["annotations"] |
|
entries = images |
|
|
|
self.classes = list(map(lambda x: {'id': x['id'], 'name': x['name']}, instances_data['categories'])) |
|
self.num_classes = len(self.classes) |
|
|
|
|
|
d = {image["id"]: image for image in images} |
|
|
|
|
|
cat_ids_dict = {} |
|
|
|
for annotation in instances_annotations: |
|
image_id = annotation["image_id"] |
|
category_id = annotation["category_id"] |
|
if image_id not in cat_ids_dict: |
|
cat_ids_dict[image_id] = set([]) |
|
cat_ids_dict[image_id].add(category_id) |
|
|
|
|
|
if split in ["train", "val"]: |
|
annotations = captions_data["annotations"] |
|
|
|
|
|
for annotation in annotations: |
|
_id = annotation["id"] |
|
image_id = annotation["image_id"] |
|
image_info = d[image_id] |
|
annotation.update(image_info) |
|
annotation["id"] = _id |
|
|
|
|
|
annotation["category_ids"] = cat_ids_dict[annotation["image_id"]] if annotation["image_id"] in cat_ids_dict else [] |
|
|
|
annotation['category_one_hot'] = torch.zeros(len(self.classes)) |
|
for category_id in annotation["category_ids"]: |
|
|
|
index = next((index for (index, d) in enumerate(self.classes) if d["id"] == category_id), None) |
|
annotation['category_one_hot'][index] = 1 |
|
|
|
entries = annotations |
|
|
|
for id_, entry in enumerate(entries): |
|
|
|
entry = {k: v for k, v in entry.items() if k in features} |
|
|
|
if split == "test": |
|
entry["image_id"] = entry["id"] |
|
entry["id"] = -1 |
|
entry["caption"] = -1 |
|
|
|
entry["caption_id"] = entry.pop("id") |
|
entry["image_path"] = os.path.join(image_dir, entry["file_name"]) |
|
|
|
entry = {k: entry[k] for k in _features if k in entry} |
|
|
|
yield str((entry["image_id"], entry["caption_id"])), entry |
|
|
|
from datasets import load_dataset |
|
if __name__ == "__main__": |
|
dataset = load_dataset( |
|
"coco_dataset_multi_label_script/coco_dataset_multi_label_script.py", |
|
"2017", |
|
keep_in_memory=False, |
|
splits=["valid"], |
|
data_dir="/workspace/pixt/clip-training/data/mscoco", |
|
) |
|
print(dataset["validation"][0]) |
|
|